@trayio/tray-openapi 2.7.0 → 2.8.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.
Files changed (71) hide show
  1. package/dist/OpenApiSchemaImporter.d.ts +1 -1
  2. package/dist/OpenApiSchemaImporter.d.ts.map +1 -1
  3. package/dist/OpenApiSchemaImporter.js +43 -28
  4. package/dist/OpenApiSchemaImporter.test.js +12 -3
  5. package/dist/OpenApiTypeDescriptors.d.ts +10 -5
  6. package/dist/OpenApiTypeDescriptors.d.ts.map +1 -1
  7. package/dist/OpenApiTypeDescriptors.js +10 -1
  8. package/dist/ResolveOptionValues.d.ts +3 -0
  9. package/dist/ResolveOptionValues.d.ts.map +1 -0
  10. package/dist/{connector/input/TransformInput.js → ResolveOptionValues.js} +16 -27
  11. package/dist/ResolveOptionValues.test.d.ts +2 -0
  12. package/dist/ResolveOptionValues.test.d.ts.map +1 -0
  13. package/dist/ResolveOptionValues.test.js +91 -0
  14. package/dist/{GenerateHandler.d.ts → file-generators/GenerateHandler.d.ts} +2 -7
  15. package/dist/file-generators/GenerateHandler.d.ts.map +1 -0
  16. package/dist/{GenerateHandler.js → file-generators/GenerateHandler.js} +53 -16
  17. package/dist/file-generators/GenerateHandler.test.d.ts +2 -0
  18. package/dist/file-generators/GenerateHandler.test.d.ts.map +1 -0
  19. package/dist/{GenerateHander.test.js → file-generators/GenerateHandler.test.js} +119 -33
  20. package/dist/{GenerateHandlerTest.d.ts → file-generators/GenerateHandlerTest.d.ts} +2 -4
  21. package/dist/file-generators/GenerateHandlerTest.d.ts.map +1 -0
  22. package/dist/{GenerateHandlerTest.js → file-generators/GenerateHandlerTest.js} +5 -16
  23. package/dist/file-generators/GenerateHandlerTest.test.d.ts.map +1 -0
  24. package/dist/{GenerateHandlerTest.test.js → file-generators/GenerateHandlerTest.test.js} +47 -34
  25. package/dist/file-generators/GenerateOperationJson.d.ts +4 -0
  26. package/dist/file-generators/GenerateOperationJson.d.ts.map +1 -0
  27. package/dist/file-generators/GenerateOperationJson.js +43 -0
  28. package/dist/file-generators/GenerateOperationJson.test.d.ts +2 -0
  29. package/dist/file-generators/GenerateOperationJson.test.d.ts.map +1 -0
  30. package/dist/file-generators/GenerateOperationJson.test.js +77 -0
  31. package/dist/file-generators/types/GenerateInputSchema.d.ts +4 -0
  32. package/dist/file-generators/types/GenerateInputSchema.d.ts.map +1 -0
  33. package/dist/{connector/input/ProcessInput.js → file-generators/types/GenerateInputSchema.js} +31 -14
  34. package/dist/file-generators/types/GenerateInputSchema.test.d.ts +2 -0
  35. package/dist/file-generators/types/GenerateInputSchema.test.d.ts.map +1 -0
  36. package/dist/{connector/input/ProcessInput.test.js → file-generators/types/GenerateInputSchema.test.js} +25 -14
  37. package/dist/file-generators/types/GenerateInputTypes.d.ts +4 -0
  38. package/dist/file-generators/types/GenerateInputTypes.d.ts.map +1 -0
  39. package/dist/file-generators/types/GenerateInputTypes.js +39 -0
  40. package/dist/file-generators/types/GenerateOutput.d.ts +9 -0
  41. package/dist/file-generators/types/GenerateOutput.d.ts.map +1 -0
  42. package/dist/file-generators/types/GenerateOutput.js +54 -0
  43. package/dist/file-generators/types/GenerateOutput.test.d.ts +2 -0
  44. package/dist/file-generators/types/GenerateOutput.test.d.ts.map +1 -0
  45. package/dist/file-generators/types/GenerateOutput.test.js +197 -0
  46. package/dist/file-generators/types/JsonSchemaToTypescriptOptions.d.ts.map +1 -0
  47. package/package.json +4 -2
  48. package/dist/GenerateHander.test.d.ts +0 -2
  49. package/dist/GenerateHander.test.d.ts.map +0 -1
  50. package/dist/GenerateHandler.d.ts.map +0 -1
  51. package/dist/GenerateHandlerTest.d.ts.map +0 -1
  52. package/dist/GenerateHandlerTest.test.d.ts.map +0 -1
  53. package/dist/connector/JsonSchemaToTypescriptOptions.d.ts.map +0 -1
  54. package/dist/connector/input/GenerateInput.d.ts +0 -8
  55. package/dist/connector/input/GenerateInput.d.ts.map +0 -1
  56. package/dist/connector/input/GenerateInput.js +0 -8
  57. package/dist/connector/input/GenerateInput.test.d.ts +0 -2
  58. package/dist/connector/input/GenerateInput.test.d.ts.map +0 -1
  59. package/dist/connector/input/GenerateInput.test.js +0 -133
  60. package/dist/connector/input/ProcessInput.d.ts +0 -4
  61. package/dist/connector/input/ProcessInput.d.ts.map +0 -1
  62. package/dist/connector/input/ProcessInput.test.d.ts +0 -2
  63. package/dist/connector/input/ProcessInput.test.d.ts.map +0 -1
  64. package/dist/connector/input/TransformInput.d.ts +0 -4
  65. package/dist/connector/input/TransformInput.d.ts.map +0 -1
  66. package/dist/connector/input/TransformInput.test.d.ts +0 -2
  67. package/dist/connector/input/TransformInput.test.d.ts.map +0 -1
  68. package/dist/connector/input/TransformInput.test.js +0 -75
  69. /package/dist/{GenerateHandlerTest.test.d.ts → file-generators/GenerateHandlerTest.test.d.ts} +0 -0
  70. /package/dist/{connector → file-generators/types}/JsonSchemaToTypescriptOptions.d.ts +0 -0
  71. /package/dist/{connector → file-generators/types}/JsonSchemaToTypescriptOptions.js +0 -0
@@ -23,11 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.processInput = void 0;
26
+ exports.generateInputSchema = void 0;
27
27
  const lodash_1 = require("lodash");
28
28
  const O = __importStar(require("fp-ts/Option"));
29
- const GenerateInput_1 = require("./GenerateInput");
30
- const TransformInput_1 = require("./TransformInput");
29
+ const openapi_schema_to_json_schema_1 = require("@openapi-contrib/openapi-schema-to-json-schema");
31
30
  const mergeRequired = (params, body) => {
32
31
  if ((0, lodash_1.isArray)(body.required) && (0, lodash_1.isArray)(params.required)) {
33
32
  return [...body.required, ...params.required];
@@ -40,6 +39,31 @@ const mergeParamsAndBody = (params, body) => ({
40
39
  required: mergeRequired(params, body),
41
40
  additionalProperties: false,
42
41
  });
42
+ const transformInput = (parameters) => {
43
+ const transformedParameters = {};
44
+ const requiredParameters = [];
45
+ parameters.forEach((parameter) => {
46
+ const parameterWithJsdocAnnotation = {
47
+ name: parameter.name,
48
+ description: `@description ${parameter.description}`,
49
+ required: parameter.required,
50
+ schema: {
51
+ type: parameter.schema.type,
52
+ format: O.toNullable(parameter.schema.format) || undefined,
53
+ },
54
+ };
55
+ transformedParameters[parameter.name] = Object.assign(Object.assign({}, (0, openapi_schema_to_json_schema_1.fromParameter)(parameterWithJsdocAnnotation)), { in: parameter.in });
56
+ if (parameter.required === true) {
57
+ requiredParameters.push(parameter.name);
58
+ }
59
+ });
60
+ return {
61
+ type: 'object',
62
+ properties: Object.assign({}, transformedParameters),
63
+ required: [...requiredParameters],
64
+ additionalProperties: false,
65
+ };
66
+ };
43
67
  const getApplicationJsonMediaType = (content) => {
44
68
  // TODO: we should support any value here, but there may also be more than 1 media type so we need to iterate through
45
69
  // and combine all the schemas within into a single set of inputs
@@ -50,17 +74,17 @@ const getApplicationJsonMediaType = (content) => {
50
74
  const mediaTypes = Object.keys(content);
51
75
  throw new Error(`Unsupported media type: ${mediaTypes}`);
52
76
  };
53
- const extractInput = (endpoint) => {
77
+ const generateInputSchema = (endpoint) => {
54
78
  const parameters = O.toNullable(endpoint.parameters);
55
79
  const requestBody = O.toNullable(endpoint.requestBody);
56
80
  if (parameters && !requestBody) {
57
- return (0, TransformInput_1.transformInput)(parameters);
81
+ return transformInput(parameters);
58
82
  }
59
83
  if (requestBody && !parameters) {
60
84
  return getApplicationJsonMediaType(requestBody.content).schema;
61
85
  }
62
86
  if (requestBody && parameters) {
63
- const params = (0, TransformInput_1.transformInput)(parameters);
87
+ const params = transformInput(parameters);
64
88
  const body = getApplicationJsonMediaType(requestBody.content).schema;
65
89
  return mergeParamsAndBody(params, body);
66
90
  }
@@ -71,11 +95,4 @@ const extractInput = (endpoint) => {
71
95
  additionalProperties: false,
72
96
  };
73
97
  };
74
- const processInput = (endpoint) => {
75
- const input = extractInput(endpoint);
76
- return (0, GenerateInput_1.generateInput)({
77
- operationNameCamelCase: endpoint.operationId,
78
- parameters: input,
79
- });
80
- };
81
- exports.processInput = processInput;
98
+ exports.generateInputSchema = generateInputSchema;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=GenerateInputSchema.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateInputSchema.test.d.ts","sourceRoot":"","sources":["../../../src/file-generators/types/GenerateInputSchema.test.ts"],"names":[],"mappings":""}
@@ -34,8 +34,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  const TE = __importStar(require("fp-ts/TaskEither"));
36
36
  const O = __importStar(require("fp-ts/Option"));
37
- const ProcessInput_1 = require("./ProcessInput");
38
- describe('Input: ProcessInput', () => {
37
+ const GenerateInputSchema_1 = require("./GenerateInputSchema");
38
+ const GenerateInputTypes_1 = require("./GenerateInputTypes");
39
+ describe('GenerateInputSchema', () => {
39
40
  it('it should process an endpoint with path and query string parameters and generate input.ts file contents', () => __awaiter(void 0, void 0, void 0, function* () {
40
41
  const endpoint = {
41
42
  operationId: 'getPost',
@@ -51,14 +52,17 @@ describe('Input: ProcessInput', () => {
51
52
  type: 'integer',
52
53
  format: O.some('int64'),
53
54
  },
55
+ additionalProperties: O.none,
54
56
  },
55
57
  ]),
56
58
  requestBody: O.none,
59
+ responses: O.none,
57
60
  };
58
- const processedInput = (0, ProcessInput_1.processInput)(endpoint);
61
+ const inputSchema = (0, GenerateInputSchema_1.generateInputSchema)(endpoint);
62
+ const inputTypes = (0, GenerateInputTypes_1.generateInputTypes)(endpoint.operationId, inputSchema);
59
63
  const generatedInputFile = yield TE.getOrElse((error) => {
60
64
  throw new Error('Should have been right');
61
- })(processedInput)();
65
+ })(inputTypes)();
62
66
  const expectedInputFile = `export interface GetPostInput {
63
67
  /**
64
68
  * @description The user id.
@@ -94,17 +98,19 @@ describe('Input: ProcessInput', () => {
94
98
  type: 'string',
95
99
  },
96
100
  },
97
- additionalProperties: false,
101
+ additionalProperties: O.some(false),
98
102
  },
99
103
  },
100
104
  },
101
105
  required: true,
102
106
  }),
107
+ responses: O.none,
103
108
  };
104
- const processedInput = (0, ProcessInput_1.processInput)(endpoint);
109
+ const inputSchema = (0, GenerateInputSchema_1.generateInputSchema)(endpoint);
110
+ const inputTypes = (0, GenerateInputTypes_1.generateInputTypes)(endpoint.operationId, inputSchema);
105
111
  const generatedInputFile = yield TE.getOrElse((error) => {
106
112
  throw new Error('Should have been right');
107
- })(processedInput)();
113
+ })(inputTypes)();
108
114
  const expectedInputFile = `export interface CreatePostInput {
109
115
  id: number;
110
116
  userId: number;
@@ -148,17 +154,19 @@ describe('Input: ProcessInput', () => {
148
154
  type: 'string',
149
155
  },
150
156
  },
151
- additionalProperties: false,
157
+ additionalProperties: O.some(false),
152
158
  },
153
159
  },
154
160
  },
155
161
  required: true,
156
162
  }),
163
+ responses: O.none,
157
164
  };
158
- const processedInput = (0, ProcessInput_1.processInput)(endpoint);
165
+ const inputSchema = (0, GenerateInputSchema_1.generateInputSchema)(endpoint);
166
+ const inputTypes = (0, GenerateInputTypes_1.generateInputTypes)(endpoint.operationId, inputSchema);
159
167
  const generatedInputFile = yield TE.getOrElse((error) => {
160
168
  throw new Error('Should have been right');
161
- })(processedInput)();
169
+ })(inputTypes)();
162
170
  const expectedInputFile = `export interface GetPostInput {
163
171
  userId: number;
164
172
  title: string;
@@ -178,11 +186,13 @@ describe('Input: ProcessInput', () => {
178
186
  tags: [],
179
187
  parameters: O.none,
180
188
  requestBody: O.none,
189
+ responses: O.none,
181
190
  };
182
- const processedInput = (0, ProcessInput_1.processInput)(endpoint);
191
+ const inputSchema = (0, GenerateInputSchema_1.generateInputSchema)(endpoint);
192
+ const inputTypes = (0, GenerateInputTypes_1.generateInputTypes)(endpoint.operationId, inputSchema);
183
193
  const generatedInputFile = yield TE.getOrElse((error) => {
184
194
  throw new Error('Should have been right');
185
- })(processedInput)();
195
+ })(inputTypes)();
186
196
  const expectedInputFile = `export interface GetPostsInput {}
187
197
  `;
188
198
  expect(generatedInputFile).toEqual(expectedInputFile);
@@ -213,15 +223,16 @@ describe('Input: ProcessInput', () => {
213
223
  type: 'string',
214
224
  },
215
225
  },
216
- additionalProperties: false,
226
+ additionalProperties: O.some(false),
217
227
  },
218
228
  },
219
229
  },
220
230
  required: true,
221
231
  }),
232
+ responses: O.none,
222
233
  };
223
234
  yield expect(() => __awaiter(void 0, void 0, void 0, function* () {
224
- (0, ProcessInput_1.processInput)(endpoint);
235
+ (0, GenerateInputSchema_1.generateInputSchema)(endpoint);
225
236
  })).rejects.toThrow('Unsupported media type: unsupported-media-type');
226
237
  }));
227
238
  });
@@ -0,0 +1,4 @@
1
+ import { JSONSchema4 } from 'json-schema';
2
+ import * as TE from 'fp-ts/TaskEither';
3
+ export declare const generateInputTypes: (operationNameCamelCase: string, parameters: JSONSchema4) => TE.TaskEither<Error, string>;
4
+ //# sourceMappingURL=GenerateInputTypes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateInputTypes.d.ts","sourceRoot":"","sources":["../../../src/file-generators/types/GenerateInputTypes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAKvC,eAAO,MAAM,kBAAkB,2BACN,MAAM,cAClB,WAAW,KACrB,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAiB7B,CAAC"}
@@ -0,0 +1,39 @@
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.generateInputTypes = void 0;
27
+ const json_schema_to_typescript_1 = require("json-schema-to-typescript");
28
+ const TaskEither_1 = require("fp-ts/TaskEither");
29
+ const E = __importStar(require("fp-ts/Either"));
30
+ const JsonSchemaToTypescriptOptions_1 = require("./JsonSchemaToTypescriptOptions");
31
+ const ResolveOptionValues_1 = require("../../ResolveOptionValues");
32
+ const generateInputTypes = (operationNameCamelCase, parameters) => {
33
+ const resolver = (0, ResolveOptionValues_1.resolveOptionValues)(parameters);
34
+ const resolvedParameters = E.getOrElse((error) => {
35
+ throw new Error(`${error}`);
36
+ })(resolver);
37
+ return (0, TaskEither_1.tryCatch)(() => (0, json_schema_to_typescript_1.compile)(resolvedParameters, `${operationNameCamelCase}Input`, JsonSchemaToTypescriptOptions_1.JsonSchemaToTypescriptOptions), (reason) => new Error(`${reason}`));
38
+ };
39
+ exports.generateInputTypes = generateInputTypes;
@@ -0,0 +1,9 @@
1
+ import { JSONSchema4 } from 'json-schema';
2
+ import * as TE from 'fp-ts/TaskEither';
3
+ import * as E from 'fp-ts/Either';
4
+ import * as O from 'fp-ts/Option';
5
+ import { Responses, Response, MediaType } from '../../OpenApiTypeDescriptors';
6
+ export declare const getApplicationJsonMediaType: (successResponse: Response) => E.Either<Error, MediaType>;
7
+ export declare const generateOutputSchema: (responses: O.Option<Responses>) => TE.TaskEither<Error, JSONSchema4>;
8
+ export declare const generateOutputTypes: (operationNameCamelCase: string, outputSchema: JSONSchema4) => TE.TaskEither<Error, string>;
9
+ //# sourceMappingURL=GenerateOutput.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateOutput.d.ts","sourceRoot":"","sources":["../../../src/file-generators/types/GenerateOutput.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAGlC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAsB9E,eAAO,MAAM,2BAA2B,oBACtB,QAAQ,KACvB,EAAE,MAAM,CAAC,KAAK,EAAE,SAAS,CAgB1B,CAAC;AAEH,eAAO,MAAM,oBAAoB,cACrB,EAAE,MAAM,CAAC,SAAS,CAAC,KAC5B,GAAG,UAAU,CAAC,KAAK,EAAE,WAAW,CAcjC,CAAC;AAEH,eAAO,MAAM,mBAAmB,2BACP,MAAM,gBAChB,WAAW,KACvB,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAS5B,CAAC"}
@@ -0,0 +1,54 @@
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.generateOutputTypes = exports.generateOutputSchema = exports.getApplicationJsonMediaType = void 0;
27
+ const json_schema_to_typescript_1 = require("json-schema-to-typescript");
28
+ const TE = __importStar(require("fp-ts/TaskEither"));
29
+ const E = __importStar(require("fp-ts/Either"));
30
+ const O = __importStar(require("fp-ts/Option"));
31
+ const function_1 = require("fp-ts/function");
32
+ const JsonSchemaToTypescriptOptions_1 = require("./JsonSchemaToTypescriptOptions");
33
+ const ResolveOptionValues_1 = require("../../ResolveOptionValues");
34
+ const get2XXResponse = (responses) => O.fold(() => TE.left(new Error('No responses found')), (someResponses) => {
35
+ const statusCode2XX = Object.keys(someResponses).find((statusCode) => /^2\d{2}$/.test(statusCode));
36
+ if (statusCode2XX) {
37
+ return TE.right(someResponses[statusCode2XX]);
38
+ }
39
+ const responseCodes = Object.keys(someResponses);
40
+ return TE.left(new Error(`Unsupported success response code: ${responseCodes}`));
41
+ })(responses);
42
+ const getApplicationJsonMediaType = (successResponse) => (0, function_1.pipe)(O.fold(() => E.left(new Error('No success response content found')), (content) => E.right(content))(successResponse.content), E.chain((content) => {
43
+ const mediaTypeApplicationJson = Object.keys(content).find((mediaType) => /^application\/.*json.*$/.test(mediaType));
44
+ if (mediaTypeApplicationJson) {
45
+ return E.right(content[mediaTypeApplicationJson]);
46
+ }
47
+ const mediaTypes = Object.keys(content);
48
+ return E.left(new Error(`Unsupported media type: ${mediaTypes}`));
49
+ }));
50
+ exports.getApplicationJsonMediaType = getApplicationJsonMediaType;
51
+ const generateOutputSchema = (responses) => (0, function_1.pipe)(get2XXResponse(responses), TE.chain((successResponse) => TE.fromEither((0, exports.getApplicationJsonMediaType)(successResponse))), TE.chain((applicationJson) => TE.fromEither((0, ResolveOptionValues_1.resolveOptionValues)(applicationJson.schema))));
52
+ exports.generateOutputSchema = generateOutputSchema;
53
+ const generateOutputTypes = (operationNameCamelCase, outputSchema) => TE.tryCatch(() => (0, json_schema_to_typescript_1.compile)(outputSchema, `${operationNameCamelCase}Output`, JsonSchemaToTypescriptOptions_1.JsonSchemaToTypescriptOptions), (error) => new Error(`Failed to generate output types: ${error}`));
54
+ exports.generateOutputTypes = generateOutputTypes;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=GenerateOutput.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenerateOutput.test.d.ts","sourceRoot":"","sources":["../../../src/file-generators/types/GenerateOutput.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,197 @@
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
+ const TE = __importStar(require("fp-ts/TaskEither"));
36
+ const E = __importStar(require("fp-ts/Either"));
37
+ const O = __importStar(require("fp-ts/Option"));
38
+ const GenerateOutput_1 = require("./GenerateOutput");
39
+ const generateOutputFileContents = (responses) => __awaiter(void 0, void 0, void 0, function* () {
40
+ const operationNameCamelCase = 'CreatePost';
41
+ const generatedOutputSchema = yield (0, GenerateOutput_1.generateOutputSchema)(responses)();
42
+ if (E.isLeft(generatedOutputSchema)) {
43
+ throw new Error('Should have been right');
44
+ }
45
+ const generatedOutputTypes = (0, GenerateOutput_1.generateOutputTypes)(operationNameCamelCase, generatedOutputSchema.right);
46
+ return TE.getOrElse((error) => {
47
+ throw new Error('Should have been right');
48
+ })(generatedOutputTypes)();
49
+ });
50
+ describe('Generate output: ', () => {
51
+ it('it should generate output.ts file contents for a response code 200 content json', () => __awaiter(void 0, void 0, void 0, function* () {
52
+ const responses = O.some({
53
+ '200': {
54
+ description: 'A post was created',
55
+ content: O.some({
56
+ 'application/json': {
57
+ schema: {
58
+ type: 'object',
59
+ required: ['id', 'userId', 'title', 'body'],
60
+ properties: {
61
+ id: {
62
+ type: 'integer',
63
+ },
64
+ userId: {
65
+ type: 'integer',
66
+ },
67
+ title: {
68
+ type: 'string',
69
+ },
70
+ body: {
71
+ type: 'string',
72
+ },
73
+ },
74
+ additionalProperties: O.some(false),
75
+ },
76
+ },
77
+ }),
78
+ },
79
+ });
80
+ const generatedOutputFileContents = yield generateOutputFileContents(responses);
81
+ const expectedOutputFileContents = `export interface CreatePostOutput {
82
+ id: number;
83
+ userId: number;
84
+ title: string;
85
+ body: string;
86
+ }
87
+ `;
88
+ expect(generatedOutputFileContents).toEqual(expectedOutputFileContents);
89
+ }));
90
+ it('it should generate output.ts file contents for a response code 201 content json', () => __awaiter(void 0, void 0, void 0, function* () {
91
+ const responses = O.some({
92
+ '201': {
93
+ description: 'A post was created',
94
+ content: O.some({
95
+ 'application/json': {
96
+ schema: {
97
+ type: 'object',
98
+ required: ['id', 'userId', 'title', 'body'],
99
+ properties: {
100
+ id: {
101
+ type: 'integer',
102
+ },
103
+ userId: {
104
+ type: 'integer',
105
+ },
106
+ title: {
107
+ type: 'string',
108
+ },
109
+ body: {
110
+ type: 'string',
111
+ },
112
+ },
113
+ additionalProperties: O.some(false),
114
+ },
115
+ },
116
+ }),
117
+ },
118
+ });
119
+ const generatedOutputFileContents = yield generateOutputFileContents(responses);
120
+ const expectedOutputFileContents = `export interface CreatePostOutput {
121
+ id: number;
122
+ userId: number;
123
+ title: string;
124
+ body: string;
125
+ }
126
+ `;
127
+ expect(generatedOutputFileContents).toEqual(expectedOutputFileContents);
128
+ }));
129
+ it('it should extract different json media types: application/vnd.tableau.analyticsextensions.v1.ServerSettings+json', () => __awaiter(void 0, void 0, void 0, function* () {
130
+ const response = {
131
+ description: 'A post was created',
132
+ content: O.some({
133
+ 'application/vnd.tableau.analyticsextensions.v1.ServerSettings+json': {
134
+ schema: {
135
+ type: 'object',
136
+ properties: {
137
+ id: {
138
+ type: 'integer',
139
+ },
140
+ },
141
+ additionalProperties: O.some(false),
142
+ },
143
+ },
144
+ }),
145
+ };
146
+ const applicationJson = (0, GenerateOutput_1.getApplicationJsonMediaType)(response);
147
+ const actualApplicationJson = E.getOrElse((error) => {
148
+ throw new Error('Should have been right');
149
+ })(applicationJson);
150
+ const expectedApplicationJson = {
151
+ schema: {
152
+ type: 'object',
153
+ properties: {
154
+ id: {
155
+ type: 'integer',
156
+ },
157
+ },
158
+ additionalProperties: O.some(false),
159
+ },
160
+ };
161
+ expect(actualApplicationJson).toEqual(expectedApplicationJson);
162
+ }));
163
+ it('it should extract different json media types: application/json; charset=utf-8', () => __awaiter(void 0, void 0, void 0, function* () {
164
+ const response = {
165
+ description: 'A post was created',
166
+ content: O.some({
167
+ 'application/json; charset=utf-8': {
168
+ schema: {
169
+ type: 'object',
170
+ properties: {
171
+ id: {
172
+ type: 'integer',
173
+ },
174
+ },
175
+ additionalProperties: O.some(false),
176
+ },
177
+ },
178
+ }),
179
+ };
180
+ const applicationJson = (0, GenerateOutput_1.getApplicationJsonMediaType)(response);
181
+ const actualApplicationJson = E.getOrElse((error) => {
182
+ throw new Error('Should have been right');
183
+ })(applicationJson);
184
+ const expectedApplicationJson = {
185
+ schema: {
186
+ type: 'object',
187
+ properties: {
188
+ id: {
189
+ type: 'integer',
190
+ },
191
+ },
192
+ additionalProperties: O.some(false),
193
+ },
194
+ };
195
+ expect(actualApplicationJson).toEqual(expectedApplicationJson);
196
+ }));
197
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSchemaToTypescriptOptions.d.ts","sourceRoot":"","sources":["../../../src/file-generators/types/JsonSchemaToTypescriptOptions.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,6BAA6B;;;;;;;CAOzC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/tray-openapi",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "Creating CDK Projects from OpenAPI 3.0 Schemas",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -23,6 +23,8 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@openapi-contrib/openapi-schema-to-json-schema": "5.1.0",
26
- "json-schema-to-typescript": "13.1.1"
26
+ "@stoplight/json-ref-resolver": "3.1.6",
27
+ "json-schema-to-typescript": "13.1.1",
28
+ "json-to-typescript": "1.0.1"
27
29
  }
28
30
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=GenerateHander.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateHander.test.d.ts","sourceRoot":"","sources":["../src/GenerateHander.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateHandler.d.ts","sourceRoot":"","sources":["../src/GenerateHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,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,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAoB9C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateHandlerTest.d.ts","sourceRoot":"","sources":["../src/GenerateHandlerTest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAExC,MAAM,MAAM,wBAAwB,GAAG;IACtC,sBAAsB,EAAE,MAAM,CAAC;IAC/B,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,MAAM,EAAE,IAAI,EAAE,CAAC;CACf,CAAC;AA+CF,eAAO,MAAM,mBAAmB,+CAI7B,wBAAwB,KAAG,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CA2BlD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateHandlerTest.test.d.ts","sourceRoot":"","sources":["../src/GenerateHandlerTest.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"JsonSchemaToTypescriptOptions.d.ts","sourceRoot":"","sources":["../../src/connector/JsonSchemaToTypescriptOptions.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,6BAA6B;;;;;;;CAOzC,CAAC"}
@@ -1,8 +0,0 @@
1
- import { JSONSchema4 } from 'json-schema';
2
- import * as TE from 'fp-ts/TaskEither';
3
- export type GenerateInputParams = {
4
- operationNameCamelCase: string;
5
- parameters: JSONSchema4;
6
- };
7
- export declare const generateInput: ({ operationNameCamelCase, parameters, }: GenerateInputParams) => TE.TaskEither<Error, string>;
8
- //# sourceMappingURL=GenerateInput.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateInput.d.ts","sourceRoot":"","sources":["../../../src/connector/input/GenerateInput.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAGvC,MAAM,MAAM,mBAAmB,GAAG;IACjC,sBAAsB,EAAE,MAAM,CAAC;IAC/B,UAAU,EAAE,WAAW,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,aAAa,4CAGvB,mBAAmB,KAAG,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CASlD,CAAC"}
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateInput = void 0;
4
- const json_schema_to_typescript_1 = require("json-schema-to-typescript");
5
- const TaskEither_1 = require("fp-ts/TaskEither");
6
- const JsonSchemaToTypescriptOptions_1 = require("../JsonSchemaToTypescriptOptions");
7
- const generateInput = ({ operationNameCamelCase, parameters, }) => (0, TaskEither_1.tryCatch)(() => (0, json_schema_to_typescript_1.compile)(parameters, `${operationNameCamelCase}Input`, JsonSchemaToTypescriptOptions_1.JsonSchemaToTypescriptOptions), (reason) => new Error(`${reason}`));
8
- exports.generateInput = generateInput;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=GenerateInput.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GenerateInput.test.d.ts","sourceRoot":"","sources":["../../../src/connector/input/GenerateInput.test.ts"],"names":[],"mappings":""}