mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[AAE-10499] feat: create angular based custom http client for alfresco js api (#7800)
This commit is contained in:
313
lib/core/api/alfresco-api/alfresco-api.http-client.spec.ts
Normal file
313
lib/core/api/alfresco-api/alfresco-api.http-client.spec.ts
Normal file
@@ -0,0 +1,313 @@
|
||||
/*!
|
||||
* @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 { Emitters, RequestOptions, ResultListDataRepresentationTaskRepresentation, SecurityOptions } from '@alfresco/js-api';
|
||||
import { HttpParams } from '@angular/common/http';
|
||||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AlfrescoApiHttpClient } from './alfresco-api.http-client';
|
||||
import { AlfrescoApiResponseError } from './alfresco-api.response-error';
|
||||
|
||||
const securityOptions: SecurityOptions = {
|
||||
authentications: {},
|
||||
defaultHeaders: {},
|
||||
isBpmRequest: false,
|
||||
enableCsrf: true,
|
||||
withCredentials: false
|
||||
};
|
||||
|
||||
const emitter = {
|
||||
emit: () => {},
|
||||
off: () => {},
|
||||
on: () => {},
|
||||
once: () => {}
|
||||
};
|
||||
|
||||
const emitters: Emitters = {
|
||||
eventEmitter: emitter,
|
||||
apiClientEmitter: emitter
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: [
|
||||
{
|
||||
id: 14,
|
||||
name: 'nameFake1',
|
||||
created: '2017-03-01T12:25:17.189+0000'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
describe('AlfrescoApiHttpClient', () => {
|
||||
let angularHttpClient: AlfrescoApiHttpClient;
|
||||
let controller: HttpTestingController;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
HttpClientTestingModule
|
||||
]
|
||||
});
|
||||
angularHttpClient = TestBed.inject(AlfrescoApiHttpClient);
|
||||
controller = TestBed.inject(HttpTestingController);
|
||||
});
|
||||
|
||||
|
||||
describe('deserialize', () => {
|
||||
|
||||
afterEach(() => {
|
||||
controller.verify();
|
||||
});
|
||||
|
||||
it('should deserialize incoming request based on return type', (done) => {
|
||||
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
returnType: ResultListDataRepresentationTaskRepresentation,
|
||||
headerParams: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
accepts: ['application/json']
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).then((res: ResultListDataRepresentationTaskRepresentation) => {
|
||||
expect(res instanceof ResultListDataRepresentationTaskRepresentation).toBeTruthy();
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
expect(res.data![0].created instanceof Date).toBeTruthy();
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
req.flush(mockResponse);
|
||||
|
||||
});
|
||||
|
||||
it('should return parsed json object when responseType is json', (done) => {
|
||||
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
responseType: 'json'
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).then((res) => {
|
||||
expect(res).toEqual(mockResponse);
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
req.flush(mockResponse);
|
||||
|
||||
});
|
||||
|
||||
it('should emit unauthorized message for 401 request', (done) => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST'
|
||||
};
|
||||
|
||||
const spy = spyOn(emitter, 'emit').and.callThrough();
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch(() => {
|
||||
expect(spy).toHaveBeenCalledWith('unauthorized');
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
req.flush('<div></div>', { status: 401, statusText: 'unauthorized'});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('upload', () => {
|
||||
|
||||
afterEach(() => {
|
||||
controller.verify();
|
||||
});
|
||||
|
||||
it('should behave...', () => {
|
||||
const requestOptions: RequestOptions = {
|
||||
path: '/nodes/{nodeId}/children',
|
||||
httpMethod: 'POST',
|
||||
queryParams: {
|
||||
autoRename: true,
|
||||
include: 'allowableOperations',
|
||||
fields: null
|
||||
},
|
||||
formParams: {
|
||||
filedata: new File([], 'file.txt'),
|
||||
relativePath: '',
|
||||
include: ['allowableOperations'],
|
||||
renditions: 'doclib',
|
||||
autoRename: true,
|
||||
nodeType: 'cm:content'
|
||||
},
|
||||
bodyParam: {
|
||||
name: 'demo.txt',
|
||||
nodeType: 'cm:content',
|
||||
relativePath: '',
|
||||
newVersion: false,
|
||||
majorVersion: false,
|
||||
parentId: '-my-',
|
||||
path: ''
|
||||
},
|
||||
contentType: 'multipart/form-data',
|
||||
accept: 'application/json',
|
||||
returnType: null
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', requestOptions, securityOptions, emitters);
|
||||
const req = controller.expectOne('http://example.com?autoRename=true&include=allowableOperations');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
const body = req.request.body as HttpParams;
|
||||
|
||||
expect(body.get('relativePath')).toBe('');
|
||||
expect(body.get('renditions')).toBe('doclib');
|
||||
expect(body.get('autoRename')).toBeTruthy();
|
||||
expect(body.get('nodeType')).toBe('cm:content');
|
||||
expect(body.get('include')).toBe('allowableOperations');
|
||||
expect(body.get('filedata')).toEqual(jasmine.any(File));
|
||||
|
||||
req.flush('');
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a Error type on failed promise, for backward compatibility, with string value to prevent JSON.parse from crashing when we try to get status code from message', (done) => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST'
|
||||
};
|
||||
|
||||
const errorResponse = {
|
||||
error: {
|
||||
errorKey: 'Cant perform action',
|
||||
statusCode: 403
|
||||
}
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch((res: AlfrescoApiResponseError) => {
|
||||
expect(res instanceof Error).toBeTruthy();
|
||||
expect(res.message).toBe(JSON.stringify(errorResponse));
|
||||
expect(res.status).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
req.flush(errorResponse, { status: 403, statusText: 'Forbidden' });
|
||||
});
|
||||
|
||||
it('should return a Error type on failed promise with response body', (done) => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
responseType: 'blob'
|
||||
};
|
||||
|
||||
const errorResponse = new Blob();
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch((res: AlfrescoApiResponseError) => {
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.error.response.body instanceof Blob).toBeTruthy();
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
|
||||
req.flush(errorResponse, { status: 400, statusText: 'Bad request' });
|
||||
});
|
||||
|
||||
it('should correctly handle queryParams with arrays', () => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
queryParams: {
|
||||
skipCount: 0,
|
||||
status: [
|
||||
'RUNNING',
|
||||
'SUSPENDED'
|
||||
],
|
||||
sort: 'startDate,DESC'
|
||||
}
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com/candidatebaseapp/query/v1/process-instances', options, securityOptions, emitters);
|
||||
|
||||
const req = controller.expectOne('http://example.com/candidatebaseapp/query/v1/process-instances?skipCount=0&status=RUNNING&status=SUSPENDED&sort=startDate%2CDESC');
|
||||
expect(req.request.method).toEqual('POST');
|
||||
|
||||
req.flush(null, { status: 200, statusText: 'Ok' });
|
||||
});
|
||||
|
||||
it('should convert null values to empty stirng for backward compatibility', (done) => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'GET'
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).then((res) => {
|
||||
expect(res).toEqual('');
|
||||
done();
|
||||
});
|
||||
|
||||
const req = controller.expectOne('http://example.com');
|
||||
|
||||
req.flush(null, { status: 200, statusText: 'Ok' });
|
||||
});
|
||||
|
||||
it('should correctly decode types to string', () => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
queryParams: {
|
||||
lastModifiedFrom: '2022-08-17T00:00:00.000+02:00'
|
||||
}
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters);
|
||||
|
||||
const req = controller.expectOne('http://example.com?lastModifiedFrom=2022-08-17T00%3A00%3A00.000%2B02%3A00');
|
||||
|
||||
req.flush(null, { status: 200, statusText: 'Ok' });
|
||||
});
|
||||
|
||||
it('should correctly decode Date types to string ', () => {
|
||||
const options: RequestOptions = {
|
||||
path: '',
|
||||
httpMethod: 'POST',
|
||||
queryParams: {
|
||||
lastModifiedFrom: new Date('2022-08-17T00:00:00.000Z')
|
||||
}
|
||||
};
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters);
|
||||
|
||||
const req = controller.expectOne('http://example.com?lastModifiedFrom=2022-08-17T00%3A00%3A00.000Z');
|
||||
|
||||
req.flush(null, { status: 200, statusText: 'Ok' });
|
||||
});
|
||||
|
||||
});
|
224
lib/core/api/alfresco-api/alfresco-api.http-client.ts
Normal file
224
lib/core/api/alfresco-api/alfresco-api.http-client.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
/*!
|
||||
* @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 { Emitters as JsApiEmitters, HttpClient as JsApiHttpClient, RequestOptions, SecurityOptions, isBrowser } from '@alfresco/js-api';
|
||||
import { HttpClient, HttpErrorResponse, HttpEvent, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, of, Subject, throwError } from 'rxjs';
|
||||
import { catchError, map, takeUntil } from 'rxjs/operators';
|
||||
import { convertObjectToFormData, getQueryParamsWithCustomEncoder, isBlobResponse, isConstructor, isHttpResponseEvent, isHttpUploadProgressEvent, removeNilValues } from './alfresco-api.utils';
|
||||
import { AlfrescoApiParamEncoder } from './alfresco-api.param-encoder';
|
||||
import { AlfrescoApiResponseError } from './alfresco-api.response-error';
|
||||
import { Constructor } from '../types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AlfrescoApiHttpClient implements JsApiHttpClient {
|
||||
|
||||
constructor(private httpClient: HttpClient) {}
|
||||
|
||||
request<T = any>(url: string, options: RequestOptions, sc: SecurityOptions, emitters: JsApiEmitters): Promise<T> {
|
||||
const body = AlfrescoApiHttpClient.getBody(options);
|
||||
const params = getQueryParamsWithCustomEncoder(options.queryParams, new AlfrescoApiParamEncoder());
|
||||
const headers = AlfrescoApiHttpClient.getHeaders(options);
|
||||
const responseType = AlfrescoApiHttpClient.getResponseType(options);
|
||||
|
||||
const request = this.httpClient.request(
|
||||
options.httpMethod,
|
||||
url,
|
||||
{
|
||||
...(body && { body }),
|
||||
...(responseType && { responseType }),
|
||||
...(sc.withCredentials && { withCredentials: true }),
|
||||
...(params && { params }),
|
||||
headers,
|
||||
observe: 'events',
|
||||
reportProgress: true
|
||||
}
|
||||
);
|
||||
|
||||
return this.requestWithLegacyEventEmitters<T>(request, emitters, options.returnType);
|
||||
}
|
||||
|
||||
post<T = any>(url: string, options: RequestOptions, sc: SecurityOptions, emitters: JsApiEmitters): Promise<T> {
|
||||
return this.request<T>(url, { ...options, httpMethod: 'POST' }, sc, emitters);
|
||||
}
|
||||
|
||||
put<T = any>(url: string, options: RequestOptions, sc: SecurityOptions, emitters: JsApiEmitters): Promise<T> {
|
||||
return this.request<T>(url, { ...options, httpMethod: 'PUT' }, sc, emitters);
|
||||
}
|
||||
|
||||
get<T = any>(url: string, options: RequestOptions, sc: SecurityOptions, emitters: JsApiEmitters): Promise<T> {
|
||||
return this.request<T>(url, { ...options, httpMethod: 'GET' }, sc, emitters);
|
||||
}
|
||||
|
||||
delete<T = void>(url: string, options: RequestOptions, sc: SecurityOptions, emitters: JsApiEmitters): Promise<T> {
|
||||
return this.request<T>(url, { ...options, httpMethod: 'DELETE' }, sc, emitters);
|
||||
}
|
||||
|
||||
private requestWithLegacyEventEmitters<T = any>(request$: Observable<HttpEvent<T>>, emitters: JsApiEmitters, returnType: any): Promise<T> {
|
||||
|
||||
const abort$ = new Subject<void>();
|
||||
const { eventEmitter, apiClientEmitter } = emitters;
|
||||
|
||||
const promise = request$.pipe(
|
||||
map((res) => {
|
||||
if (isHttpUploadProgressEvent(res)) {
|
||||
const percent = Math.round((res.loaded / res.total) * 100);
|
||||
eventEmitter.emit('progress', { loaded: res.loaded, total: res.total, percent });
|
||||
}
|
||||
|
||||
if (isHttpResponseEvent(res)) {
|
||||
eventEmitter.emit('success', res.body);
|
||||
return AlfrescoApiHttpClient.deserialize(res, returnType);
|
||||
}
|
||||
}),
|
||||
catchError((err: HttpErrorResponse): Observable<AlfrescoApiResponseError> => {
|
||||
|
||||
// since we can't always determinate ahead of time if the response is going to be xml or plain text response
|
||||
// we need to handle false positive cases here.
|
||||
|
||||
if (err.status === 200) {
|
||||
eventEmitter.emit('success', err.error.text);
|
||||
return of(err.error.text);
|
||||
}
|
||||
|
||||
eventEmitter.emit('error', err);
|
||||
apiClientEmitter.emit('error', err);
|
||||
|
||||
if (err.status === 401) {
|
||||
eventEmitter.emit('unauthorized');
|
||||
apiClientEmitter.emit('unauthorized');
|
||||
}
|
||||
|
||||
// for backwards compatibility we need to convert it to error class as the HttpErrorResponse only implements Error interface, not extending it,
|
||||
// and we need to be able to correctly pass instanceof Error conditions used inside repository
|
||||
// we also need to pass error as Stringify string as we are detecting statusCodes using JSON.parse(error.message) in some places
|
||||
const msg = typeof err.error === 'string' ? err.error : JSON.stringify(err.error);
|
||||
|
||||
// for backwards compatibility to handle cases in code where we try read response.error.response.body;
|
||||
|
||||
const error = {
|
||||
response: { ...err, body: err.error }
|
||||
};
|
||||
|
||||
const alfrescoApiError = new AlfrescoApiResponseError(msg, err.status, error);
|
||||
|
||||
return throwError(alfrescoApiError);
|
||||
}),
|
||||
takeUntil(abort$)
|
||||
).toPromise();
|
||||
|
||||
(promise as any).abort = function() {
|
||||
eventEmitter.emit('abort');
|
||||
abort$.next();
|
||||
abort$.complete();
|
||||
return this;
|
||||
};
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
private static getBody(options: RequestOptions): any {
|
||||
const contentType = options.contentType;
|
||||
const isFormData = contentType === 'multipart/form-data';
|
||||
const isFormUrlEncoded = contentType === 'application/x-www-form-urlencoded';
|
||||
const body = options.bodyParam;
|
||||
|
||||
if (isFormData) {
|
||||
return convertObjectToFormData(options.formParams);
|
||||
}
|
||||
|
||||
if (isFormUrlEncoded) {
|
||||
return new HttpParams({ fromObject: removeNilValues(options.formParams) });
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private static getHeaders(options: RequestOptions): HttpHeaders {
|
||||
const optionsHeaders = {
|
||||
...options.headerParams,
|
||||
...(options.accept && { Accept: options.accept }),
|
||||
...((options.contentType) && { 'Content-Type': options.contentType })
|
||||
};
|
||||
|
||||
return new HttpHeaders(optionsHeaders);
|
||||
}
|
||||
|
||||
private static getResponseType(options: RequestOptions): 'blob' | 'json' | 'text' {
|
||||
|
||||
const isBlobType = options.returnType?.toString().toLowerCase() === 'blob' || options.responseType?.toString().toLowerCase() === 'blob';
|
||||
|
||||
if (isBlobType) {
|
||||
return 'blob';
|
||||
}
|
||||
|
||||
if (options.returnType === 'String') {
|
||||
return 'text';
|
||||
}
|
||||
|
||||
return 'json';
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize an HTTP response body into a value of the specified type.
|
||||
*/
|
||||
private static deserialize<T>(response: HttpResponse<T>, returnType?: Constructor<unknown> | 'blob'): any {
|
||||
|
||||
if (response === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const body = response.body;
|
||||
|
||||
if (!returnType) {
|
||||
// for backwards compatibility we need to return empty string instead of null,
|
||||
// to avoid issues when accessing null response would break application [C309878]
|
||||
// cannot read property 'entry' of null in cases like
|
||||
// return this.post(apiUrl, saveFormRepresentation).pipe(map((res: any) => res.entry))
|
||||
|
||||
return body !== null ? body : '';
|
||||
}
|
||||
|
||||
if (isBlobResponse(response, returnType)) {
|
||||
return AlfrescoApiHttpClient.deserializeBlobResponse(response);
|
||||
}
|
||||
|
||||
if (!isConstructor(returnType)) {
|
||||
return body;
|
||||
}
|
||||
|
||||
if (Array.isArray(body)) {
|
||||
return body.map((element) => new returnType(element));
|
||||
}
|
||||
|
||||
return new returnType(body);
|
||||
}
|
||||
|
||||
|
||||
private static deserializeBlobResponse(response: HttpResponse<Blob>) {
|
||||
|
||||
if (isBrowser()) {
|
||||
return new Blob([response.body], { type: response.headers.get('Content-Type') });
|
||||
}
|
||||
|
||||
return Buffer.from(response.body as unknown as WithImplicitCoercion<string>, 'binary');
|
||||
}
|
||||
}
|
||||
|
29
lib/core/api/alfresco-api/alfresco-api.param-encoder.spec.ts
Normal file
29
lib/core/api/alfresco-api/alfresco-api.param-encoder.spec.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*!
|
||||
* @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 { AlfrescoApiParamEncoder } from './alfresco-api.param-encoder';
|
||||
|
||||
describe('AlfrescoApiParamEncoder', () => {
|
||||
it('should propely encode special "+" character', () => {
|
||||
const encoder = new AlfrescoApiParamEncoder();
|
||||
const value = '2022-08-17T00:00:00.000+02:00';
|
||||
const encodeValue = '2022-08-17T00%3A00%3A00.000%2B02%3A00';
|
||||
|
||||
expect(encoder.encodeValue(value)).toBe(encodeValue);
|
||||
expect(encoder.decodeValue(encodeValue)).toBe(value);
|
||||
});
|
||||
});
|
40
lib/core/api/alfresco-api/alfresco-api.param-encoder.ts
Normal file
40
lib/core/api/alfresco-api/alfresco-api.param-encoder.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*!
|
||||
* @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 { HttpParameterCodec } from '@angular/common/http';
|
||||
|
||||
// The default implementation of HttpParameterCodec from angular
|
||||
// does not encode some special characters like + with is causing issues with the alfresco js API and returns 500 error
|
||||
|
||||
export class AlfrescoApiParamEncoder implements HttpParameterCodec {
|
||||
|
||||
encodeKey(key: string): string {
|
||||
return encodeURIComponent(key);
|
||||
}
|
||||
|
||||
encodeValue(value: string): string {
|
||||
return encodeURIComponent(value);
|
||||
}
|
||||
|
||||
decodeKey(key: string): string {
|
||||
return decodeURIComponent(key);
|
||||
}
|
||||
|
||||
decodeValue(value: string): string {
|
||||
return decodeURIComponent(value);
|
||||
}
|
||||
}
|
25
lib/core/api/alfresco-api/alfresco-api.response-error.ts
Normal file
25
lib/core/api/alfresco-api/alfresco-api.response-error.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/*!
|
||||
* @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.
|
||||
*/
|
||||
|
||||
export class AlfrescoApiResponseError extends Error {
|
||||
|
||||
public name = 'AlfrescoApiResponseError';
|
||||
|
||||
constructor(msg: string, public status: number, public error: { response: Record<string, any> }) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
99
lib/core/api/alfresco-api/alfresco-api.utils.spec.ts
Normal file
99
lib/core/api/alfresco-api/alfresco-api.utils.spec.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
/*!
|
||||
* @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 { isConstructor, getQueryParamsWithCustomEncoder, removeNilValues } from './alfresco-api.utils';
|
||||
|
||||
describe('AlfrescoApiUtils', () => {
|
||||
|
||||
describe('isConstructor', () => {
|
||||
class MockClass {}
|
||||
function mockFUnction() {}
|
||||
|
||||
it('should return true for class and functions', () => {
|
||||
expect(isConstructor(MockClass)).toBe(true);
|
||||
expect(isConstructor(mockFUnction)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for instances of a class/function', () => {
|
||||
expect(isConstructor(new MockClass())).toBe(false);
|
||||
expect(isConstructor(new mockFUnction())).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for object', () => {
|
||||
expect(isConstructor({})).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for primitive types', () => {
|
||||
expect(isConstructor('test')).toBe(false);
|
||||
expect(isConstructor(1)).toBe(false);
|
||||
expect(isConstructor(true)).toBe(false);
|
||||
expect(isConstructor(false)).toBe(false);
|
||||
expect(isConstructor(null)).toBe(false);
|
||||
expect(isConstructor(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('getQueryParamsWithCustomEncoder', () => {
|
||||
|
||||
it('should return queryParams with removed undefined values', () => {
|
||||
const actual = getQueryParamsWithCustomEncoder({
|
||||
key1: 'value1',
|
||||
key2: undefined
|
||||
});
|
||||
|
||||
expect(actual?.has('key2')).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle array values', () => {
|
||||
const actual = getQueryParamsWithCustomEncoder({
|
||||
key1: 'value1',
|
||||
key2: [undefined, 'value2', null, 'value3', '']
|
||||
});
|
||||
|
||||
expect(actual?.get('key2')).toEqual('value2');
|
||||
expect(actual?.getAll('key2')).toEqual(['value2', 'value3']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('removeUndefinedValues', () => {
|
||||
|
||||
it('should return queryParams with removed undefined values', () => {
|
||||
const actual = removeNilValues({
|
||||
key1: 'value1',
|
||||
key2: undefined,
|
||||
key3: null
|
||||
});
|
||||
|
||||
expect(actual).toEqual({
|
||||
key1: 'value1'
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array values', () => {
|
||||
const actual = getQueryParamsWithCustomEncoder({
|
||||
key1: 'value1',
|
||||
key2: [undefined, 'value2', null, 'value3', '']
|
||||
});
|
||||
|
||||
expect(actual?.get('key2')).toEqual('value2');
|
||||
expect(actual?.getAll('key2')).toEqual(['value2', 'value3']);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
91
lib/core/api/alfresco-api/alfresco-api.utils.ts
Normal file
91
lib/core/api/alfresco-api/alfresco-api.utils.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/*!
|
||||
* @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 { HttpEvent, HttpUploadProgressEvent, HttpEventType, HttpResponse, HttpParams, HttpParameterCodec, HttpUrlEncodingCodec } from '@angular/common/http';
|
||||
import { Constructor } from '../types';
|
||||
|
||||
export const isHttpUploadProgressEvent = <T>(val: HttpEvent<T>): val is HttpUploadProgressEvent => val.type === HttpEventType.UploadProgress;
|
||||
export const isHttpResponseEvent = <T>(val: HttpEvent<T>): val is HttpResponse<T> => val.type === HttpEventType.Response;
|
||||
export const isDate = (value: unknown): value is Date => value instanceof Date;
|
||||
export const isXML = (value: unknown): boolean => typeof value === 'string' && value.startsWith('<?xml');
|
||||
export const isBlobResponse = (response: HttpResponse<any>, returnType: Constructor<unknown> | 'blob'): response is HttpResponse<Blob> => returnType === 'blob' || response.body instanceof Blob;
|
||||
export const isConstructor = <T = unknown>(value: any): value is Constructor<T> => typeof value === 'function' && !!value?.prototype?.constructor.name;
|
||||
|
||||
const convertParamsToString = (value: any): any => isDate(value) ? value.toISOString() : value;
|
||||
export const getQueryParamsWithCustomEncoder = (obj: Record<string | number, unknown>, encoder: HttpParameterCodec = new HttpUrlEncodingCodec()): HttpParams | undefined => {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let httpParams = new HttpParams({
|
||||
encoder
|
||||
});
|
||||
|
||||
const params = removeNilValues(obj);
|
||||
|
||||
for (const key in params) {
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(params, key)) {
|
||||
const value = params[key];
|
||||
if (value instanceof Array) {
|
||||
const array = value.map(convertParamsToString).filter(Boolean);
|
||||
httpParams = httpParams.appendAll({
|
||||
[key]: array
|
||||
});
|
||||
} else {
|
||||
httpParams = httpParams.append(key, convertParamsToString(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return httpParams;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes null and undefined values from an object.
|
||||
*/
|
||||
export const removeNilValues = (obj: Record<string | number, unknown>) => {
|
||||
|
||||
if (!obj) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return Object.keys(obj).reduce((acc, key) => {
|
||||
const value = obj[key];
|
||||
const isNil = value === undefined || value === null;
|
||||
return isNil ? acc : { ...acc, [key]: value };
|
||||
}, {});
|
||||
};
|
||||
|
||||
|
||||
export const convertObjectToFormData = (formParams: Record<string | number, string | Blob>): FormData => {
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
for (const key in formParams) {
|
||||
if (Object.prototype.hasOwnProperty.call(formParams, key)) {
|
||||
const value = formParams[key];
|
||||
if (value instanceof File) {
|
||||
formData.append(key, value, value.name);
|
||||
} else {
|
||||
formData.append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formData;
|
||||
};
|
@@ -19,3 +19,4 @@ export * from './api-client.factory';
|
||||
export * from './api-clients.service';
|
||||
export * from './clients';
|
||||
export * from './types';
|
||||
export * from './alfresco-api/alfresco-api.http-client';
|
||||
|
Reference in New Issue
Block a user