@fern-api/typescript-dynamic-snippets 0.0.6 → 0.0.8

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 (41) hide show
  1. package/cjs/DynamicSnippetsGenerator.cjs +17 -5508
  2. package/cjs/DynamicSnippetsGenerator.d.cts +4 -2
  3. package/cjs/{DynamicSnippetsGeneratorContext-DFkk3dho.d.cts → DynamicSnippetsGeneratorContext-CmDa2k9n.d.cts} +1 -1
  4. package/cjs/EndpointSnippetGenerator.cjs +17 -3539
  5. package/cjs/EndpointSnippetGenerator.d.cts +1 -1
  6. package/cjs/context/DynamicSnippetsGeneratorContext.cjs +17 -4863
  7. package/cjs/context/DynamicSnippetsGeneratorContext.d.cts +1 -1
  8. package/cjs/context/DynamicTypeLiteralMapper.cjs +17 -3493
  9. package/cjs/context/DynamicTypeLiteralMapper.d.cts +1 -1
  10. package/cjs/context/FilePropertyMapper.cjs +17 -3073
  11. package/cjs/context/FilePropertyMapper.d.cts +1 -1
  12. package/cjs/index.cjs +29 -8811
  13. package/cjs/index.d.cts +2 -1
  14. package/esm/DynamicSnippetsGenerator.d.ts +4 -2
  15. package/esm/DynamicSnippetsGenerator.js +1 -12
  16. package/esm/{DynamicSnippetsGeneratorContext-DFkk3dho.d.ts → DynamicSnippetsGeneratorContext-CmDa2k9n.d.ts} +1 -1
  17. package/esm/EndpointSnippetGenerator.d.ts +1 -1
  18. package/esm/EndpointSnippetGenerator.js +1 -8
  19. package/esm/chunk-3SA25SVX.js +1 -0
  20. package/esm/chunk-D7ZDXIQ6.js +1 -0
  21. package/esm/chunk-DJQMPRPJ.js +1 -0
  22. package/esm/chunk-JGNWFXNM.js +1 -0
  23. package/esm/chunk-RL2XA35J.js +27 -0
  24. package/esm/chunk-T2HPPEKP.js +14 -0
  25. package/esm/chunk-XC2QAVJY.js +1 -0
  26. package/esm/context/DynamicSnippetsGeneratorContext.d.ts +1 -1
  27. package/esm/context/DynamicSnippetsGeneratorContext.js +1 -10
  28. package/esm/context/DynamicTypeLiteralMapper.d.ts +1 -1
  29. package/esm/context/DynamicTypeLiteralMapper.js +1 -8
  30. package/esm/context/FilePropertyMapper.d.ts +1 -1
  31. package/esm/context/FilePropertyMapper.js +1 -7
  32. package/esm/index.d.ts +2 -1
  33. package/esm/index.js +13 -3311
  34. package/package.json +2 -2
  35. package/esm/chunk-37WY6L3Q.js +0 -551
  36. package/esm/chunk-6Y2IOBCK.js +0 -501
  37. package/esm/chunk-KUITDN45.js +0 -538
  38. package/esm/chunk-MR7ZAFRL.js +0 -819
  39. package/esm/chunk-PLZLTFT4.js +0 -90
  40. package/esm/chunk-TKT54OF7.js +0 -86
  41. package/esm/chunk-WPDO6IRW.js +0 -3060
@@ -1,501 +0,0 @@
1
- import {
2
- Severity
3
- } from "./chunk-37WY6L3Q.js";
4
- import {
5
- __publicField,
6
- assertNever,
7
- init_buffer,
8
- init_process,
9
- typescript_exports
10
- } from "./chunk-WPDO6IRW.js";
11
-
12
- // src/context/DynamicTypeLiteralMapper.ts
13
- init_process();
14
- init_buffer();
15
- var UNION_VALUE_KEY = "value";
16
- var DynamicTypeLiteralMapper = class {
17
- constructor({ context }) {
18
- __publicField(this, "context");
19
- this.context = context;
20
- }
21
- convert(args) {
22
- if (args.value == null) {
23
- return typescript_exports.TypeLiteral.nop();
24
- }
25
- switch (args.typeReference.type) {
26
- case "list":
27
- return this.convertList({ list: args.typeReference.value, value: args.value });
28
- case "literal":
29
- return this.convertLiteral({ literalType: args.typeReference.value, value: args.value });
30
- case "map":
31
- return this.convertMap({ map: args.typeReference, value: args.value });
32
- case "named": {
33
- const named = this.context.resolveNamedType({ typeId: args.typeReference.value });
34
- if (named == null) {
35
- return typescript_exports.TypeLiteral.nop();
36
- }
37
- return this.convertNamed({ named, value: args.value, as: args.as });
38
- }
39
- case "optional":
40
- return this.convert({ typeReference: args.typeReference.value, value: args.value, as: args.as });
41
- case "primitive":
42
- return this.convertPrimitive({ primitive: args.typeReference.value, value: args.value, as: args.as });
43
- case "set":
44
- return this.convertSet({ set: args.typeReference.value, value: args.value });
45
- case "unknown":
46
- return this.convertUnknown({ value: args.value });
47
- default:
48
- assertNever(args.typeReference);
49
- }
50
- }
51
- convertLiteral({
52
- literalType,
53
- value
54
- }) {
55
- switch (literalType.type) {
56
- case "boolean": {
57
- const bool = this.context.getValueAsBoolean({ value });
58
- if (bool == null) {
59
- return typescript_exports.TypeLiteral.nop();
60
- }
61
- return typescript_exports.TypeLiteral.boolean(bool);
62
- }
63
- case "string": {
64
- const str = this.context.getValueAsString({ value });
65
- if (str == null) {
66
- return typescript_exports.TypeLiteral.nop();
67
- }
68
- return typescript_exports.TypeLiteral.string(str);
69
- }
70
- default:
71
- assertNever(literalType);
72
- }
73
- }
74
- convertList({ list, value }) {
75
- if (!Array.isArray(value)) {
76
- this.context.errors.add({
77
- severity: Severity.Critical,
78
- message: `Expected array but got: ${typeof value}`
79
- });
80
- return typescript_exports.TypeLiteral.nop();
81
- }
82
- return typescript_exports.TypeLiteral.array({
83
- values: value.map((v, index) => {
84
- this.context.errors.scope({ index });
85
- try {
86
- return this.convert({ typeReference: list, value: v });
87
- } finally {
88
- this.context.errors.unscope();
89
- }
90
- })
91
- });
92
- }
93
- convertSet({ set, value }) {
94
- if (!Array.isArray(value)) {
95
- this.context.errors.add({
96
- severity: Severity.Critical,
97
- message: `Expected array but got: ${typeof value}`
98
- });
99
- return typescript_exports.TypeLiteral.nop();
100
- }
101
- return typescript_exports.TypeLiteral.set({
102
- values: value.map((v, index) => {
103
- this.context.errors.scope({ index });
104
- try {
105
- return this.convert({ typeReference: set, value: v });
106
- } finally {
107
- this.context.errors.unscope();
108
- }
109
- })
110
- });
111
- }
112
- convertMap({ map, value }) {
113
- if (typeof value !== "object" || value == null) {
114
- this.context.errors.add({
115
- severity: Severity.Critical,
116
- message: `Expected object but got: ${value == null ? "null" : typeof value}`
117
- });
118
- return typescript_exports.TypeLiteral.nop();
119
- }
120
- return typescript_exports.TypeLiteral.record({
121
- entries: Object.entries(value).map(([key, value2]) => {
122
- this.context.errors.scope(key);
123
- try {
124
- return {
125
- key: this.convert({ typeReference: map.key, value: key, as: "key" }),
126
- value: this.convert({ typeReference: map.value, value: value2 })
127
- };
128
- } finally {
129
- this.context.errors.unscope();
130
- }
131
- })
132
- });
133
- }
134
- convertNamed({
135
- named,
136
- value,
137
- as
138
- }) {
139
- var _a;
140
- switch (named.type) {
141
- case "alias": {
142
- if ((_a = this.context.customConfig) == null ? void 0 : _a.useBrandedStringAliases) {
143
- return typescript_exports.TypeLiteral.reference(
144
- typescript_exports.codeblock((writer) => {
145
- writer.writeNode(
146
- typescript_exports.reference({
147
- name: this.context.namespaceExport,
148
- importFrom: this.context.getModuleImport(),
149
- memberName: this.context.getFullyQualifiedReference({
150
- declaration: named.declaration
151
- })
152
- })
153
- );
154
- writer.write("(");
155
- writer.writeNode(this.convert({ typeReference: named.typeReference, value, as }));
156
- writer.write(")");
157
- })
158
- );
159
- }
160
- return this.convert({ typeReference: named.typeReference, value, as });
161
- }
162
- case "discriminatedUnion":
163
- return this.convertDiscriminatedUnion({
164
- discriminatedUnion: named,
165
- value
166
- });
167
- case "enum":
168
- return this.convertEnum({ enum_: named, value });
169
- case "object":
170
- return this.convertObject({ object_: named, value });
171
- case "undiscriminatedUnion":
172
- return this.convertUndicriminatedUnion({ undicriminatedUnion: named, value });
173
- default:
174
- assertNever(named);
175
- }
176
- }
177
- convertDiscriminatedUnion({
178
- discriminatedUnion,
179
- value
180
- }) {
181
- var _a;
182
- const discriminatedUnionTypeInstance = this.context.resolveDiscriminatedUnionTypeInstance({
183
- discriminatedUnion,
184
- value
185
- });
186
- if (discriminatedUnionTypeInstance == null) {
187
- return typescript_exports.TypeLiteral.nop();
188
- }
189
- const unionVariant = discriminatedUnionTypeInstance.singleDiscriminatedUnionType;
190
- const unionProperties = this.convertDiscriminatedUnionProperties({
191
- discriminatedUnionTypeInstance,
192
- unionVariant
193
- });
194
- if (unionProperties == null) {
195
- return typescript_exports.TypeLiteral.nop();
196
- }
197
- if ((_a = this.context.customConfig) == null ? void 0 : _a.includeUtilsOnUnionMembers) {
198
- return typescript_exports.TypeLiteral.reference(
199
- typescript_exports.codeblock((writer) => {
200
- writer.writeNode(
201
- typescript_exports.invokeMethod({
202
- on: typescript_exports.reference({
203
- name: this.context.namespaceExport,
204
- importFrom: this.context.getModuleImport(),
205
- memberName: this.context.getFullyQualifiedReference({
206
- declaration: discriminatedUnion.declaration
207
- })
208
- }),
209
- method: this.context.getMethodName(unionVariant.discriminantValue.name),
210
- arguments_: this.convertDiscriminatedUnionUtilsArgs({
211
- discriminatedUnionTypeInstance,
212
- unionVariant,
213
- unionProperties
214
- })
215
- })
216
- );
217
- })
218
- );
219
- }
220
- const discriminantProperty = {
221
- name: this.context.getPropertyName(discriminatedUnion.discriminant.name),
222
- value: typescript_exports.TypeLiteral.string(unionVariant.discriminantValue.wireValue)
223
- };
224
- return typescript_exports.TypeLiteral.object({
225
- fields: [discriminantProperty, ...unionProperties]
226
- });
227
- }
228
- convertDiscriminatedUnionProperties({
229
- discriminatedUnionTypeInstance,
230
- unionVariant
231
- }) {
232
- const baseFields = this.getBaseFields({
233
- discriminatedUnionTypeInstance,
234
- singleDiscriminatedUnionType: unionVariant
235
- });
236
- switch (unionVariant.type) {
237
- case "samePropertiesAsObject": {
238
- const named = this.context.resolveNamedType({
239
- typeId: unionVariant.typeId
240
- });
241
- if (named == null) {
242
- return void 0;
243
- }
244
- const converted = this.convertNamed({ named, value: discriminatedUnionTypeInstance.value });
245
- if (!converted.isObject()) {
246
- this.context.errors.add({
247
- severity: Severity.Critical,
248
- message: "Internal error; expected union value to be an object"
249
- });
250
- return void 0;
251
- }
252
- const object_ = converted.asObjectOrThrow();
253
- return [...baseFields, ...object_.fields];
254
- }
255
- case "singleProperty": {
256
- try {
257
- this.context.errors.scope(unionVariant.discriminantValue.wireValue);
258
- const record = this.context.getRecord(discriminatedUnionTypeInstance.value);
259
- if (record == null) {
260
- return [
261
- ...baseFields,
262
- {
263
- name: UNION_VALUE_KEY,
264
- value: this.convert({
265
- typeReference: unionVariant.typeReference,
266
- value: discriminatedUnionTypeInstance.value
267
- })
268
- }
269
- ];
270
- }
271
- return [
272
- ...baseFields,
273
- {
274
- name: this.context.getPropertyName(unionVariant.discriminantValue.name),
275
- value: this.convert({
276
- typeReference: unionVariant.typeReference,
277
- value: record[unionVariant.discriminantValue.wireValue]
278
- })
279
- }
280
- ];
281
- } finally {
282
- this.context.errors.unscope();
283
- }
284
- }
285
- case "noProperties":
286
- return baseFields;
287
- default:
288
- assertNever(unionVariant);
289
- }
290
- }
291
- convertDiscriminatedUnionUtilsArgs({
292
- discriminatedUnionTypeInstance,
293
- unionVariant,
294
- unionProperties
295
- }) {
296
- if (unionVariant.type === "singleProperty") {
297
- const record = this.context.getRecord(discriminatedUnionTypeInstance.value);
298
- if (record == null && unionProperties.length === 1) {
299
- return [
300
- this.convert({
301
- typeReference: unionVariant.typeReference,
302
- value: discriminatedUnionTypeInstance.value
303
- })
304
- ];
305
- }
306
- }
307
- return unionProperties.length > 0 ? [typescript_exports.TypeLiteral.object({ fields: unionProperties })] : [];
308
- }
309
- getBaseFields({
310
- discriminatedUnionTypeInstance,
311
- singleDiscriminatedUnionType
312
- }) {
313
- var _a, _b;
314
- const properties = this.context.associateByWireValue({
315
- parameters: (_a = singleDiscriminatedUnionType.properties) != null ? _a : [],
316
- values: (_b = this.context.getRecord(discriminatedUnionTypeInstance.value)) != null ? _b : {},
317
- // We're only selecting the base properties here. The rest of the properties
318
- // are handled by the union variant.
319
- ignoreMissingParameters: true
320
- });
321
- return properties.map((property) => {
322
- this.context.errors.scope(property.name.wireValue);
323
- try {
324
- return {
325
- name: this.context.getPropertyName(property.name.name),
326
- value: this.convert(property)
327
- };
328
- } finally {
329
- this.context.errors.unscope();
330
- }
331
- });
332
- }
333
- convertObject({ object_, value }) {
334
- var _a;
335
- const properties = this.context.associateByWireValue({
336
- parameters: object_.properties,
337
- values: (_a = this.context.getRecord(value)) != null ? _a : {}
338
- });
339
- return typescript_exports.TypeLiteral.object({
340
- fields: properties.map((property) => {
341
- this.context.errors.scope(property.name.wireValue);
342
- try {
343
- return {
344
- name: this.context.getPropertyName(property.name.name),
345
- value: this.convert(property)
346
- };
347
- } finally {
348
- this.context.errors.unscope();
349
- }
350
- })
351
- });
352
- }
353
- convertEnum({ enum_, value }) {
354
- const enumValue = this.getEnumValue({ enum_, value });
355
- if (enumValue == null) {
356
- return typescript_exports.TypeLiteral.nop();
357
- }
358
- return typescript_exports.TypeLiteral.string(enumValue);
359
- }
360
- getEnumValue({ enum_, value }) {
361
- if (typeof value !== "string") {
362
- this.context.errors.add({
363
- severity: Severity.Critical,
364
- message: `Expected enum value string, got: ${typeof value}`
365
- });
366
- return void 0;
367
- }
368
- const enumValue = enum_.values.find((v) => v.wireValue === value);
369
- if (enumValue == null) {
370
- this.context.errors.add({
371
- severity: Severity.Critical,
372
- message: `An enum value named "${value}" does not exist in this context`
373
- });
374
- return void 0;
375
- }
376
- return value;
377
- }
378
- convertUndicriminatedUnion({
379
- undicriminatedUnion,
380
- value
381
- }) {
382
- const result = this.findMatchingUndiscriminatedUnionType({
383
- undicriminatedUnion,
384
- value
385
- });
386
- if (result == null) {
387
- return typescript_exports.TypeLiteral.nop();
388
- }
389
- return result;
390
- }
391
- findMatchingUndiscriminatedUnionType({
392
- undicriminatedUnion,
393
- value
394
- }) {
395
- for (const typeReference of undicriminatedUnion.types) {
396
- try {
397
- return this.convert({ typeReference, value });
398
- } catch (e) {
399
- continue;
400
- }
401
- }
402
- this.context.errors.add({
403
- severity: Severity.Critical,
404
- message: `None of the types in the undicriminated union matched the given "${typeof value}" value`
405
- });
406
- return void 0;
407
- }
408
- convertUnknown({ value }) {
409
- return typescript_exports.TypeLiteral.unknown(value);
410
- }
411
- convertPrimitive({
412
- primitive,
413
- value,
414
- as
415
- }) {
416
- var _a, _b;
417
- switch (primitive) {
418
- case "INTEGER":
419
- case "UINT": {
420
- const num = this.getValueAsNumber({ value, as });
421
- if (num == null) {
422
- return typescript_exports.TypeLiteral.nop();
423
- }
424
- return typescript_exports.TypeLiteral.number(num);
425
- }
426
- case "LONG":
427
- case "UINT_64": {
428
- const num = this.getValueAsNumber({ value, as });
429
- if (num == null) {
430
- return typescript_exports.TypeLiteral.nop();
431
- }
432
- if ((_a = this.context.customConfig) == null ? void 0 : _a.useBigInt) {
433
- return typescript_exports.TypeLiteral.bigint(BigInt(num));
434
- }
435
- return typescript_exports.TypeLiteral.number(num);
436
- }
437
- case "FLOAT":
438
- case "DOUBLE": {
439
- const num = this.getValueAsNumber({ value });
440
- if (num == null) {
441
- return typescript_exports.TypeLiteral.nop();
442
- }
443
- return typescript_exports.TypeLiteral.number(num);
444
- }
445
- case "BOOLEAN": {
446
- const bool = this.getValueAsBoolean({ value, as });
447
- if (bool == null) {
448
- return typescript_exports.TypeLiteral.nop();
449
- }
450
- return typescript_exports.TypeLiteral.boolean(bool);
451
- }
452
- case "BASE_64":
453
- case "DATE":
454
- case "UUID":
455
- case "STRING": {
456
- const str = this.context.getValueAsString({ value });
457
- if (str == null) {
458
- return typescript_exports.TypeLiteral.nop();
459
- }
460
- return typescript_exports.TypeLiteral.string(str);
461
- }
462
- case "DATE_TIME": {
463
- const str = this.context.getValueAsString({ value });
464
- if (str == null) {
465
- return typescript_exports.TypeLiteral.nop();
466
- }
467
- return typescript_exports.TypeLiteral.datetime(str);
468
- }
469
- case "BIG_INTEGER": {
470
- const bigInt = this.context.getValueAsString({ value });
471
- if (bigInt == null) {
472
- return typescript_exports.TypeLiteral.nop();
473
- }
474
- if ((_b = this.context.customConfig) == null ? void 0 : _b.useBigInt) {
475
- return typescript_exports.TypeLiteral.bigint(BigInt(bigInt));
476
- }
477
- return typescript_exports.TypeLiteral.string(bigInt);
478
- }
479
- default:
480
- assertNever(primitive);
481
- }
482
- }
483
- getValueAsNumber({
484
- value,
485
- as
486
- }) {
487
- const num = as === "key" ? typeof value === "string" ? Number(value) : value : value;
488
- return this.context.getValueAsNumber({ value: num });
489
- }
490
- getValueAsBoolean({
491
- value,
492
- as
493
- }) {
494
- const bool = as === "key" ? typeof value === "string" ? value === "true" : value === "false" ? false : value : value;
495
- return this.context.getValueAsBoolean({ value: bool });
496
- }
497
- };
498
-
499
- export {
500
- DynamicTypeLiteralMapper
501
- };