@atlasconsulting/bedita-sdk v3.2.3
BEdita API SDK
A simple JavaScript SDK for BEdita API based on axios. It is fully written in Typescript and then compiled to JavaScript. Use it as you prefer.
Install
yarn add @atlasconsulting/bedita-sdkGetting started
The following code uses Promise but you can take advantage of async functions and await operator.
import { ApiProvider } from '@atlasconsulting/bedita-sdk';
// The first argument is the name of the client
// used to register that specific client
const client = ApiProvider.get('bedita', {
baseUrl: 'https://api-bedita.example.com',
clientId: '123456',
clientSecret: 'abcdefg',
// apiKey: '123456', // as alternative you can use apiKey removing clientId and clientSecret (discouraged)
});
client.get('/documents')
.then(response => {
console.log(res.data);
});
// Authenticate user.
// After authentication the Authorization header will be added to every request.
// The client takes care of JWT token renew too.
client.authenticate('username', 'password')
.then(response => {
// Use save() to create or update an object.
// To update the object add object id to data
client.save('documents', {
title: 'My document',
description: 'The description is here',
});
})After having configured a client you can get that instance everywhere
import { ApiProvider } from '@atlasconsulting/bedita-sdk';
// get the previous registered client
const client = ApiProvider.get('bedita');
// get user data previously authenticated
client.getUserAuth()
.then(response => {
// the formattedData property contains a modified response data
console.log(response.data.formattedData);
});In this way it is possible to instantiate different BEdita API clients distinguished by name.
Interceptors
The client takes advantage of Axios Interceptors to intercept request and response before they are handled by then or catch.
A set of default interceptor are always used:
AuthInterceptoris responsible for addingAuthorizationheader if neededContentTypeInterceptorset default content type if no one is passedRefreshAuthInterceptoris responsible for refreshing the access token when expired
Other optional interceptor that can be used are:
RemoveLinksInterceptorremove thelinksattribute from the responseMapIncludedInterceptorembedincludedresource inside the relatedrelationshipsattribute
An interceptor can be added to the client or to a request. When added to the client it will be used unless it was removed.
import { ApiProvider, MapIncludedInterceptor } from '@atlasconsulting/bedita-sdk';
const client = ApiProvider.get('bedita');
// Map included data inside the relationships
client.addInterceptor(new MapIncludedInterceptor());A request interceptor must implements RequestInterceptorInterface.
A response interceptor must implements ResponseInterceptorInterface.
Storage Adapters
By default the JWT tokens are saved in localStorage but it is possibile to use a different adapter
to store that data. There are two different adapters:
LocalStorageAdaperMemoryStorageAdapter(to save only in memory)
If you want to store data in a different storage you can implement a new adapter that must implement StorageAdapterInterface.
const client = ApiProvider.get('bedita', {
baseUrl: 'https://api-bedita.example.com',
clientId: '123456',
clientSecret: 'abcdefg',
storageAdapter: new InnoDbAdapter(), // where class InnoDbAdapter implements StorageAdapterInterface
});Develop
@todo
Testing
run docker image of BEdita API as
docker run -p 8090:80 --env BEDITA_ADMIN_USR=admin --env BEDITA_ADMIN_PWD=admin --env BEDITA_API_KEY=1234567890 bedita/bedita:latestSet the values you want for
BEDITA_ADMIN_USR,BEDITA_ADMIN_PWDandBEDITA_API_KEYand the version of docker image, for examplebedita/bedita:4.7.0.run test with
yarn test- launch
yarn coverageif you want a coverage report
Linter
run eslint as follows:
yarn lint
10 months ago
10 months ago
10 months ago
9 months ago
1 year ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago

