34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
|
|
import { ENDPOINTS } from "./endpoints.js";
|
||
|
|
function toCamelCase(name) {
|
||
|
|
return name.charAt(0).toLowerCase() + name.slice(1);
|
||
|
|
}
|
||
|
|
function pickMethod(endpoint) {
|
||
|
|
if (endpoint.httpMethods.includes("POST"))
|
||
|
|
return "POST";
|
||
|
|
if (endpoint.httpMethods.includes("GET"))
|
||
|
|
return "GET";
|
||
|
|
if (endpoint.httpMethods.includes("PUT"))
|
||
|
|
return "PUT";
|
||
|
|
return endpoint.httpMethods[0];
|
||
|
|
}
|
||
|
|
export function createNamespaceService(http, namespace) {
|
||
|
|
const endpoints = ENDPOINTS.filter((e) => e.namespace === namespace);
|
||
|
|
const handler = async (operation, body, query) => {
|
||
|
|
const endpoint = endpoints.find((e) => e.operation.toLowerCase() === operation.toLowerCase());
|
||
|
|
if (!endpoint) {
|
||
|
|
throw new Error(`Unknown ${namespace} operation: ${operation}`);
|
||
|
|
}
|
||
|
|
return http.request(endpoint.path, {
|
||
|
|
method: pickMethod(endpoint),
|
||
|
|
body,
|
||
|
|
query,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const service = handler;
|
||
|
|
for (const endpoint of endpoints) {
|
||
|
|
const methodName = toCamelCase(endpoint.operation);
|
||
|
|
service[methodName] = (body, query) => handler(endpoint.operation, body, query);
|
||
|
|
}
|
||
|
|
return service;
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=service.js.map
|