Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove parse-query-paramter fn #412

Merged
merged 3 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ coverage
npm-debug.log*
.idea
reports
docs/.vitepress/cache
File renamed without changes.
49 changes: 49 additions & 0 deletions docs/guide/fields-api-reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
# Fields

## `parseQueryFields`

**Type**
```ts
declare function parseQueryFields<T>(
input: unknown,
options?: FieldsParseOptions<T>
): FieldsParseOutput
```

**Example**

```typescript
import {
parseQueryFields,
} from 'rapiq';

const output = parseQueryFields(
['+name'],
{
allowed: ['id', 'name'],
defaultAlias: 'user'
}
);

console.log(output);
// [{key: 'id', value: FieldOperator.INCLUDE}] ||
// [{key: 'id', value: '+'}]
```

**Type parameters**

| Name | Description |
|:------|:------------|


**Parameters**

| Name | Type | Description |
|:----------|:------------------------|:-------------------------------------------------------------|
| `input` | `unknown` | Query input data passed e.g. via URL . |
| `options` | `FieldsParseOptions<T>` | Options for parsing fields data [more](#fieldsparseoptions). |

**Returns**

[FieldsParseOutput](#fieldsparseoutput)

The function returns an object.

## `FieldsBuildInput`
The following types are defined, to illustrate which kind of input data is covered by the
`FieldsBuildInput<T>` type.
Expand Down
48 changes: 48 additions & 0 deletions docs/guide/filters-api-reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# Filters

## `parseQueryFilters`

**Type**
```ts
declare function parseQueryFilters<T>(
input: unknown,
options?: FiltersParseOptions<T>
): FiltersParseOutput
```

**Example**

```typescript
import {
parseQueryFilters
} from 'rapiq';

const output = parseQueryFilters(
{id: 1},
{
allowed: ['id', 'name'],
defaultAlias: 'user'
}
);

console.log(output);
// [{alias: 'user', key: 'id', value: 1, }]
```

**Type parameters**

| Name | Description |
|:------|:------------|


**Parameters**

| Name | Type | Description |
|:----------|:-------------------------|:---------------------------------------------------------------|
| `input` | `unknown` | Query input data passed e.g. via URL . |
| `options` | `FiltersParseOptions<T>` | Options for parsing filters data [more](#filtersparseoptions). |

**Returns**

[FiltersParseOutput](#filtersparseoutput)

The function returns an object.

## `FiltersBuildInput`

The following types are defined, to illustrate which kind of input data is covered by the
Expand Down
47 changes: 47 additions & 0 deletions docs/guide/pagination-api-reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
# Pagination

## `parseQueryPagination`

**Type**
```ts
declare function parseQueryPagination(
input: unknown,
options?: PaginationParseOptions
): PaginationParseOutput
```

**Example**

```typescript
import {
parseQueryPagination
} from 'rapiq';

const output = parseQueryPagination(
{limit: 100},
{
maxLimit: 50
}
);

console.log(output);
// {limit: 50}
```

**Type parameters**

| Name | Description |
|:------|:------------|


**Parameters**

| Name | Type | Description |
|:----------|:----------------------------|:---------------------------------------------------------------------|
| `input` | `unknown` | Query input data passed e.g. via URL . |
| `options` | `PaginationParseOptions<T>` | Options for parsing pagination data [more](#paginationparseoptions). |

**Returns**

[PaginationParseOutput](#paginationparseoutput)

The function returns an object.

## `PaginationBuildInput`

```typescript
Expand Down
193 changes: 17 additions & 176 deletions docs/guide/parse-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,165 +59,26 @@ The function returns an object.
- [ParseOutput](#parseoutput)
- [ParseOptions](#parseoptions)

## `parseQueryParameter`

Parse a query string to an efficient data structure ⚡. The output will
be an object with each possible value of the [Parameter](parameter-api-reference#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**

| Name | Description |
|:------|:------------|


**Parameters**

| Name | Type | Description |
|:----------|:-----------------------------------|:-----------------------------------------------------------------------|
| `input` | `unknown` | Query input data passed e.g. via URL [more](#parseinput). |
| `options` | `ParseParameterOptions<Parameter>` | Options for parsing fields, filter, include, ... [more](#parseoptions) |

**Returns**

[ParseOutput](#parseoutput)

The function returns an object.

## ParseOptions

```typescript
type ParseOptions = {
/**
* On default all query keys are enabled.
*/
[K in Parameter]?: ParseParameterOptions<K> | boolean
} & {
[Parameter.FIELDS]?: FieldsParseOptions<T> | boolean,
[Parameter.FILTERS]?: FiltersParseOptions<T> | boolean,
[Parameter.RELATIONS]?: RelationsParseOptions<T> | boolean,
[Parameter.PAGINATION]?: PaginationParseOptions | boolean,
[Parameter.SORT]?: SortParseOptions<T> | boolean,
defaultPath?: string,
throwOnFailure?: boolean
}
```
**References**
- [ParseParameterOptions](#parseparameteroptions)
- [Parameter](parameter-api-reference#parameter)
- [FieldsParseOptions](fields-api-reference.md#fieldsparseoptions),
- [FiltersParseOptions](filters-api-reference.md#filtersparseoptions)
- [PaginationParseOptions](pagination-api-reference.md#paginationparseoptions)
- [RelationsParseOptions](relations-api-reference.md#relationsparseoptions)
- [SortParseOptions](sort-api-reference.md#sortparseoptions)

## ParseInput

Expand All @@ -234,35 +95,15 @@ type ParseInput = {

```typescript
type ParseOutput = {
[K in Parameter]?: ParseParameterOutput<K>
[Parameter.FIELDS]?: FieldsParseOutput,
[Parameter.FILTERS]?: FiltersParseOutput,
[Parameter.RELATIONS]?: RelationsParseOutput,
[Parameter.PAGINATION]?: PaginationParseOutput,
[Parameter.SORT]?: SortParseOutput,
}
```
**References**
- [ParseParameterOutput](#parseparameteroutput)

## 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**
- [FieldsParseOptions](fields-api-reference.md#fieldsparseoptions),
- [FiltersParseOptions](filters-api-reference.md#filtersparseoptions)
- [PaginationParseOptions](pagination-api-reference.md#paginationparseoptions)
- [RelationsParseOptions](relations-api-reference.md#relationsparseoptions)
- [SortParseOptions](sort-api-reference.md#sortparseoptions)

## ParseParameterOutput
```typescript
type ParseParameterOutput<P extends ParameterType | URLParameterType>;
```

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

**References**
- [Parameter](parameter-api-reference#parameter)
- [FieldsParseOutput](fields-api-reference.md#fieldsparseoutput)
- [FiltersParseOutput](filters-api-reference.md#filtersparseoutput)
- [PaginationParseOutput](pagination-api-reference.md#paginationparseoutput)
Expand Down
Loading