Skip to content

Commit bacbaf2

Browse files
Tim Streichercoronoro
Tim Streicher
authored andcommitted
feat: add type docs
1 parent f63b3f8 commit bacbaf2

File tree

2 files changed

+211
-3
lines changed

2 files changed

+211
-3
lines changed

docs/advanced-typescript.md

+106-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,110 @@
11
# Typescript
22

3+
This package exposes the type `FilterSetConfig` for typing hints.
4+
The Type is generic and expects at least one Parameter.
5+
In the following section examples are given on how to use these types to achieve different typing behaviours.
6+
7+
8+
## Data Parameter
9+
Let's assume there exists a model `Category`:
10+
``` ts
11+
interface Category {
12+
id: number
13+
name: string
14+
}
15+
```
16+
Passing the `Category` type as the first type parameter allows only attributes that the interface actually posses:
17+
``` ts
18+
const config: FilterSetConfig<Category> = {
19+
id: {
20+
in: [1,2,3]
21+
}
22+
}
23+
```
24+
25+
### Nested Types
26+
Nested types are also supported:
27+
``` ts
28+
interface Book {
29+
id: number
30+
category: Category
31+
}
32+
33+
...
34+
35+
const config: FilterSetConfig<Book> = {
36+
category: {
37+
id:{
38+
in: [1,2,3]
39+
}
40+
}
41+
}
42+
```
43+
44+
## Key-Configuration
45+
46+
With a key configuration it is possible to specify which filters are valid for a specific parameter.
47+
``` ts
48+
interface CategoryKeyConfig {
49+
name : 'exact' | 'startwith' | 'endswith'
50+
}
51+
52+
...
53+
54+
const config: FilterSetConfig<Category, CategoryKeyConfig> = {
55+
name: {
56+
exact: 'Fantasy'
57+
}
58+
}
59+
```
60+
This would only allow `exact`, `startwith` and `endswith` for the `name` attribute
61+
62+
::: tip
63+
Attributes that are not mentioned in the key configuration still allow all filters.
64+
Note that the `value` filter cannot be disallowed.
65+
:::
66+
67+
### Nested Types
68+
69+
For nested types the config could look like so:
70+
``` ts
71+
interface BookKeyConfig {
72+
category : {
73+
name: 'exact' | 'startwith' | 'endswith'
74+
}
75+
}
76+
```
77+
This is a little redundant but that way it is possible to define different behaviours for the models.
78+
To remove the redundant Part the config can also be written like this:
79+
``` ts
80+
interface BookKeyConfig {
81+
category : CategoryKeyConfig
82+
}
83+
```
84+
85+
## Custom Filter
86+
Custom Filter can be defined via:
87+
``` ts
88+
interface CustomFilter {
89+
notIn: any[]
90+
}
91+
```
392
::: warning
4-
This is a work in progress. The documentation is not complete yet.
93+
There is no generic type hinting for custom filters yet.
94+
Using `any` as the type allows the filter to be used everywhere but also loosens the typing of the passed value.
595
:::
96+
97+
To enable type hinting them they need to be added to a key configuration.
98+
``` ts
99+
interface CategoryKeyConfig {
100+
id: 'in' | 'notIn'
101+
}
102+
103+
...
104+
105+
const config: FilterSetConfig<Category, CategoryKeyConfig, CustomFilter> = {
106+
id: {
107+
notIn: [1,2,3]
108+
}
109+
}
110+
```

docs/de/advanced-typescript.md

+105-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,108 @@
11
# Typescript
22

3-
::: warning
4-
Diese Seite ist noch in Arbeit. Die Dokumentation ist noch nicht vollständig.
3+
Dieses Package nutzt den Typ `FilterSetConfig` für Type-Hints.
4+
Der Typ ist Generisch aud erwartet mindestens einen Parameter.
5+
Im den folgenden Abschnitten werden beispiele Vorgestellt, wie diese genutzt werden können um verschiedene Verhalten abzubilden.
6+
7+
8+
## Data Parameter
9+
Angenommen es existiert ein Model `Category`:
10+
``` ts
11+
interface Category {
12+
id: number
13+
name: string
14+
}
15+
```
16+
`Category` als Parameter zu übergeben bewirkt, dass nur Attribute des Interfaces in der Konfiguration erlaubt sind:
17+
``` ts
18+
const config: FilterSetConfig<Category> = {
19+
id: {
20+
in: [1,2,3]
21+
}
22+
}
23+
```
24+
25+
### Nested Types
26+
Nested-Types werden auch unterstützt:
27+
``` ts
28+
interface Book {
29+
id: number
30+
category: Category
31+
}
32+
33+
...
34+
35+
const config: FilterSetConfig<Book> = {
36+
category: {
37+
id:{
38+
in: [1,2,3]
39+
}
40+
}
41+
}
42+
```
43+
44+
## Key-Configuration
45+
Mit einer Key-Konfiguration ist es möglich zu spezifizieren welche Filter für eine Attribut erlaubt sind.
46+
``` ts
47+
interface CategoryKeyConfig {
48+
name : 'exact' | 'startwith' | 'endswith'
49+
}
50+
51+
...
52+
53+
const config: FilterSetConfig<Category, CategoryKeyConfig> = {
54+
name: {
55+
exact: 'Fantasy'
56+
}
57+
}
58+
```
59+
Diese Konfiguration erlaubt nur `exact`, `startwith` und `endswith` für das Attribut `name`
60+
61+
::: tip
62+
Attribute, die in der Key-Konnfiguration nicht erwähnt werden, erlauben weiterhin alle Filter.
63+
Beachte, dass der `value`-Filter nicht ausgeschlossen werden kann.
564
:::
65+
66+
### Nested Types
67+
68+
Für Nested-Types wie folgt aussehen:
69+
``` ts
70+
interface BookKeyConfig {
71+
category : {
72+
name: 'exact' | 'startwith' | 'endswith'
73+
}
74+
}
75+
```
76+
Das ist zwar etwas redundant, aber ermöglicht es, unterschiedliche Verhaltensweisen für die Modelle zu definieren.
77+
Um den redundanten Teil zu entfernen, kann die Konfiguration auch folgendermaßen geschrieben werden:
78+
``` ts
79+
interface BookKeyConfig {
80+
category : CategoryKeyConfig
81+
}
82+
```
83+
84+
## Custom Filter
85+
Custom-Filter können wie folgt definiert werden:
86+
``` ts
87+
interface CustomFilter {
88+
notIn: any[]
89+
}
90+
```
91+
::: warning
92+
Es gibt noch keine generische Type-Hints für benutzerdefinierte Filter.
93+
Die Verwendung von `any` als Typ erlaubt die Verwendung des Filters überall, lockert aber auch die Typisierung des übergebenen Wertes.:::
94+
95+
Um die Type-Hints zu aktivieren, muss der Filter einer Schlüsselkonfiguration hinzugefügt werden.
96+
``` ts
97+
interface CategoryKeyConfig {
98+
id: 'in' | 'notIn'
99+
}
100+
101+
...
102+
103+
const config: FilterSetConfig<Category, CategoryKeyConfig, CustomFilter> = {
104+
id: {
105+
notIn: [1,2,3]
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)