@frollo/frollo-web-sdk
v9.0.4
Published
Frollo's SDK for the web
Downloads
133
Maintainers
Readme
Frollo Web SDK
A JavaScript and TypeScript SDK for Frollo API
Note
For compatability with the different Frollo Server versions please use the web-sdk versions below:
v9.x.x -> Frollo Server v2.28
v7.x.x -> Frollo Server v2.23
v4.x.x -> Frollo Server v2.21
v3.x.x -> Frollo Server v2.20
v2.x.x -> Frollo Server v2.20
v1.x.x -> Frollo Server v2.19
v0.2.x -> Frollo Server v2.18
v0.1.5 -> Frollo Server v2.17
Management API is supported from v3.x.x onwards.
Installation
npm i @frollo/frollo-web-sdk
or
yarn add @frollo/frollo-web-sdk
Note: For local development please refer to the general development readme.
Usage
Initialise the client by providing the api base url as a parameter.
import { FrolloSDK } from '@frollo/frollo-web-sdk'
FrolloSDK().init('api-base.url')
Once initialised and authorised (Cookie/Header), services will be available. Types can also be imported. E.g.
import { userService, UserDetailsResponse, UserAccountPayload } from '@frollo/frollo-web-sdk'
const { getUserDetails, updateUserAccount } = userService();
const userDetails: UserDetailsResponse = (await getUserDetails()).data;
const userAccountPayload: UserAccountPayload = {...}
await updateUserAccount(userAccountPayload);
Interceptors
Axios interceptors can be added by hooking onto the global FrolloClient
after initialisation.
// Add a request interceptor
globalThis.FrolloClient.interceptors.request.use(
(config: AxiosRequestConfig) => {
// Do something before request is sent
return config;
},
(error) => {
// Do something with request error
return Promise.reject(error);
}
);
// Add a response interceptor
globalThis.FrolloClient.interceptors.response.use(
(response: AxiosResponse) => {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response;
},
(error: AxiosError) => {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
If you need to remove an interceptor later you can.
const myInterceptor = globalThis.FrolloClient.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);