@cododel/alto 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/default/default/extensions/filters/comment.ts +9 -0
  2. package/dist/default/default/extensions/filters/contains.ts +8 -0
  3. package/dist/default/default/extensions/filters/directus.ts +336 -0
  4. package/dist/default/default/extensions/filters/drop_first.ts +8 -0
  5. package/dist/default/default/extensions/filters/entries.ts +13 -0
  6. package/dist/default/default/extensions/filters/falsey.ts +17 -0
  7. package/dist/default/default/extensions/filters/indent.ts +6 -0
  8. package/dist/default/default/extensions/filters/inflections.ts +25 -0
  9. package/dist/default/default/extensions/filters/json.ts +10 -0
  10. package/dist/default/default/extensions/filters/log.ts +5 -0
  11. package/dist/default/default/extensions/filters/object_set.ts +11 -0
  12. package/dist/default/default/extensions/filters/push.ts +10 -0
  13. package/dist/default/default/extensions/filters/quote.ts +19 -0
  14. package/dist/default/default/extensions/filters/regex_replace.ts +10 -0
  15. package/dist/default/default/extensions/filters/splice.ts +13 -0
  16. package/dist/default/default/extensions/filters/split.ts +9 -0
  17. package/dist/default/default/extensions/filters/string_cases.ts +72 -0
  18. package/dist/default/default/extensions/filters/truthy.ts +17 -0
  19. package/dist/default/default/extensions/filters/typescript.ts +10 -0
  20. package/dist/default/default/extensions/filters/unshift.ts +10 -0
  21. package/dist/default/default/extensions/filters/wrap.ts +10 -0
  22. package/dist/default/default/extensions/tags/.gitkeep +0 -0
  23. package/dist/default/default/includes/typescript/get-field-jsdoc.liquid +11 -0
  24. package/dist/default/default/includes/typescript/get-field-type.liquid +121 -0
  25. package/dist/default/default/macros/typescript/types.njk +1 -0
  26. package/dist/default/default/templates/default/client.ts.njk +518 -0
  27. package/dist/default/default/templates/default/types.ts.njk +134 -0
  28. package/dist/default/extensions/filters/comment.ts +9 -0
  29. package/dist/default/extensions/filters/contains.ts +8 -0
  30. package/dist/default/extensions/filters/directus.ts +336 -0
  31. package/dist/default/extensions/filters/drop_first.ts +8 -0
  32. package/dist/default/extensions/filters/entries.ts +13 -0
  33. package/dist/default/extensions/filters/falsey.ts +17 -0
  34. package/dist/default/extensions/filters/indent.ts +6 -0
  35. package/dist/default/extensions/filters/inflections.ts +25 -0
  36. package/dist/default/extensions/filters/json.ts +10 -0
  37. package/dist/default/extensions/filters/log.ts +5 -0
  38. package/dist/default/extensions/filters/object_set.ts +11 -0
  39. package/dist/default/extensions/filters/push.ts +10 -0
  40. package/dist/default/extensions/filters/quote.ts +19 -0
  41. package/dist/default/extensions/filters/regex_replace.ts +10 -0
  42. package/dist/default/extensions/filters/splice.ts +13 -0
  43. package/dist/default/extensions/filters/split.ts +9 -0
  44. package/dist/default/extensions/filters/string_cases.ts +72 -0
  45. package/dist/default/extensions/filters/truthy.ts +17 -0
  46. package/dist/default/extensions/filters/typescript.ts +10 -0
  47. package/dist/default/extensions/filters/unshift.ts +10 -0
  48. package/dist/default/extensions/filters/wrap.ts +10 -0
  49. package/dist/default/extensions/tags/.gitkeep +0 -0
  50. package/dist/default/includes/typescript/get-field-jsdoc.liquid +11 -0
  51. package/dist/default/includes/typescript/get-field-type.liquid +121 -0
  52. package/dist/default/macros/typescript/types.njk +1 -0
  53. package/dist/default/templates/default/client.ts.njk +518 -0
  54. package/dist/default/templates/default/types.ts.njk +134 -0
  55. package/dist/index.js +757 -740
  56. package/package.json +13 -9
@@ -0,0 +1,121 @@
1
+ {%- liquid
2
+ assign types = []
3
+ assign schema = field.type.raw.schema
4
+ assign meta = field.type.raw.meta
5
+ assign nullable = false
6
+
7
+ assign is_json = false
8
+ case schema.data_type
9
+ when 'uuid'
10
+ assign types = types | array_unshift: 'UUID'
11
+ when 'json'
12
+ assign is_json = true
13
+ when 'text'
14
+ assign types = types | array_unshift: 'string'
15
+ when 'integer'
16
+ assign types = types | array_unshift: 'number'
17
+ when 'bigint'
18
+ assign types = types | array_unshift: 'BigInt'
19
+ when 'boolean'
20
+ assign types = types | array_unshift: 'boolean'
21
+ when 'character varying'
22
+ assign types = types | array_unshift: 'string'
23
+ when 'timestamp':
24
+ assign types = types | array_unshift: 'Date'
25
+ when 'timestamp with time zone':
26
+ assign types = types | array_unshift: 'Date'
27
+ when 'timestamp without time zone':
28
+ assign types = types | array_unshift: 'Date'
29
+ endcase
30
+
31
+ assign json_type = false
32
+ assign is_cast_json = meta.special | array_contains: "cast-json"
33
+ if is_cast_json or is_json
34
+ if schema.json_schema != null
35
+ assign json_type = '"json_schema"'
36
+ else
37
+ assign json_type = 'any'
38
+ endif
39
+ endif
40
+
41
+ case meta.interface
42
+ when 'tags'
43
+ assign types = types | array_unshift: 'string[]'
44
+ when 'select-dropdown'
45
+ assign values = meta.options.choices | map: 'value' | quote
46
+ for value in values
47
+ if value == null
48
+ # assign types = types | array_unshift: 'null'
49
+ assign nullable = true
50
+ else
51
+ assign types = types | array_unshift: value
52
+ endif
53
+ endfor
54
+ assign json_type = false
55
+ endcase
56
+
57
+ if schema.is_nullable
58
+ # assign types = types | array_push: 'null'
59
+ assign nullable = true
60
+ endif
61
+ if json_type != false
62
+ assign types = types | array_unshift: json_type
63
+ endif
64
+
65
+ assign is_user = meta.special | array_contains: "user-created"
66
+ if is_user
67
+ assign types = types | array_push: "Directus.DirectusUser<Schema>"
68
+ endif
69
+
70
+ assign is_user = meta.special | array_contains: "user-updated"
71
+ if is_user
72
+ assign types = types | array_push: "Directus.DirectusUser<Schema>"
73
+ endif
74
+
75
+ assign is_file = meta.special | array_contains: "file"
76
+ if is_file
77
+ assign types = types | array_push: "Directus.DirectusFile<Schema>"
78
+ endif
79
+
80
+ assign is_files = meta.special | array_contains: "files"
81
+ if is_files
82
+ assign types = types | array_push: "Directus.DirectusFile<Schema>[]"
83
+ endif
84
+
85
+ if field.is_translations
86
+ assign types = types | array_push: field.translations_collection
87
+ endif
88
+
89
+ if field.type.is_relationship
90
+ if field.type.relationship.type == 'o2m'
91
+ assign types = types | array_push: "CARALHO1"
92
+ endif
93
+ if field.type.relationship.type == 'm2o'
94
+ assign types = types | array_push: "CARALHO2"
95
+ endif
96
+ if field.type.relationship.type == 'a2o'
97
+ assign types = types | array_push: "CARALHO3"
98
+ endif
99
+ endif
100
+
101
+ assign type_count = types | array_length
102
+ if type_count <= 0
103
+ assign schemaStr = schema | json
104
+ assign metaStr = meta | json
105
+ assign unknown = "UnknownType<{ schema: " | append: schemaStr | append: ", meta: " | append: metaStr | append: " }>"
106
+ assign types = types | array_unshift: unknown
107
+ endif
108
+
109
+ assign output = ""
110
+ assign is_no_data = meta.special | array_contains: "no-data"
111
+ if is_no_data
112
+ assign output = "never"
113
+ else
114
+ assign output = types | join: ' | '
115
+ if nullable
116
+ assign output = output | wrap: 'Optional<', '>'
117
+ endif
118
+ endif
119
+
120
+ echo output
121
+ -%}
@@ -0,0 +1,518 @@
1
+ {% block header %}
2
+ /**
3
+ * This file is automatically generated by the `@indirectus/cli` package.
4
+ * Follow the package's instruction to update this file with the latest schema.
5
+ */
6
+ {% endblock %}
7
+
8
+ {% block imports %}
9
+ import type * as Directus from "@directus/sdk";
10
+ import type { System, Collections} from "./types";
11
+
12
+ import * as DirectusSDK from "@directus/sdk";
13
+
14
+ type DirectusSDK = typeof DirectusSDK;
15
+
16
+ {% endblock %}
17
+
18
+ /**
19
+ * Schema definition.
20
+ */
21
+ export interface Schema extends System {
22
+ {% for collection in registry.collections -%}
23
+ {% if not collection.is_system %}
24
+ {% set suffix = "" if collection.is_singleton else "[]" %}
25
+ /**
26
+ * The {{ collection.name | to_collection_text }} collection.
27
+ */
28
+ {{ collection.name | to_ts_identifier }}: Collections.{{ collection.name | pascal_case }}{{ suffix }};
29
+ {% endif %}
30
+ {% endfor %}
31
+ }
32
+
33
+ export interface TypedCollectionItemsWrapper<Collection extends object>
34
+ {
35
+ /**
36
+ * Creates many items in the collection.
37
+ */
38
+ create<const Query extends DirectusSDK.Query<Schema, Collection>>(items: Partial<Collection>[], query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']>[]>;
39
+
40
+ /**
41
+ * Read many items from the collection.
42
+ */
43
+ query<const Query extends DirectusSDK.Query<Schema, Collection>>(query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']>[]>;
44
+
45
+ /**
46
+ * Read the first item from the collection matching the query.
47
+ */
48
+ find<const Query extends DirectusSDK.Query<Schema, Collection>>(query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']> | undefined>;
49
+
50
+ /**
51
+ * Update many items in the collection.
52
+ */
53
+ update<const Query extends DirectusSDK.Query<Schema, Collection[]>>(keys: string[] | number[], patch: Partial<Collection>, query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']>[]>;
54
+
55
+ /**
56
+ * Remove many items in the collection.
57
+ */
58
+ remove<const Query extends DirectusSDK.Query<Schema, Collection>>(keys: string[] | number[]): Promise<void>;
59
+ }
60
+
61
+ export interface TypedCollectionItemWrapper<Collection extends object>
62
+ {
63
+ /**
64
+ * Create a single item in the collection.
65
+ */
66
+ create<const Query extends DirectusSDK.Query<Schema, Collection>>(item: Partial<Collection>, query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']>>;
67
+
68
+ /**
69
+ * Read a single item from the collection.
70
+ */
71
+ get<const Query extends DirectusSDK.Query<Schema, Collection>>(key: string | number, query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']> | undefined>;
72
+
73
+ /**
74
+ * Update a single item from the collection.
75
+ */
76
+ update<const Query extends DirectusSDK.Query<Schema, Collection>>(key: string | number, patch: Partial<Collection>, query?: Query): Promise<DirectusSDK.ApplyQueryFields<Schema, Collection, Query['fields']> | undefined>;
77
+
78
+ /**
79
+ * Remove many items in the collection.
80
+ */
81
+ remove<const Query extends DirectusSDK.Query<Schema, Collection>>(key: string | number): Promise<void>;
82
+ }
83
+
84
+ /**
85
+ * Helper functions
86
+ */
87
+
88
+ {% for collection in registry.collections -%}
89
+
90
+ {% set collectionName = collection.name | to_collection_name %}
91
+ {% set collectionString = collection.name | to_collection_string %}
92
+ {% set collectionType = ["Collections.", collection.name | to_collection_name] | join %}
93
+ {% set genericQuery = ["const Query extends Directus.Query<Schema, ", collectionType, ">"] | join %}
94
+ {% set genericQueryArray = ["const Query extends Directus.Query<Schema, ", collectionType, "[]>"] | join %}
95
+ {% set applyType = ["DirectusSDK.ApplyQueryFields<Schema, ", collectionType, ", Query['fields']>"] | join %}
96
+
97
+ {% if not collection.is_system %}
98
+
99
+ {% if collection.is_singleton %}
100
+ /**
101
+ * Reads the {{ collection.name | to_collection_text }} singleton.
102
+ */
103
+ export function read{{ collectionName }}<
104
+ {{ genericQuery }},
105
+ >(query?: Query) {
106
+ return DirectusSDK.readSingleton<Schema, {{ collectionString }}, Query>("{{ collection.name }}", query);
107
+ }
108
+
109
+ /**
110
+ * Reads the {{ collection.name | to_collection_text }} singleton.
111
+ */
112
+ export const get{{ collectionName }} = read{{ collectionName }};
113
+
114
+ {% else %}
115
+
116
+ /**
117
+ * Create many {{ collection.name | to_collection_text }} items.
118
+ */
119
+ export function create{{ collectionName }}Items<
120
+ {{ genericQueryArray}}
121
+ >(items: Partial<{{ collectionType }}>[], query?: Query) {
122
+ return DirectusSDK.createItems<Schema, {{ collectionString }}, Query>("{{ collection.name }}", items, query);
123
+ }
124
+
125
+ /**
126
+ * Create a single {{ collection.name | to_collection_text }} item.
127
+ */
128
+ export function create{{ collectionName }}Item<
129
+ const Query extends DirectusSDK.Query<Schema, {{ collectionType }}[]> // Is this a mistake? Why []?
130
+ >(item: Partial<{{ collectionType }}>, query?: Query) {
131
+ return DirectusSDK.createItem<Schema, {{ collectionString }}, Query>("{{ collection.name }}", item, query);
132
+ }
133
+
134
+ /**
135
+ * Read many {{ collection.name | to_collection_text }} items.
136
+ */
137
+ export function read{{ collectionName }}Items<
138
+ {{ genericQuery }},
139
+ >(query?: Query) {
140
+ return DirectusSDK.readItems<Schema, {{ collectionString }}, Query>("{{ collection.name }}", query);
141
+ }
142
+
143
+ /**
144
+ * Read many {{ collection.name | to_collection_text }} items.
145
+ */
146
+ export const list{{ collectionName }} = read{{ collectionName }}Items;
147
+
148
+ /**
149
+ * Gets a single known {{ collection.name | to_collection_text }} item by id.
150
+ */
151
+ export function read{{ collectionName }}Item<
152
+ {{ genericQuery }},
153
+ >(key: string | number, query?: Query) {
154
+ return DirectusSDK.readItem<Schema, {{ collectionString }}, Query>("{{ collection.name }}", key, query);
155
+ }
156
+
157
+ /**
158
+ * Gets a single known {{ collection.name | to_collection_text }} item by id.
159
+ */
160
+ export const read{{ collectionName }} = read{{ collectionName }}Item;
161
+
162
+ /**
163
+ * Read many {{ collection.name | to_collection_text }} items.
164
+ */
165
+ export function update{{ collectionName }}Items<
166
+ {{ genericQueryArray }},
167
+ >(keys: string[] | number[], patch: Partial<{{ collectionType }}>, query?: Query) {
168
+ return DirectusSDK.updateItems<Schema, {{ collectionString }}, Query>("{{ collection.name }}", keys, patch, query);
169
+ }
170
+
171
+ /**
172
+ * Gets a single known {{ collection.name | to_collection_text }} item by id.
173
+ */
174
+ export function update{{ collectionName }}Item<
175
+ {{ genericQueryArray }},
176
+ >(key: string | number, patch: Partial<{{ collectionType }}>, query?: Query) {
177
+ return DirectusSDK.updateItem<Schema, {{ collectionString }}, Query>("{{ collection.name }}", key, patch, query);
178
+ }
179
+
180
+ /**
181
+ * Deletes many {{ collection.name | to_collection_text }} items.
182
+ */
183
+ export function delete{{ collectionName }}Items<
184
+ {{ genericQueryArray }},
185
+ >(keys: string[] | number[]) {
186
+ return DirectusSDK.deleteItems<Schema, {{ collectionString }}, Query>("{{ collection.name }}", keys);
187
+ }
188
+
189
+ /**
190
+ * Deletes a single known {{ collection.name | to_collection_text }} item by id.
191
+ */
192
+ export function delete{{ collectionName }}Item(key: string | number) {
193
+ return DirectusSDK.deleteItem<Schema, {{ collectionString }}>("{{ collection.name }}", key);
194
+ }
195
+
196
+ export class {{ collectionName }}Items implements TypedCollectionItemsWrapper<{{ collectionType }}>
197
+ {
198
+ /**
199
+ *
200
+ */
201
+ constructor(private client: Directus.DirectusClient<Schema> & Directus.RestClient<Schema>)
202
+ {
203
+ }
204
+
205
+ /**
206
+ * Creates many items in the collection.
207
+ */
208
+ async create<
209
+ const Query extends DirectusSDK.Query<Schema, {{ collectionType }}>
210
+ >(
211
+ items: Partial<{{ collectionType }}>[],
212
+ query?: Query
213
+ ): Promise<
214
+ {{ applyType }}[]
215
+ > {
216
+ return await this.client.request(create{{ collectionName }}Items(items, query as any)) as any; // Seems like a bug in the SDK.
217
+ }
218
+
219
+ /**
220
+ * Read many items from the collection.
221
+ */
222
+ async query<{{ genericQuery }}>(query?: Query): Promise<{{ applyType }}[]>
223
+ {
224
+ return await this.client.request(read{{ collectionName }}Items(query));
225
+ }
226
+
227
+ /**
228
+ * Read the first item from the collection matching the query.
229
+ */
230
+ async find<{{ genericQuery }}>(query?: Query): Promise<{{ applyType }} | undefined>
231
+ {
232
+ const items = await this.client.request(read{{ collectionName }}Items({
233
+ ...query,
234
+ limit: 1,
235
+ }));
236
+ return items?.[0] as any; // TODO: fix
237
+ }
238
+
239
+ /**
240
+ * Update many items in the collection.
241
+ */
242
+ async update<{{ genericQueryArray }}>(keys: string[] | number[], patch: Partial<{{ collectionType }}>, query?: Query): Promise<{{ applyType }}[]>
243
+ {
244
+ return await this.client.request(update{{ collectionName }}Items(keys, patch, query));
245
+ }
246
+
247
+ /**
248
+ * Remove many items in the collection.
249
+ */
250
+ async remove<{{ genericQuery }}>(keys: string[] | number[]): Promise<void>
251
+ {
252
+
253
+ }
254
+ }
255
+
256
+ export class {{ collectionName }}Item implements TypedCollectionItemWrapper<{{ collectionType }}>
257
+ {
258
+ /**
259
+ *
260
+ */
261
+ constructor(private client: Directus.DirectusClient<Schema> & Directus.RestClient<Schema>)
262
+ {
263
+ }
264
+
265
+ /**
266
+ * Create a single item in the collection.
267
+ */
268
+ async create<{{ genericQuery }}>(item: Partial<{{ collectionType }}>, query?: Query): Promise<{{ applyType }}>
269
+ {
270
+ return await this.client.request(create{{ collectionName }}Item(item, query as any)) as any;
271
+ }
272
+
273
+ /**
274
+ * Read a single item from the collection.
275
+ */
276
+ async get<{{ genericQuery }}>(key: string | number, query?: Query): Promise<{{ applyType }} | undefined>
277
+ {
278
+ return await this.client.request(read{{ collectionName }}Item(key, query));
279
+ }
280
+
281
+ /**
282
+ * Update a single item from the collection.
283
+ */
284
+ async update<{{ genericQuery }}>(key: string | number, patch: Partial<{{ collectionType }}>, query?: Query): Promise<{{ applyType }} | undefined>
285
+ {
286
+ return await this.client.request(update{{ collectionName }}Item(key, patch, query as any)) as any;
287
+ }
288
+
289
+ /**
290
+ * Remove many items in the collection.
291
+ */
292
+ async remove<{{ genericQuery }}>(key: string | number): Promise<void>
293
+ {
294
+ return await this.client.request(delete{{ collectionName }}Item(key));
295
+ }
296
+ }
297
+
298
+ {% endif %}
299
+
300
+
301
+ {% endif %}
302
+ {% endfor %}
303
+
304
+
305
+ /**
306
+ * The Directus Client.
307
+ */
308
+
309
+ export type DirectusRestCommands<T extends Record<any, any>> = keyof {
310
+ [K in keyof T as T[K] extends (
311
+ ...any: any[]
312
+ ) => Directus.RestCommand<any, any>
313
+ ? K
314
+ : never]: K;
315
+ };
316
+
317
+ export type TypedClient = {
318
+ {%- for collection in registry.collections %}
319
+
320
+ {% set collectionType = ["Collections.", collection.name | to_collection_name] | join %}
321
+ {% set genericQuery = ["const Query extends Directus.Query<Schema, ", collectionType, ">"] | join %}
322
+ {% set applyType = ["DirectusSDK.ApplyQueryFields<Schema, ", collectionType, ", Query['fields']>"] | join %}
323
+
324
+ {% if not collection.is_system %}
325
+
326
+ {% if not collection.is_singleton %}
327
+ /**
328
+ * Manages multiple items from the {{ collection.name.raw | to_collection_name }} collection.
329
+ */
330
+ {{ collection.name.raw | pluralize | to_collection_string }}: TypedCollectionItemsWrapper<Collections.{{ collection.name.raw | to_collection_name }}>;
331
+
332
+ /**
333
+ * Manages individual items from the {{ collection.name.raw | to_collection_name }} collection.
334
+ */
335
+ {{ collection.name.raw | singularize | to_collection_string }}: TypedCollectionItemWrapper<Collections.{{ collection.name.raw | to_collection_name }}>;
336
+ {% else %}
337
+
338
+ /**
339
+ * Fetches the only {{ collection.name.raw | singularize | to_collection_name }} instance available.
340
+ */
341
+ [{{ collection.name | to_collection_string }}]<{{ genericQuery }}>(query?: Query): Promise<{{ applyType }}>;
342
+
343
+ {% endif %}
344
+
345
+ {% endif %}
346
+ {%- endfor %}
347
+ } & DirectusCommands;
348
+
349
+ type ExcludedDirectusCommands = "withOptions" | "withToken" | "withSearch";
350
+
351
+ /**
352
+ * This is almost a sanity check for protecting against breaking changes in the SDK.
353
+ * If this is erroring for you, the SDK probably changed and there's an update needed.
354
+ */
355
+
356
+ const excludedDirectusCommands: {
357
+ [K in keyof Omit<
358
+ DirectusSDK,
359
+ Exclude<keyof DirectusCommands, ExcludedDirectusCommands>
360
+ >]: true;
361
+ } = {
362
+ ["auth"]: true,
363
+ ["authentication"]: true,
364
+ ["createDirectus"]: true,
365
+ ["rest"]: true,
366
+ ["formatFields"]: true,
367
+ ["generateUid"]: true,
368
+ ["getAuthEndpoint"]: true,
369
+ ["graphql"]: true,
370
+ ["isDirectusError"]: true,
371
+ ["memoryStorage"]: true,
372
+ ["messageCallback"]: true,
373
+ ["pong"]: true,
374
+ ["queryToParams"]: true,
375
+ ["realtime"]: true,
376
+ ["sleep"]: true,
377
+ ["staticToken"]: true,
378
+ ["throwIfCoreCollection"]: true,
379
+ ["throwIfEmpty"]: true,
380
+ ["withOptions"]: true,
381
+ ["withToken"]: true,
382
+ ["withSearch"]: true,
383
+ } as const;
384
+
385
+ export type _InjectSchemaSystemTypes<T, Schema>
386
+ = T extends Directus.Query<any, infer C> ? Directus.Query<Schema, C>
387
+ {% for collection in registry.collections | skip_collections(skipCollections) -%}
388
+ {% if collection.is_system %}
389
+ : T extends Directus.{{ collection.name | to_collection_name }}<any> ? Directus.{{ collection.name | to_collection_name }}<Schema>
390
+ {% endif %}
391
+ {% endfor %}
392
+ : T extends Directus.DirectusUser<any> ? Directus.DirectusUser<Schema>
393
+ : T;
394
+
395
+ export type InjectSchemaSystemTypes<T, Schema>
396
+ = T extends Partial<infer Nested> ? Partial<_InjectSchemaSystemTypes<Nested, Schema>>
397
+ : _InjectSchemaSystemTypes<T, Schema>
398
+ ;
399
+
400
+ export type InjectSchema<T, Schema>
401
+ = T extends [] ? []
402
+ : T extends [infer Param] ? [InjectSchema<Param, Schema>]
403
+ : T extends [infer Param, ...infer Rest] ? [InjectSchema<Param, Schema>, ...InjectSchema<Rest, Schema>]
404
+ : InjectSchemaSystemTypes<T, Schema>;
405
+
406
+ export type DirectusCommands = {
407
+ [K in DirectusRestCommands<DirectusSDK>]: (
408
+ ...args: InjectSchema<Parameters<DirectusSDK[K]>, Schema>
409
+ ) => Promise<
410
+ ReturnType<DirectusSDK[K]> extends Directus.RestCommand<infer Output, any>
411
+ ? Output
412
+ : unknown
413
+ >;
414
+ }
415
+
416
+ function isDirectusRestCommand(
417
+ pair: [any, any],
418
+ ): pair is [string, (...args: any[]) => Directus.RestCommand<any, any>] {
419
+ return (
420
+ !((pair?.[0] as any) in excludedDirectusCommands) &&
421
+ typeof pair?.[1] === "function"
422
+ );
423
+ }
424
+
425
+ function isDirectusRestClient<Schema>(
426
+ client: DirectusSDK.DirectusClient<Schema>,
427
+ ): client is DirectusSDK.DirectusClient<Schema> &
428
+ DirectusSDK.RestClient<Schema> {
429
+ return client && "request" in client;
430
+ }
431
+
432
+ export const schema = () => {
433
+ return <Schema>(client: Directus.DirectusClient<Schema>): TypedClient => {
434
+
435
+ if (!isDirectusRestClient(client)) {
436
+ throw new Error("Directus client must have the REST plugin enabled.");
437
+ }
438
+
439
+ return Object.fromEntries([
440
+ ...Object.entries(DirectusSDK)
441
+ .filter(isDirectusRestCommand)
442
+ .map(([key, value]) => {
443
+ return [
444
+ key,
445
+ (...args: any[]): any => {
446
+ return client.request(
447
+ value(...args),
448
+ );
449
+ },
450
+ ];
451
+ }),
452
+
453
+ {% for collection in registry.collections %}
454
+ {% if not collection.is_system%}
455
+ {% if not collection.is_singleton %}
456
+ [{{ collection.name.raw | pluralize | to_collection_string }}, new {{ collection.name | to_collection_name }}Items(client as any)],
457
+ [{{ collection.name.raw | singularize | to_collection_string }}, new {{ collection.name | to_collection_name }}Item(client as any)],
458
+ {% else %}
459
+ [{{ collection.name | to_collection_string }}, (query: any) => {
460
+ return client.request(read{{ collection.name | to_collection_name }}(query));
461
+ }],
462
+ {% endif %}
463
+ {% endif %}
464
+ {% endfor %}
465
+ ]);
466
+
467
+ };
468
+ }
469
+
470
+ export interface BindableClient {
471
+ with: <
472
+ Client extends DirectusSDK.DirectusClient<any>,
473
+ Extension extends object,
474
+ >(
475
+ createExtension: (client: Client) => Extension,
476
+ ) => this & Extension;
477
+ }
478
+
479
+ export const bindings = () => {
480
+ return <Schema, Client extends DirectusSDK.DirectusClient<Schema>>(
481
+ client: Client,
482
+ ): BindableClient => {
483
+ return {
484
+ with(createExtension: any) {
485
+ const extension = createExtension(this);
486
+ const extensions = Object.entries(
487
+ extension,
488
+ ).reduce<PropertyDescriptorMap>((properties, [name, value]) => {
489
+ return {
490
+ ...properties,
491
+ [name]: {
492
+ value,
493
+ configurable: true,
494
+ writable: true,
495
+ enumerable: true,
496
+ },
497
+ };
498
+ }, {});
499
+
500
+ Object.defineProperties(this, extensions);
501
+
502
+ return this;
503
+ },
504
+ } as any;
505
+ };
506
+ };
507
+
508
+ export function createDirectusWithTypes(
509
+ url: string,
510
+ options?: Directus.ClientOptions
511
+ ): Directus.DirectusClient<Schema> & Directus.RestClient<Schema> & TypedClient {
512
+ return DirectusSDK.createDirectus<Schema>(url, options)
513
+ .with(bindings())
514
+ .with(DirectusSDK.rest())
515
+ .with(schema());
516
+ }
517
+
518
+ export const createTypedClient = createDirectusWithTypes;