UNPKG

@aep_dev/aep-lib-ts

Version:

Utility libraries for AEP TypeScript-based tools including case conversion, OpenAPI utilities, and API clients

118 lines 4.46 kB
export class Client { headers; client; requestLoggingFunction; responseLoggingFunction; constructor(client, headers, requestLoggingFunction, responseLoggingFunction) { this.client = client; this.headers = headers; this.requestLoggingFunction = requestLoggingFunction; this.responseLoggingFunction = responseLoggingFunction; } async create(ctx, resource, serverUrl, body, parameters) { let suffix = ""; if (resource.createMethod?.supportsUserSettableCreate) { const id = body.id; if (!id) { throw new Error(`id field not found in ${JSON.stringify(body)}`); } if (typeof id === "string") { suffix = `?id=${id}`; } } const url = this.basePath(ctx, resource, serverUrl, parameters, suffix); const response = await this.makeRequest(ctx, "POST", url, body); return response; } async list(ctx, resource, serverUrl, parameters) { const url = this.basePath(ctx, resource, serverUrl, parameters, ""); const response = await this.makeRequest(ctx, "GET", url); const kebab = this.kebabToCamelCase(resource.plural); const lowerKebab = kebab.length > 1 ? kebab.charAt(0).toLowerCase() + kebab.slice(1) : ""; const possibleKeys = ["results", resource.plural, kebab, lowerKebab]; for (const key of possibleKeys) { if (response[key] && Array.isArray(response[key])) { return response[key].filter((item) => typeof item === "object"); } } throw new Error("No valid list key was found"); } async get(ctx, serverUrl, path) { const url = `${serverUrl}/${path.replace(/^\//, "")}`; return this.makeRequest(ctx, "GET", url); } async getWithFullUrl(ctx, url) { return this.makeRequest(ctx, "GET", url); } async delete(ctx, serverUrl, path) { const url = `${serverUrl}/${path.replace(/^\//, "")}`; await this.makeRequest(ctx, "DELETE", url); } async update(ctx, serverUrl, path, body) { const url = `${serverUrl}/${path.replace(/^\//, "")}`; return this.makeRequest(ctx, "PATCH", url, body); } async makeRequest(ctx, method, url, body) { if (body) { Object.keys(body).forEach(key => { if (body[key] === null) { delete body[key]; } }); } const config = { method, url, headers: this.headers, data: body, }; this.requestLoggingFunction(ctx, config); try { const response = await this.client.request(config); this.responseLoggingFunction(ctx, response); const data = response.data; this.checkErrors(data); return data; } catch (error) { if (error.response) { this.responseLoggingFunction(ctx, error.response); throw new Error(`Request failed: ${JSON.stringify(error.response.data)} for request ${JSON.stringify(config)}`); } throw error; } } checkErrors(response) { if (response.error) { throw new Error(`Returned errors: ${JSON.stringify(response.error)}`); } } basePath(ctx, resource, serverUrl, parameters, suffix) { serverUrl = serverUrl.replace(/\/$/, ""); const urlElems = [serverUrl]; for (let i = 0; i < resource.patternElems.length - 1; i++) { const elem = resource.patternElems[i]; if (i % 2 === 0) { urlElems.push(elem); } else { const paramName = elem.slice(1, -1); const value = parameters[paramName]; if (!value) { throw new Error(`Parameter ${paramName} not found in parameters ${JSON.stringify(parameters)}`); } const lastValue = value.split("/").pop() || value; urlElems.push(lastValue); } } let result = urlElems.join("/"); if (suffix) { result += suffix; } return result; } kebabToCamelCase(str) { return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()); } } //# sourceMappingURL=client.js.map