/*! * @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 = (val: HttpEvent): val is HttpUploadProgressEvent => val.type === HttpEventType.UploadProgress; export const isHttpResponseEvent = (val: HttpEvent): val is HttpResponse => 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(', returnType: Constructor | 'blob'): response is HttpResponse => returnType === 'blob' || response.body instanceof Blob; export const isConstructor = (value: any): value is Constructor => typeof value === 'function' && !!value?.prototype?.constructor.name; const convertParamsToString = (value: any): any => isDate(value) ? value.toISOString() : value; export const getQueryParamsWithCustomEncoder = (obj: Record, 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) => { 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): 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; };