@sanity/codegen 5.10.0 → 6.0.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.
package/dist/index.d.ts DELETED
@@ -1,562 +0,0 @@
1
- import { BooleanFlag } from '@oclif/core/interfaces';
2
- import { CustomOptions } from '@oclif/core/interfaces';
3
- import { DefinedTelemetryTrace } from '@sanity/telemetry';
4
- import { ExprNode } from 'groq-js';
5
- import { FSWatcher } from 'chokidar';
6
- import { OptionFlag } from '@oclif/core/interfaces';
7
- import { SanityCommand } from '@sanity/cli-core';
8
- import { SchemaType } from 'groq-js';
9
- import { spinner } from '@sanity/cli-core/ux';
10
- import * as t from '@babel/types';
11
- import { TransformOptions } from '@babel/core';
12
- import { WorkerChannel } from '@sanity/worker-channels';
13
- import { WorkerChannelReporter } from '@sanity/worker-channels';
14
- import * as z from 'zod';
15
-
16
- /**
17
- * @deprecated use TypeGenConfig
18
- */
19
- export declare type CodegenConfig = TypeGenConfig;
20
-
21
- /**
22
- * @internal
23
- */
24
- export declare const configDefinition: z.ZodObject<{
25
- formatGeneratedCode: z.ZodDefault<z.ZodBoolean>;
26
- generates: z.ZodDefault<z.ZodString>;
27
- overloadClientMethods: z.ZodDefault<z.ZodBoolean>;
28
- path: z.ZodDefault<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString>]>>;
29
- schema: z.ZodDefault<z.ZodString>;
30
- }, z.core.$strip>;
31
-
32
- /**
33
- * A module containing queries that have been evaluated.
34
- * @public
35
- */
36
- export declare interface EvaluatedModule {
37
- errors: (QueryEvaluationError | QueryExtractionError)[];
38
- filename: string;
39
- queries: EvaluatedQuery[];
40
- }
41
-
42
- /**
43
- * An `ExtractedQuery` that has been evaluated against a schema, yielding a TypeScript type.
44
- * @public
45
- */
46
- export declare interface EvaluatedQuery extends ExtractedQuery {
47
- ast: t.ExportNamedDeclaration;
48
- code: string;
49
- id: t.Identifier;
50
- stats: TypeEvaluationStats;
51
- tsType: t.TSType;
52
- }
53
-
54
- /**
55
- * A module (file) containing extracted GROQ queries.
56
- * @public
57
- */
58
- export declare interface ExtractedModule {
59
- errors: QueryExtractionError[];
60
- filename: string;
61
- queries: ExtractedQuery[];
62
- }
63
-
64
- /**
65
- * A GROQ query extracted from a source file.
66
- * @public
67
- */
68
- export declare interface ExtractedQuery {
69
- filename: string;
70
- query: string;
71
- variable: QueryVariable;
72
- }
73
-
74
- /**
75
- * Filter a union of object types by the _type property. This is handy when working with page builder
76
- * setups where the returned type is an array containing multiple types.
77
- *
78
- * @example
79
- * ```ts
80
- *
81
- * export type Callout = {
82
- * _type: 'callout'
83
- * title?: string
84
- * content?: string
85
- * }
86
- *
87
- * export type Video = {
88
- * _type: 'video'
89
- * url?: string
90
- * caption?: string
91
- * }
92
- * type FORM_QUERY_RESULT = {
93
- * _id: string
94
- * title?: string
95
- * content?: Array<
96
- * | ({ _key: string } & Callout)
97
- * | ({ _key: string } & Video)
98
- * >
99
- * } | null
100
- *
101
- * // Get the type of the content with the Get helper
102
- * type Content = Get<FORM_QUERY_RESULT, 'content', number>
103
- *
104
- * // Get the type for a callout module from the page builder type
105
- * type CalloutModule = FilterByType<Content, 'callout'>
106
- * // → { _key: string } & Callout
107
- * ```
108
- */
109
- export declare type FilterByType<U extends {
110
- _type: string;
111
- }, T extends U['_type']> = Extract<U, {
112
- _type: T;
113
- }>;
114
-
115
- /**
116
- * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
117
- * @param path - The path or array of paths to search for queries
118
- * @param babelOptions - The babel configuration to use when parsing the source
119
- * @param resolver - A resolver function to use when resolving module imports
120
- * @returns An async generator that yields the results of the search
121
- * @beta
122
- * @internal
123
- */
124
- export declare function findQueriesInPath({ babelOptions, path, resolver, }: FindQueriesInPathOptions): {
125
- files: string[];
126
- queries: AsyncIterable<ExtractedModule>;
127
- };
128
-
129
- declare interface FindQueriesInPathOptions {
130
- path: string | string[];
131
- babelOptions?: TransformOptions;
132
- resolver?: NodeJS.RequireResolve;
133
- }
134
-
135
- /**
136
- * findQueriesInSource takes a source string and returns all GROQ queries in it.
137
- * @param source - The source code to search for queries
138
- * @param filename - The filename of the source code
139
- * @param babelConfig - The babel configuration to use when parsing the source
140
- * @param resolver - A resolver function to use when resolving module imports
141
- * @returns
142
- * @beta
143
- * @internal
144
- */
145
- export declare function findQueriesInSource(source: string, filename: string, babelConfig?: TransformOptions, resolver?: NodeJS.RequireResolve): ExtractedModule;
146
-
147
- export declare interface GenerateTypesOptions {
148
- schema: SchemaType;
149
- overloadClientMethods?: boolean;
150
- queries?: AsyncIterable<ExtractedModule>;
151
- reporter?: WorkerChannelReporter<TypegenWorkerChannel>;
152
- root?: string;
153
- schemaPath?: string;
154
- }
155
-
156
- /**
157
- * Result from a single generation run.
158
- * @internal
159
- */
160
- export declare interface GenerationResult {
161
- code: string;
162
- duration: number;
163
- emptyUnionTypeNodesGenerated: number;
164
- filesWithErrors: number;
165
- outputSize: number;
166
- queriesCount: number;
167
- queryFilesCount: number;
168
- schemaTypesCount: number;
169
- typeNodesGenerated: number;
170
- unknownTypeNodesGenerated: number;
171
- unknownTypeNodesRatio: number;
172
- }
173
-
174
- /**
175
- * Get a deeply nested property type from a complex type structure. Safely navigates
176
- * through nullable types (`T | null | undefined`) at each level, preserving the
177
- * nullability of the final accessed property.
178
- *
179
- * Supports up to 20 levels of nesting.
180
- *
181
- * @example
182
- * ```ts
183
- * type POST_QUERY_RESULT = {
184
- * _id: string
185
- * author: {
186
- * profile: {
187
- * name: string;
188
- * } | null;
189
- * } | null;
190
- * links: Array<{
191
- * _key: string
192
- * type: 'link'
193
- * label: string
194
- * url: string
195
- * }> | null
196
- * } | null
197
- *
198
- * // Basic property access:
199
- * type Id = Get<POST_QUERY_RESULT, '_id'>;
200
- * // → string
201
- *
202
- * // Nested property access:
203
- * type Profile = Get<POST_QUERY_RESULT, 'author', 'profile';
204
- * // → { name: string } | null
205
- *
206
- * // Array element access using `number`:
207
- * type Link = Get<POST_QUERY_RESULT, 'links', number, 'label'>;
208
- * // → string
209
- * ```
210
- */
211
- export declare type Get<T, K1 extends keyof NonNullish<T>, K2 extends keyof NavigatePath<T, [K1]> = never, K3 extends keyof NavigatePath<T, [K1, K2]> = never, K4 extends keyof NavigatePath<T, [K1, K2, K3]> = never, K5 extends keyof NavigatePath<T, [K1, K2, K3, K4]> = never, K6 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5]> = never, K7 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6]> = never, K8 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7]> = never, K9 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8]> = never, K10 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9]> = never, K11 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10]> = never, K12 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11]> = never, K13 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12]> = never, K14 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13]> = never, K15 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14]> = never, K16 extends keyof NavigatePath<T, [
212
- K1,
213
- K2,
214
- K3,
215
- K4,
216
- K5,
217
- K6,
218
- K7,
219
- K8,
220
- K9,
221
- K10,
222
- K11,
223
- K12,
224
- K13,
225
- K14,
226
- K15
227
- ]> = never, K17 extends keyof NavigatePath<T, [
228
- K1,
229
- K2,
230
- K3,
231
- K4,
232
- K5,
233
- K6,
234
- K7,
235
- K8,
236
- K9,
237
- K10,
238
- K11,
239
- K12,
240
- K13,
241
- K14,
242
- K15,
243
- K16
244
- ]> = never, K18 extends keyof NavigatePath<T, [
245
- K1,
246
- K2,
247
- K3,
248
- K4,
249
- K5,
250
- K6,
251
- K7,
252
- K8,
253
- K9,
254
- K10,
255
- K11,
256
- K12,
257
- K13,
258
- K14,
259
- K15,
260
- K16,
261
- K17
262
- ]> = never, K19 extends keyof NavigatePath<T, [
263
- K1,
264
- K2,
265
- K3,
266
- K4,
267
- K5,
268
- K6,
269
- K7,
270
- K8,
271
- K9,
272
- K10,
273
- K11,
274
- K12,
275
- K13,
276
- K14,
277
- K15,
278
- K16,
279
- K17,
280
- K18
281
- ]> = never, K20 extends keyof NavigatePath<T, [
282
- K1,
283
- K2,
284
- K3,
285
- K4,
286
- K5,
287
- K6,
288
- K7,
289
- K8,
290
- K9,
291
- K10,
292
- K11,
293
- K12,
294
- K13,
295
- K14,
296
- K15,
297
- K16,
298
- K17,
299
- K18,
300
- K19
301
- ]> = never> = GetAtPath<T, TakeUntilNever<[
302
- K1,
303
- K2,
304
- K3,
305
- K4,
306
- K5,
307
- K6,
308
- K7,
309
- K8,
310
- K9,
311
- K10,
312
- K11,
313
- K12,
314
- K13,
315
- K14,
316
- K15,
317
- K16,
318
- K17,
319
- K18,
320
- K19,
321
- K20
322
- ]>>;
323
-
324
- /** Recursively gets value at path, preserving nullability at final access. */
325
- declare type GetAtPath<T, Path extends unknown[]> = Path extends [] ? T : Path extends [infer K] ? K extends keyof NonNullish<T> ? NonNullish<T>[K] : never : Path extends [infer K, ...infer Rest] ? K extends keyof NonNullish<T> ? GetAtPath<NonNullish<T>[K], Rest> : never : never;
326
-
327
- /**
328
- * This is a custom implementation of require.resolve that takes into account the paths
329
- * configuration in tsconfig.json. This is necessary if we want to resolve paths that are
330
- * custom defined in the tsconfig.json file.
331
- * Resolving here is best effort and might not work in all cases.
332
- * @beta
333
- */
334
- export declare function getResolver(cwd?: string): NodeJS.RequireResolve;
335
-
336
- /** Recursively navigates through a path, stripping nullability for key lookup. */
337
- declare type NavigatePath<T, Path extends unknown[]> = Path extends [] ? NonNullish<T> : Path extends [infer K, ...infer Rest] ? K extends keyof NonNullish<T> ? NavigatePath<NonNullish<T>[K], Rest> : never : never;
338
-
339
- /** Excludes `null` and `undefined` from a type. */
340
- declare type NonNullish<T> = T extends null | undefined ? never : T;
341
-
342
- /**
343
- * An error that occurred during query evaluation.
344
- * @public
345
- */
346
- declare class QueryEvaluationError extends Error {
347
- filename: string;
348
- variable?: QueryVariable;
349
- constructor({ cause, filename, variable }: QueryEvaluationErrorOptions);
350
- }
351
-
352
- declare interface QueryEvaluationErrorOptions {
353
- cause: unknown;
354
- filename: string;
355
- variable?: QueryVariable;
356
- }
357
-
358
- /**
359
- * An error that occurred during query extraction.
360
- * @public
361
- */
362
- export declare class QueryExtractionError extends Error {
363
- filename: string;
364
- variable?: QueryVariable;
365
- constructor({ cause, filename, variable }: QueryExtractionErrorOptions);
366
- }
367
-
368
- declare interface QueryExtractionErrorOptions {
369
- cause: unknown;
370
- filename: string;
371
- variable?: QueryVariable;
372
- }
373
-
374
- declare interface QueryVariable {
375
- id: t.Identifier;
376
- end?: number;
377
- start?: number;
378
- }
379
-
380
- /**
381
- * Read, parse and process a config file
382
- * @internal
383
- */
384
- export declare function readConfig(path: string): Promise<TypeGenConfig>;
385
-
386
- /**
387
- * Read a schema from a given path
388
- * @param path - The path to the schema
389
- * @returns The schema
390
- * @internal
391
- * @beta
392
- **/
393
- export declare function readSchema(path: string): Promise<SchemaType>;
394
-
395
- /**
396
- * Register Babel with the given options
397
- *
398
- * @param babelOptions - The options to use when registering Babel
399
- * @beta
400
- */
401
- export declare function registerBabel(babelOptions?: TransformOptions): void;
402
-
403
- /**
404
- * Runs a single typegen generation.
405
- *
406
- * This is the programmatic API for generating TypeScript types from GROQ queries.
407
- * It spawns a worker thread to perform the generation and displays progress via CLI spinners.
408
- *
409
- * @param options - Configuration options including typegen config and working directory
410
- * @returns Generation result containing the generated code and statistics
411
- */
412
- export declare function runTypegenGenerate(options: RunTypegenOptions): Promise<GenerationResult>;
413
-
414
- /**
415
- * Options for running a single typegen generation.
416
- * This is the programmatic API for one-off generation without file watching.
417
- */
418
- export declare interface RunTypegenOptions {
419
- /** Working directory (usually project root) */
420
- workDir: string;
421
- /** Typegen configuration */
422
- config?: Partial<TypeGenConfig>;
423
- /** Optional spinner instance for progress display */
424
- spin?: ReturnType<typeof spinner>;
425
- }
426
-
427
- /**
428
- * Starts a file watcher that triggers typegen on changes.
429
- * Watches both query files (via patterns) and the schema JSON file.
430
- * Implements debouncing and concurrency control to prevent multiple generations.
431
- */
432
- export declare function runTypegenWatcher(options: RunTypegenOptions): {
433
- getStats: () => WatcherStats;
434
- stop: () => Promise<void>;
435
- watcher: FSWatcher;
436
- };
437
-
438
- /**
439
- * safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_
440
- * intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.
441
- * @internal
442
- */
443
- export declare function safeParseQuery(query: string): ExprNode;
444
-
445
- /** Builds a tuple from elements, stopping at the first `never`. */
446
- declare type TakeUntilNever<T extends unknown[]> = T extends [infer H, ...infer Rest] ? [H] extends [never] ? [] : [H, ...TakeUntilNever<Rest>] : [];
447
-
448
- /**
449
- * Statistics from the query type evaluation process.
450
- * @public
451
- */
452
- declare interface TypeEvaluationStats {
453
- allTypes: number;
454
- emptyUnions: number;
455
- unknownTypes: number;
456
- }
457
-
458
- export declare type TypeGenConfig = z.infer<typeof configDefinition>;
459
-
460
- /**
461
- * A class used to generate TypeScript types from a given schema
462
- * @beta
463
- */
464
- export declare class TypeGenerator {
465
- private getSchemaTypeGenerator;
466
- private getSchemaTypeDeclarations;
467
- private getAllSanitySchemaTypesDeclaration;
468
- private getArrayOfDeclaration;
469
- private getInternalReferenceSymbolDeclaration;
470
- private static getEvaluatedModules;
471
- private static getQueryMapDeclaration;
472
- generateTypes(options: GenerateTypesOptions): Promise<{
473
- ast: t.Program;
474
- code: string;
475
- }>;
476
- }
477
-
478
- /**
479
- * @internal
480
- */
481
- export declare class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerateCommand> {
482
- static description: string;
483
- static examples: {
484
- command: string;
485
- description: string;
486
- }[];
487
- static flags: {
488
- 'config-path': OptionFlag<string | undefined, CustomOptions>;
489
- watch: BooleanFlag<boolean>;
490
- };
491
- run(): Promise<void>;
492
- private getConfig;
493
- private runSingle;
494
- private runWatcher;
495
- }
496
-
497
- export declare const TypegenWatchModeTrace: DefinedTelemetryTrace<TypegenWatchModeTraceAttributes, void>;
498
-
499
- /**
500
- * Attributes for typegen watch mode trace - tracks the start and stop of watch mode
501
- * sessions with statistics about generation runs.
502
- */
503
- declare type TypegenWatchModeTraceAttributes = {
504
- averageGenerationDuration: number;
505
- generationFailedCount: number;
506
- generationSuccessfulCount: number;
507
- step: 'stopped';
508
- watcherDuration: number;
509
- } | {
510
- step: 'started';
511
- };
512
-
513
- export declare type TypegenWorkerChannel = WorkerChannel.Definition<{
514
- evaluatedModules: WorkerChannel.Stream<EvaluatedModule>;
515
- generatedQueryTypes: WorkerChannel.Event<{
516
- queryMapDeclaration: {
517
- ast: t.Program;
518
- code: string;
519
- };
520
- }>;
521
- generatedSchemaTypes: WorkerChannel.Event<{
522
- allSanitySchemaTypesDeclaration: {
523
- ast: t.ExportNamedDeclaration;
524
- code: string;
525
- id: t.Identifier;
526
- };
527
- internalReferenceSymbol: {
528
- ast: t.ExportNamedDeclaration;
529
- code: string;
530
- id: t.Identifier;
531
- };
532
- schemaTypeDeclarations: {
533
- ast: t.ExportNamedDeclaration;
534
- code: string;
535
- id: t.Identifier;
536
- name: string;
537
- tsType: t.TSType;
538
- }[];
539
- }>;
540
- }>;
541
-
542
- export declare const TypesGeneratedTrace: DefinedTelemetryTrace<TypesGeneratedTraceAttributes, void>;
543
-
544
- declare interface TypesGeneratedTraceAttributes {
545
- configMethod: 'cli' | 'legacy';
546
- configOverloadClientMethods: boolean;
547
- emptyUnionTypeNodesGenerated: number;
548
- filesWithErrors: number;
549
- outputSize: number;
550
- queriesCount: number;
551
- queryFilesCount: number;
552
- schemaTypesCount: number;
553
- typeNodesGenerated: number;
554
- unknownTypeNodesGenerated: number;
555
- unknownTypeNodesRatio: number;
556
- }
557
-
558
- declare type WatcherStats = Omit<Extract<TypegenWatchModeTraceAttributes, {
559
- step: 'stopped';
560
- }>, 'step'>;
561
-
562
- export { }