@fern-api/typescript-dynamic-snippets 0.0.5 → 0.0.7

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,538 +0,0 @@
1
- import {
2
- Scope,
3
- Severity
4
- } from "./chunk-K6P3GMIJ.js";
5
- import {
6
- __publicField,
7
- assertNever,
8
- init_buffer,
9
- init_process,
10
- typescript_exports
11
- } from "./chunk-SGEN5HN3.js";
12
-
13
- // src/EndpointSnippetGenerator.ts
14
- init_process();
15
- init_buffer();
16
- var CLIENT_VAR_NAME = "client";
17
- var MAIN_FUNCTION_NAME = "main";
18
- var STRING_TYPE_REFERENCE = {
19
- type: "primitive",
20
- value: "STRING"
21
- };
22
- var EndpointSnippetGenerator = class {
23
- constructor({ context }) {
24
- __publicField(this, "context");
25
- this.context = context;
26
- }
27
- async generateSnippet({
28
- endpoint,
29
- request
30
- }) {
31
- const code = this.buildCodeBlock({ endpoint, snippet: request });
32
- return await code.toString({ customConfig: this.context.customConfig });
33
- }
34
- generateSnippetSync({
35
- endpoint,
36
- request
37
- }) {
38
- const code = this.buildCodeBlock({ endpoint, snippet: request });
39
- return code.toStringSync({ customConfig: this.context.customConfig });
40
- }
41
- buildCodeBlock({
42
- endpoint,
43
- snippet
44
- }) {
45
- return typescript_exports.codeblock((writer) => {
46
- writer.writeNode(
47
- typescript_exports.function_({
48
- name: MAIN_FUNCTION_NAME,
49
- async: true,
50
- parameters: [],
51
- body: typescript_exports.codeblock((writer2) => {
52
- writer2.writeNodeStatement(this.constructClient({ endpoint, snippet }));
53
- writer2.writeNodeStatement(this.callMethod({ endpoint, snippet }));
54
- })
55
- })
56
- );
57
- writer.writeNodeStatement(
58
- typescript_exports.invokeFunction({
59
- function_: typescript_exports.reference({
60
- name: MAIN_FUNCTION_NAME
61
- }),
62
- arguments_: []
63
- })
64
- );
65
- });
66
- }
67
- constructClient({
68
- endpoint,
69
- snippet
70
- }) {
71
- return typescript_exports.variable({
72
- name: CLIENT_VAR_NAME,
73
- const: true,
74
- initializer: typescript_exports.instantiateClass({
75
- class_: typescript_exports.reference({
76
- name: this.context.getRootClientName(),
77
- importFrom: this.context.getModuleImport()
78
- }),
79
- arguments_: [this.getConstructorArgs({ endpoint, snippet })]
80
- })
81
- });
82
- }
83
- getConstructorArgs({
84
- endpoint,
85
- snippet
86
- }) {
87
- const fields = [];
88
- const environmentArgs = this.getConstructorEnvironmentArgs({
89
- baseUrl: snippet.baseURL,
90
- environment: snippet.environment
91
- });
92
- if (environmentArgs.length > 0) {
93
- fields.push(...environmentArgs);
94
- }
95
- if (endpoint.auth != null) {
96
- if (snippet.auth != null) {
97
- fields.push(...this.getConstructorAuthArgs({ auth: endpoint.auth, values: snippet.auth }));
98
- } else {
99
- this.context.errors.add({
100
- severity: Severity.Warning,
101
- message: `Auth with ${endpoint.auth.type} configuration is required for this endpoint`
102
- });
103
- }
104
- }
105
- this.context.errors.scope(Scope.Headers);
106
- if (this.context.ir.headers != null && snippet.headers != null) {
107
- fields.push(
108
- ...this.getConstructorHeaderArgs({ headers: this.context.ir.headers, values: snippet.headers })
109
- );
110
- }
111
- this.context.errors.unscope();
112
- if (fields.length === 0) {
113
- return typescript_exports.TypeLiteral.nop();
114
- }
115
- return typescript_exports.TypeLiteral.object({ fields });
116
- }
117
- getConstructorEnvironmentArgs({
118
- baseUrl,
119
- environment
120
- }) {
121
- const environmentValue = this.getEnvironmentValue({ baseUrl, environment });
122
- if (environmentValue == null) {
123
- return [];
124
- }
125
- return [
126
- {
127
- name: "environment",
128
- value: environmentValue
129
- }
130
- ];
131
- }
132
- getEnvironmentValue({
133
- baseUrl,
134
- environment
135
- }) {
136
- if (baseUrl != null && environment != null) {
137
- this.context.errors.add({
138
- severity: Severity.Critical,
139
- message: "Cannot specify both baseUrl and environment options"
140
- });
141
- return void 0;
142
- }
143
- if (baseUrl != null) {
144
- return typescript_exports.TypeLiteral.string(baseUrl);
145
- }
146
- if (environment != null) {
147
- if (this.context.isSingleEnvironmentID(environment)) {
148
- const environmentTypeReference = this.context.getEnvironmentTypeReferenceFromID(environment);
149
- if (environmentTypeReference == null) {
150
- this.context.errors.add({
151
- severity: Severity.Warning,
152
- message: `Environment ${JSON.stringify(environment)} was not found`
153
- });
154
- return void 0;
155
- }
156
- return typescript_exports.TypeLiteral.reference(environmentTypeReference);
157
- }
158
- if (this.context.isMultiEnvironmentValues(environment)) {
159
- if (!this.context.validateMultiEnvironmentUrlValues(environment)) {
160
- return void 0;
161
- }
162
- return typescript_exports.TypeLiteral.object({
163
- fields: Object.entries(environment).map(([key, value]) => ({
164
- name: key,
165
- value: this.context.dynamicTypeLiteralMapper.convert({
166
- typeReference: STRING_TYPE_REFERENCE,
167
- value
168
- })
169
- }))
170
- });
171
- }
172
- }
173
- return void 0;
174
- }
175
- getConstructorAuthArgs({
176
- auth,
177
- values
178
- }) {
179
- switch (auth.type) {
180
- case "basic":
181
- if (values.type !== "basic") {
182
- this.context.errors.add({
183
- severity: Severity.Critical,
184
- message: this.context.newAuthMismatchError({ auth, values }).message
185
- });
186
- return [];
187
- }
188
- return this.getConstructorBasicAuthArg({ auth, values });
189
- case "bearer":
190
- if (values.type !== "bearer") {
191
- this.context.errors.add({
192
- severity: Severity.Critical,
193
- message: this.context.newAuthMismatchError({ auth, values }).message
194
- });
195
- return [];
196
- }
197
- return this.getConstructorBearerAuthArgs({ auth, values });
198
- case "header":
199
- if (values.type !== "header") {
200
- this.context.errors.add({
201
- severity: Severity.Critical,
202
- message: this.context.newAuthMismatchError({ auth, values }).message
203
- });
204
- return [];
205
- }
206
- return this.getConstructorHeaderAuthArgs({ auth, values });
207
- default:
208
- assertNever(auth);
209
- }
210
- }
211
- getConstructorBasicAuthArg({
212
- auth,
213
- values
214
- }) {
215
- return [
216
- {
217
- name: this.context.getPropertyName(auth.username),
218
- value: typescript_exports.TypeLiteral.string(values.username)
219
- },
220
- {
221
- name: this.context.getPropertyName(auth.password),
222
- value: typescript_exports.TypeLiteral.string(values.password)
223
- }
224
- ];
225
- }
226
- getConstructorBearerAuthArgs({
227
- auth,
228
- values
229
- }) {
230
- return [
231
- {
232
- name: this.context.getPropertyName(auth.token),
233
- value: typescript_exports.TypeLiteral.string(values.token)
234
- }
235
- ];
236
- }
237
- getConstructorHeaderAuthArgs({
238
- auth,
239
- values
240
- }) {
241
- return [
242
- {
243
- name: this.context.getPropertyName(auth.header.name.name),
244
- value: this.context.dynamicTypeLiteralMapper.convert({
245
- typeReference: auth.header.typeReference,
246
- value: values.value
247
- })
248
- }
249
- ];
250
- }
251
- getConstructorHeaderArgs({
252
- headers,
253
- values
254
- }) {
255
- const fields = [];
256
- for (const header of headers) {
257
- const field = this.getConstructorHeaderArg({ header, value: values.value });
258
- if (field != null) {
259
- fields.push(field);
260
- }
261
- }
262
- return fields;
263
- }
264
- getConstructorHeaderArg({
265
- header,
266
- value
267
- }) {
268
- const typeLiteral = this.context.dynamicTypeLiteralMapper.convert({
269
- typeReference: header.typeReference,
270
- value
271
- });
272
- if (typescript_exports.TypeLiteral.isNop(typeLiteral)) {
273
- return void 0;
274
- }
275
- return {
276
- name: this.context.getPropertyName(header.name.name),
277
- value: typeLiteral
278
- };
279
- }
280
- callMethod({
281
- endpoint,
282
- snippet
283
- }) {
284
- return typescript_exports.invokeMethod({
285
- on: typescript_exports.reference({ name: CLIENT_VAR_NAME }),
286
- method: this.getMethod({ endpoint }),
287
- async: true,
288
- arguments_: this.getMethodArgs({ endpoint, snippet })
289
- });
290
- }
291
- getMethodArgs({
292
- endpoint,
293
- snippet
294
- }) {
295
- switch (endpoint.request.type) {
296
- case "inlined":
297
- return this.getMethodArgsForInlinedRequest({ request: endpoint.request, snippet });
298
- case "body":
299
- return this.getMethodArgsForBodyRequest({ request: endpoint.request, snippet });
300
- default:
301
- assertNever(endpoint.request);
302
- }
303
- }
304
- getMethodArgsForBodyRequest({
305
- request,
306
- snippet
307
- }) {
308
- const args = [];
309
- this.context.errors.scope(Scope.PathParameters);
310
- if (request.pathParameters != null) {
311
- const pathParameterFields = this.getPathParameters({ namedParameters: request.pathParameters, snippet });
312
- args.push(...pathParameterFields.map((field) => field.value));
313
- }
314
- this.context.errors.unscope();
315
- this.context.errors.scope(Scope.RequestBody);
316
- if (request.body != null) {
317
- args.push(this.getBodyRequestArg({ body: request.body, value: snippet.requestBody }));
318
- }
319
- this.context.errors.unscope();
320
- return args;
321
- }
322
- getBodyRequestArg({
323
- body,
324
- value
325
- }) {
326
- switch (body.type) {
327
- case "bytes":
328
- return this.getBytesBodyRequestArg({ value });
329
- case "typeReference":
330
- return this.context.dynamicTypeLiteralMapper.convert({ typeReference: body.value, value });
331
- default:
332
- assertNever(body);
333
- }
334
- }
335
- getBytesBodyRequestArg({ value }) {
336
- if (typeof value !== "string") {
337
- this.context.errors.add({
338
- severity: Severity.Critical,
339
- message: `Expected bytes value to be a string, got ${typeof value}`
340
- });
341
- return typescript_exports.TypeLiteral.nop();
342
- }
343
- return typescript_exports.TypeLiteral.blob(value);
344
- }
345
- getMethodArgsForInlinedRequest({
346
- request,
347
- snippet
348
- }) {
349
- var _a, _b, _c, _d;
350
- const args = [];
351
- const { inlinePathParameters, inlineFileProperties } = {
352
- inlinePathParameters: (_b = (_a = this.context.customConfig) == null ? void 0 : _a.inlinePathParameters) != null ? _b : false,
353
- inlineFileProperties: (_d = (_c = this.context.customConfig) == null ? void 0 : _c.inlineFileProperties) != null ? _d : false
354
- };
355
- this.context.errors.scope(Scope.PathParameters);
356
- const pathParameterFields = [];
357
- if (request.pathParameters != null) {
358
- pathParameterFields.push(...this.getPathParameters({ namedParameters: request.pathParameters, snippet }));
359
- }
360
- this.context.errors.unscope();
361
- this.context.errors.scope(Scope.RequestBody);
362
- const filePropertyInfo = this.getFilePropertyInfo({ request, snippet });
363
- this.context.errors.unscope();
364
- if (!this.context.includePathParametersInWrappedRequest({
365
- request,
366
- inlinePathParameters
367
- })) {
368
- args.push(...pathParameterFields.map((field) => field.value));
369
- }
370
- if (!inlineFileProperties) {
371
- args.push(...filePropertyInfo.fileFields.map((field) => field.value));
372
- }
373
- if (this.context.needsRequestParameter({
374
- request,
375
- inlinePathParameters,
376
- inlineFileProperties
377
- })) {
378
- args.push(
379
- this.getInlinedRequestArg({
380
- request,
381
- snippet,
382
- pathParameterFields: this.context.includePathParametersInWrappedRequest({
383
- request,
384
- inlinePathParameters
385
- }) ? pathParameterFields : [],
386
- filePropertyInfo
387
- })
388
- );
389
- }
390
- return args;
391
- }
392
- getFilePropertyInfo({
393
- request,
394
- snippet
395
- }) {
396
- if (request.body == null || !this.context.isFileUploadRequestBody(request.body)) {
397
- return {
398
- fileFields: [],
399
- bodyPropertyFields: []
400
- };
401
- }
402
- return this.context.filePropertyMapper.getFilePropertyInfo({
403
- body: request.body,
404
- value: snippet.requestBody
405
- });
406
- }
407
- getInlinedRequestArg({
408
- request,
409
- snippet,
410
- pathParameterFields,
411
- filePropertyInfo
412
- }) {
413
- var _a, _b, _c, _d;
414
- this.context.errors.scope(Scope.QueryParameters);
415
- const queryParameters = this.context.associateQueryParametersByWireValue({
416
- parameters: (_a = request.queryParameters) != null ? _a : [],
417
- values: (_b = snippet.queryParameters) != null ? _b : {}
418
- });
419
- const queryParameterFields = queryParameters.map((queryParameter) => ({
420
- name: this.context.getPropertyName(queryParameter.name.name),
421
- value: this.context.dynamicTypeLiteralMapper.convert(queryParameter)
422
- }));
423
- this.context.errors.unscope();
424
- this.context.errors.scope(Scope.Headers);
425
- const headers = this.context.associateByWireValue({
426
- parameters: (_c = request.headers) != null ? _c : [],
427
- values: (_d = snippet.headers) != null ? _d : {}
428
- });
429
- const headerFields = headers.map((header) => ({
430
- name: this.context.getPropertyName(header.name.name),
431
- value: this.context.dynamicTypeLiteralMapper.convert(header)
432
- }));
433
- this.context.errors.unscope();
434
- this.context.errors.scope(Scope.RequestBody);
435
- const requestBodyFields = request.body != null ? this.getInlinedRequestBodyObjectFields({
436
- body: request.body,
437
- value: snippet.requestBody,
438
- filePropertyInfo
439
- }) : [];
440
- this.context.errors.unscope();
441
- return typescript_exports.TypeLiteral.object({
442
- fields: [...pathParameterFields, ...queryParameterFields, ...headerFields, ...requestBodyFields]
443
- });
444
- }
445
- getInlinedRequestBodyObjectFields({
446
- body,
447
- value,
448
- filePropertyInfo
449
- }) {
450
- switch (body.type) {
451
- case "properties":
452
- return this.getInlinedRequestBodyPropertyObjectFields({ parameters: body.value, value });
453
- case "referenced":
454
- return [this.getReferencedRequestBodyPropertyObjectField({ body, value })];
455
- case "fileUpload":
456
- return this.getFileUploadRequestBodyObjectFields({ filePropertyInfo });
457
- default:
458
- assertNever(body);
459
- }
460
- }
461
- getFileUploadRequestBodyObjectFields({
462
- filePropertyInfo
463
- }) {
464
- var _a;
465
- if ((_a = this.context.customConfig) == null ? void 0 : _a.inlineFileProperties) {
466
- return [...filePropertyInfo.fileFields, ...filePropertyInfo.bodyPropertyFields];
467
- }
468
- return filePropertyInfo.bodyPropertyFields;
469
- }
470
- getReferencedRequestBodyPropertyObjectField({
471
- body,
472
- value
473
- }) {
474
- return {
475
- name: this.context.getPropertyName(body.bodyKey),
476
- value: this.getReferencedRequestBodyPropertyTypeLiteral({ body: body.bodyType, value })
477
- };
478
- }
479
- getReferencedRequestBodyPropertyTypeLiteral({
480
- body,
481
- value
482
- }) {
483
- switch (body.type) {
484
- case "bytes":
485
- return this.getBytesBodyRequestArg({ value });
486
- case "typeReference":
487
- return this.context.dynamicTypeLiteralMapper.convert({ typeReference: body.value, value });
488
- default:
489
- assertNever(body);
490
- }
491
- }
492
- getInlinedRequestBodyPropertyObjectFields({
493
- parameters,
494
- value
495
- }) {
496
- var _a;
497
- const fields = [];
498
- const bodyProperties = this.context.associateByWireValue({
499
- parameters,
500
- values: (_a = this.context.getRecord(value)) != null ? _a : {}
501
- });
502
- for (const parameter of bodyProperties) {
503
- fields.push({
504
- name: this.context.getPropertyName(parameter.name.name),
505
- value: this.context.dynamicTypeLiteralMapper.convert(parameter)
506
- });
507
- }
508
- return fields;
509
- }
510
- getPathParameters({
511
- namedParameters,
512
- snippet
513
- }) {
514
- var _a;
515
- const args = [];
516
- const pathParameters = this.context.associateByWireValue({
517
- parameters: namedParameters,
518
- values: (_a = snippet.pathParameters) != null ? _a : {}
519
- });
520
- for (const parameter of pathParameters) {
521
- args.push({
522
- name: this.context.getPropertyName(parameter.name.name),
523
- value: this.context.dynamicTypeLiteralMapper.convert(parameter)
524
- });
525
- }
526
- return args;
527
- }
528
- getMethod({ endpoint }) {
529
- if (endpoint.declaration.fernFilepath.allParts.length > 0) {
530
- return `${endpoint.declaration.fernFilepath.allParts.map((val) => this.context.getMethodName(val)).join(".")}.${this.context.getMethodName(endpoint.declaration.name)}`;
531
- }
532
- return this.context.getMethodName(endpoint.declaration.name);
533
- }
534
- };
535
-
536
- export {
537
- EndpointSnippetGenerator
538
- };