Parse
parseQuery
Parse a query string to an efficient data structure ⚡. The output will be an object with each possible value of the Parameter enum as property key and the parsed data as value.
Type
declare function parseQuery(input: ParseInput, options?: ParseOptions): string;
Example
import {
parseQuery,
} from 'rapiq';
const output = parseQuery({
fields: ['+age'],
filters: {
name: '~pe'
}
});
console.log(output);
//{
// fields: [
// {key: 'age', operator: FieldOperator.INCLUDE}
// ],
// filters: [
// {key: 'name', value: 'pe', operator: FilterComparisonOperator.LIKE}
// ]
//}
Type Parameters
Name | Description |
---|
Parameters
Name | Type | Description |
---|---|---|
input | ParseInput | Query input data passed e.g. via URL more. |
options | ParseOptions | Options for parsing fields, filter, include, ... more |
Returns
The function returns an object.
References
parseQueryParameter
Parse a query string to an efficient data structure ⚡. The output will be an object with each possible value of the Parameter enum as property key and the parsed data as value.
Type
declare function parseQueryParameter(
key: T,
input: unknown,
options?: ParseParameterOptions<T>
): ParseParameterOutput<T>
Example: Fields
import {
parseQueryParameter,
} from 'rapiq';
const output = parseQueryParameter(
// 'fields' ||
// Parameter.FIELDS | URLParameter.FIELDS
'fields',
['+name'],
{
allowed: ['id', 'name'],
defaultAlias: 'user'
}
);
console.log(output);
// [{key: 'id', value: FieldOperator.INCLUDE}] ||
// [{key: 'id', value: '+'}]
Example: Filters
import {
parseQueryParameter
} from 'rapiq';
const output = parseQueryParameter(
// 'filters' | 'filter' |
// Parameter.FILTERS | URLParameter.FILTERS
'filters',
{id: 1},
{
allowed: ['id', 'name'],
defaultAlias: 'user'
}
);
console.log(output);
// [{alias: 'user', key: 'id', value: 1, }]
Example: Pagination
import {
parseQueryParameter
} from 'rapiq';
const output = parseQueryParameter(
// 'pagination' | 'page' |
// Parameter.PAGINATION | URLParameter.PAGINATION
'pagination',
{limit: 100},
{
maxLimit: 50
}
);
console.log(output);
// {limit: 50}
Example: Relations
import {
parseQueryParameter
} from 'rapiq';
const output = parseQueryParameter(
// 'relations' || 'include' ||
// Parameter.RELATIONS | URLParameter.RELATIONS
'relations',
['roles'],
{
allowed: ['roles', 'photos'],
defaultAlias: 'user'
}
);
console.log(output);
// [{key: 'user.roles', value: 'roles'}]
Example: Sort
import {
parseQueryParameter
} from 'rapiq';
const output = parseQueryParameter(
// 'sort' ||
// Parameter.SORT || URLParameter.SORT
'sort',
['-name'],
{
allowed: ['id', 'name'],
defaultAlias: 'user'
}
);
console.log(output);
// [{alias: 'user', key: 'name', value: 'DESC'}]
Type parameters
Name | Description |
---|
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Query input data passed e.g. via URL more. |
options | ParseParameterOptions<Parameter> | Options for parsing fields, filter, include, ... more |
Returns
The function returns an object.
ParseOptions
type ParseOptions = {
/**
* On default all query keys are enabled.
*/
[K in Parameter]?: ParseParameterOptions<K> | boolean
} & {
defaultPath?: string,
throwOnFailure?: boolean
}
References
ParseInput
type ParseInput = {
[K in Parameter | URLParameter]?: any
}
References
ParseOutput
type ParseOutput = {
[K in Parameter]?: ParseParameterOutput<K>
}
References
ParseParameterOptions
type ParseParameterOptions<
P extends ParameterType | URLParameterType,
T extends Record<string, any> = Record<string, any>
>;
is a generic type and returns the available options for a given parameter type: References
ParseParameterOutput
type ParseParameterOutput<P extends ParameterType | URLParameterType>;
is a generic type and returns the parsed output data for a given parameter type:
References