UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

48 lines (47 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = require("axios"); const app_repo_utility_1 = require("../shared/app.repo.utility"); class APIClient { constructor(config) { const { api_base_url, authentication_type, api_request_headers, end_points } = config; this.reqHeaders = this.getRequestHeaders(api_request_headers); this.baseURL = api_base_url; this.endPoints = this.getEndpoints(end_points); } getResponse(args) { const { endPoint, path, options, headers } = args; let { uri, method } = this.endPoints[endPoint]; let reqHeader = this.reqHeaders; if (headers) { reqHeader = Object.assign(reqHeader, headers); } uri = app_repo_utility_1.App_Repository_Utility.formatURLPath(uri, path); switch (method.toUpperCase()) { case 'GET': return axios_1.default.get(this.baseURL + uri, { headers: reqHeader }); case 'POST': return axios_1.default.post(this.baseURL + uri, options, { headers: reqHeader }); } } getRequestHeaders(headers) { let reqHeaders = {}; if (headers && headers.length > 0) { headers.forEach((header) => { reqHeaders = Object.assign(reqHeaders, header); }); } return reqHeaders; } getEndpoints(end_points) { if (end_points instanceof Array && end_points.length > 0) { end_points = end_points[0]; } return Object.keys(end_points).reduce((methods, name) => { const { endpoint, method } = end_points[name]; methods[name] = Object.assign({}, { uri: endpoint, method }); return methods; }, {}); } } exports.APIClient = APIClient;