swaggie 1.5.3-beta.3 → 1.5.3-beta.4

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.
@@ -15,13 +15,13 @@ var _utils = require('../utils');
15
15
  }
16
16
 
17
17
  const viewData = {
18
- servicePrefix: clientOptions.servicePrefix || '',
18
+ servicePrefix: clientOptions.servicePrefix,
19
19
  clients: files
20
20
  .filter((c) => c)
21
21
  .map((c) => ({
22
- fileName: (clientOptions.servicePrefix || '') + c,
23
- className: `${(clientOptions.servicePrefix || '') + c}Client`,
24
- camelCaseName: _case.camel.call(void 0, `${(clientOptions.servicePrefix || '') + c}Client`),
22
+ fileName: clientOptions.servicePrefix + c,
23
+ className: `${clientOptions.servicePrefix + c}Client`,
24
+ camelCaseName: _case.camel.call(void 0, `${clientOptions.servicePrefix + c}Client`),
25
25
  })),
26
26
  };
27
27
 
@@ -25,7 +25,7 @@ var _jsDocs = require('./jsDocs');
25
25
  ) {
26
26
  const operations = _swagger.getOperations.call(void 0, spec);
27
27
  const groups = _utils.groupOperationsByGroupName.call(void 0, operations);
28
- const servicePrefix = _nullishCoalesce(options.servicePrefix, () => ( ''));
28
+ const servicePrefix = options.servicePrefix;
29
29
  let result = _utils.renderFile.call(void 0, 'baseClient.ejs', {
30
30
  servicePrefix,
31
31
  baseUrl: options.baseUrl,
@@ -84,10 +84,7 @@ function prepareClient(
84
84
  * @param options
85
85
  * @returns List of operations prepared for client generation
86
86
  */
87
- function prepareOperations(
88
- operations,
89
- options
90
- ) {
87
+ function prepareOperations(operations, options) {
91
88
  let ops = fixDuplicateOperations(operations);
92
89
 
93
90
  if (options.skipDeprecated) {
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  var _gen = require('./gen'); var _gen2 = _interopRequireDefault(_gen);
5
5
 
6
+ var _types = require('./types');
6
7
  var _utils = require('./utils');
7
8
 
8
9
  /**
@@ -33,7 +34,7 @@ function verifyOptions(options) {
33
34
  }
34
35
 
35
36
  function gen(spec, options) {
36
- _utils.loadAllTemplateFiles.call(void 0, options.template || 'axios');
37
+ _utils.loadAllTemplateFiles.call(void 0, options.template);
37
38
 
38
39
  return _gen2.default.call(void 0, spec, options);
39
40
  }
@@ -68,20 +69,16 @@ function readFile(filePath) {
68
69
 
69
70
 
70
71
 
71
- const defaultQueryParamsConfig = {
72
- allowDots: true,
73
- arrayFormat: 'repeat' ,
74
- };
75
-
76
72
  /**
77
73
  * CLI options are flat, but within the app we use nested objects.
78
74
  * This function converts flat options structure to the nested one and
79
- * merges it with the default values.
80
- * */
81
- function prepareAppOptions(cliOpts) {
75
+ * merges it with the default values, producing a fully-initialized AppOptions
76
+ * object where every defaultable field is guaranteed to be present.
77
+ */
78
+ function prepareAppOptions(cliOpts) {
82
79
  const { allowDots, arrayFormat, template, queryParamsSerialization = {}, ...rest } = cliOpts;
83
80
  const mergedQueryParamsSerialization = {
84
- ...defaultQueryParamsConfig,
81
+ ..._types.APP_DEFAULTS.queryParamsSerialization,
85
82
  ...Object.fromEntries(
86
83
  Object.entries(queryParamsSerialization).filter(([_, v]) => v !== undefined)
87
84
  ),
@@ -91,7 +88,9 @@ function prepareAppOptions(cliOpts) {
91
88
 
92
89
  return {
93
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)),
94
94
  queryParamsSerialization: mergedQueryParamsSerialization,
95
- template: _nullishCoalesce(template, () => ( 'axios')),
96
95
  };
97
- }
96
+ } exports.prepareAppOptions = prepareAppOptions;
@@ -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(); } }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
  var _utils = require('../utils');
4
4
 
@@ -24,7 +24,7 @@ var _utils = require('../utils');
24
24
  return unknownType;
25
25
  }
26
26
 
27
- return getTypeFromSchema(param.schema, options);
27
+ return getTypeFromSchemaResolved(param.schema, options);
28
28
  } exports.getParameterType = getParameterType;
29
29
 
30
30
  /**
@@ -36,6 +36,17 @@ var _utils = require('../utils');
36
36
  function getTypeFromSchema(
37
37
  schema,
38
38
  options
39
+ ) {
40
+ return getTypeFromSchemaResolved(schema, options);
41
+ } exports.getTypeFromSchema = getTypeFromSchema;
42
+
43
+ /**
44
+ * Internal implementation of getTypeFromSchema that operates on fully-resolved AppOptions.
45
+ * All private functions in this module call this version directly to avoid redundant resolution.
46
+ */
47
+ function getTypeFromSchemaResolved(
48
+ schema,
49
+ options
39
50
  ) {
40
51
  const unknownType = options.preferAny ? 'any' : 'unknown';
41
52
 
@@ -59,25 +70,21 @@ var _utils = require('../utils');
59
70
 
60
71
  // OpenAPI 3.0 nullable: nullable: true
61
72
  const isNullable = 'nullable' in schema && schema.nullable === true;
62
- const strategy = _nullishCoalesce(options.nullableStrategy, () => ( 'ignore'));
63
- const isNullableSuffix = isNullable && strategy === 'include' ? ' | null' : '';
73
+ const isNullableSuffix = isNullable && options.nullableStrategy === 'include' ? ' | null' : '';
64
74
  const type = getTypeFromSchemaInternal(schema, options);
65
75
 
66
76
  if (isNullableSuffix && type.endsWith('| null')) {
67
77
  return type;
68
78
  }
69
79
  return type + isNullableSuffix;
70
- } exports.getTypeFromSchema = getTypeFromSchema;
80
+ }
71
81
 
72
82
  /**
73
83
  * Handles OpenAPI 3.1 schemas where `type` is an array (e.g. `["string", "null"]`).
74
84
  * The presence of `"null"` in the array is the OA3.1 way of marking a field as nullable.
75
85
  * Respects `nullableStrategy` the same way as OA3.0 `nullable: true`.
76
86
  */
77
- function getTypeFromOA31ArrayType(
78
- schema,
79
- options
80
- ) {
87
+ function getTypeFromOA31ArrayType(schema, options) {
81
88
  const unknownType = options.preferAny ? 'any' : 'unknown';
82
89
  const types = schema.type ;
83
90
  const isNullable = types.includes('null');
@@ -107,8 +114,7 @@ function getTypeFromOA31ArrayType(
107
114
  return 'null';
108
115
  }
109
116
 
110
- const strategy = _nullishCoalesce(options.nullableStrategy, () => ( 'ignore'));
111
- if (strategy === 'include') {
117
+ if (options.nullableStrategy === 'include') {
112
118
  // We don't want multiple nulls in the type string
113
119
  if (baseType.endsWith('| null')) {
114
120
  return baseType;
@@ -162,24 +168,22 @@ function getNestedTypeFromSchema(
162
168
  schema,
163
169
  options
164
170
  ) {
165
- const strategy = _nullishCoalesce(options.nullableStrategy, () => ( 'ignore'));
166
-
167
171
  // OA3.0 nullable: true
168
172
  const isOA30NullableAndActive =
169
- 'nullable' in schema && schema.nullable === true && strategy === 'include';
173
+ 'nullable' in schema && schema.nullable === true && options.nullableStrategy === 'include';
170
174
 
171
175
  // OA3.1 nullable: type array containing 'null'
172
176
  const isOA31NullableAndActive =
173
177
  'type' in schema &&
174
178
  Array.isArray(schema.type) &&
175
179
  schema.type.includes('null') &&
176
- strategy === 'include';
180
+ options.nullableStrategy === 'include';
177
181
 
178
182
  if (isOA30NullableAndActive || isOA31NullableAndActive || ('enum' in schema && schema.enum)) {
179
- return `(${getTypeFromSchema(schema, options)})`;
183
+ return `(${getTypeFromSchemaResolved(schema, options)})`;
180
184
  }
181
185
 
182
- return getTypeFromSchema(schema, options);
186
+ return getTypeFromSchemaResolved(schema, options);
183
187
  }
184
188
 
185
189
  /**
@@ -195,7 +199,7 @@ function getTypeFromObject(
195
199
  if (schema.additionalProperties) {
196
200
  const extraProps = schema.additionalProperties;
197
201
  return `{ [key: string]: ${
198
- extraProps === true ? 'any' : getTypeFromSchema(extraProps, options)
202
+ extraProps === true ? 'any' : getTypeFromSchemaResolved(extraProps, options)
199
203
  } }`;
200
204
  }
201
205
 
@@ -209,7 +213,7 @@ function getTypeFromObject(
209
213
  const isRequired = required.includes(prop);
210
214
  const safePropName = _utils.escapePropName.call(void 0, prop);
211
215
  result.push(
212
- `${safePropName}${isRequired ? '' : '?'}: ${getTypeFromSchema(propDefinition, options)};`
216
+ `${safePropName}${isRequired ? '' : '?'}: ${getTypeFromSchemaResolved(propDefinition, options)};`
213
217
  );
214
218
  }
215
219
 
@@ -225,7 +229,9 @@ function getTypeFromObject(
225
229
  function getTypeFromComposites(schema, options) {
226
230
  const composite = schema.allOf || schema.oneOf || schema.anyOf;
227
231
 
228
- return composite.map((s) => getTypeFromSchema(s, options)).join(schema.allOf ? ' & ' : ' | ');
232
+ return composite
233
+ .map((s) => getTypeFromSchemaResolved(s, options))
234
+ .join(schema.allOf ? ' & ' : ' | ');
229
235
  }
230
236
 
231
237
  /**
package/dist/types.d.ts CHANGED
@@ -50,6 +50,27 @@ export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head'
50
50
  export type DateSupport = 'string' | 'Date';
51
51
  export type ArrayFormat = 'indices' | 'repeat' | 'brackets';
52
52
  export type NullableStrategy = 'include' | 'nullableAsOptional' | 'ignore';
53
+ /**
54
+ * Internal options type used throughout the app after `prepareAppOptions` has run.
55
+ * All fields that have defaults are required here so the rest of the codebase never
56
+ * needs to perform its own `?? fallback` logic.
57
+ */
58
+ export interface AppOptions extends ClientOptions {
59
+ template: Template;
60
+ servicePrefix: string;
61
+ nullableStrategy: NullableStrategy;
62
+ queryParamsSerialization: {
63
+ allowDots: boolean;
64
+ arrayFormat: ArrayFormat;
65
+ };
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;
53
74
  /**
54
75
  * Local type that represent Operation as understood by Swaggie
55
76
  **/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggie",
3
- "version": "1.5.3-beta.3",
3
+ "version": "1.5.3-beta.4",
4
4
  "description": "Generate TypeScript REST client code from an OpenAPI spec",
5
5
  "author": {
6
6
  "name": "Piotr Dabrowski",