@prisma-next/contract 0.3.0-dev.135 → 0.3.0-dev.136

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.
@@ -1,383 +0,0 @@
1
- import { ifDefined } from '@prisma-next/utils/defined';
2
-
3
- export type AuthoringArgRef = {
4
- readonly kind: 'arg';
5
- readonly index: number;
6
- readonly path?: readonly string[];
7
- readonly default?: AuthoringTemplateValue;
8
- };
9
-
10
- export type AuthoringTemplateValue =
11
- | string
12
- | number
13
- | boolean
14
- | null
15
- | AuthoringArgRef
16
- | readonly AuthoringTemplateValue[]
17
- | { readonly [key: string]: AuthoringTemplateValue };
18
-
19
- export type AuthoringArgumentDescriptor =
20
- | {
21
- readonly kind: 'string';
22
- readonly optional?: boolean;
23
- }
24
- | {
25
- readonly kind: 'number';
26
- readonly optional?: boolean;
27
- readonly integer?: boolean;
28
- readonly minimum?: number;
29
- readonly maximum?: number;
30
- }
31
- | {
32
- readonly kind: 'stringArray';
33
- readonly optional?: boolean;
34
- }
35
- | {
36
- readonly kind: 'object';
37
- readonly optional?: boolean;
38
- readonly properties: Record<string, AuthoringArgumentDescriptor>;
39
- };
40
-
41
- export interface AuthoringStorageTypeTemplate {
42
- readonly codecId: string;
43
- readonly nativeType: AuthoringTemplateValue;
44
- readonly typeParams?: Record<string, AuthoringTemplateValue>;
45
- }
46
-
47
- export interface AuthoringTypeConstructorDescriptor {
48
- readonly kind: 'typeConstructor';
49
- readonly args?: readonly AuthoringArgumentDescriptor[];
50
- readonly output: AuthoringStorageTypeTemplate;
51
- }
52
-
53
- export interface AuthoringColumnDefaultTemplateLiteral {
54
- readonly kind: 'literal';
55
- readonly value: AuthoringTemplateValue;
56
- }
57
-
58
- export interface AuthoringColumnDefaultTemplateFunction {
59
- readonly kind: 'function';
60
- readonly expression: AuthoringTemplateValue;
61
- }
62
-
63
- export type AuthoringColumnDefaultTemplate =
64
- | AuthoringColumnDefaultTemplateLiteral
65
- | AuthoringColumnDefaultTemplateFunction;
66
-
67
- export interface AuthoringFieldPresetOutput extends AuthoringStorageTypeTemplate {
68
- readonly nullable?: boolean;
69
- readonly default?: AuthoringColumnDefaultTemplate;
70
- readonly executionDefault?: AuthoringTemplateValue;
71
- readonly id?: boolean;
72
- readonly unique?: boolean;
73
- }
74
-
75
- export interface AuthoringFieldPresetDescriptor {
76
- readonly kind: 'fieldPreset';
77
- readonly args?: readonly AuthoringArgumentDescriptor[];
78
- readonly output: AuthoringFieldPresetOutput;
79
- }
80
-
81
- export type AuthoringTypeNamespace = {
82
- readonly [name: string]: AuthoringTypeConstructorDescriptor | AuthoringTypeNamespace;
83
- };
84
-
85
- export type AuthoringFieldNamespace = {
86
- readonly [name: string]: AuthoringFieldPresetDescriptor | AuthoringFieldNamespace;
87
- };
88
-
89
- export interface AuthoringContributions {
90
- readonly type?: AuthoringTypeNamespace;
91
- readonly field?: AuthoringFieldNamespace;
92
- }
93
-
94
- export function isAuthoringArgRef(value: unknown): value is AuthoringArgRef {
95
- if (typeof value !== 'object' || value === null || (value as { kind?: unknown }).kind !== 'arg') {
96
- return false;
97
- }
98
- const { index, path } = value as { index?: unknown; path?: unknown };
99
- if (typeof index !== 'number' || !Number.isInteger(index) || index < 0) {
100
- return false;
101
- }
102
- if (path !== undefined && (!Array.isArray(path) || path.some((s) => typeof s !== 'string'))) {
103
- return false;
104
- }
105
- return true;
106
- }
107
-
108
- function isAuthoringTemplateRecord(value: unknown): value is Record<string, unknown> {
109
- return typeof value === 'object' && value !== null && !Array.isArray(value);
110
- }
111
-
112
- export function isAuthoringTypeConstructorDescriptor(
113
- value: unknown,
114
- ): value is AuthoringTypeConstructorDescriptor {
115
- return (
116
- typeof value === 'object' &&
117
- value !== null &&
118
- (value as { kind?: unknown }).kind === 'typeConstructor' &&
119
- typeof (value as { output?: unknown }).output === 'object' &&
120
- (value as { output?: unknown }).output !== null
121
- );
122
- }
123
-
124
- export function isAuthoringFieldPresetDescriptor(
125
- value: unknown,
126
- ): value is AuthoringFieldPresetDescriptor {
127
- return (
128
- typeof value === 'object' &&
129
- value !== null &&
130
- (value as { kind?: unknown }).kind === 'fieldPreset' &&
131
- typeof (value as { output?: unknown }).output === 'object' &&
132
- (value as { output?: unknown }).output !== null
133
- );
134
- }
135
-
136
- export function resolveAuthoringTemplateValue(
137
- template: AuthoringTemplateValue,
138
- args: readonly unknown[],
139
- ): unknown {
140
- if (isAuthoringArgRef(template)) {
141
- let value = args[template.index];
142
-
143
- for (const segment of template.path ?? []) {
144
- if (!isAuthoringTemplateRecord(value) || !Object.hasOwn(value, segment)) {
145
- value = undefined;
146
- break;
147
- }
148
- value = (value as Record<string, unknown>)[segment];
149
- }
150
-
151
- if (value === undefined && template.default !== undefined) {
152
- return resolveAuthoringTemplateValue(template.default, args);
153
- }
154
-
155
- return value;
156
- }
157
- if (Array.isArray(template)) {
158
- return template.map((value) => resolveAuthoringTemplateValue(value, args));
159
- }
160
- if (typeof template === 'object' && template !== null) {
161
- const resolved: Record<string, unknown> = {};
162
- for (const [key, value] of Object.entries(template)) {
163
- const resolvedValue = resolveAuthoringTemplateValue(value, args);
164
- if (resolvedValue !== undefined) {
165
- resolved[key] = resolvedValue;
166
- }
167
- }
168
- return resolved;
169
- }
170
- return template;
171
- }
172
-
173
- function validateAuthoringArgument(
174
- descriptor: AuthoringArgumentDescriptor,
175
- value: unknown,
176
- path: string,
177
- ): void {
178
- if (value === undefined) {
179
- if (descriptor.optional) {
180
- return;
181
- }
182
- throw new Error(`Missing required authoring helper argument at ${path}`);
183
- }
184
-
185
- if (descriptor.kind === 'string') {
186
- if (typeof value !== 'string') {
187
- throw new Error(`Authoring helper argument at ${path} must be a string`);
188
- }
189
- return;
190
- }
191
-
192
- if (descriptor.kind === 'stringArray') {
193
- if (!Array.isArray(value)) {
194
- throw new Error(`Authoring helper argument at ${path} must be an array of strings`);
195
- }
196
- for (const entry of value) {
197
- if (typeof entry !== 'string') {
198
- throw new Error(`Authoring helper argument at ${path} must be an array of strings`);
199
- }
200
- }
201
- return;
202
- }
203
-
204
- if (descriptor.kind === 'object') {
205
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
206
- throw new Error(`Authoring helper argument at ${path} must be an object`);
207
- }
208
-
209
- const input = value as Record<string, unknown>;
210
- const expectedKeys = new Set(Object.keys(descriptor.properties));
211
-
212
- for (const key of Object.keys(input)) {
213
- if (!expectedKeys.has(key)) {
214
- throw new Error(`Authoring helper argument at ${path} contains unknown property "${key}"`);
215
- }
216
- }
217
-
218
- for (const [key, propertyDescriptor] of Object.entries(descriptor.properties)) {
219
- validateAuthoringArgument(propertyDescriptor, input[key], `${path}.${key}`);
220
- }
221
-
222
- return;
223
- }
224
-
225
- if (typeof value !== 'number' || Number.isNaN(value)) {
226
- throw new Error(`Authoring helper argument at ${path} must be a number`);
227
- }
228
-
229
- if (descriptor.integer && !Number.isInteger(value)) {
230
- throw new Error(`Authoring helper argument at ${path} must be an integer`);
231
- }
232
- if (descriptor.minimum !== undefined && value < descriptor.minimum) {
233
- throw new Error(
234
- `Authoring helper argument at ${path} must be >= ${descriptor.minimum}, received ${value}`,
235
- );
236
- }
237
- if (descriptor.maximum !== undefined && value > descriptor.maximum) {
238
- throw new Error(
239
- `Authoring helper argument at ${path} must be <= ${descriptor.maximum}, received ${value}`,
240
- );
241
- }
242
- }
243
-
244
- export function validateAuthoringHelperArguments(
245
- helperPath: string,
246
- descriptors: readonly AuthoringArgumentDescriptor[] | undefined,
247
- args: readonly unknown[],
248
- ): void {
249
- const expected = descriptors ?? [];
250
- const minimumArgs = expected.reduce(
251
- (count, descriptor, index) => (descriptor.optional ? count : index + 1),
252
- 0,
253
- );
254
- if (args.length < minimumArgs || args.length > expected.length) {
255
- throw new Error(
256
- `${helperPath} expects ${minimumArgs === expected.length ? expected.length : `${minimumArgs}-${expected.length}`} argument(s), received ${args.length}`,
257
- );
258
- }
259
-
260
- expected.forEach((descriptor, index) => {
261
- validateAuthoringArgument(descriptor, args[index], `${helperPath}[${index}]`);
262
- });
263
- }
264
-
265
- function resolveAuthoringStorageTypeTemplate(
266
- template: AuthoringStorageTypeTemplate,
267
- args: readonly unknown[],
268
- ): {
269
- readonly codecId: string;
270
- readonly nativeType: string;
271
- readonly typeParams?: Record<string, unknown>;
272
- } {
273
- const nativeType = resolveAuthoringTemplateValue(template.nativeType, args);
274
- if (typeof nativeType !== 'string') {
275
- throw new Error(
276
- `Resolved authoring nativeType must be a string for codec "${template.codecId}", received ${String(nativeType)}`,
277
- );
278
- }
279
- const typeParams =
280
- template.typeParams === undefined
281
- ? undefined
282
- : resolveAuthoringTemplateValue(template.typeParams, args);
283
- if (typeParams !== undefined && !isAuthoringTemplateRecord(typeParams)) {
284
- throw new Error(
285
- `Resolved authoring typeParams must be an object for codec "${template.codecId}", received ${String(typeParams)}`,
286
- );
287
- }
288
-
289
- return {
290
- codecId: template.codecId,
291
- nativeType,
292
- ...(typeParams === undefined ? {} : { typeParams }),
293
- };
294
- }
295
-
296
- function resolveAuthoringColumnDefaultTemplate(
297
- template: AuthoringColumnDefaultTemplate,
298
- args: readonly unknown[],
299
- ):
300
- | {
301
- readonly kind: 'literal';
302
- readonly value: unknown;
303
- }
304
- | {
305
- readonly kind: 'function';
306
- readonly expression: string;
307
- } {
308
- if (template.kind === 'literal') {
309
- const value = resolveAuthoringTemplateValue(template.value, args);
310
- if (value === undefined) {
311
- throw new Error('Resolved authoring literal default must not be undefined');
312
- }
313
- return {
314
- kind: 'literal',
315
- value,
316
- };
317
- }
318
-
319
- const expression = resolveAuthoringTemplateValue(template.expression, args);
320
- if (expression === undefined || (typeof expression === 'object' && expression !== null)) {
321
- throw new Error(
322
- `Resolved authoring function default expression must resolve to a primitive, received ${String(expression)}`,
323
- );
324
- }
325
- return {
326
- kind: 'function',
327
- expression: String(expression),
328
- };
329
- }
330
-
331
- export function instantiateAuthoringTypeConstructor(
332
- descriptor: AuthoringTypeConstructorDescriptor,
333
- args: readonly unknown[],
334
- ): {
335
- readonly codecId: string;
336
- readonly nativeType: string;
337
- readonly typeParams?: Record<string, unknown>;
338
- } {
339
- return resolveAuthoringStorageTypeTemplate(descriptor.output, args);
340
- }
341
-
342
- export function instantiateAuthoringFieldPreset(
343
- descriptor: AuthoringFieldPresetDescriptor,
344
- args: readonly unknown[],
345
- ): {
346
- readonly descriptor: {
347
- readonly codecId: string;
348
- readonly nativeType: string;
349
- readonly typeParams?: Record<string, unknown>;
350
- };
351
- readonly nullable: boolean;
352
- readonly default?:
353
- | {
354
- readonly kind: 'literal';
355
- readonly value: unknown;
356
- }
357
- | {
358
- readonly kind: 'function';
359
- readonly expression: string;
360
- };
361
- readonly executionDefault?: unknown;
362
- readonly id: boolean;
363
- readonly unique: boolean;
364
- } {
365
- return {
366
- descriptor: resolveAuthoringStorageTypeTemplate(descriptor.output, args),
367
- nullable: descriptor.output.nullable ?? false,
368
- ...ifDefined(
369
- 'default',
370
- descriptor.output.default !== undefined
371
- ? resolveAuthoringColumnDefaultTemplate(descriptor.output.default, args)
372
- : undefined,
373
- ),
374
- ...ifDefined(
375
- 'executionDefault',
376
- descriptor.output.executionDefault !== undefined
377
- ? resolveAuthoringTemplateValue(descriptor.output.executionDefault, args)
378
- : undefined,
379
- ),
380
- id: descriptor.output.id ?? false,
381
- unique: descriptor.output.unique ?? false,
382
- };
383
- }