UNPKG

huddle-record

Version:

Record is a Object-Relational Mapping (ORM) TypeScript plugin that makes it easy to work with data.

110 lines (109 loc) 4.5 kB
import Http from '../http.js'; import { record } from '../main.js'; export default class Api { model; repository; config; constructor(model, repository, config) { this.model = model; this.repository = repository; this.config = config; } async get(endpoint, options) { return await this.makeRequest("GET" /* HttpRequestMethod.GET */, { url: endpoint, options, callback: (url) => Http.get(url, options?.config), }); } async post(endpoint, data, options) { return await this.makeRequest("POST" /* HttpRequestMethod.POST */, { url: endpoint, options, callback: (url) => Http.post(url, data, options?.config), }); } async put(endpoint, data, options) { /* * TODO: Currently, Http.put will replace the entire record with the data provided, instead of updating the record. * We can fix this but we'll have to write a function that can update a model and all of its relationships. */ return await this.makeRequest("PUT" /* HttpRequestMethod.PUT */, { url: endpoint, options, callback: (url) => Http.put(url, data, options?.config), }); } async patch(endpoint, data, options) { return await this.makeRequest("PATCH" /* HttpRequestMethod.PATCH */, { url: endpoint, options, callback: (url) => Http.patch(url, data, options?.config), }); } async head(endpoint, options) { return await this.makeRequest("HEAD" /* HttpRequestMethod.HEAD */, { url: endpoint, options, callback: (url) => Http.head(url, options?.config), }); } async delete(endpoint, options) { return await this.makeRequest("DELETE" /* HttpRequestMethod.DELETE */, { url: endpoint, options, callback: (url) => Http.delete(url, options?.config), }); } async makeRequest(method, apiRequest) { const requestOptions = this.returnApiOptions(apiRequest.options); if (requestOptions.dataset && requestOptions.dataset === '') { requestOptions.dataset = 'all'; } return new Promise(async (resolve, reject) => { await apiRequest .callback(apiRequest.url && apiRequest.url !== '' ? apiRequest.url : this.config.route) .then((res) => { let instances = []; if (!["HEAD" /* HttpRequestMethod.HEAD */, "DELETE" /* HttpRequestMethod.DELETE */].includes(method)) { const dataToTransform = requestOptions.source ? res.data[requestOptions.source] : res.data; if (dataToTransform) { const savedInstances = this.repository.transform(requestOptions.source ? res.data[requestOptions.source] : res.data, requestOptions); // if savedInstances is an array, then we can just assign it to instances if (Array.isArray(savedInstances)) { instances = savedInstances; } else { // if savedInstances is not an array, then we need to wrap it in an array instances.push(savedInstances); } } } else if (method === "DELETE" /* HttpRequestMethod.DELETE */ && typeof requestOptions.delete !== 'undefined') { this.repository.delete(requestOptions.delete, requestOptions.deleteValue, { dataset: requestOptions?.dataset ?? 'all', }); } resolve({ response: res, instances, }); }) .catch((err) => { reject(err); }); }); } returnApiOptions(options) { let defaultEndpointOptions = { config: record.api.axiosInstance.defaults, replace: true, save: true, source: record.api.source, dataset: 'all', prepend: false, }; defaultEndpointOptions = { ...defaultEndpointOptions, ...this.config }; return options ? { ...defaultEndpointOptions, ...options } : defaultEndpointOptions; } }