libs/ngx-pfe/pfe-service-activator/params/params.service.ts
Methods |
|
| Public getPathParams | ||||||||
getPathParams(pathParams?: PathParams[])
|
||||||||
|
Processes path parameters and returns them as a key-value object. Evaluates conditions for each parameter and retrieves values from the application state.
Parameters :
Example :
Returns :
literal type | undefined
An object containing the processed path parameters as key-value pairs, or undefined if no parameters provided |
| Public getQueryParams | ||||||||
getQueryParams(queryParams?: PfeQueryParam[])
|
||||||||
|
Processes query parameters and returns them as an Angular HttpParams object. Evaluates conditions for each parameter and retrieves values from the application state.
Parameters :
Example :
Returns :
HttpParams | undefined
An HttpParams instance containing the processed query parameters, or undefined if no valid parameters |
| Public validateQueryAndPathParams | ||||||||
validateQueryAndPathParams(params: literal type)
|
||||||||
|
Validates query or path parameters to ensure they contain only primitive values. Throws an error if any parameter is undefined or a non-primitive value (object/array) that would be incorrectly serialized as "[object Object]" in the URL.
Parameters :
Example :
Returns :
void
|
import { inject, Injectable } from '@angular/core';
import { HttpParams } from '@angular/common/http';
import { PfeStateService } from '../../services/pfe-state-service/state.service';
import { PfeServiceActivatorGetValueService } from '../get-value';
import { PathParams, PfeQueryParam } from './params.model';
import { PfeConditionsService } from '../../pfe-conditions/public-api';
@Injectable()
export class PfeServiceActivatorParamsService {
protected getValueService = inject(PfeServiceActivatorGetValueService);
protected pfeStateService = inject(PfeStateService);
protected pfeConditionsService = inject(PfeConditionsService);
/**
* Processes path parameters and returns them as a key-value object.
* Evaluates conditions for each parameter and retrieves values from the application state.
*
* @param pathParams - Array of path parameter configurations to process
* @returns An object containing the processed path parameters as key-value pairs, or undefined if no parameters provided
* @throws Error if any parameter values are undefined or non-primitive (objects/arrays)
*
* @example
* const pathParams = [{ id: 'contractId', value: '$.contract.id' }];
* const result = getPathParams(pathParams); // Returns { contractId: 'CONTRACT_ID' }
*/
public getPathParams(pathParams?: PathParams[]): { [x: string]: unknown } | undefined {
if (!pathParams?.length) return;
// Get the params as a JSON Object
const params = this.getPathParamsAsObject(pathParams);
// Ensure no JSON or undefined values present
this.validateQueryAndPathParams(params);
return Object.keys(params).length ? params : undefined;
}
/**
* Processes query parameters and returns them as an Angular HttpParams object.
* Evaluates conditions for each parameter and retrieves values from the application state.
*
* @param queryParams - Array of query parameter configurations to process
* @returns An HttpParams instance containing the processed query parameters, or undefined if no valid parameters
* @throws Error if any parameter values are undefined or non-primitive (objects/arrays)
*
* @example
* const queryParams = [{ name: 'status', value: '$.filter.status' }];
* const result = getQueryParams(queryParams); // Returns HttpParams with status=active
*/
public getQueryParams(queryParams?: PfeQueryParam[]): HttpParams | undefined {
if (!queryParams?.length) return;
// Get the params as JSON Object
const params = this.getQueryParamsAsObject(queryParams);
// Ensure no JSON or undefined values present
this.validateQueryAndPathParams(params);
return Object.keys(params).length ? new HttpParams({ fromObject: params }) : undefined;
}
/**
* Converts path parameters array into a key-value object.
* Evaluates optional conditions for each parameter and retrieves values from the state.
*
* @param pathParams - Array of path parameter configurations
* @returns Object with parameter IDs as keys and resolved values
* @private
*/
private getPathParamsAsObject(pathParams: PathParams[]): { [x: string]: unknown } {
const params: { [x: string]: unknown } = {};
const fullState = this.pfeStateService.getFullState();
pathParams.forEach((param) => {
const shouldExecute = param.conditions ? this.pfeConditionsService.evaluateConditions(param.conditions, fullState) : true;
if (shouldExecute) {
params[param.id] = this.getValueService.getValueByExpression(fullState, param.value);
}
});
return params;
}
/**
* Converts query parameters array into a key-value object.
* Evaluates optional conditions for each parameter and retrieves values from the state.
*
* @param queryParams - Array of query parameter configurations
* @returns Object with parameter names as keys and resolved values
* @private
*/
private getQueryParamsAsObject(queryParams: PfeQueryParam[]): { [x: string]: any } {
const params: { [x: string]: unknown } = {};
const fullState = this.pfeStateService.getFullState();
queryParams.forEach((param) => {
const shouldExecute = param.conditions ? this.pfeConditionsService.evaluateConditions(param.conditions, fullState) : true;
if (shouldExecute) {
params[param.name] = this.getValueService.getValueByExpression(fullState, param.value);
}
});
return params;
}
/**
* Validates query or path parameters to ensure they contain only primitive values.
* Throws an error if any parameter is undefined or a non-primitive value (object/array)
* that would be incorrectly serialized as "[object Object]" in the URL.
*
* @param params - Object containing parameter key-value pairs to validate
* @throws Error if any parameter value is undefined or a non-primitive type
*
* @example
* validateQueryAndPathParams({ id: 'SOMETHING_ID', status: 'ACTIVE' }); // Valid - no error
* validateQueryAndPathParams({ id: undefined }); // Throws error - undefined value
* validateQueryAndPathParams({ data: { nested: 'object' } }); // Throws error - object value
*/
public validateQueryAndPathParams(params: { [x: string]: unknown }) {
const undefinedValues = Object.keys(params).filter((key) => {
const value = params[key];
return typeof value === 'undefined';
});
const objectValues = Object.keys(params).filter((key) => {
const value = params[key];
return typeof value === 'object' && value !== null;
});
if (objectValues?.length || undefinedValues?.length) {
const parts: string[] = ['Unable to build the URL / Path.'];
if (objectValues?.length) {
parts.push(`Object/array values for keys: ${objectValues.join(', ')}.`);
}
if (undefinedValues?.length) {
parts.push(`Undefined values for keys: ${undefinedValues.join(', ')}.`);
}
parts.push('Only primitive values should be used in query/path params.');
throw new Error(parts.join(' '));
}
}
}