@trayio/tray-openapi 1.17.1 → 2.3.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.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=GenerateHander.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateHander.test.d.ts","sourceRoot":"","sources":["../src/GenerateHander.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const GenerateHandler_1 = require("./GenerateHandler");
4
+ const getGenerateHandlerInput = (input) => (Object.assign({ connectorNamePascalCase: 'OpenApiTest', operationNamePascalCase: 'GetPhoto', operationNameCamelCase: 'getPhoto', httpMethod: 'get', baseUrl: 'https://jsonplaceholder.typicode.com/', input: [], path: 'photos' }, input));
5
+ describe('GenerateHandler', () => {
6
+ it('it should generate a handler', () => {
7
+ const payload = getGenerateHandlerInput({});
8
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
9
+ expect(generatedHandler)
10
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
11
+ import { OpenApiTest } from "../OpenApiTest";
12
+ import { GetPhotoInput } from "./input";
13
+ import { GetPhotoOutput } from "./output";
14
+
15
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
16
+ OpenApiTestAuth,
17
+ GetPhotoInput,
18
+ GetPhotoOutput
19
+ >((handler) =>
20
+ handler.usingHttp((http) =>
21
+ http
22
+ .get("https://jsonplaceholder.typicode.com/photos")
23
+ .handleRequest((ctx, input, request) => request
24
+ .handleResponse((response) => response.parseWithBodyAsJson())
25
+ )
26
+ );`);
27
+ });
28
+ it('it should generate a handler with a path parameter', () => {
29
+ const input = [{ type: 'string', name: 'photoId', in: 'path' }];
30
+ const payload = getGenerateHandlerInput({ input });
31
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
32
+ expect(generatedHandler)
33
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
34
+ import { OpenApiTest } from "../OpenApiTest";
35
+ import { GetPhotoInput } from "./input";
36
+ import { GetPhotoOutput } from "./output";
37
+
38
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
39
+ OpenApiTestAuth,
40
+ GetPhotoInput,
41
+ GetPhotoOutput
42
+ >((handler) =>
43
+ handler.usingHttp((http) =>
44
+ http
45
+ .get("https://jsonplaceholder.typicode.com/photos/:photoId")
46
+ .handleRequest((ctx, input, request) => request.addPathParameter("photoId", input.photoId)
47
+ .handleResponse((response) => response.parseWithBodyAsJson())
48
+ )
49
+ );`);
50
+ });
51
+ it('it should generate a handler with a body input as json', () => {
52
+ const input = [{ type: 'object', name: 'payload', in: 'body' }];
53
+ const payload = getGenerateHandlerInput({ input });
54
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
55
+ expect(generatedHandler)
56
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
57
+ import { OpenApiTest } from "../OpenApiTest";
58
+ import { GetPhotoInput } from "./input";
59
+ import { GetPhotoOutput } from "./output";
60
+
61
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
62
+ OpenApiTestAuth,
63
+ GetPhotoInput,
64
+ GetPhotoOutput
65
+ >((handler) =>
66
+ handler.usingHttp((http) =>
67
+ http
68
+ .get("https://jsonplaceholder.typicode.com/photos")
69
+ .handleRequest((ctx, input, request) => request.withBodyAsJson(input)
70
+ .handleResponse((response) => response.parseWithBodyAsJson())
71
+ )
72
+ );`);
73
+ });
74
+ it('it should generate a handler with a query string input', () => {
75
+ const input = [{ type: 'string', name: 'id', in: 'query' }];
76
+ const payload = getGenerateHandlerInput({ input });
77
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
78
+ expect(generatedHandler)
79
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
80
+ import { OpenApiTest } from "../OpenApiTest";
81
+ import { GetPhotoInput } from "./input";
82
+ import { GetPhotoOutput } from "./output";
83
+
84
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
85
+ OpenApiTestAuth,
86
+ GetPhotoInput,
87
+ GetPhotoOutput
88
+ >((handler) =>
89
+ handler.usingHttp((http) =>
90
+ http
91
+ .get("https://jsonplaceholder.typicode.com/photos")
92
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id)
93
+ .handleResponse((response) => response.parseWithBodyAsJson())
94
+ )
95
+ );`);
96
+ });
97
+ it('it should generate a handler with a header input', () => {
98
+ const input = [
99
+ { type: 'string', name: 'Content-Type', in: 'header' },
100
+ ];
101
+ const payload = getGenerateHandlerInput({ input });
102
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
103
+ expect(generatedHandler)
104
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
105
+ import { OpenApiTest } from "../OpenApiTest";
106
+ import { GetPhotoInput } from "./input";
107
+ import { GetPhotoOutput } from "./output";
108
+
109
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
110
+ OpenApiTestAuth,
111
+ GetPhotoInput,
112
+ GetPhotoOutput
113
+ >((handler) =>
114
+ handler.usingHttp((http) =>
115
+ http
116
+ .get("https://jsonplaceholder.typicode.com/photos")
117
+ .handleRequest((ctx, input, request) => request.addHeader("Content-Type", input.Content-Type)
118
+ .handleResponse((response) => response.parseWithBodyAsJson())
119
+ )
120
+ );`);
121
+ });
122
+ it('it should generate a handler with a query string and path param input', () => {
123
+ const input = [
124
+ { type: 'string', name: 'id', in: 'query' },
125
+ { type: 'string', name: 'photoId', in: 'path' },
126
+ { type: 'string', name: 'commentId', in: 'path' },
127
+ ];
128
+ const payload = getGenerateHandlerInput({ input });
129
+ const generatedHandler = (0, GenerateHandler_1.generateHandler)(payload);
130
+ expect(generatedHandler)
131
+ .toEqual(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
132
+ import { OpenApiTest } from "../OpenApiTest";
133
+ import { GetPhotoInput } from "./input";
134
+ import { GetPhotoOutput } from "./output";
135
+
136
+ export const getPhotoHandler = OperationHandlerSetup.configureHandler<
137
+ OpenApiTestAuth,
138
+ GetPhotoInput,
139
+ GetPhotoOutput
140
+ >((handler) =>
141
+ handler.usingHttp((http) =>
142
+ http
143
+ .get("https://jsonplaceholder.typicode.com/photos/:photoId/:commentId")
144
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id).addPathParameter("photoId", input.photoId).addPathParameter("commentId", input.commentId)
145
+ .handleResponse((response) => response.parseWithBodyAsJson())
146
+ )
147
+ );`);
148
+ });
149
+ });
@@ -0,0 +1,19 @@
1
+ type InputMethod = 'path' | 'query' | 'body' | 'header';
2
+ type HandlerHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
3
+ type Input = {
4
+ name: string;
5
+ type: string;
6
+ in: InputMethod;
7
+ };
8
+ export type GenerateHandlerInput = {
9
+ connectorNamePascalCase: string;
10
+ operationNamePascalCase: string;
11
+ operationNameCamelCase: string;
12
+ httpMethod: HandlerHttpMethod;
13
+ baseUrl: string;
14
+ path: string;
15
+ input: Input[];
16
+ };
17
+ export declare const generateHandler: ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }: GenerateHandlerInput) => string;
18
+ export {};
19
+ //# sourceMappingURL=GenerateHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateHandler.d.ts","sourceRoot":"","sources":["../src/GenerateHandler.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAC;AAExD,KAAK,iBAAiB,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;AAErE,KAAK,KAAK,GAAG;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,WAAW,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IAClC,uBAAuB,EAAE,MAAM,CAAC;IAChC,uBAAuB,EAAE,MAAM,CAAC;IAChC,sBAAsB,EAAE,MAAM,CAAC;IAC/B,UAAU,EAAE,iBAAiB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,KAAK,EAAE,CAAC;CACf,CAAC;AAyCF,eAAO,MAAM,eAAe,oHAQzB,oBAAoB,KAAG,MAgBf,CAAC"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateHandler = void 0;
4
+ const handleInput = (inputs) => {
5
+ if (!inputs.length) {
6
+ return 'request';
7
+ }
8
+ const handlerInput = inputs.map((input, index) => {
9
+ const prefix = index === 0 ? 'request.' : '';
10
+ let inputMapping;
11
+ switch (input.in) {
12
+ case 'body':
13
+ inputMapping = `withBodyAsJson(input)`;
14
+ break;
15
+ case 'path':
16
+ inputMapping = `addPathParameter("${input.name}", input.${input.name})`;
17
+ break;
18
+ case 'query':
19
+ inputMapping = `addQueryString("${input.name}", input.${input.name})`;
20
+ break;
21
+ case 'header':
22
+ inputMapping = `addHeader("${input.name}", input.${input.name})`;
23
+ break;
24
+ }
25
+ return prefix + inputMapping;
26
+ });
27
+ return handlerInput.join('.');
28
+ };
29
+ const generatePath = (path, inputs) => inputs.reduce((acc, input) => {
30
+ if (input.in === 'path') {
31
+ return `${acc}/:${input.name}`;
32
+ }
33
+ return acc;
34
+ }, path);
35
+ const generateHandler = ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }) => `import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
36
+ import { ${connectorNamePascalCase} } from "../${connectorNamePascalCase}";
37
+ import { ${operationNamePascalCase}Input } from "./input";
38
+ import { ${operationNamePascalCase}Output } from "./output";
39
+
40
+ export const ${operationNameCamelCase}Handler = OperationHandlerSetup.configureHandler<
41
+ ${connectorNamePascalCase}Auth,
42
+ ${operationNamePascalCase}Input,
43
+ ${operationNamePascalCase}Output
44
+ >((handler) =>
45
+ handler.usingHttp((http) =>
46
+ http
47
+ .${httpMethod}("${baseUrl}${generatePath(path, input)}")
48
+ .handleRequest((ctx, input, request) => ${handleInput(input)}
49
+ .handleResponse((response) => response.parseWithBodyAsJson())
50
+ )
51
+ );`;
52
+ exports.generateHandler = generateHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/tray-openapi",
3
- "version": "1.17.1",
3
+ "version": "2.3.0",
4
4
  "description": "Creating CDK Projects from OpenAPI 3.0 Schemas",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"