swaggie 1.5.3-beta.4 → 1.5.3-beta.6

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.
@@ -8,12 +8,11 @@ var _swagger = require('../swagger');
8
8
 
9
9
 
10
10
 
11
+
11
12
  var _utils = require('../utils');
12
13
  var _createBarrel = require('./createBarrel');
13
14
 
14
15
 
15
-
16
-
17
16
  var _jsDocs = require('./jsDocs');
18
17
 
19
18
  /**
@@ -34,7 +33,7 @@ var _jsDocs = require('./jsDocs');
34
33
 
35
34
  for (const name in groups) {
36
35
  const group = groups[name];
37
- const clientData = prepareClient(servicePrefix + name, group, options);
36
+ const clientData = prepareClient(servicePrefix + name, group, spec.components, options);
38
37
 
39
38
  if (!clientData) {
40
39
  continue;
@@ -56,9 +55,10 @@ var _jsDocs = require('./jsDocs');
56
55
  function prepareClient(
57
56
  name,
58
57
  operations,
58
+ components,
59
59
  options
60
60
  ) {
61
- const preparedOperations = prepareOperations(operations, options);
61
+ const preparedOperations = prepareOperations(operations, options, components);
62
62
 
63
63
  if (preparedOperations.length === 0) {
64
64
  return null;
@@ -84,7 +84,11 @@ function prepareClient(
84
84
  * @param options
85
85
  * @returns List of operations prepared for client generation
86
86
  */
87
- function prepareOperations(operations, options) {
87
+ function prepareOperations(
88
+ operations,
89
+ options,
90
+ components
91
+ ) {
88
92
  let ops = fixDuplicateOperations(operations);
89
93
 
90
94
  if (options.skipDeprecated) {
@@ -92,10 +96,10 @@ function prepareClient(
92
96
  }
93
97
 
94
98
  return ops.map((op) => {
95
- const [respObject, responseContentType] = _utils.getBestResponse.call(void 0, op);
99
+ const [respObject, responseContentType] = _utils.getBestResponse.call(void 0, op, components);
96
100
  const returnType = _swagger.getParameterType.call(void 0, respObject, options);
97
101
 
98
- const body = getRequestBody(op.requestBody, options);
102
+ const body = getRequestBody(op.requestBody, components, options);
99
103
  const queryParams = getParams(op.parameters , options, ['query']);
100
104
  const params = getParams(op.parameters , options);
101
105
 
@@ -276,23 +280,40 @@ function prepareUrl(path) {
276
280
  } exports.getParamName = getParamName;
277
281
 
278
282
  function getRequestBody(
279
- reqBody,
283
+ rawReqBody,
284
+ components,
280
285
  options
281
286
  ) {
282
- if (reqBody && 'content' in reqBody) {
283
- const [bodyContent, contentType] = _utils.getBestContentType.call(void 0, reqBody);
284
- const isFormData = contentType === 'form-data';
285
-
286
- if (bodyContent) {
287
- return {
288
- originalName: _nullishCoalesce(reqBody['x-name'], () => ( 'body')),
289
- name: getParamName(_nullishCoalesce(reqBody['x-name'], () => ( 'body'))),
290
- type: isFormData ? 'FormData' : _swagger.getParameterType.call(void 0, bodyContent, options),
291
- optional: !reqBody.required,
292
- original: reqBody,
293
- contentType,
294
- };
287
+ if (!rawReqBody) {
288
+ return null;
289
+ }
290
+
291
+ let reqBody;
292
+ if ('$ref' in rawReqBody) {
293
+ const refName = rawReqBody.$ref.replace('#/components/requestBodies/', '');
294
+ const resolved = _optionalChain([components, 'optionalAccess', _8 => _8.requestBodies, 'optionalAccess', _9 => _9[refName]]);
295
+ if (!resolved || '$ref' in resolved) {
296
+ console.error(`RequestBody $ref '${rawReqBody.$ref}' not found in components/requestBodies`);
297
+ return null;
295
298
  }
299
+ reqBody = resolved;
300
+ } else {
301
+ reqBody = rawReqBody;
296
302
  }
303
+
304
+ const [bodyContent, contentType] = _utils.getBestContentType.call(void 0, reqBody);
305
+ const isFormData = contentType === 'form-data';
306
+
307
+ if (bodyContent) {
308
+ return {
309
+ originalName: _nullishCoalesce(reqBody['x-name'], () => ( 'body')),
310
+ name: getParamName(_nullishCoalesce(reqBody['x-name'], () => ( 'body'))),
311
+ type: isFormData ? 'FormData' : _swagger.getParameterType.call(void 0, bodyContent, options),
312
+ optional: !reqBody.required,
313
+ original: reqBody,
314
+ contentType,
315
+ };
316
+ }
317
+
297
318
  return null;
298
319
  }
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@
3
3
 
4
4
  var _gen = require('./gen'); var _gen2 = _interopRequireDefault(_gen);
5
5
 
6
- var _types = require('./types');
7
6
  var _utils = require('./utils');
7
+ var _swagger = require('./swagger');
8
8
 
9
9
  /**
10
10
  * Runs the whole code generation process.
@@ -78,7 +78,7 @@ function readFile(filePath) {
78
78
  function prepareAppOptions(cliOpts) {
79
79
  const { allowDots, arrayFormat, template, queryParamsSerialization = {}, ...rest } = cliOpts;
80
80
  const mergedQueryParamsSerialization = {
81
- ..._types.APP_DEFAULTS.queryParamsSerialization,
81
+ ..._swagger.APP_DEFAULTS.queryParamsSerialization,
82
82
  ...Object.fromEntries(
83
83
  Object.entries(queryParamsSerialization).filter(([_, v]) => v !== undefined)
84
84
  ),
@@ -88,9 +88,9 @@ function readFile(filePath) {
88
88
 
89
89
  return {
90
90
  ...rest,
91
- template: _nullishCoalesce(template, () => ( _types.APP_DEFAULTS.template)),
92
- servicePrefix: _nullishCoalesce(rest.servicePrefix, () => ( _types.APP_DEFAULTS.servicePrefix)),
93
- nullableStrategy: _nullishCoalesce(rest.nullableStrategy, () => ( _types.APP_DEFAULTS.nullableStrategy)),
91
+ template: _nullishCoalesce(template, () => ( _swagger.APP_DEFAULTS.template)),
92
+ servicePrefix: _nullishCoalesce(rest.servicePrefix, () => ( _swagger.APP_DEFAULTS.servicePrefix)),
93
+ nullableStrategy: _nullishCoalesce(rest.nullableStrategy, () => ( _swagger.APP_DEFAULTS.nullableStrategy)),
94
94
  queryParamsSerialization: mergedQueryParamsSerialization,
95
95
  };
96
96
  } exports.prepareAppOptions = prepareAppOptions;
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
2
 
3
3
  var _utils = require('../utils');
4
4
 
@@ -256,3 +256,32 @@ function getTypeFromComposites(schema, options) {
256
256
  .filter((v) => '$ref' in v)
257
257
  .map((s) => getSafeIdentifier(s.$ref.split('/').pop()));
258
258
  } exports.getRefCompositeTypes = getRefCompositeTypes;
259
+
260
+ /** Default values applied to every field of AppOptions that has a default. */
261
+ const APP_DEFAULTS = {
262
+ template: 'axios',
263
+ servicePrefix: '',
264
+ nullableStrategy: 'ignore',
265
+ queryParamsSerialization: {
266
+ allowDots: true,
267
+ arrayFormat: 'repeat',
268
+ },
269
+ }; exports.APP_DEFAULTS = APP_DEFAULTS;
270
+
271
+ /**
272
+ * Fills in all AppOptions defaults for a partial ClientOptions object.
273
+ * Used at the boundary between public API / test helpers and the internal pipeline.
274
+ */
275
+ function resolveOptions(opts) {
276
+ return {
277
+ src: _nullishCoalesce(opts.src, () => ( '')),
278
+ ...opts,
279
+ template: _nullishCoalesce(opts.template, () => ( exports.APP_DEFAULTS.template)),
280
+ servicePrefix: _nullishCoalesce(opts.servicePrefix, () => ( exports.APP_DEFAULTS.servicePrefix)),
281
+ nullableStrategy: _nullishCoalesce(opts.nullableStrategy, () => ( exports.APP_DEFAULTS.nullableStrategy)),
282
+ queryParamsSerialization: {
283
+ ...exports.APP_DEFAULTS.queryParamsSerialization,
284
+ ...opts.queryParamsSerialization,
285
+ },
286
+ };
287
+ } exports.resolveOptions = resolveOptions;
package/dist/types.d.ts CHANGED
@@ -64,13 +64,6 @@ export interface AppOptions extends ClientOptions {
64
64
  arrayFormat: ArrayFormat;
65
65
  };
66
66
  }
67
- /** Default values applied to every field of AppOptions that has a default. */
68
- export declare const APP_DEFAULTS: Partial<AppOptions>;
69
- /**
70
- * Fills in all AppOptions defaults for a partial ClientOptions object.
71
- * Used at the boundary between public API / test helpers and the internal pipeline.
72
- */
73
- export declare function resolveOptions(opts: Partial<ClientOptions>): AppOptions;
74
67
  /**
75
68
  * Local type that represent Operation as understood by Swaggie
76
69
  **/
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _nodefs = require('node:fs');
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _nodefs = require('node:fs');
2
2
  var _nodepath = require('node:path');
3
3
 
4
4
 
@@ -171,11 +171,15 @@ const reservedKeywords = new Set([
171
171
  * Other media types are not supported at this time.
172
172
  * @returns Response or reference of the success response
173
173
  */
174
- function getBestResponse(op) {
174
+ function getBestResponse(
175
+ op,
176
+ components
177
+ ) {
175
178
  const NOT_FOUND = 100000;
176
179
  const lowestCode = _nullishCoalesce(Object.keys(op.responses).sort().shift(), () => ( NOT_FOUND));
177
180
 
178
- const resp = lowestCode === NOT_FOUND ? op.responses[0] : op.responses[lowestCode.toString()];
181
+ const rawResp = lowestCode === NOT_FOUND ? op.responses[0] : op.responses[lowestCode.toString()];
182
+ const resp = resolveResponseRef(rawResp, components);
179
183
 
180
184
  if (resp && 'content' in resp) {
181
185
  return getBestContentType(resp);
@@ -183,6 +187,33 @@ const reservedKeywords = new Set([
183
187
  return [null, null];
184
188
  } exports.getBestResponse = getBestResponse;
185
189
 
190
+ /**
191
+ * Resolves a $ref in a response object to the actual response object from components/responses.
192
+ * If the response is already an object (not a reference), it is returned as-is.
193
+ */
194
+ function resolveResponseRef(
195
+ resp,
196
+ components
197
+ ) {
198
+ if (!resp) {
199
+ return null;
200
+ }
201
+
202
+ if (!('$ref' in resp)) {
203
+ return resp;
204
+ }
205
+
206
+ const refName = resp.$ref.replace('#/components/responses/', '');
207
+ const resolved = _optionalChain([components, 'optionalAccess', _ => _.responses, 'optionalAccess', _2 => _2[refName]]);
208
+
209
+ if (!resolved) {
210
+ console.error(`Response $ref '${resp.$ref}' not found in components/responses`);
211
+ return null;
212
+ }
213
+
214
+ return resolved ;
215
+ }
216
+
186
217
  /** This method tries to fix potentially wrong out parameter given from commandline */
187
218
  function prepareOutputFilename(out) {
188
219
  if (!out) {
@@ -206,7 +237,7 @@ const reservedKeywords = new Set([
206
237
  return arr.concat().sort(sortByKey(key));
207
238
  } exports.orderBy = orderBy;
208
239
 
209
- const sortByKey = (key) => (a, b) => a[key] > b[key] ? 1 : b[key] > a[key] ? -1 : 0;
240
+ const sortByKey = (key) => (a, b) => (a[key] > b[key] ? 1 : b[key] > a[key] ? -1 : 0);
210
241
 
211
242
  const orderedContentTypes = [
212
243
  'application/json',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggie",
3
- "version": "1.5.3-beta.4",
3
+ "version": "1.5.3-beta.6",
4
4
  "description": "Generate TypeScript REST client code from an OpenAPI spec",
5
5
  "author": {
6
6
  "name": "Piotr Dabrowski",