Skip to content
On this page

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

ts
declare function parseQuery(input: ParseInput, options?: ParseOptions): string;

Example

typescript
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

NameDescription

Parameters

NameTypeDescription
inputParseInputQuery input data passed e.g. via URL more.
optionsParseOptionsOptions for parsing fields, filter, include, ... more

Returns

ParseOutput

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

ts
declare function parseQueryParameter(
    key: T,
    input: unknown,
    options?: ParseParameterOptions<T>
): ParseParameterOutput<T>

Example: Fields

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

NameDescription

Parameters

NameTypeDescription
inputunknownQuery input data passed e.g. via URL more.
optionsParseParameterOptions<Parameter>Options for parsing fields, filter, include, ... more

Returns

ParseOutput

The function returns an object.

ParseOptions

typescript
type ParseOptions = {
    /**
     * On default all query keys are enabled.
     */
    [K in Parameter]?: ParseParameterOptions<K> | boolean
} & {
    defaultPath?: string,
    throwOnFailure?: boolean
}

References

ParseInput

typescript
type ParseInput = {
    [K in Parameter | URLParameter]?: any
}

References

ParseOutput

typescript
type ParseOutput = {
    [K in Parameter]?: ParseParameterOutput<K>
}

References

ParseParameterOptions

typescript
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

typescript
type ParseParameterOutput<P extends ParameterType | URLParameterType>;

is a generic type and returns the parsed output data for a given parameter type:

References