fmptools
v0.1.9
Published
Transform Foresight Mobile Platform RPC requests
Downloads
55
Readme
Install
npm i fmptools
Usage
Use Axios interceptors to convert response and/or request data automatically for all FMP requests:
import axios from 'axios';
import { createRequestInterceptor, createResponseInterceptor } from 'fmptools';
const http = axios.create();
const init = async () => {
const { data: schema } = await http.get('https://example.com/api/v1/schema');
const requestInterceptor = createRequestInterceptor(schema);
http.interceptors.request.use(requestInterceptor);
const responseInterceptor = createResponseInterceptor(schema);
http.interceptors.response.use(responseInterceptor);
};
const makeRequest = async () => {
const url = 'https://example.com/api/v1/rpc/ZFM_USERDATA/';
const { data: responseData } = await http.post(url);
return responseData;
};
Or convert response/request data manually:
import axios from 'axios';
import { convertRequestData, convertResponseData } from 'fmptools';
const makeRequest = async () => {
const { data: schema } = await axios.get('https://example.com/api/v1/schema');
const resource = 'ZFM_CREATE_TASK';
const requestData = {
IT_DATA: [
{
TITLE: 'Task #1',
USERID: 1,
DATE: '2023-09-26',
TIME: '06:48:17',
EMAIL: '[email protected]',
},
],
};
/* Table entities will be converted to rows as arrays
* IT_DATA: [['Task #1', 1, '2023-09-26', '06:48:17', '[email protected]']]
*/
const requestDataConverted = convertRequestData(requestData, schema, resource);
const url = `https://example.com/api/v1/rpc/${resource}/`;
const options = { data: requestDataConverted };
const { data } = await axios.post(url, options);
const responseData = convertResponseData(data, schema, resource);
return responseData;
};