-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
types.ts
490 lines (428 loc) · 12.5 KB
/
types.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import { QueryFunctionContext, QueryKey } from "@tanstack/react-query";
import { DocumentNode } from "graphql";
import { UseListConfig } from "../../hooks/data/useList";
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
export type BaseKey = string | number;
export type BaseRecord = {
id?: BaseKey;
[key: string]: any;
};
export type BaseOption = {
label: any;
value: any;
};
/**
* @deprecated Use `BaseOption` instead.
*/
export interface Option extends BaseOption {}
export type NestedField = {
operation: string;
variables: QueryBuilderOptions[];
fields: Fields;
};
export type Fields = Array<string | object | NestedField>;
export type VariableOptions =
| {
type?: string;
name?: string;
value: any;
list?: boolean;
required?: boolean;
}
| { [k: string]: any };
export interface QueryBuilderOptions {
operation?: string;
fields?: Fields;
variables?: VariableOptions;
}
export type GraphQLQueryOptions = {
/**
* @description GraphQL query to be used by data providers.
* @optional
* @example
* ```tsx
* import gql from 'graphql-tag'
* import { useOne } from '@refinedev/core'
*
* const PRODUCT_QUERY = gql`
* query GetProduct($id: ID!) {
* product(id: $id) {
* id
* name
* }
* }
* `
*
* useOne({
* id: 1,
* meta: { gqlQuery: PRODUCT_QUERY }
* })
* ```
*/
gqlQuery?: DocumentNode;
/**
* @description GraphQL mutation to be used by data providers.
* @optional
* @example
* ```tsx
* import gql from 'graphql-tag'
* import { useCreate } from '@refinedev/core'
*
* const PRODUCT_CREATE_MUTATION = gql`
* mutation CreateProduct($input: CreateOneProductInput!) {
* createProduct(input: $input) {
* id
* name
* }
* }
* `
* const { mutate } = useCreate()
*
* mutate({
* values: { name: "My Product" },
* meta: { gqlQuery: PRODUCT_QUERY }
* })
* ```
*/
gqlMutation?: DocumentNode;
};
export type MetaQuery = {
[k: string]: any;
queryContext?: Omit<QueryFunctionContext, "meta">;
} & QueryBuilderOptions &
GraphQLQueryOptions;
export interface Pagination {
/**
* Initial page index
* @default 1
*/
current?: number;
/**
* Initial number of items per page
* @default 10
*/
pageSize?: number;
/**
* Whether to use server side pagination or not.
* @default "server"
*/
mode?: "client" | "server" | "off";
}
/**
* @deprecated `MetaDataQuery` is deprecated with refine@4, use `MetaQuery` instead, however, we still support `MetaDataQuery` for backward compatibility.
*/
export type MetaDataQuery = {
[k: string]: any;
queryContext?: Omit<QueryFunctionContext, "meta">;
} & QueryBuilderOptions;
export interface IQueryKeys {
all: QueryKey;
resourceAll: QueryKey;
list: (
config?:
| UseListConfig
| {
pagination?: Required<Pagination>;
hasPagination?: boolean;
sorters?: CrudSort[];
filters?: CrudFilter[];
}
| undefined,
) => QueryKey;
many: (ids?: BaseKey[]) => QueryKey;
detail: (id?: BaseKey) => QueryKey;
logList: (meta?: Record<number | string, any>) => QueryKey;
}
export interface ValidationErrors {
[field: string]:
| string
| string[]
| boolean
| { key: string; message: string };
}
export interface HttpError extends Record<string, any> {
message: string;
statusCode: number;
errors?: ValidationErrors;
}
export type RefineError = HttpError;
export type MutationMode = "pessimistic" | "optimistic" | "undoable";
export type QueryResponse<T = BaseRecord> =
| GetListResponse<T>
| GetOneResponse<T>;
export type PreviousQuery<TData> = [QueryKey, TData | unknown];
export type PrevContext<TData> = {
previousQueries: PreviousQuery<TData>[];
/**
* @deprecated `QueryKeys` is deprecated in favor of `keys`. Please use `keys` instead to construct query keys for queries and mutations.
*/
queryKey: IQueryKeys;
};
export type Context = {
previousQueries: ContextQuery[];
};
export type ContextQuery<T = BaseRecord> = {
query: QueryResponse<T>;
queryKey: QueryKey;
};
// Filters are used as a suffix of a field name:
// | Filter | Description |
// | ------------------- | --------------------------------- |
// | `eq` | Equal |
// | ne | Not equal |
// | lt | Less than |
// | gt | Greater than |
// | lte | Less than or equal to |
// | gte | Greater than or equal to |
// | in | Included in an array |
// | nin | Not included in an array |
// | contains | Contains |
// | ncontains | Doesn't contain |
// | containss | Contains, case sensitive |
// | ncontainss | Doesn't contain, case sensitive |
// | null | Is null or not null |
// | startswith | Starts with |
// | nstartswith | Doesn't start with |
// | startswiths | Starts with, case sensitive |
// | nstartswiths | Doesn't start with, case sensitive|
// | endswith | Ends with |
// | nendswith | Doesn't end with |
// | endswiths | Ends with, case sensitive |
// | nendswiths | Doesn't end with, case sensitive |
export type CrudOperators =
| "eq"
| "ne"
| "lt"
| "gt"
| "lte"
| "gte"
| "in"
| "nin"
| "contains"
| "ncontains"
| "containss"
| "ncontainss"
| "between"
| "nbetween"
| "null"
| "nnull"
| "startswith"
| "nstartswith"
| "startswiths"
| "nstartswiths"
| "endswith"
| "nendswith"
| "endswiths"
| "nendswiths"
| "or"
| "and";
export type SortOrder = "desc" | "asc" | null;
export type LogicalFilter = {
field: string;
operator: Exclude<CrudOperators, "or" | "and">;
value: any;
};
export type ConditionalFilter = {
key?: string;
operator: Extract<CrudOperators, "or" | "and">;
value: (LogicalFilter | ConditionalFilter)[];
};
export type CrudFilter = LogicalFilter | ConditionalFilter;
export type CrudSort = {
field: string;
order: "asc" | "desc";
};
export type CrudFilters = CrudFilter[];
export type CrudSorting = CrudSort[];
export interface CustomResponse<TData = BaseRecord> {
data: TData;
}
export interface GetListResponse<TData = BaseRecord> {
data: TData[];
total: number;
[key: string]: any;
}
export interface CreateResponse<TData = BaseRecord> {
data: TData;
}
export interface CreateManyResponse<TData = BaseRecord> {
data: TData[];
}
export interface UpdateResponse<TData = BaseRecord> {
data: TData;
}
export interface UpdateManyResponse<TData = BaseRecord> {
data: TData[];
}
export interface GetOneResponse<TData = BaseRecord> {
data: TData;
}
export interface GetManyResponse<TData = BaseRecord> {
data: TData[];
}
export interface DeleteOneResponse<TData = BaseRecord> {
data: TData;
}
export interface DeleteManyResponse<TData = BaseRecord> {
data: TData[];
}
export interface GetListParams {
resource: string;
pagination?: Pagination;
/**
* @deprecated `hasPagination` is deprecated, use `pagination.mode` instead.
*/
hasPagination?: boolean;
/**
* @deprecated `sort` is deprecated, use `sorters` instead.
*/
sort?: CrudSort[];
sorters?: CrudSort[];
filters?: CrudFilter[];
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
dataProviderName?: string;
}
export interface GetManyParams {
resource: string;
ids: BaseKey[];
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
dataProviderName?: string;
}
export interface GetOneParams {
resource: string;
id: BaseKey;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface CreateParams<TVariables = {}> {
resource: string;
variables: TVariables;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface CreateManyParams<TVariables = {}> {
resource: string;
variables: TVariables[];
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface UpdateParams<TVariables = {}> {
resource: string;
id: BaseKey;
variables: TVariables;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface UpdateManyParams<TVariables = {}> {
resource: string;
ids: BaseKey[];
variables: TVariables;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface DeleteOneParams<TVariables = {}> {
resource: string;
id: BaseKey;
variables?: TVariables;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface DeleteManyParams<TVariables = {}> {
resource: string;
ids: BaseKey[];
variables?: TVariables;
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export interface CustomParams<TQuery = unknown, TPayload = unknown> {
url: string;
method: "get" | "delete" | "head" | "options" | "post" | "put" | "patch";
/**
* @deprecated `sort` is deprecated, use `sorters` instead.
*/
sort?: CrudSort[];
sorters?: CrudSort[];
filters?: CrudFilter[];
payload?: TPayload;
query?: TQuery;
headers?: {};
meta?: MetaQuery;
/**
* @deprecated `metaData` is deprecated with refine@4, refine will pass `meta` instead, however, we still support `metaData` for backward compatibility.
*/
metaData?: MetaQuery;
}
export type DataProvider = {
getList: <TData extends BaseRecord = BaseRecord>(
params: GetListParams,
) => Promise<GetListResponse<TData>>;
getMany?: <TData extends BaseRecord = BaseRecord>(
params: GetManyParams,
) => Promise<GetManyResponse<TData>>;
getOne: <TData extends BaseRecord = BaseRecord>(
params: GetOneParams,
) => Promise<GetOneResponse<TData>>;
create: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: CreateParams<TVariables>,
) => Promise<CreateResponse<TData>>;
createMany?: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: CreateManyParams<TVariables>,
) => Promise<CreateManyResponse<TData>>;
update: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: UpdateParams<TVariables>,
) => Promise<UpdateResponse<TData>>;
updateMany?: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: UpdateManyParams<TVariables>,
) => Promise<UpdateManyResponse<TData>>;
deleteOne: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: DeleteOneParams<TVariables>,
) => Promise<DeleteOneResponse<TData>>;
deleteMany?: <TData extends BaseRecord = BaseRecord, TVariables = {}>(
params: DeleteManyParams<TVariables>,
) => Promise<DeleteManyResponse<TData>>;
getApiUrl: () => string;
custom?: <
TData extends BaseRecord = BaseRecord,
TQuery = unknown,
TPayload = unknown,
>(
params: CustomParams<TQuery, TPayload>,
) => Promise<CustomResponse<TData>>;
};
export type DataProviders = {
default: DataProvider;
[key: string]: DataProvider;
};
export type IDataContext = DataProviders;
export type DataBindings = DataProvider | DataProviders;