Smallapi client for javascript and typescript
Smallapi-js is a small wrapper wrote in typescript that allows smallapi users to uses their APIs cloud functions from the client side.
This package requires NodeJS and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following commands:
node --version
# v21.5.0
npm --version
# 10.2.4
BEFORE YOU INSTALL: please read the prerequisites
Install the package using npm:
npm i -S smallapi-js
import { smallapi } from 'smallapi-js';
const api = await smallapi('https://my-api-url.com/', {
apiKey: 'my-secret-key',
});
const createdUser = await api.createUser({
firstName: 'John',
lastName: 'Doe',
email: `[email protected]`,
age: 32,
});
A repository with complete examples can be found here: https://github.com/tutanck/small-demo.
This package export a type definition file so you can use it, out of the box, inside your Typescript project.
import { smallapi, Api, Config } from 'smallapi-js';
const myApiUrl: string = 'https://my-api-url.com/';
const myConfig: Config = {
apiKey: 'my-secret-key',
};
const api: Api = await smallapi(myApiUrl, myConfig);
const userInfos: object = {
firstName: 'John',
lastName: 'Doe',
email: `[email protected]`,
age: 32,
};
const createdUser: object = await api.createUser(userInfos);
Here is the list of cloud functions generated when you create a model called {Model} on the smallapi platform:
Counts the number of documents matching the query parameter in the {{modelName}} collection.
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
Number • The number of documents matching the query parameter in the collection {{modelName}}
const resultsCount = count{Model}Documents(query)
Insert one document or an array of documents to the {{modelName}} collection.
docs : Array|Object • The documents to insert in the collection {{modelName}}.
Document|Array<Document> • The list of documents inserted in the collection {{modelName}}.
const results = create{Model}(docs)
Finds a single document by its _id field in the {{modelName}} collection.
-
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
-
[projection] : Object|String|Array<String> • Optional • fields to return from the document found in the collection {{modelName}}.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- limit
- skip
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
Document • The document identified by its _id in the collection {{modelName}}.
const result = find{Model}ById(id)
const result = find{Model}ById(id, { propertyA: 1, propertyB: -1 }, options)
const result = find{Model}ById(id, "propertyA -propertyB", options)
const result = find{Model}ById(id, ["propertyA", "propertyC"], options)
Learn more about field selection
Learn more about the populate option
Find the documents matching the query parameter in the {{modelName}} collection.
-
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
-
[projection] : Object|String|Array<String> • Optional • fields to return from the document found in the collection {{modelName}}.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- limit
- skip
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOne instead of find • default to false.
Array<Document> • The documents matching the query parameter in the collection {{modelName}}
const result = find{Model}ByQuery(query)
const result = find{Model}ByQuery(query, { propertyA: 1, propertyB: -1 }, options)
const result = find{Model}ByQuery(query, "propertyA -propertyB", options)
const result = find{Model}ByQuery(query, ["propertyA", "propertyC"], options)
Learn more about field selection
Learn more about the populate option
Delete a single document by its _id field in the {{modelName}} collection.
-
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.- populate
Document • The document identified by its _id in the collection {{modelName}}.
const result = remove{Model}ById(id, options)
Learn more about the populate option
Delete all the documents matching the query parameter in the {{modelName}} collection.
-
query : Object • Indicates how to filter the documents in the collection {{modelName}}.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- sort • Works only if useFindOne is set to true.
- populate • Object|String • Populate the specified path • Works only if useFindOne is set to true.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOneAndDelete instead of deleteMany • default to false.
DeleteResult|Document • an object with the property deletedCount containing the number of documents deleted OR the document matching the query parameter in the collection {{modelName}} if useFindOne is set to true.
const result = remove{Model}ByQuery(query, options)
Learn more about the populate option
Updates a single document by its _id field in the {{modelName}} collection.
-
id : ObjectId • Required • value of _id field to query by {{modelName}} collection.
-
[update] : Object • Required • The update object to replace the one found while querying the collection {{modelName}}.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- upsert : true|false • if true, and no documents found, insert a new document • default to false.
- new : true|false • if true, return the modified document rather than the original • default to true.
- runValidators : true|false • if true, validate the update operation against the model's definition first • default to true.
- select Object|String • sets the document fields to return.
- sort
- populate • Object|String • Populate the specified path.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
Document • The document identified by its _id in the collection {{modelName}}.
const result = update{Model}ById(id, update, options)
Learn more about field selection
Learn more about the populate option
Updates all the documents matching the query parameter in the {{modelName}} collection.
-
query : Object • Required • value of _id field to query by {{modelName}} collection.
-
[update] : Object • Required • The update object to replace the one found while querying the collection {{modelName}}.
-
[options] : Object • Optional • Additional options to apply while querying the collection {{modelName}}.
The following options are available:
- upsert : true|false • if true, and no documents found, insert a new document • default to false.
- new : true|false • if true, return the modified document rather than the original • default to false • Works only if useFindOne is set to true.
- runValidators : true|false • if true, validate the update operation against the model's definition first • default to false • Works only if useFindOne is set to true.
- fields Object|String • sets the document fields to return • Works only if useFindOne is set to true.
- sort• Works only if useFindOne is set to true.
- populate • Object|String • Populate the specified path • Works only if useFindOne is set to true.
- path • String • Specify the name of the property to be populated.
- select • String • Specify the fields to fetch from the referenced document.
- useFindOne : true|false • If set to true the driver will use findOneAndDelete instead of deleteMany • default to false.
UpdateResult|Document • an UpdateResult object OR the document matching the query parameter in the collection {{modelName}} if useFindOne is set to true.
const result = update{Model}ByQuery(id, update, options)
Learn more about field selection
Learn more about the populate option
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Add your changes:
git add .
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 😎
MIT License © Anagbla Joan (tutanck)
Thanks goes to these wonderful people for their contribution:
Anagbla Joan |
This project follows the all-contributors specification. Contributions of any kind welcome!