@trayio/tray-openapi 1.18.0 → 2.4.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.
@@ -1,13 +1,45 @@
1
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
2
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
+ const E = __importStar(require("fp-ts/Either"));
3
36
  const GenerateHandler_1 = require("./GenerateHandler");
4
37
  const getGenerateHandlerInput = (input) => (Object.assign({ connectorNamePascalCase: 'OpenApiTest', operationNamePascalCase: 'GetPhoto', operationNameCamelCase: 'getPhoto', httpMethod: 'get', baseUrl: 'https://jsonplaceholder.typicode.com/', input: [], path: 'photos' }, input));
5
38
  describe('GenerateHandler', () => {
6
- it('it should generate a handler', () => {
39
+ it('it should generate a handler', () => __awaiter(void 0, void 0, void 0, function* () {
7
40
  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";
41
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
42
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
11
43
  import { OpenApiTest } from "../OpenApiTest";
12
44
  import { GetPhotoInput } from "./input";
13
45
  import { GetPhotoOutput } from "./output";
@@ -20,17 +52,16 @@ describe('GenerateHandler', () => {
20
52
  handler.usingHttp((http) =>
21
53
  http
22
54
  .get("https://jsonplaceholder.typicode.com/photos")
23
- .handleRequest((ctx, input, request) => request
55
+ .handleRequest((ctx, input, request) => request)
24
56
  .handleResponse((response) => response.parseWithBodyAsJson())
25
57
  )
26
- );`);
27
- });
28
- it('it should generate a handler with a path parameter', () => {
58
+ );`)).toEqual(generatedHandler);
59
+ }));
60
+ it('it should generate a handler with a path parameter', () => __awaiter(void 0, void 0, void 0, function* () {
29
61
  const input = [{ type: 'string', name: 'photoId', in: 'path' }];
30
62
  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";
63
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
64
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
34
65
  import { OpenApiTest } from "../OpenApiTest";
35
66
  import { GetPhotoInput } from "./input";
36
67
  import { GetPhotoOutput } from "./output";
@@ -43,17 +74,16 @@ describe('GenerateHandler', () => {
43
74
  handler.usingHttp((http) =>
44
75
  http
45
76
  .get("https://jsonplaceholder.typicode.com/photos/:photoId")
46
- .handleRequest((ctx, input, request) => request.addPathParameter("photoId", input.photoId)
77
+ .handleRequest((ctx, input, request) => request.addPathParameter("photoId", input.photoId))
47
78
  .handleResponse((response) => response.parseWithBodyAsJson())
48
79
  )
49
- );`);
50
- });
51
- it('it should generate a handler with a body input as json', () => {
80
+ );`)).toEqual(generatedHandler);
81
+ }));
82
+ it('it should generate a handler with a body input as json', () => __awaiter(void 0, void 0, void 0, function* () {
52
83
  const input = [{ type: 'object', name: 'payload', in: 'body' }];
53
84
  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";
85
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
86
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
57
87
  import { OpenApiTest } from "../OpenApiTest";
58
88
  import { GetPhotoInput } from "./input";
59
89
  import { GetPhotoOutput } from "./output";
@@ -66,17 +96,16 @@ describe('GenerateHandler', () => {
66
96
  handler.usingHttp((http) =>
67
97
  http
68
98
  .get("https://jsonplaceholder.typicode.com/photos")
69
- .handleRequest((ctx, input, request) => request.withBodyAsJson(input)
99
+ .handleRequest((ctx, input, request) => request.withBodyAsJson(input))
70
100
  .handleResponse((response) => response.parseWithBodyAsJson())
71
101
  )
72
- );`);
73
- });
74
- it('it should generate a handler with a query string input', () => {
102
+ );`)).toEqual(generatedHandler);
103
+ }));
104
+ it('it should generate a handler with a query string input', () => __awaiter(void 0, void 0, void 0, function* () {
75
105
  const input = [{ type: 'string', name: 'id', in: 'query' }];
76
106
  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";
107
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
108
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
80
109
  import { OpenApiTest } from "../OpenApiTest";
81
110
  import { GetPhotoInput } from "./input";
82
111
  import { GetPhotoOutput } from "./output";
@@ -89,19 +118,18 @@ describe('GenerateHandler', () => {
89
118
  handler.usingHttp((http) =>
90
119
  http
91
120
  .get("https://jsonplaceholder.typicode.com/photos")
92
- .handleRequest((ctx, input, request) => request.addQueryString("id", input.id)
121
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id))
93
122
  .handleResponse((response) => response.parseWithBodyAsJson())
94
123
  )
95
- );`);
96
- });
97
- it('it should generate a handler with a header input', () => {
124
+ );`)).toEqual(generatedHandler);
125
+ }));
126
+ it('it should generate a handler with a header input', () => __awaiter(void 0, void 0, void 0, function* () {
98
127
  const input = [
99
128
  { type: 'string', name: 'Content-Type', in: 'header' },
100
129
  ];
101
130
  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";
131
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
132
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
105
133
  import { OpenApiTest } from "../OpenApiTest";
106
134
  import { GetPhotoInput } from "./input";
107
135
  import { GetPhotoOutput } from "./output";
@@ -114,21 +142,20 @@ describe('GenerateHandler', () => {
114
142
  handler.usingHttp((http) =>
115
143
  http
116
144
  .get("https://jsonplaceholder.typicode.com/photos")
117
- .handleRequest((ctx, input, request) => request.addHeader("Content-Type", input.Content-Type)
145
+ .handleRequest((ctx, input, request) => request.addHeader("Content-Type", input.Content-Type))
118
146
  .handleResponse((response) => response.parseWithBodyAsJson())
119
147
  )
120
- );`);
121
- });
122
- it('it should generate a handler with a query string and path param input', () => {
148
+ );`)).toEqual(generatedHandler);
149
+ }));
150
+ it('it should generate a handler with a query string and path param input', () => __awaiter(void 0, void 0, void 0, function* () {
123
151
  const input = [
124
152
  { type: 'string', name: 'id', in: 'query' },
125
153
  { type: 'string', name: 'photoId', in: 'path' },
126
154
  { type: 'string', name: 'commentId', in: 'path' },
127
155
  ];
128
156
  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";
157
+ const generatedHandler = yield (0, GenerateHandler_1.generateHandler)(payload)();
158
+ expect(E.right(`import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
132
159
  import { OpenApiTest } from "../OpenApiTest";
133
160
  import { GetPhotoInput } from "./input";
134
161
  import { GetPhotoOutput } from "./output";
@@ -141,9 +168,9 @@ describe('GenerateHandler', () => {
141
168
  handler.usingHttp((http) =>
142
169
  http
143
170
  .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)
171
+ .handleRequest((ctx, input, request) => request.addQueryString("id", input.id).addPathParameter("photoId", input.photoId).addPathParameter("commentId", input.commentId))
145
172
  .handleResponse((response) => response.parseWithBodyAsJson())
146
173
  )
147
- );`);
148
- });
174
+ );`)).toEqual(generatedHandler);
175
+ }));
149
176
  });
@@ -1,3 +1,4 @@
1
+ import * as TE from 'fp-ts/TaskEither';
1
2
  type InputMethod = 'path' | 'query' | 'body' | 'header';
2
3
  type HandlerHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
3
4
  type Input = {
@@ -14,6 +15,6 @@ export type GenerateHandlerInput = {
14
15
  path: string;
15
16
  input: Input[];
16
17
  };
17
- export declare const generateHandler: ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }: GenerateHandlerInput) => string;
18
+ export declare const generateHandler: ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }: GenerateHandlerInput) => TE.TaskEither<Error, string>;
18
19
  export {};
19
20
  //# sourceMappingURL=GenerateHandler.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"GenerateHandler.d.ts","sourceRoot":"","sources":["../src/GenerateHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEvC,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,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAoBnD,CAAC"}
@@ -1,6 +1,39 @@
1
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
2
34
  Object.defineProperty(exports, "__esModule", { value: true });
3
35
  exports.generateHandler = void 0;
36
+ const TE = __importStar(require("fp-ts/TaskEither"));
4
37
  const handleInput = (inputs) => {
5
38
  if (!inputs.length) {
6
39
  return 'request';
@@ -32,7 +65,8 @@ const generatePath = (path, inputs) => inputs.reduce((acc, input) => {
32
65
  }
33
66
  return acc;
34
67
  }, path);
35
- const generateHandler = ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }) => `import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
68
+ const generateHandler = ({ connectorNamePascalCase, operationNamePascalCase, operationNameCamelCase, httpMethod, baseUrl, path, input, }) => TE.tryCatch(() => __awaiter(void 0, void 0, void 0, function* () {
69
+ return `import { OperationHandlerSetup } from "@trayio/cdk-dsl/connector/operation/OperationHandlerSetup";
36
70
  import { ${connectorNamePascalCase} } from "../${connectorNamePascalCase}";
37
71
  import { ${operationNamePascalCase}Input } from "./input";
38
72
  import { ${operationNamePascalCase}Output } from "./output";
@@ -45,8 +79,9 @@ const generateHandler = ({ connectorNamePascalCase, operationNamePascalCase, ope
45
79
  handler.usingHttp((http) =>
46
80
  http
47
81
  .${httpMethod}("${baseUrl}${generatePath(path, input)}")
48
- .handleRequest((ctx, input, request) => ${handleInput(input)}
82
+ .handleRequest((ctx, input, request) => ${handleInput(input)})
49
83
  .handleResponse((response) => response.parseWithBodyAsJson())
50
84
  )
51
85
  );`;
86
+ }), (error) => new Error(`Failed to generate handler: ${error}`));
52
87
  exports.generateHandler = generateHandler;
@@ -0,0 +1,3 @@
1
+ import { OpenAPISchema } from './OpenApiTypeDescriptors';
2
+ export declare const openApiSpecCodec: import("commons/dist/codec/Codec").Codec<OpenAPISchema>;
3
+ //# sourceMappingURL=OpenApiCodecs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenApiCodecs.d.ts","sourceRoot":"","sources":["../src/OpenApiCodecs.ts"],"names":[],"mappings":"AACA,OAAO,EACN,aAAa,EAEb,MAAM,0BAA0B,CAAC;AAElC,eAAO,MAAM,gBAAgB,yDAE5B,CAAC"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openApiSpecCodec = void 0;
4
+ const TypeCodec_1 = require("@trayio/commons/codec/TypeCodec");
5
+ const OpenApiTypeDescriptors_1 = require("./OpenApiTypeDescriptors");
6
+ exports.openApiSpecCodec = TypeCodec_1.TypeCodec.fromDescriptor(OpenApiTypeDescriptors_1.openApiSpecTypeDescriptor);
@@ -0,0 +1,8 @@
1
+ import * as TE from 'fp-ts/TaskEither';
2
+ import { OpenAPISchema } from './OpenApiTypeDescriptors';
3
+ export declare class OpenApiSchemaImporter {
4
+ buildConnector(openApiSpecPath: string): TE.TaskEither<Error, undefined>;
5
+ getOpenApiSpec(openApiSpecPath: string): TE.TaskEither<Error, OpenAPISchema>;
6
+ generateHandlerFile(openApiSpec: OpenAPISchema): TE.TaskEither<Error, undefined>;
7
+ }
8
+ //# sourceMappingURL=OpenApiSchemaImporter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenApiSchemaImporter.d.ts","sourceRoot":"","sources":["../src/OpenApiSchemaImporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAIvC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAIzD,qBAAa,qBAAqB;IACjC,cAAc,CAAC,eAAe,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAOxE,cAAc,CAAC,eAAe,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,aAAa,CAAC;IAwB5E,mBAAmB,CAClB,WAAW,EAAE,aAAa,GACxB,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;CA2BlC"}
@@ -0,0 +1,61 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.OpenApiSchemaImporter = void 0;
36
+ const TE = __importStar(require("fp-ts/TaskEither"));
37
+ const function_1 = require("fp-ts/function");
38
+ const fse = __importStar(require("fs-extra"));
39
+ const Task_1 = require("@trayio/commons/task/Task");
40
+ const OpenApiCodecs_1 = require("./OpenApiCodecs");
41
+ const GenerateHandler_1 = require("./GenerateHandler");
42
+ class OpenApiSchemaImporter {
43
+ buildConnector(openApiSpecPath) {
44
+ return (0, function_1.pipe)(this.getOpenApiSpec(openApiSpecPath), TE.chain((openApiSpec) => this.generateHandlerFile(openApiSpec)));
45
+ }
46
+ getOpenApiSpec(openApiSpecPath) {
47
+ return (0, function_1.pipe)(TE.tryCatch(() => __awaiter(this, void 0, void 0, function* () { return fse.readFileSync(openApiSpecPath, 'utf-8'); }), (error) => new Error(`Failed to read OpenAPI spec from path ${openApiSpecPath}: ${error}`)), TE.chain((openApiSpec) => TE.tryCatch(() => __awaiter(this, void 0, void 0, function* () { return JSON.parse(openApiSpec); }), (error) => new Error(`Failed to parse OpenAPI spec as JSON from path ${openApiSpecPath}: ${error}`))), TE.chain((openApiSpec) => (0, Task_1.createTaskEitherFromEither)(OpenApiCodecs_1.openApiSpecCodec.decode(openApiSpec))));
48
+ }
49
+ generateHandlerFile(openApiSpec) {
50
+ return (0, function_1.pipe)(TE.right({
51
+ connectorNamePascalCase: 'test',
52
+ operationNameCamelCase: 'test',
53
+ operationNamePascalCase: 'test',
54
+ httpMethod: 'get',
55
+ baseUrl: 'https://jsonplaceholder.typicode.com/',
56
+ path: 'test',
57
+ input: [],
58
+ }), TE.chain((parameters) => (0, GenerateHandler_1.generateHandler)(parameters)), TE.chain((handler) => TE.tryCatch(() => __awaiter(this, void 0, void 0, function* () { return fse.writeFileSync(`${__dirname}/handler.ts`, handler, 'utf-8'); }), (error) => new Error(`Failed to write handler to file system: ${error}`))), TE.map(() => undefined), TE.mapLeft((error) => new Error(`Failed to generate handler for operation: ${error}`)));
59
+ }
60
+ }
61
+ exports.OpenApiSchemaImporter = OpenApiSchemaImporter;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=OpenApiSchemaImporter.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenApiSchemaImporter.test.d.ts","sourceRoot":"","sources":["../src/OpenApiSchemaImporter.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const OpenApiSchemaImporter_1 = require("./OpenApiSchemaImporter");
13
+ describe('openApiSchemaImporter', () => {
14
+ const openApiSchemaImporter = new OpenApiSchemaImporter_1.OpenApiSchemaImporter();
15
+ it('should decode the openApiSpec', () => __awaiter(void 0, void 0, void 0, function* () {
16
+ const schema = yield openApiSchemaImporter.buildConnector(`${__dirname}/single-get-openapi.json`)();
17
+ expect(schema._tag).toEqual('Right');
18
+ }));
19
+ });
@@ -0,0 +1,50 @@
1
+ import * as t from 'io-ts';
2
+ type Path = {
3
+ description: string;
4
+ tags: string[];
5
+ operationId: string;
6
+ parameters: {
7
+ name: string;
8
+ in: string;
9
+ description: string;
10
+ required: boolean;
11
+ schema: {
12
+ type: string;
13
+ format?: string;
14
+ };
15
+ additionalProperties?: boolean;
16
+ }[];
17
+ };
18
+ type ArraySchema = {
19
+ type: 'array';
20
+ items: {
21
+ $ref: string;
22
+ };
23
+ };
24
+ type ObjectSchema = {
25
+ type: 'object';
26
+ required?: string[];
27
+ properties: Record<string, PropertySchema>;
28
+ };
29
+ type PropertySchema = {
30
+ type: string;
31
+ } | ArraySchema | ObjectSchema;
32
+ type ComponentSchema = {
33
+ schemas: Record<string, ArraySchema | ObjectSchema>;
34
+ };
35
+ export type OpenAPISchema = {
36
+ openapi: string;
37
+ info: {
38
+ title: string;
39
+ description: string;
40
+ version: string;
41
+ };
42
+ servers: {
43
+ url: string;
44
+ }[];
45
+ paths: Record<string, Record<string, Path>>;
46
+ components: ComponentSchema;
47
+ };
48
+ export declare const openApiSpecTypeDescriptor: t.Type<OpenAPISchema, unknown>;
49
+ export {};
50
+ //# sourceMappingURL=OpenApiTypeDescriptors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenApiTypeDescriptors.d.ts","sourceRoot":"","sources":["../src/OpenApiTypeDescriptors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,KAAK,IAAI,GAAG;IACX,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,EAAE,EAAE,MAAM,CAAC;QACX,WAAW,EAAE,MAAM,CAAC;QACpB,QAAQ,EAAE,OAAO,CAAC;QAClB,MAAM,EAAE;YACP,IAAI,EAAE,MAAM,CAAC;YACb,MAAM,CAAC,EAAE,MAAM,CAAC;SAChB,CAAC;QACF,oBAAoB,CAAC,EAAE,OAAO,CAAC;KAC/B,EAAE,CAAC;CACJ,CAAC;AAEF,KAAK,WAAW,GAAG;IAClB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;KACb,CAAC;CACF,CAAC;AAEF,KAAK,YAAY,GAAG;IACnB,IAAI,EAAE,QAAQ,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CAC3C,CAAC;AAEF,KAAK,cAAc,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,WAAW,GAAG,YAAY,CAAC;AAEpE,KAAK,eAAe,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CAAC,CAAA;CAAE,CAAC;AAE/E,MAAM,MAAM,aAAa,GAAG;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,OAAO,EAAE;QACR,GAAG,EAAE,MAAM,CAAC;KACZ,EAAE,CAAC;IACJ,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAC5C,UAAU,EAAE,eAAe,CAAC;CAC5B,CAAC;AA0CF,eAAO,MAAM,yBAAyB,EAAE,CAAC,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,CAWpE,CAAC"}
@@ -0,0 +1,58 @@
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.openApiSpecTypeDescriptor = void 0;
27
+ const t = __importStar(require("io-ts"));
28
+ const openApiSpecPropertySchemaTypeDescriptor = t.type({ type: t.string });
29
+ const openApiSpecObjectSchemaTypeDescriptor = t.type({
30
+ type: t.literal('object'),
31
+ properties: t.record(t.string, openApiSpecPropertySchemaTypeDescriptor),
32
+ });
33
+ const openApiSpecArraySchemaTypeDescriptor = t.type({ type: t.literal('array'), items: t.any });
34
+ const openApiSpecComponentSchemaTypeDescriptor = t.type({
35
+ schemas: t.record(t.string, t.union([
36
+ openApiSpecObjectSchemaTypeDescriptor,
37
+ openApiSpecArraySchemaTypeDescriptor,
38
+ ])),
39
+ });
40
+ const openApiSpecPathTypeDescriptor = t.type({
41
+ description: t.string,
42
+ tags: t.array(t.string),
43
+ operationId: t.string,
44
+ parameters: t.array(t.type({
45
+ name: t.string,
46
+ in: t.string,
47
+ description: t.string,
48
+ required: t.boolean,
49
+ schema: t.type({ type: t.string }),
50
+ })),
51
+ });
52
+ exports.openApiSpecTypeDescriptor = t.type({
53
+ openapi: t.string,
54
+ info: t.type({ title: t.string, description: t.string, version: t.string }),
55
+ servers: t.array(t.type({ url: t.string })),
56
+ paths: t.record(t.string, t.record(t.string, openApiSpecPathTypeDescriptor)),
57
+ components: openApiSpecComponentSchemaTypeDescriptor,
58
+ });
@@ -0,0 +1,78 @@
1
+ {
2
+ "openapi": "3.0.0",
3
+ "info": {
4
+ "version": "1.0.0",
5
+ "title": "JSON Placeholder API",
6
+ "description": "See https://jsonplaceholder.typicode.com/"
7
+ },
8
+ "servers": [
9
+ {
10
+ "url": "https://jsonplaceholder.typicode.com"
11
+ }
12
+ ],
13
+ "paths": {
14
+ "/posts/{id}": {
15
+ "get": {
16
+ "description": "Returns a post by id",
17
+ "tags": ["Posts"],
18
+ "operationId": "getPost",
19
+ "parameters": [
20
+ {
21
+ "name": "id",
22
+ "in": "path",
23
+ "required": true,
24
+ "description": "The user id.",
25
+ "schema": {
26
+ "type": "integer",
27
+ "format": "int64"
28
+ },
29
+ "additionalProperties": false
30
+ }
31
+ ],
32
+ "responses": {
33
+ "200": {
34
+ "description": "Successful response",
35
+ "content": {
36
+ "application/json": {
37
+ "schema": {
38
+ "$ref": "#/components/schemas/PostsList"
39
+ }
40
+ }
41
+ }
42
+ },
43
+ "404": {
44
+ "description": "Post not found"
45
+ }
46
+ }
47
+ }
48
+ }
49
+ },
50
+ "components": {
51
+ "schemas": {
52
+ "PostsList": {
53
+ "type": "array",
54
+ "items": {
55
+ "$ref": "#/components/schemas/Post"
56
+ }
57
+ },
58
+ "Post": {
59
+ "type": "object",
60
+ "required": ["id", "userId", "title", "body"],
61
+ "properties": {
62
+ "id": {
63
+ "type": "integer"
64
+ },
65
+ "userId": {
66
+ "type": "integer"
67
+ },
68
+ "title": {
69
+ "type": "string"
70
+ },
71
+ "body": {
72
+ "type": "string"
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/tray-openapi",
3
- "version": "1.18.0",
3
+ "version": "2.4.0",
4
4
  "description": "Creating CDK Projects from OpenAPI 3.0 Schemas",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"