generated from jasonsturges/vite-typescript-npm-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
114 lines (99 loc) · 2.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const SQL_OPS = [
'*in_',
'*eq',
'*neq',
'*contains',
'*icontains',
'*websearch_to_tsquery',
'*gt',
'*gte',
'*lt',
'*lte',
'*like',
'*contained_by',
'*has_key',
'*has_all',
'*intersects',
'*intersects_nd',
'*same',
'*above',
'*below',
'*datetime',
'*datetime_naive',
'*startswith',
'*endswith'
] as const;
// request params
export type BaseAuthRequestParams = {
auth_token?: string;
};
export type SortParam<TModel> = {
key: keyof TModel;
desc: boolean;
};
export interface BaseRequestParams<TModel> extends BaseAuthRequestParams {
page: number;
limit: number;
columns?: Array<keyof TModel>;
sort?: SortParam<TModel>[];
where?: Where<TModel>;
}
// utils
type UnionKeys<K> = K extends K ? keyof K : never;
type Expand<K> = K extends K ? { [key in keyof K]: K[key] } : never;
type OneOf<K extends unknown[]> = {
[key in keyof K]: Expand<K[key] & Partial<Record<Exclude<UnionKeys<K[number]>, keyof K[key]>, never>>>;
}[number];
type AndGrouping<TModel> = {
'*and': (Clause<TModel> | LogicalGrouping<TModel>)[];
};
type OrGrouping<TModel> = {
'*or': (Clause<TModel> | LogicalGrouping<TModel>)[];
};
type NotGrouping<TModel> = {
'*not': Clause<TModel> | LogicalGrouping<TModel>;
};
export type LogicalGrouping<TModel> = OneOf<[AndGrouping<TModel>, OrGrouping<TModel>, NotGrouping<TModel>]>;
type Value = string | number | boolean | null;
type SqlOps = (typeof SQL_OPS)[number];
type SqlOp<S extends SqlOps> = {
[key in Exclude<SqlOps, Exclude<SqlOps, S>>]: Value | Value[];
};
export type Logic = OneOf<
[
SqlOp<'*in_'>,
SqlOp<'*eq'>,
SqlOp<'*neq'>,
SqlOp<'*contains'>,
SqlOp<'*icontains'>,
SqlOp<'*websearch_to_tsquery'>,
SqlOp<'*gt'>,
SqlOp<'*gte'>,
SqlOp<'*lt'>,
SqlOp<'*lte'>,
SqlOp<'*like'>,
SqlOp<'*contained_by'>,
SqlOp<'*has_key'>,
SqlOp<'*has_all'>,
SqlOp<'*intersects'>,
SqlOp<'*intersects_nd'>,
SqlOp<'*same'>,
SqlOp<'*above'>,
SqlOp<'*below'>,
SqlOp<'*datetime'>,
SqlOp<'*datetime_naive'>,
SqlOp<'*startswith'>,
SqlOp<'*endswith'>
]
>;
// (SM) make a type where all keys of T with '.${any string}'following them are valid keys
type Dots<T> = {
[K in keyof T & string as `${K}.${string}`]: T[K];
};
export type Clause<TModel> = {
[key in keyof (TModel & Dots<TModel>)]: Record<key, Logic> &
Partial<Record<Exclude<keyof TModel, key>, never>> extends infer O
? Expand<O>
: never;
}[keyof (TModel & Dots<TModel>)];
export type Where<TModel> = LogicalGrouping<TModel> | Clause<TModel>;