2.2.0-alpha • Published 6 years ago
@misakey/api v2.2.0-alpha
@misakey/api
yarn add @misakey/apiAn instance of Class API implements one method: API.use().
use accepts as parameter an endpoint Types Definition defined
like this:
export default {
read: {
method: 'GET',
path: '/applications/:mainDomain',
auth: true,
},
};It's possible to nest Types Defs
API instance is constructed with these Types Definition
that are given as parameters to the use method:
// src/screens/MyScreen
import API from '@misakey/api';
import Endpoint from '@misakey/api/endpoints/Endpoint';
const myEndpoint = API.use(API.endpoints.application.read);
console.log(myEndpoint instanceof Endpoint); // trueAs you can see use returns a Class Endpoint instance which implements
the following methods (build and send):
// src/screens/MyScreen
import API from '@misakey/api';
console.log(locationParams); // { mainDomain: 'misakey.com', otherLocationParam: true }
const params = locationParams;
const payload = undefined;
const queryParams = undefined;
const myRequestPromise = API.use(API.endpoints.application.read)
.build(params, payload, queryParams)
.send();
console.log(myRequestPromise instanceof Promise); // true
myRequestPromise.then(response => console.log(response)); // { httpStatus: 200, body: {} }If a mock is implemented you can call fakeReponse method with an httpStatus code:
API.use(API.endpoints.application.read).fakeResponse(200).then(handleResponse);
New endpoints need to compel with rules defined in @src/api/API.test.js