/*! * @license * Copyright 2019 Alfresco Software, Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { paramToString, RequestOptions } from '@alfresco/js-api'; import { JsApiHttpClient } from '../js-api/js-api-http-client'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; /** tslint:disable-next line */ export class JsApiAngularHttpClient implements JsApiHttpClient { public basePath: string; constructor ( private host: string, private contextRoot: string, private servicePath: string, private httpClient: HttpClient ) { this.basePath = `${this.host}/${this.contextRoot}${this.servicePath}`; } request(options: RequestOptions): Promise { const responseType = this.getResponseType(options); return this.httpClient.request( options.httpMethod, options.url, { ...(options.bodyParam ? { body: options.bodyParam } : {}), ...(options.headerParams ? { headers: new HttpHeaders(options.headerParams) } : {}), observe: 'body', ...(options.queryParams ? { params: new HttpParams({ fromObject: options.queryParams })} : {}), ...(responseType ? { responseType } : {}), }).toPromise() as unknown as Promise; } private getResponseType(options: RequestOptions): 'arraybuffer' | 'blob' | 'json' | 'text' { let responseType = null; if (options.returnType?.toString().toLowerCase() === 'blob' || options.responseType?.toString().toLowerCase() === 'blob') { responseType = 'blob'; } else if (options.returnType === 'String') { responseType = 'text'; } return responseType; } post(options: RequestOptions): Promise { return this.request({ ...options, httpMethod: 'POST', contentTypes: options.contentTypes || ['application/json'], accepts: options.accepts || ['application/json'] }); } put(options: RequestOptions): Promise { return this.request({ ...options, httpMethod: 'PUT', contentTypes: options.contentTypes || ['application/json'], accepts: options.accepts || ['application/json'] }); } get(options: RequestOptions): Promise { return this.request({ ...options, httpMethod: 'GET', contentTypes: options.contentTypes || ['application/json'], accepts: options.accepts || ['application/json'] }); } delete(options: RequestOptions): Promise { return this.request({ ...options, httpMethod: 'DELETE', contentTypes: options.contentTypes || ['application/json'], accepts: options.accepts || ['application/json'] }); } /** @deprecated */ callApi( path: string, httpMethod: string, pathParams?: any, queryParams?: any, headerParams?: any, formParams?: any, bodyParam?: any, contentTypes?: string[], accepts?: string[], returnType?: any, contextRoot?: string, responseType?: string, url?: string ): Promise { const basePath = contextRoot ? `${this.host}/${contextRoot}` : this.basePath; url = url ?? this.buildUrl(basePath, path, pathParams); return this.request({ path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, returnType, contextRoot, responseType, url }); } /** @deprecated */ callCustomApi( fullPath: string, httpMethod: string, pathParams?: any, queryParams?: any, headerParams?: any, formParams?: any, bodyParam?: any, contentTypes?: string[], accepts?: string[], returnType?: any, contextRoot?: string, responseType?: string ): Promise { const url = this.buildUrl(fullPath, '', pathParams); return this.request({ path: fullPath, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, returnType, contextRoot, responseType, url }); } private buildUrl(basePath: string, path: string, pathParams: any): string { if (path && path !== '' && !path.match(/^\//)) { path = '/' + path; } let url = basePath + path; url = url.replace(/\{([\w-]+)\}/g, function (fullMatch, key) { let value; if (pathParams.hasOwnProperty(key)) { value = paramToString(pathParams[key]); } else { value = fullMatch; } return encodeURIComponent(value); }); return url; } }