@sanity/codegen 5.7.0-next.8 → 5.7.0

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 (59) hide show
  1. package/bin/run.js +31 -0
  2. package/dist/_exports/index.js +11 -0
  3. package/dist/_exports/index.js.map +1 -0
  4. package/dist/actions/generatedFileWarning.js +15 -0
  5. package/dist/actions/generatedFileWarning.js.map +1 -0
  6. package/dist/actions/typegenGenerate.worker.js +54 -0
  7. package/dist/actions/typegenGenerate.worker.js.map +1 -0
  8. package/dist/actions/types.js +3 -0
  9. package/dist/actions/types.js.map +1 -0
  10. package/dist/casing.js +27 -0
  11. package/dist/casing.js.map +1 -0
  12. package/dist/commands/typegen/generate.js +237 -0
  13. package/dist/commands/typegen/generate.js.map +1 -0
  14. package/dist/getBabelConfig.js +37 -0
  15. package/dist/getBabelConfig.js.map +1 -0
  16. package/dist/index.d.ts +459 -0
  17. package/dist/readConfig.js +38 -0
  18. package/dist/readConfig.js.map +1 -0
  19. package/dist/readSchema.js +14 -0
  20. package/dist/readSchema.js.map +1 -0
  21. package/dist/safeParseQuery.js +37 -0
  22. package/dist/safeParseQuery.js.map +1 -0
  23. package/dist/typeUtils.js +37 -0
  24. package/dist/typeUtils.js.map +1 -0
  25. package/dist/typescript/constants.js +12 -0
  26. package/dist/typescript/constants.js.map +1 -0
  27. package/dist/typescript/expressionResolvers.js +356 -0
  28. package/dist/typescript/expressionResolvers.js.map +1 -0
  29. package/dist/typescript/findQueriesInPath.js +69 -0
  30. package/dist/typescript/findQueriesInPath.js.map +1 -0
  31. package/dist/typescript/findQueriesInSource.js +175 -0
  32. package/dist/typescript/findQueriesInSource.js.map +1 -0
  33. package/dist/typescript/helpers.js +86 -0
  34. package/dist/typescript/helpers.js.map +1 -0
  35. package/dist/typescript/moduleResolver.js +33 -0
  36. package/dist/typescript/moduleResolver.js.map +1 -0
  37. package/dist/typescript/parseSource.js +75 -0
  38. package/dist/typescript/parseSource.js.map +1 -0
  39. package/dist/typescript/registerBabel.js +23 -0
  40. package/dist/typescript/registerBabel.js.map +1 -0
  41. package/dist/typescript/schemaTypeGenerator.js +323 -0
  42. package/dist/typescript/schemaTypeGenerator.js.map +1 -0
  43. package/dist/typescript/typeGenerator.js +240 -0
  44. package/dist/typescript/typeGenerator.js.map +1 -0
  45. package/dist/typescript/types.js +31 -0
  46. package/dist/typescript/types.js.map +1 -0
  47. package/dist/utils/count.js +6 -0
  48. package/dist/utils/count.js.map +1 -0
  49. package/dist/utils/formatPath.js +8 -0
  50. package/dist/utils/formatPath.js.map +1 -0
  51. package/dist/utils/getMessage.js +3 -0
  52. package/dist/utils/getMessage.js.map +1 -0
  53. package/dist/utils/percent.js +8 -0
  54. package/dist/utils/percent.js.map +1 -0
  55. package/oclif.manifest.json +39 -0
  56. package/package.json +49 -23
  57. package/lib/index.d.ts +0 -433
  58. package/lib/index.js +0 -1011
  59. package/lib/index.js.map +0 -1
package/lib/index.d.ts DELETED
@@ -1,433 +0,0 @@
1
- import {ExprNode} from 'groq-js'
2
- import {SchemaType} from 'groq-js'
3
- import * as t from '@babel/types'
4
- import {TransformOptions} from '@babel/core'
5
- import {WorkerChannel} from '@sanity/worker-channels'
6
- import {WorkerChannelReporter} from '@sanity/worker-channels'
7
- import * as z from 'zod'
8
-
9
- /**
10
- * @deprecated use TypeGenConfig
11
- */
12
- export declare type CodegenConfig = TypeGenConfig
13
-
14
- /**
15
- * @internal
16
- */
17
- export declare const configDefinition: z.ZodObject<
18
- {
19
- path: z.ZodDefault<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, 'many'>]>>
20
- schema: z.ZodDefault<z.ZodString>
21
- generates: z.ZodDefault<z.ZodString>
22
- formatGeneratedCode: z.ZodDefault<z.ZodBoolean>
23
- overloadClientMethods: z.ZodDefault<z.ZodBoolean>
24
- },
25
- 'strip',
26
- z.ZodTypeAny,
27
- {
28
- path: string | string[]
29
- schema: string
30
- generates: string
31
- formatGeneratedCode: boolean
32
- overloadClientMethods: boolean
33
- },
34
- {
35
- path?: string | string[] | undefined
36
- schema?: string | undefined
37
- generates?: string | undefined
38
- formatGeneratedCode?: boolean | undefined
39
- overloadClientMethods?: boolean | undefined
40
- }
41
- >
42
-
43
- /**
44
- * A module containing queries that have been evaluated.
45
- * @public
46
- */
47
- export declare interface EvaluatedModule {
48
- filename: string
49
- queries: EvaluatedQuery[]
50
- errors: (QueryExtractionError | QueryEvaluationError)[]
51
- }
52
-
53
- /**
54
- * An `ExtractedQuery` that has been evaluated against a schema, yielding a TypeScript type.
55
- * @public
56
- */
57
- export declare interface EvaluatedQuery extends ExtractedQuery {
58
- id: t.Identifier
59
- code: string
60
- tsType: t.TSType
61
- ast: t.ExportNamedDeclaration
62
- stats: TypeEvaluationStats
63
- }
64
-
65
- /**
66
- * A module (file) containing extracted GROQ queries.
67
- * @public
68
- */
69
- export declare interface ExtractedModule {
70
- filename: string
71
- queries: ExtractedQuery[]
72
- errors: QueryExtractionError[]
73
- }
74
-
75
- /**
76
- * A GROQ query extracted from a source file.
77
- * @public
78
- */
79
- export declare interface ExtractedQuery {
80
- variable: QueryVariable
81
- query: string
82
- filename: string
83
- }
84
-
85
- /**
86
- * Filter a union of object types by the _type property. This is handy when working with page builder
87
- * setups where the returned type is an array containing multiple types.
88
- *
89
- * @example
90
- * ```ts
91
- *
92
- * export type Callout = {
93
- * _type: 'callout'
94
- * title?: string
95
- * content?: string
96
- * }
97
- *
98
- * export type Video = {
99
- * _type: 'video'
100
- * url?: string
101
- * caption?: string
102
- * }
103
- * type FORM_QUERY_RESULT = {
104
- * _id: string
105
- * title?: string
106
- * content?: Array<
107
- * | ({ _key: string } & Callout)
108
- * | ({ _key: string } & Video)
109
- * >
110
- * } | null
111
- *
112
- * // Get the type of the content with the Get helper
113
- * type Content = Get<FORM_QUERY_RESULT, 'content', number>
114
- *
115
- * // Get the type for a callout module from the page builder type
116
- * type CalloutModule = FilterByType<Content, 'callout'>
117
- * // → { _key: string } & Callout
118
- * ```
119
- */
120
- export declare type FilterByType<
121
- U extends {
122
- _type: string
123
- },
124
- T extends U['_type'],
125
- > = Extract<
126
- U,
127
- {
128
- _type: T
129
- }
130
- >
131
-
132
- /**
133
- * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
134
- * @param path - The path or array of paths to search for queries
135
- * @param babelOptions - The babel configuration to use when parsing the source
136
- * @param resolver - A resolver function to use when resolving module imports
137
- * @returns An async generator that yields the results of the search
138
- * @beta
139
- * @internal
140
- */
141
- export declare function findQueriesInPath({
142
- path,
143
- babelOptions,
144
- resolver,
145
- }: FindQueriesInPathOptions): {
146
- files: string[]
147
- queries: AsyncIterable<ExtractedModule>
148
- }
149
-
150
- declare interface FindQueriesInPathOptions {
151
- path: string | string[]
152
- babelOptions?: TransformOptions
153
- resolver?: NodeJS.RequireResolve
154
- }
155
-
156
- /**
157
- * findQueriesInSource takes a source string and returns all GROQ queries in it.
158
- * @param source - The source code to search for queries
159
- * @param filename - The filename of the source code
160
- * @param babelConfig - The babel configuration to use when parsing the source
161
- * @param resolver - A resolver function to use when resolving module imports
162
- * @returns
163
- * @beta
164
- * @internal
165
- */
166
- export declare function findQueriesInSource(
167
- source: string,
168
- filename: string,
169
- babelConfig?: TransformOptions,
170
- resolver?: NodeJS.RequireResolve,
171
- ): ExtractedModule
172
-
173
- export declare interface GenerateTypesOptions {
174
- schema: SchemaType
175
- schemaPath?: string
176
- queries?: AsyncIterable<ExtractedModule>
177
- root?: string
178
- overloadClientMethods?: boolean
179
- reporter?: WorkerChannelReporter<TypegenWorkerChannel>
180
- }
181
-
182
- /**
183
- * Get a deeply nested property type from a complex type structure. Safely navigates
184
- * through nullable types (`T | null | undefined`) at each level, preserving the
185
- * nullability of the final accessed property.
186
- *
187
- * Supports up to 20 levels of nesting.
188
- *
189
- * @example
190
- * ```ts
191
- * type POST_QUERY_RESULT = {
192
- * _id: string
193
- * author: {
194
- * profile: {
195
- * name: string;
196
- * } | null;
197
- * } | null;
198
- * links: Array<{
199
- * _key: string
200
- * type: 'link'
201
- * label: string
202
- * url: string
203
- * }> | null
204
- * } | null
205
- *
206
- * // Basic property access:
207
- * type Id = Get<POST_QUERY_RESULT, '_id'>;
208
- * // → string
209
- *
210
- * // Nested property access:
211
- * type Profile = Get<POST_QUERY_RESULT, 'author', 'profile';
212
- * // → { name: string } | null
213
- *
214
- * // Array element access using `number`:
215
- * type Link = Get<POST_QUERY_RESULT, 'links', number, 'label'>;
216
- * // → string
217
- * ```
218
- */
219
- export declare type Get<
220
- T,
221
- K1 extends keyof NonNullish<T>,
222
- K2 extends keyof NavigatePath<T, [K1]> = never,
223
- K3 extends keyof NavigatePath<T, [K1, K2]> = never,
224
- K4 extends keyof NavigatePath<T, [K1, K2, K3]> = never,
225
- K5 extends keyof NavigatePath<T, [K1, K2, K3, K4]> = never,
226
- K6 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5]> = never,
227
- K7 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6]> = never,
228
- K8 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7]> = never,
229
- K9 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8]> = never,
230
- K10 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9]> = never,
231
- K11 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10]> = never,
232
- K12 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11]> = never,
233
- K13 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12]> = never,
234
- K14 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13]> =
235
- never,
236
- K15 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14]> =
237
- never,
238
- K16 extends keyof NavigatePath<
239
- T,
240
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15]
241
- > = never,
242
- K17 extends keyof NavigatePath<
243
- T,
244
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16]
245
- > = never,
246
- K18 extends keyof NavigatePath<
247
- T,
248
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17]
249
- > = never,
250
- K19 extends keyof NavigatePath<
251
- T,
252
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18]
253
- > = never,
254
- K20 extends keyof NavigatePath<
255
- T,
256
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18, K19]
257
- > = never,
258
- > = GetAtPath<
259
- T,
260
- TakeUntilNever<
261
- [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K20]
262
- >
263
- >
264
-
265
- /** Recursively gets value at path, preserving nullability at final access. */
266
- declare type GetAtPath<T, Path extends unknown[]> = Path extends []
267
- ? T
268
- : Path extends [infer K]
269
- ? K extends keyof NonNullish<T>
270
- ? NonNullish<T>[K]
271
- : never
272
- : Path extends [infer K, ...infer Rest]
273
- ? K extends keyof NonNullish<T>
274
- ? GetAtPath<NonNullish<T>[K], Rest>
275
- : never
276
- : never
277
-
278
- /**
279
- * This is a custom implementation of require.resolve that takes into account the paths
280
- * configuration in tsconfig.json. This is necessary if we want to resolve paths that are
281
- * custom defined in the tsconfig.json file.
282
- * Resolving here is best effort and might not work in all cases.
283
- * @beta
284
- */
285
- export declare function getResolver(cwd?: string): NodeJS.RequireResolve
286
-
287
- /** Recursively navigates through a path, stripping nullability for key lookup. */
288
- declare type NavigatePath<T, Path extends unknown[]> = Path extends []
289
- ? NonNullish<T>
290
- : Path extends [infer K, ...infer Rest]
291
- ? K extends keyof NonNullish<T>
292
- ? NavigatePath<NonNullish<T>[K], Rest>
293
- : never
294
- : never
295
-
296
- /** Excludes `null` and `undefined` from a type. */
297
- declare type NonNullish<T> = T extends null | undefined ? never : T
298
-
299
- /**
300
- * An error that occurred during query evaluation.
301
- * @public
302
- */
303
- declare class QueryEvaluationError extends Error {
304
- variable?: QueryVariable
305
- filename: string
306
- constructor({variable, cause, filename}: QueryEvaluationErrorOptions)
307
- }
308
-
309
- declare interface QueryEvaluationErrorOptions {
310
- variable?: QueryVariable
311
- cause: unknown
312
- filename: string
313
- }
314
-
315
- /**
316
- * An error that occurred during query extraction.
317
- * @public
318
- */
319
- export declare class QueryExtractionError extends Error {
320
- variable?: QueryVariable
321
- filename: string
322
- constructor({variable, cause, filename}: QueryExtractionErrorOptions)
323
- }
324
-
325
- declare interface QueryExtractionErrorOptions {
326
- variable?: QueryVariable
327
- cause: unknown
328
- filename: string
329
- }
330
-
331
- declare interface QueryVariable {
332
- id: t.Identifier
333
- start?: number
334
- end?: number
335
- }
336
-
337
- /**
338
- * Read, parse and process a config file
339
- * @internal
340
- */
341
- export declare function readConfig(path: string): Promise<TypeGenConfig>
342
-
343
- /**
344
- * Read a schema from a given path
345
- * @param path - The path to the schema
346
- * @returns The schema
347
- * @internal
348
- * @beta
349
- **/
350
- export declare function readSchema(path: string): Promise<SchemaType>
351
-
352
- /**
353
- * Register Babel with the given options
354
- *
355
- * @param babelOptions - The options to use when registering Babel
356
- * @beta
357
- */
358
- export declare function registerBabel(babelOptions?: TransformOptions): void
359
-
360
- /**
361
- * safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_
362
- * intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.
363
- * @internal
364
- */
365
- export declare function safeParseQuery(query: string): ExprNode
366
-
367
- /** Builds a tuple from elements, stopping at the first `never`. */
368
- declare type TakeUntilNever<T extends unknown[]> = T extends [infer H, ...infer Rest]
369
- ? [H] extends [never]
370
- ? []
371
- : [H, ...TakeUntilNever<Rest>]
372
- : []
373
-
374
- /**
375
- * Statistics from the query type evaluation process.
376
- * @public
377
- */
378
- declare interface TypeEvaluationStats {
379
- allTypes: number
380
- unknownTypes: number
381
- emptyUnions: number
382
- }
383
-
384
- export declare type TypeGenConfig = z.infer<typeof configDefinition>
385
-
386
- /**
387
- * A class used to generate TypeScript types from a given schema
388
- * @beta
389
- */
390
- export declare class TypeGenerator {
391
- private getInternalReferenceSymbolDeclaration
392
- private getArrayOfDeclaration
393
- private getSchemaTypeGenerator
394
- private getSchemaTypeDeclarations
395
- private getAllSanitySchemaTypesDeclaration
396
- private static getEvaluatedModules
397
- private static getQueryMapDeclaration
398
- generateTypes(options: GenerateTypesOptions): Promise<{
399
- code: string
400
- ast: t.Program
401
- }>
402
- }
403
-
404
- export declare type TypegenWorkerChannel = WorkerChannel.Definition<{
405
- generatedSchemaTypes: WorkerChannel.Event<{
406
- internalReferenceSymbol: {
407
- id: t.Identifier
408
- code: string
409
- ast: t.ExportNamedDeclaration
410
- }
411
- schemaTypeDeclarations: {
412
- id: t.Identifier
413
- name: string
414
- code: string
415
- tsType: t.TSType
416
- ast: t.ExportNamedDeclaration
417
- }[]
418
- allSanitySchemaTypesDeclaration: {
419
- code: string
420
- id: t.Identifier
421
- ast: t.ExportNamedDeclaration
422
- }
423
- }>
424
- evaluatedModules: WorkerChannel.Stream<EvaluatedModule>
425
- generatedQueryTypes: WorkerChannel.Event<{
426
- queryMapDeclaration: {
427
- code: string
428
- ast: t.Program
429
- }
430
- }>
431
- }>
432
-
433
- export {}