UNPKG

@itwin/imodels-client-management

Version:

iModels API client wrapper for applications that manage iModels.

140 lines 5.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OperationsBase = void 0; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const Constants_1 = require("../../Constants"); const types_1 = require("../types"); const IModelsErrorParser_1 = require("./IModelsErrorParser"); class OperationsBase { _options; constructor(_options) { this._options = _options; } async sendGetRequest(params) { const urlAndHeaders = { url: params.url, headers: await this.formHeaders(params), }; if (params.responseType === types_1.ContentType.Png) return this.executeRequest(async () => this._options.restClient.sendGetRequest({ responseType: types_1.ContentType.Png, ...urlAndHeaders, })); const responseType = params.responseType ?? types_1.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: types_1.ContentType.Json, content: params.body, }, headers: await this.formHeaders({ ...params, contentType: types_1.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: types_1.ContentType.Json, content: params.body, }, headers: await this.formHeaders({ ...params, contentType: types_1.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 IModelsErrorParser_1.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_1.Constants.headers.authorization] = `${authorizationInfo.scheme} ${authorizationInfo.token}`; headers[Constants_1.Constants.headers.accept] = `application/vnd.bentley.${this._options.api.version}+json`; if (params.preferReturn) headers[Constants_1.Constants.headers.prefer] = `return=${params.preferReturn}`; if (params.contentType) headers[Constants_1.Constants.headers.contentType] = params.contentType; this.addOrUpdateHeaders(headers, this._options.headers); this.addOrUpdateHeaders(headers, params.headers); return headers; } } exports.OperationsBase = OperationsBase; //# sourceMappingURL=OperationsBase.js.map