Customer JS SDK main repo.
npm install customer-js-sdkThe customer-js-sdk is a Front-End layer of abstraction for communicating with Back-End services.
bash
npm install --save customer-js-sdk
`Import
`javascript
import { v0 as sdk } from 'customer-js-sdk';
`BaseUrl
- Setting the
baseUrl`javascript
sdk.baseUrl.set('http://custom.api/');
`Headers
- Setting a header
`javascript
sdk.headers.append('Content-Type', 'application/json');
`- Retrieving all headers
`javascript
sdk.headers.get();
`- Removing a header
`javascript
sdk.headers.remove('Content-Type');
`Usage Examples
-
GET request without parameters`javascript
sdk.merchants.partners.get();
`-
GET request with URL parameter`javascript
sdk.merchants.addresses.get({
urlParam: '123456789'
});
`-
POST request with body`javascript
sdk.merchants.contacts.post({}, {
body: '123456789'
});
`-
POST request with URL parameter and body`javascript
sdk.merchants.contacts.post({
urlParam: '123456789'
}, {
body: '123456789'
});
`- Passing a custom
baseUrl to overide the default in a specific request`javascript
sdk.merchants.partners.get('https://custom.api/');sdk.merchants.addresses.get('https://custom.api/', {
urlParam: '123456789'
});
sdk.merchants.contacts.post('https://custom.api/', {
urlParam: '123456789'
}, {
body: '123456789'
});
`Using Middlewares
Middlewares run before and after every request in the same order that they were declared. Every middleware must call either proceed or quit in order to work.`javascript
sdk.middleware.use({
beforeRequest({ proceed, quit }) {
if (conditionMet) {
proceed();
} else {
quit();
}
},
afterRequest({ proceed }) {
logSomething();
proceed();
}
});sdk.middleware.use({
async beforeRequest({ proceed }) {
await doSomethingAsync();
proceed();
}
});
``