Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@data-provider/axios
Advanced tools
This Data Provider addon provides an API REST data origin using Axios.
Read the Data Provider docs to learn how to use addons.
Apart of the common Data Provider options, next ones are available:
url
(String): Url of the API resource.baseUrl
(String): Added as prefix to the url
option.createVerb
(String): HTTP verb to be used in axios requests for create method.readVerb
(String): HTTP verb to be used in axios requests for read method.updateVerb
(String): HTTP verb to be used in axios requests for update method.deleteVerb
(String): HTTP verb to be used in axios requests for delete method.authErrorStatus
(Number): Status code that will be considered as an authentication error. When detected, the authErrorHandler function will be executed instead of returning an error.authErrorHandler
(Function): Handler that will be executed when an authentication error is received.
provider
(Object): provider instance As first argument, the function will receive the provider itself. This will allow to set custom authentication headers after executing login, as example.retry
(Function): As second argument, a retry function is received, it has to be executed in order to retry the authentication failed request.onBeforeRequest
(Function): Handler that will be executed before any request method. Useful, for example, to set provider headers just before each request is executed.
provider
(Object): provider instance As first argument, the function will receive the provider itself.onceBeforeRequest
(Function): Handler that will be executed once time just before the first request. Useful to set provider configuration, for example.
provider
(Object): provider instance As first argument, the function will receive the provider itself.expirationTime
(Number): The cache will be automatically cleaned each expirationTime miliseconds.retries
(Number): Number of retries that will be executed when a request fails before definitively failing. Requests will be retried for network errors or a 5xx error on an idempotent request (GET, PUT, PATCH, DELETE).fullResponse
(Boolean): If true, the full response object will be used as value, so you can consult headers, etc. If false, only the response.data will be returned, which is the default behavior.validateStatus
(Function): Function that will be used to determine if response status has to be considered as failed or success.
status
(Number): Status code of the request.validateResponse
(Function): Function that will be used to determine if response has to be considered as failed or success.
response
(Object): Response of the request.errorHandler
(Function): Function used to parse errors. The returned value will be setted as error in the provider error property.
error
(Error): Error object produced by a failed request.axiosConfig
(Object): Options for the Axios request. If provided, this object is passed directly to Axios as request configuration.queryStringConfig
(Object): Options for the query-string library, which is used under the hood to serialize query string parameters. If provided, this object is passed directly to query-string
as options for the stringify
method. Default options are the same from the library.When querying providers created with this addon, the query object can have one of the next properties:
queryString
(Object): Object containing all query string parameters to send in the request.urlParams
(Object): Keys of the object will be replaced by correspondant url parameters defined in the url as ":param". Please refer to the path-to-regexp package for further info.When chaining provider queries, the
queryString
andurlParams
objects will be extended with the previous ones
urlParams
example:
import { Axios } from "@data-provider/axios";
const booksModel = new Axios({
id: "books-model",
url: "/books/:id",
baseUrl: "http://foo.api.com"
});
// GET http://foo.api.com/books/2
booksModel.query({
urlParams: {
id: 2
}
}).read();
queryString
example:
import { Axios } from "@data-provider/axios";
const booksCollection = new Axios({
id: "books-collection",
url: "/books/:id",
baseUrl: "http://foo.api.com"
});
// GET http://foo.api.com/books/?author=2
booksCollection.query({
queryString: {
author: 2
}
}).read();
Chainability example:
const author2Books = booksCollection.query({
queryString: {
author: 2
}
});
// GET http://foo.api.com/books/?author=2&page=1
author2Books.query({
queryString: {
page: 1
}
}).read();
Apart of the common Data Provider methods, next ones are available:
create(data)
Sends a "create" request. By default, it will use the POST
HTTP verb, but this behavior can be changed using the createVerb
option. It emits a createSuccess
event when the request finish successfully, and automatically cleans the cache of the provider.
data
(Object): Data to be sent as request body.booksCollection.create({
title: "1984"
});
update(data)
Sends an "update" request. By default, it will use the PATCH
HTTP verb, but this behavior can be changed using the updateVerb
option. It emits an updateSuccess
event when the request finish successfully, and automatically cleans the cache of the provider.
data
(Object): Data to be sent as request body.booksCollection.query({
queryString: {
id: 2
}
}).update({
title: "Fahrenheit 451"
});
delete(data)
Sends a "delete" request. By default, it will use the DELETE
HTTP verb, but this behavior can be changed using the deleteVerb
option. It emits an deleteSuccess
event when the request finish successfully, and automatically cleans the cache of the provider.
data
(Object): Optional. Data to be sent as request body.With no request body
booksCollection.query({
queryString: {
id: 2
}
}).delete();
With request body
booksCollection.query().delete({ id: 2 });
When cleaning the cache after a successful request, all methods use the
force
option under the hood, so the cache will be cleaned inmediately, no matter thecleanCacheThrottle
option configured for the provider, as the resource should have changed in the API also inmediatly.
setHeaders(headers)
Defines headers that will be applied to all subsequent requests.
headers
(Object): Each property in the object will be applied as a request header.booksCollection.setHeaders({
"Cache-Control": "no-cache"
});
addHeaders(headers)
Add a new header. Current headers will be extended with received headers object, and applied to all subsequent requests:
headers
(Object): Each property in the object will be applied as a request header.booksCollection.addHeaders({
Authorization: `Bearer ${token}`
});
Apart of the common events emitted by any Data Provider origin, this addon emits extra events when some custom methods are executed successfully.
Event names are available at the top-level export eventNames
:
create
method finish successfully.update
method finish successfully.delete
method finish successfully.import { eventNames } from "@data-provider/axios";
booksCollection.on(eventNames.CREATE_SUCCESS, () => {
console.log("A book was created!");
});
Providers created with this addon will have automatically the axios
tag, so you can select all of them together using the providers
methods as in:
import { providers } from "@data-provider/core";
providers.getByTag("axios").cleanCache();
Contributors are welcome. Please read the contributing guidelines and code of conduct.
FAQs
REST API Data Provider origin addon using Axios
The npm package @data-provider/axios receives a total of 15 weekly downloads. As such, @data-provider/axios popularity was classified as not popular.
We found that @data-provider/axios demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.