UNPKG

@itwin/imodels-client-management

Version:

iModels API client wrapper for applications that manage iModels.

136 lines 5.45 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { Constants } from "../../Constants"; import { ContentType, } from "../types"; import { IModelsErrorBaseImpl } from "./IModelsErrorParser"; export class OperationsBase { _options; constructor(_options) { this._options = _options; } async sendGetRequest(params) { const urlAndHeaders = { url: params.url, headers: await this.formHeaders(params), }; if (params.responseType === ContentType.Png) return this.executeRequest(async () => this._options.restClient.sendGetRequest({ responseType: ContentType.Png, ...urlAndHeaders, })); const responseType = params.responseType ?? ContentType.Json; return this.executeRequest(async () => this._options.restClient.sendGetRequest({ responseType, ...urlAndHeaders, })); } async sendPostRequest(params) { return this.executeRequest(async () => this._options.restClient.sendPostRequest({ url: params.url, body: { contentType: ContentType.Json, content: params.body, }, headers: await this.formHeaders({ ...params, contentType: ContentType.Json, }), })); } async sendPutRequest(params) { const body = params.contentType ? { contentType: params.contentType, content: params.body, } : undefined; return this.executeRequest(async () => this._options.restClient.sendPutRequest({ url: params.url, body: body, headers: await this.formHeaders({ ...params, contentType: params.contentType, }), })); } async sendPatchRequest(params) { return this.executeRequest(async () => this._options.restClient.sendPatchRequest({ url: params.url, body: { contentType: ContentType.Json, content: params.body, }, headers: await this.formHeaders({ ...params, contentType: ContentType.Json, }), })); } async sendDeleteRequest(params) { return this.executeRequest(async () => this._options.restClient.sendDeleteRequest({ url: params.url, headers: await this.formHeaders(params), })); } async getEntityCollectionPage(params) { const response = await this.executeRequest(async () => this.sendGetRequest(params)); return { entities: params.entityCollectionAccessor(response), next: response.body._links.next ? async () => this.getEntityCollectionPage({ ...params, url: response.body._links.next.href, }) : undefined, }; } async executeRequest(requestFunc) { try { const response = await requestFunc(); return response; // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error) { if (error instanceof IModelsErrorBaseImpl) throw error; const parsedError = this._options.parseErrorFunc({ statusCode: error.response?.status, body: error.response?.data }, // eslint-disable-next-line @typescript-eslint/no-unsafe-argument error); throw parsedError; } } resolveHeaderValue(headerOrHeaderFactory) { if (typeof headerOrHeaderFactory === "function") return headerOrHeaderFactory(); return headerOrHeaderFactory; } addOrUpdateHeaders(existingHeaders, additionalHeaders) { if (!additionalHeaders) return; for (const headerName in additionalHeaders) { if (Object.prototype.hasOwnProperty.call(additionalHeaders, headerName)) { const headerValue = this.resolveHeaderValue(additionalHeaders[headerName]); if (typeof headerValue === "string") existingHeaders[headerName] = headerValue; else delete existingHeaders[headerName]; } } } async formHeaders(params) { const headers = {}; const authorizationInfo = await params.authorization(); headers[Constants.headers.authorization] = `${authorizationInfo.scheme} ${authorizationInfo.token}`; headers[Constants.headers.accept] = `application/vnd.bentley.${this._options.api.version}+json`; if (params.preferReturn) headers[Constants.headers.prefer] = `return=${params.preferReturn}`; if (params.contentType) headers[Constants.headers.contentType] = params.contentType; this.addOrUpdateHeaders(headers, this._options.headers); this.addOrUpdateHeaders(headers, params.headers); return headers; } } //# sourceMappingURL=OperationsBase.js.map