@trayio/tray-openapi 0.0.1-beta

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 (62) hide show
  1. package/LICENSE.txt +22 -0
  2. package/README.md +39 -0
  3. package/dist/OpenApiCodecs.d.ts +5 -0
  4. package/dist/OpenApiCodecs.d.ts.map +1 -0
  5. package/dist/OpenApiCodecs.js +8 -0
  6. package/dist/OpenApiSchemaImporter.d.ts +17 -0
  7. package/dist/OpenApiSchemaImporter.d.ts.map +1 -0
  8. package/dist/OpenApiSchemaImporter.js +138 -0
  9. package/dist/OpenApiSchemaImporter.test.d.ts +2 -0
  10. package/dist/OpenApiSchemaImporter.test.d.ts.map +1 -0
  11. package/dist/OpenApiSchemaImporter.test.js +85 -0
  12. package/dist/OpenApiSchemaTransformer.d.ts +9 -0
  13. package/dist/OpenApiSchemaTransformer.d.ts.map +1 -0
  14. package/dist/OpenApiSchemaTransformer.js +68 -0
  15. package/dist/OpenApiSchemaTransformer.test.d.ts +2 -0
  16. package/dist/OpenApiSchemaTransformer.test.d.ts.map +1 -0
  17. package/dist/OpenApiSchemaTransformer.test.js +118 -0
  18. package/dist/OpenApiTypeDescriptors.d.ts +109 -0
  19. package/dist/OpenApiTypeDescriptors.d.ts.map +1 -0
  20. package/dist/OpenApiTypeDescriptors.js +107 -0
  21. package/dist/file-generators/GenerateHandler.d.ts +15 -0
  22. package/dist/file-generators/GenerateHandler.d.ts.map +1 -0
  23. package/dist/file-generators/GenerateHandler.js +110 -0
  24. package/dist/file-generators/GenerateHandler.test.d.ts +2 -0
  25. package/dist/file-generators/GenerateHandler.test.d.ts.map +1 -0
  26. package/dist/file-generators/GenerateHandler.test.js +427 -0
  27. package/dist/file-generators/GenerateHandlerTest.d.ts +9 -0
  28. package/dist/file-generators/GenerateHandlerTest.d.ts.map +1 -0
  29. package/dist/file-generators/GenerateHandlerTest.js +88 -0
  30. package/dist/file-generators/GenerateHandlerTest.test.d.ts +2 -0
  31. package/dist/file-generators/GenerateHandlerTest.test.d.ts.map +1 -0
  32. package/dist/file-generators/GenerateHandlerTest.test.js +640 -0
  33. package/dist/file-generators/GenerateOperationJson.d.ts +4 -0
  34. package/dist/file-generators/GenerateOperationJson.d.ts.map +1 -0
  35. package/dist/file-generators/GenerateOperationJson.js +44 -0
  36. package/dist/file-generators/GenerateOperationJson.test.d.ts +2 -0
  37. package/dist/file-generators/GenerateOperationJson.test.d.ts.map +1 -0
  38. package/dist/file-generators/GenerateOperationJson.test.js +78 -0
  39. package/dist/file-generators/types/GenerateInputSchema.d.ts +4 -0
  40. package/dist/file-generators/types/GenerateInputSchema.d.ts.map +1 -0
  41. package/dist/file-generators/types/GenerateInputSchema.js +131 -0
  42. package/dist/file-generators/types/GenerateInputSchema.test.d.ts +2 -0
  43. package/dist/file-generators/types/GenerateInputSchema.test.d.ts.map +1 -0
  44. package/dist/file-generators/types/GenerateInputSchema.test.js +260 -0
  45. package/dist/file-generators/types/GenerateInputType.test.d.ts +2 -0
  46. package/dist/file-generators/types/GenerateInputType.test.d.ts.map +1 -0
  47. package/dist/file-generators/types/GenerateInputType.test.js +447 -0
  48. package/dist/file-generators/types/GenerateInputTypes.d.ts +4 -0
  49. package/dist/file-generators/types/GenerateInputTypes.d.ts.map +1 -0
  50. package/dist/file-generators/types/GenerateInputTypes.js +45 -0
  51. package/dist/file-generators/types/GenerateOutput.d.ts +8 -0
  52. package/dist/file-generators/types/GenerateOutput.d.ts.map +1 -0
  53. package/dist/file-generators/types/GenerateOutput.js +81 -0
  54. package/dist/file-generators/types/GenerateOutput.test.d.ts +2 -0
  55. package/dist/file-generators/types/GenerateOutput.test.d.ts.map +1 -0
  56. package/dist/file-generators/types/GenerateOutput.test.js +213 -0
  57. package/dist/file-generators/types/JsonSchemaToTypescriptOptions.d.ts +9 -0
  58. package/dist/file-generators/types/JsonSchemaToTypescriptOptions.d.ts.map +1 -0
  59. package/dist/file-generators/types/JsonSchemaToTypescriptOptions.js +11 -0
  60. package/dist/templates/connector-template.zip +0 -0
  61. package/dist/test-openapi-spec.json +160 -0
  62. package/package.json +30 -0
@@ -0,0 +1,427 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const E = __importStar(require("fp-ts/Either"));
27
+ const O = __importStar(require("fp-ts/Option"));
28
+ const GenerateHandler_1 = require("./GenerateHandler");
29
+ const getGenerateHandlerInput = (input) => (Object.assign({ connectorNamePascalCase: 'OpenApiTest', operationNamePascalCase: 'GetPhoto', operationNameCamelCase: 'getPhoto', httpMethod: 'get', baseUrl: 'https://jsonplaceholder.typicode.com', input: {
30
+ title: O.none,
31
+ properties: O.none,
32
+ additionalProperties: O.none,
33
+ required: O.none,
34
+ allOf: O.none,
35
+ oneOf: O.none,
36
+ anyOf: O.none,
37
+ not: O.none,
38
+ in: O.none,
39
+ type: O.none,
40
+ }, httpPath: '/photos' }, input));
41
+ describe('GenerateHandler', () => {
42
+ it('it should generate a handler with no inputs', () => {
43
+ const payload = getGenerateHandlerInput({});
44
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
45
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
46
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
47
+ import { GetPhotoInput } from "./input";
48
+ import { GetPhotoOutput } from "./output";
49
+
50
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
51
+ OpenApiTestAuth,
52
+ GetPhotoInput,
53
+ GetPhotoOutput
54
+ >((handler) =>
55
+ handler.usingHttp((http) =>
56
+ http
57
+ .get("https://jsonplaceholder.typicode.com/photos")
58
+ .handleRequest((ctx, input, request) => request.withoutBody())
59
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
60
+ )
61
+ );`)).toEqual(generatedHandler);
62
+ });
63
+ it('it should generate a handler with a path parameter', () => {
64
+ const input = {
65
+ type: O.some('object'),
66
+ title: O.none,
67
+ properties: O.some({
68
+ photoId: {
69
+ title: O.none,
70
+ type: O.some('string'),
71
+ in: O.some('path'),
72
+ properties: O.none,
73
+ additionalProperties: O.none,
74
+ required: O.none,
75
+ allOf: O.none,
76
+ oneOf: O.none,
77
+ anyOf: O.none,
78
+ not: O.none,
79
+ },
80
+ }),
81
+ required: O.some(['photoId']),
82
+ additionalProperties: O.some(false),
83
+ allOf: O.none,
84
+ oneOf: O.none,
85
+ anyOf: O.none,
86
+ not: O.none,
87
+ in: O.none,
88
+ };
89
+ const payload = getGenerateHandlerInput({
90
+ input,
91
+ httpPath: '/photos/{photoId}',
92
+ });
93
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
94
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
95
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
96
+ import { GetPhotoInput } from "./input";
97
+ import { GetPhotoOutput } from "./output";
98
+
99
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
100
+ OpenApiTestAuth,
101
+ GetPhotoInput,
102
+ GetPhotoOutput
103
+ >((handler) =>
104
+ handler.usingHttp((http) =>
105
+ http
106
+ .get("https://jsonplaceholder.typicode.com/photos/:photoId")
107
+ .handleRequest((ctx, input, request) => request.addPathParameter("photoId", input.photoId).withoutBody())
108
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
109
+ )
110
+ );`)).toEqual(generatedHandler);
111
+ });
112
+ it('it should generate a handler with a body input as json', () => {
113
+ const input = {
114
+ type: O.some('object'),
115
+ title: O.none,
116
+ properties: O.some({
117
+ photoId: {
118
+ title: O.none,
119
+ type: O.some('string'),
120
+ in: O.some('body'),
121
+ properties: O.none,
122
+ additionalProperties: O.none,
123
+ required: O.none,
124
+ allOf: O.none,
125
+ oneOf: O.none,
126
+ anyOf: O.none,
127
+ not: O.none,
128
+ },
129
+ }),
130
+ required: O.some(['photoId']),
131
+ additionalProperties: O.some(false),
132
+ allOf: O.none,
133
+ oneOf: O.none,
134
+ anyOf: O.none,
135
+ not: O.none,
136
+ in: O.none,
137
+ };
138
+ const payload = getGenerateHandlerInput({ input });
139
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
140
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
141
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
142
+ import { GetPhotoInput } from "./input";
143
+ import { GetPhotoOutput } from "./output";
144
+
145
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
146
+ OpenApiTestAuth,
147
+ GetPhotoInput,
148
+ GetPhotoOutput
149
+ >((handler) =>
150
+ handler.usingHttp((http) =>
151
+ http
152
+ .get("https://jsonplaceholder.typicode.com/photos")
153
+ .handleRequest((ctx, input, request) => request.withBodyAsJson({ photoId: input.photoId }))
154
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
155
+ )
156
+ );`)).toEqual(generatedHandler);
157
+ });
158
+ it('it should generate a handler with a query string input', () => {
159
+ const input = {
160
+ type: O.some('object'),
161
+ title: O.none,
162
+ properties: O.some({
163
+ id: {
164
+ title: O.none,
165
+ type: O.some('string'),
166
+ in: O.some('query'),
167
+ properties: O.none,
168
+ additionalProperties: O.none,
169
+ required: O.none,
170
+ allOf: O.none,
171
+ oneOf: O.none,
172
+ anyOf: O.none,
173
+ not: O.none,
174
+ },
175
+ }),
176
+ required: O.some(['id']),
177
+ additionalProperties: O.some(false),
178
+ allOf: O.none,
179
+ oneOf: O.none,
180
+ anyOf: O.none,
181
+ not: O.none,
182
+ in: O.none,
183
+ };
184
+ const payload = getGenerateHandlerInput({ input });
185
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
186
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
187
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
188
+ import { GetPhotoInput } from "./input";
189
+ import { GetPhotoOutput } from "./output";
190
+
191
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
192
+ OpenApiTestAuth,
193
+ GetPhotoInput,
194
+ GetPhotoOutput
195
+ >((handler) =>
196
+ handler.usingHttp((http) =>
197
+ http
198
+ .get("https://jsonplaceholder.typicode.com/photos")
199
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id).withoutBody())
200
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
201
+ )
202
+ );`)).toEqual(generatedHandler);
203
+ });
204
+ it('it should generate a handler with a header input', () => {
205
+ const input = {
206
+ type: O.some('object'),
207
+ title: O.none,
208
+ properties: O.some({
209
+ 'Content-Type': {
210
+ title: O.none,
211
+ type: O.some('string'),
212
+ in: O.some('header'),
213
+ properties: O.none,
214
+ additionalProperties: O.none,
215
+ required: O.none,
216
+ allOf: O.none,
217
+ oneOf: O.none,
218
+ anyOf: O.none,
219
+ not: O.none,
220
+ },
221
+ }),
222
+ required: O.some(['photoId']),
223
+ additionalProperties: O.some(false),
224
+ allOf: O.none,
225
+ oneOf: O.none,
226
+ anyOf: O.none,
227
+ not: O.none,
228
+ in: O.none,
229
+ };
230
+ const payload = getGenerateHandlerInput({ input });
231
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
232
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
233
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
234
+ import { GetPhotoInput } from "./input";
235
+ import { GetPhotoOutput } from "./output";
236
+
237
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
238
+ OpenApiTestAuth,
239
+ GetPhotoInput,
240
+ GetPhotoOutput
241
+ >((handler) =>
242
+ handler.usingHttp((http) =>
243
+ http
244
+ .get("https://jsonplaceholder.typicode.com/photos")
245
+ .handleRequest((ctx, input, request) => request.addHeader("Content-Type", input.Content-Type).withoutBody())
246
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
247
+ )
248
+ );`)).toEqual(generatedHandler);
249
+ });
250
+ it('it should generate a handler and transform number type to string for input', () => {
251
+ const input = {
252
+ type: O.some('object'),
253
+ title: O.none,
254
+ properties: O.some({
255
+ 'Content-Type': {
256
+ title: O.none,
257
+ type: O.some('integer'),
258
+ in: O.some('header'),
259
+ properties: O.none,
260
+ additionalProperties: O.none,
261
+ required: O.none,
262
+ allOf: O.none,
263
+ oneOf: O.none,
264
+ anyOf: O.none,
265
+ not: O.none,
266
+ },
267
+ }),
268
+ required: O.none,
269
+ additionalProperties: O.some(false),
270
+ allOf: O.none,
271
+ oneOf: O.none,
272
+ anyOf: O.none,
273
+ not: O.none,
274
+ in: O.none,
275
+ };
276
+ const payload = getGenerateHandlerInput({ input });
277
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
278
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
279
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
280
+ import { GetPhotoInput } from "./input";
281
+ import { GetPhotoOutput } from "./output";
282
+
283
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
284
+ OpenApiTestAuth,
285
+ GetPhotoInput,
286
+ GetPhotoOutput
287
+ >((handler) =>
288
+ handler.usingHttp((http) =>
289
+ http
290
+ .get("https://jsonplaceholder.typicode.com/photos")
291
+ .handleRequest((ctx, input, request) => request.addHeader("Content-Type", input.Content-Type.toString()).withoutBody())
292
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
293
+ )
294
+ );`)).toEqual(generatedHandler);
295
+ });
296
+ it('it should generate a handler with a query string and path param input', () => {
297
+ const input = {
298
+ type: O.some('object'),
299
+ title: O.none,
300
+ properties: O.some({
301
+ id: {
302
+ title: O.none,
303
+ type: O.some('string'),
304
+ in: O.some('query'),
305
+ properties: O.none,
306
+ additionalProperties: O.none,
307
+ required: O.none,
308
+ allOf: O.none,
309
+ oneOf: O.none,
310
+ anyOf: O.none,
311
+ not: O.none,
312
+ },
313
+ photoId: {
314
+ title: O.none,
315
+ type: O.some('string'),
316
+ in: O.some('path'),
317
+ properties: O.none,
318
+ additionalProperties: O.none,
319
+ required: O.none,
320
+ allOf: O.none,
321
+ oneOf: O.none,
322
+ anyOf: O.none,
323
+ not: O.none,
324
+ },
325
+ commentId: {
326
+ title: O.none,
327
+ type: O.some('string'),
328
+ in: O.some('path'),
329
+ properties: O.none,
330
+ additionalProperties: O.none,
331
+ required: O.none,
332
+ allOf: O.none,
333
+ oneOf: O.none,
334
+ anyOf: O.none,
335
+ not: O.none,
336
+ },
337
+ }),
338
+ required: O.some(['photoId']),
339
+ additionalProperties: O.some(false),
340
+ allOf: O.none,
341
+ oneOf: O.none,
342
+ anyOf: O.none,
343
+ not: O.none,
344
+ in: O.none,
345
+ };
346
+ const payload = getGenerateHandlerInput({
347
+ input,
348
+ httpPath: '/photos/{photoId}/{commentId}',
349
+ });
350
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
351
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
352
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
353
+ import { GetPhotoInput } from "./input";
354
+ import { GetPhotoOutput } from "./output";
355
+
356
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
357
+ OpenApiTestAuth,
358
+ GetPhotoInput,
359
+ GetPhotoOutput
360
+ >((handler) =>
361
+ handler.usingHttp((http) =>
362
+ http
363
+ .get("https://jsonplaceholder.typicode.com/photos/:photoId/:commentId")
364
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id).addPathParameter("photoId", input.photoId).addPathParameter("commentId", input.commentId).withoutBody())
365
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
366
+ )
367
+ );`)).toEqual(generatedHandler);
368
+ });
369
+ it('it should generate a handler with a query string and body param input', () => {
370
+ const input = {
371
+ type: O.some('object'),
372
+ title: O.none,
373
+ properties: O.some({
374
+ id: {
375
+ title: O.none,
376
+ type: O.some('string'),
377
+ in: O.some('query'),
378
+ properties: O.none,
379
+ additionalProperties: O.none,
380
+ required: O.none,
381
+ allOf: O.none,
382
+ oneOf: O.none,
383
+ anyOf: O.none,
384
+ not: O.none,
385
+ },
386
+ photoId: {
387
+ title: O.none,
388
+ type: O.some('string'),
389
+ in: O.some('body'),
390
+ properties: O.none,
391
+ additionalProperties: O.none,
392
+ required: O.none,
393
+ allOf: O.none,
394
+ oneOf: O.none,
395
+ anyOf: O.none,
396
+ not: O.none,
397
+ },
398
+ }),
399
+ required: O.some(['id', 'photoId']),
400
+ additionalProperties: O.some(false),
401
+ allOf: O.none,
402
+ oneOf: O.none,
403
+ anyOf: O.none,
404
+ not: O.none,
405
+ in: O.none,
406
+ };
407
+ const payload = getGenerateHandlerInput({ input });
408
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
409
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
410
+ import { OpenApiTestAuth } from "../OpenApiTestAuth";
411
+ import { GetPhotoInput } from "./input";
412
+ import { GetPhotoOutput } from "./output";
413
+
414
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
415
+ OpenApiTestAuth,
416
+ GetPhotoInput,
417
+ GetPhotoOutput
418
+ >((handler) =>
419
+ handler.usingHttp((http) =>
420
+ http
421
+ .get("https://jsonplaceholder.typicode.com/photos")
422
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id).withBodyAsJson({ photoId: input.photoId }))
423
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
424
+ )
425
+ );`)).toEqual(generatedHandler);
426
+ });
427
+ });
@@ -0,0 +1,9 @@
1
+ import * as E from 'fp-ts/Either';
2
+ import { SchemaObject } from '../OpenApiTypeDescriptors';
3
+ export type GenerateHandlerTestInput = {
4
+ operationNameCamelCase: string;
5
+ input: SchemaObject;
6
+ output: SchemaObject;
7
+ };
8
+ export declare const generateHandlerTest: ({ operationNameCamelCase, input, output, }: GenerateHandlerTestInput) => E.Either<Error, string>;
9
+ //# sourceMappingURL=GenerateHandlerTest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateHandlerTest.d.ts","sourceRoot":"","sources":["../../src/file-generators/GenerateHandlerTest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAGlC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,MAAM,MAAM,wBAAwB,GAAG;IACtC,sBAAsB,EAAE,MAAM,CAAC;IAC/B,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,YAAY,CAAC;CACrB,CAAC;AAiDF,eAAO,MAAM,mBAAmB,+CAI7B,wBAAwB,KAAG,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAqClD,CAAC"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.generateHandlerTest = void 0;
27
+ const E = __importStar(require("fp-ts/Either"));
28
+ const O = __importStar(require("fp-ts/Option"));
29
+ const getDefaultValue = (prop) => {
30
+ if (prop.type === 'array') {
31
+ return prop.items ? `[${getDefaultValue(prop.items)}]` : '[]';
32
+ }
33
+ return O.fold(() => `'test_value'`, (type) => {
34
+ switch (type) {
35
+ case 'string':
36
+ return `'test'`;
37
+ case 'number':
38
+ case 'integer':
39
+ return 2;
40
+ case 'boolean':
41
+ return 'true';
42
+ // case 'array':
43
+ // return prop.items ? `[${getDefaultValue(prop.items)}]` : '[]';
44
+ case 'object':
45
+ return O.fold(() => `{}`, (properties) => handleInput(properties))(prop.properties);
46
+ // return prop.properties ? handleInput(prop.properties) : '{}';
47
+ default:
48
+ return `'test_value'`;
49
+ }
50
+ })(prop.type);
51
+ };
52
+ // TODO: make handleInput and handleOutput accept an option and do the fold inside, will make the code cleaner in the template and switch case
53
+ const handleInput = (inputs) => {
54
+ const populatedTestInputs = Object.entries(inputs).map(([key, prop]) => `${key}: ${getDefaultValue(prop)}`);
55
+ return `{ ${populatedTestInputs.join(', ')} }`;
56
+ };
57
+ const handleOutput = (outputs) => {
58
+ const expectations = Object.entries(outputs).map(([key, prop]) => `expect(outputValue.${key}).toEqual(${getDefaultValue(prop)});`);
59
+ return expectations.join('\n');
60
+ };
61
+ const generateHandlerTest = ({ operationNameCamelCase, input, output, }) => E.tryCatch(() => `import { OperationHandlerTestSetup } from '@trayio/cdk-dsl/connector/operation/OperationHandlerTest';
62
+ import { OperationHandlerResult } from '@trayio/cdk-dsl/connector/operation/OperationHandler';
63
+ import { ${operationNameCamelCase}Handler } from './handler';
64
+ import '@trayio/cdk-runtime/connector/operation/OperationHandlerTestRunner';
65
+
66
+ OperationHandlerTestSetup.configureHandlerTest(${operationNameCamelCase}Handler, (handlerTest) =>
67
+ handlerTest
68
+ .usingHandlerContext('test')
69
+ .nothingBeforeAll()
70
+ .testCase('should ${operationNameCamelCase}', (testCase) =>
71
+ testCase
72
+ .givenNothing()
73
+ .when(() => (${O.fold(() => `{}`, (properties) => handleInput(properties))(input.properties)}
74
+ ))
75
+ .then(({ output }) => {
76
+ // console.log(output);
77
+ const outputValue =
78
+ OperationHandlerResult.getSuccessfulValueOrFail(output);
79
+ ${O.fold(() => `{}`, (properties) => handleOutput(properties))(output.properties)}
80
+
81
+ })
82
+ .finallyDoNothing()
83
+ )
84
+ .nothingAfterAll()
85
+ );`, (e) => new Error(`Failed to generate handler test: ${e}`));
86
+ exports.generateHandlerTest = generateHandlerTest;
87
+ // ${handleOutput(output.properties || {})}
88
+ // .when(() => (${O.fold(input.properties ? handleInput(input.properties) : `{}`
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=GenerateHandlerTest.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateHandlerTest.test.d.ts","sourceRoot":"","sources":["../../src/file-generators/GenerateHandlerTest.test.ts"],"names":[],"mappings":""}