swagger-sdk-generator 0.0.0-stage → 3.0.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 (217) hide show
  1. package/README.md +58 -2
  2. package/dist/cli/commands/generate.d.ts +2 -0
  3. package/dist/cli/commands/generate.js +196 -0
  4. package/dist/cli/commands/init.d.ts +2 -0
  5. package/dist/cli/commands/init.js +130 -0
  6. package/dist/cli/commands/inspect.d.ts +2 -0
  7. package/dist/cli/commands/inspect.js +164 -0
  8. package/dist/cli/commands/templates.d.ts +2 -0
  9. package/dist/cli/commands/templates.js +126 -0
  10. package/dist/cli/context.d.ts +16 -0
  11. package/dist/cli/context.js +85 -0
  12. package/dist/cli/main.d.ts +2 -0
  13. package/dist/cli/main.js +25 -0
  14. package/dist/config/dotenv.d.ts +2 -0
  15. package/dist/config/dotenv.js +82 -0
  16. package/dist/config/interpolate.d.ts +8 -0
  17. package/dist/config/interpolate.js +85 -0
  18. package/dist/config/load.d.ts +11 -0
  19. package/dist/config/load.js +182 -0
  20. package/dist/config/migrate-v3.d.ts +5 -0
  21. package/dist/config/migrate-v3.js +6 -0
  22. package/dist/config/mode-compatibility.d.ts +9 -0
  23. package/dist/config/mode-compatibility.js +17 -0
  24. package/dist/config/modes.d.ts +13 -0
  25. package/dist/config/modes.js +27 -0
  26. package/dist/config/preset-definitions.d.ts +2 -0
  27. package/dist/config/preset-definitions.js +4 -0
  28. package/dist/config/presets.d.ts +25 -0
  29. package/dist/config/presets.js +36 -0
  30. package/dist/config/resolve.d.ts +32 -0
  31. package/dist/config/resolve.js +288 -0
  32. package/dist/config/schema.d.ts +93 -0
  33. package/dist/config/schema.js +173 -0
  34. package/dist/config/types.d.ts +42 -0
  35. package/dist/config/types.js +2 -0
  36. package/dist/config/workspace.d.ts +30 -0
  37. package/dist/config/workspace.js +95 -0
  38. package/dist/core/case-align.d.ts +5 -0
  39. package/dist/core/case-align.js +90 -0
  40. package/dist/core/deep-merge.d.ts +2 -0
  41. package/dist/core/deep-merge.js +21 -0
  42. package/dist/core/file-set.d.ts +10 -0
  43. package/dist/core/file-set.js +31 -0
  44. package/dist/core/import-module.d.ts +1 -0
  45. package/dist/core/import-module.js +61 -0
  46. package/dist/core/log.d.ts +9 -0
  47. package/dist/core/log.js +24 -0
  48. package/dist/core/manifest.d.ts +17 -0
  49. package/dist/core/manifest.js +195 -0
  50. package/dist/core/overlaps.d.ts +6 -0
  51. package/dist/core/overlaps.js +67 -0
  52. package/dist/core/package-version.d.ts +1 -0
  53. package/dist/core/package-version.js +44 -0
  54. package/dist/core/pipeline.d.ts +30 -0
  55. package/dist/core/pipeline.js +335 -0
  56. package/dist/core/plugin.d.ts +72 -0
  57. package/dist/core/plugin.js +2 -0
  58. package/dist/core/targets.d.ts +14 -0
  59. package/dist/core/targets.js +120 -0
  60. package/dist/core/unmanaged.d.ts +3 -0
  61. package/dist/core/unmanaged.js +101 -0
  62. package/dist/core/writer.d.ts +28 -0
  63. package/dist/core/writer.js +148 -0
  64. package/dist/index.d.ts +20 -0
  65. package/dist/index.js +37 -0
  66. package/dist/ir/casing.d.ts +5 -0
  67. package/dist/ir/casing.js +22 -0
  68. package/dist/ir/graph.d.ts +8 -0
  69. package/dist/ir/graph.js +95 -0
  70. package/dist/ir/operations/grouping.d.ts +17 -0
  71. package/dist/ir/operations/grouping.js +39 -0
  72. package/dist/ir/operations/legacy-naming.d.ts +2 -0
  73. package/dist/ir/operations/legacy-naming.js +8 -0
  74. package/dist/ir/operations/naming.d.ts +6 -0
  75. package/dist/ir/operations/naming.js +54 -0
  76. package/dist/ir/option-errors.d.ts +3 -0
  77. package/dist/ir/option-errors.js +68 -0
  78. package/dist/ir/transforms/builtins.d.ts +2 -0
  79. package/dist/ir/transforms/builtins.js +4 -0
  80. package/dist/ir/transforms/index.d.ts +10 -0
  81. package/dist/ir/transforms/index.js +39 -0
  82. package/dist/ir/transforms/types.d.ts +9 -0
  83. package/dist/ir/transforms/types.js +2 -0
  84. package/dist/ir/types.d.ts +140 -0
  85. package/dist/ir/types.js +2 -0
  86. package/dist/runtime/axios.d.ts +28 -0
  87. package/dist/runtime/axios.js +132 -0
  88. package/dist/runtime/fetch.d.ts +6 -0
  89. package/dist/runtime/fetch.js +38 -0
  90. package/dist/runtime/index.d.ts +54 -0
  91. package/dist/runtime/index.js +167 -0
  92. package/dist/runtime/rtk.d.ts +35 -0
  93. package/dist/runtime/rtk.js +36 -0
  94. package/dist/runtime/transport.d.ts +38 -0
  95. package/dist/runtime/transport.js +30 -0
  96. package/dist/runtime/wire.d.ts +27 -0
  97. package/dist/runtime/wire.js +148 -0
  98. package/dist/spec/html-entities.d.ts +1 -0
  99. package/dist/spec/html-entities.js +25 -0
  100. package/dist/spec/load.d.ts +26 -0
  101. package/dist/spec/load.js +130 -0
  102. package/dist/spec/openapi/all-of.d.ts +3 -0
  103. package/dist/spec/openapi/all-of.js +9 -0
  104. package/dist/spec/openapi/normalize.d.ts +4 -0
  105. package/dist/spec/openapi/normalize.js +31 -0
  106. package/dist/spec/openapi/operations.d.ts +4 -0
  107. package/dist/spec/openapi/operations.js +173 -0
  108. package/dist/spec/openapi/schema.d.ts +51 -0
  109. package/dist/spec/openapi/schema.js +307 -0
  110. package/dist/targets/declarative/config.d.ts +533 -0
  111. package/dist/targets/declarative/config.js +280 -0
  112. package/dist/targets/declarative/extensions.d.ts +45 -0
  113. package/dist/targets/declarative/extensions.js +123 -0
  114. package/dist/targets/declarative/files.d.ts +13 -0
  115. package/dist/targets/declarative/files.js +127 -0
  116. package/dist/targets/declarative/format.d.ts +4 -0
  117. package/dist/targets/declarative/format.js +104 -0
  118. package/dist/targets/declarative/index.d.ts +9 -0
  119. package/dist/targets/declarative/index.js +170 -0
  120. package/dist/targets/declarative/liquid.d.ts +8 -0
  121. package/dist/targets/declarative/liquid.js +57 -0
  122. package/dist/targets/declarative/naming-case.d.ts +3 -0
  123. package/dist/targets/declarative/naming-case.js +31 -0
  124. package/dist/targets/declarative/naming.d.ts +42 -0
  125. package/dist/targets/declarative/naming.js +174 -0
  126. package/dist/targets/declarative/scaffold.d.ts +5 -0
  127. package/dist/targets/declarative/scaffold.js +146 -0
  128. package/dist/targets/declarative/types.d.ts +52 -0
  129. package/dist/targets/declarative/types.js +287 -0
  130. package/dist/targets/declarative/views.d.ts +180 -0
  131. package/dist/targets/declarative/views.js +447 -0
  132. package/dist/targets/python/extensions.js +152 -0
  133. package/dist/targets/python/target.json +549 -0
  134. package/dist/targets/python/templates/client/client.liquid +114 -0
  135. package/dist/targets/python/templates/client/httpx_transport.liquid +172 -0
  136. package/dist/targets/python/templates/client/init.liquid +20 -0
  137. package/dist/targets/python/templates/client/partials/operation_method.liquid +41 -0
  138. package/dist/targets/python/templates/client/resource.liquid +28 -0
  139. package/dist/targets/python/templates/client/runtime.liquid +133 -0
  140. package/dist/targets/python/templates/client/transport.liquid +62 -0
  141. package/dist/targets/python/templates/models/module.liquid +48 -0
  142. package/dist/targets/python/templates/models/partials/docstring.liquid +5 -0
  143. package/dist/targets/python/templates/models/partials/enum_class.liquid +11 -0
  144. package/dist/targets/python/templates/models/partials/field_line.liquid +5 -0
  145. package/dist/targets/python/templates/models/partials/model_class.liquid +17 -0
  146. package/dist/targets/typescript/compatibility-naming.d.ts +6 -0
  147. package/dist/targets/typescript/compatibility-naming.js +28 -0
  148. package/dist/targets/typescript/compatibility-options.d.ts +5 -0
  149. package/dist/targets/typescript/compatibility-options.js +8 -0
  150. package/dist/targets/typescript/compatibility-target.d.ts +5 -0
  151. package/dist/targets/typescript/compatibility-target.js +10 -0
  152. package/dist/targets/typescript/http-operations.d.ts +34 -0
  153. package/dist/targets/typescript/http-operations.js +155 -0
  154. package/dist/targets/typescript/imports.d.ts +43 -0
  155. package/dist/targets/typescript/imports.js +154 -0
  156. package/dist/targets/typescript/index.d.ts +7 -0
  157. package/dist/targets/typescript/index.js +52 -0
  158. package/dist/targets/typescript/mock-compatibility.d.ts +4 -0
  159. package/dist/targets/typescript/mock-compatibility.js +6 -0
  160. package/dist/targets/typescript/mock-hints.d.ts +13 -0
  161. package/dist/targets/typescript/mock-hints.js +75 -0
  162. package/dist/targets/typescript/mock-reachability.d.ts +12 -0
  163. package/dist/targets/typescript/mock-reachability.js +87 -0
  164. package/dist/targets/typescript/model-compatibility.d.ts +14 -0
  165. package/dist/targets/typescript/model-compatibility.js +16 -0
  166. package/dist/targets/typescript/naming.d.ts +13 -0
  167. package/dist/targets/typescript/naming.js +50 -0
  168. package/dist/targets/typescript/options.d.ts +124 -0
  169. package/dist/targets/typescript/options.js +90 -0
  170. package/dist/targets/typescript/outputs/client.d.ts +67 -0
  171. package/dist/targets/typescript/outputs/client.js +187 -0
  172. package/dist/targets/typescript/outputs/mocks.d.ts +32 -0
  173. package/dist/targets/typescript/outputs/mocks.js +371 -0
  174. package/dist/targets/typescript/outputs/models.d.ts +85 -0
  175. package/dist/targets/typescript/outputs/models.js +246 -0
  176. package/dist/targets/typescript/outputs/rtk-query.d.ts +69 -0
  177. package/dist/targets/typescript/outputs/rtk-query.js +266 -0
  178. package/dist/targets/typescript/prettier.d.ts +7 -0
  179. package/dist/targets/typescript/prettier.js +44 -0
  180. package/dist/targets/typescript/templates/client/client.liquid +30 -0
  181. package/dist/targets/typescript/templates/client/partials/api-member.liquid +13 -0
  182. package/dist/targets/typescript/templates/client/partials/jsdoc.liquid +10 -0
  183. package/dist/targets/typescript/templates/client/partials/method.liquid +26 -0
  184. package/dist/targets/typescript/templates/client/partials/query-type.liquid +10 -0
  185. package/dist/targets/typescript/templates/client/resource.liquid +34 -0
  186. package/dist/targets/typescript/templates/mocks/mock.liquid +15 -0
  187. package/dist/targets/typescript/templates/mocks/partials/factory.liquid +12 -0
  188. package/dist/targets/typescript/templates/mocks/partials/imports.liquid +7 -0
  189. package/dist/targets/typescript/templates/models/alias.liquid +16 -0
  190. package/dist/targets/typescript/templates/models/enum.liquid +27 -0
  191. package/dist/targets/typescript/templates/models/index.liquid +14 -0
  192. package/dist/targets/typescript/templates/models/model.liquid +25 -0
  193. package/dist/targets/typescript/templates/models/partials/const.liquid +10 -0
  194. package/dist/targets/typescript/templates/models/partials/imports.liquid +9 -0
  195. package/dist/targets/typescript/templates/models/partials/jsdoc.liquid +9 -0
  196. package/dist/targets/typescript/templates/models/partials/property.liquid +7 -0
  197. package/dist/targets/typescript/templates/rtk-query/api.liquid +16 -0
  198. package/dist/targets/typescript/templates/rtk-query/partials/arg-types.liquid +6 -0
  199. package/dist/targets/typescript/templates/rtk-query/partials/endpoint-mutation.liquid +13 -0
  200. package/dist/targets/typescript/templates/rtk-query/partials/endpoint-query.liquid +13 -0
  201. package/dist/targets/typescript/templates/rtk-query/partials/file-warning.liquid +7 -0
  202. package/dist/targets/typescript/templates/rtk-query/partials/hooks.liquid +9 -0
  203. package/dist/targets/typescript/templates/rtk-query/partials/imports.liquid +7 -0
  204. package/dist/targets/typescript/templates/rtk-query/partials/request-config.liquid +9 -0
  205. package/dist/targets/typescript/templates/rtk-query/resource.liquid +40 -0
  206. package/dist/targets/typescript/ts-type.d.ts +70 -0
  207. package/dist/targets/typescript/ts-type.js +62 -0
  208. package/dist/targets/typescript/type-printer.d.ts +21 -0
  209. package/dist/targets/typescript/type-printer.js +115 -0
  210. package/dist/templates/engine.d.ts +20 -0
  211. package/dist/templates/engine.js +161 -0
  212. package/dist/templates/inspect.d.ts +27 -0
  213. package/dist/templates/inspect.js +331 -0
  214. package/package.json +65 -4
  215. package/schema/sdk-gen.local.schema.json +75 -0
  216. package/schema/sdk-gen.schema.json +980 -0
  217. package/schema/sdk-gen.target.schema.json +1384 -0
@@ -0,0 +1,371 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.mocksOutput = void 0;
37
+ exports.hashSeed = hashSeed;
38
+ exports.buildMockView = buildMockView;
39
+ const path = __importStar(require("path"));
40
+ const graph_1 = require("../../../ir/graph");
41
+ const imports_1 = require("../imports");
42
+ const mock_reachability_1 = require("../mock-reachability");
43
+ const mock_hints_1 = require("../mock-hints");
44
+ const naming_1 = require("../naming");
45
+ const options_1 = require("../options");
46
+ const models_1 = require("./models");
47
+ const mock_compatibility_1 = require("../mock-compatibility");
48
+ const WARNING = '/* WARNING THIS FILE IS AUTO-GENERATED DO NOT MODIFY YOUR CHANGES WILL BE OVERWRITTEN! */';
49
+ const CUT = '\0CUT\0';
50
+ const ANY = 'undefined as any';
51
+ exports.mocksOutput = {
52
+ name: 'mocks',
53
+ description: 'Deterministic faker factories get<Name>Mock() per model',
54
+ optionsSchema: options_1.mocksOptionsSchema,
55
+ defaults: {
56
+ dir: './mocks',
57
+ engine: 'ir',
58
+ subdir: '__model_mocks__',
59
+ style: 'seeded',
60
+ arrayLength: 2,
61
+ maxDepth: 4,
62
+ format: false,
63
+ },
64
+ generate(ctx) {
65
+ if ((0, mock_compatibility_1.generateCompatibilityMocks)(ctx, () => selectModels(ctx, (0, imports_1.createModelLocator)(ctx.api, modelOptionsFromPeers(ctx.peers)))))
66
+ return;
67
+ const modelOptions = modelOptionsFromPeers(ctx.peers);
68
+ const locator = (0, imports_1.createModelLocator)(ctx.api, modelOptions);
69
+ const shapes = new Map();
70
+ const shapeOf = (id) => {
71
+ const model = locator.model(id);
72
+ if (!model)
73
+ return undefined;
74
+ if (!shapes.has(id))
75
+ shapes.set(id, (0, models_1.resolveModelShape)(model, locator, modelOptions).type);
76
+ return shapes.get(id);
77
+ };
78
+ const mockRoot = path.join(ctx.options.dir, ...ctx.options.subdir.split('/'));
79
+ const cuts = (0, graph_1.backEdges)((0, graph_1.refGraph)(ctx.api));
80
+ for (const model of selectModels(ctx, locator)) {
81
+ const filePath = `${ctx.options.subdir}/${locator.modulePath(model.id)}.mock.ts`;
82
+ const view = buildMockView(model, {
83
+ fromFile: path.join(ctx.options.dir, ...filePath.split('/')),
84
+ mockRoot,
85
+ arrayLength: ctx.options.arrayLength,
86
+ maxDepth: ctx.options.maxDepth,
87
+ cuts,
88
+ locator,
89
+ shapeOf,
90
+ hints: mock_hints_1.DEFAULT_MOCK_HINTS,
91
+ });
92
+ ctx.emit({ path: filePath, content: ctx.templates.render('mocks/mock', view), format: false });
93
+ }
94
+ },
95
+ };
96
+ function selectModels(ctx, locator) {
97
+ var _a;
98
+ if (!((_a = ctx.options.reachableFrom) === null || _a === void 0 ? void 0 : _a.length))
99
+ return ctx.api.models;
100
+ const reach = (0, mock_reachability_1.findReachableMocks)(ctx.api, locator, {
101
+ roots: ctx.options.reachableFrom.map((root) => path.resolve(ctx.cwd, root)),
102
+ subdir: ctx.options.subdir,
103
+ exclude: [ctx.options.dir],
104
+ });
105
+ ctx.log.info(` 🎯 mocks: ${reach.ids.size}/${ctx.api.models.length} reachable from ${reach.scanned} scanned files`);
106
+ if (reach.unknown.length) {
107
+ ctx.log.warn(` ⚠️ mocks imported but no longer in the spec: ${reach.unknown.slice(0, 5).join(', ')}${reach.unknown.length > 5 ? ', …' : ''}`);
108
+ }
109
+ return ctx.api.models.filter((model) => reach.ids.has(model.id));
110
+ }
111
+ function modelOptionsFromPeers(peers) {
112
+ const models = peers.models;
113
+ const defined = Object.fromEntries(Object.entries(models !== null && models !== void 0 ? models : {}).filter(([, v]) => v !== undefined));
114
+ return Object.assign(Object.assign({}, models_1.modelsOutput.defaults), defined);
115
+ }
116
+ function hashSeed(name) {
117
+ let hash = 5381;
118
+ for (let i = 0; i < name.length; i++)
119
+ hash = ((hash << 5) + hash + name.charCodeAt(i)) | 0;
120
+ return hash >>> 0;
121
+ }
122
+ function buildMockView(model, file) {
123
+ const typeName = file.locator.typeName(model.id);
124
+ const createName = `create${typeName}Mock`;
125
+ const exportName = `get${typeName}Mock`;
126
+ const type = file.shapeOf(model.id);
127
+ const usesValue = type.kind === 'enum' && type.members.length > 0;
128
+ const imports = new imports_1.ImportCollector(file.fromFile, file.locator).reserve('faker', createName, exportName);
129
+ const localTypeName = imports.model(model.id, { typeOnly: !usesValue });
130
+ const builders = new BuilderImports(file, [createName, exportName]);
131
+ const values = new ValueBuilder(file, model.id, localTypeName, builders);
132
+ const value = values.build(type, { indent: 2 });
133
+ return {
134
+ header: WARNING,
135
+ imports: (0, models_1.importViews)(imports.list()),
136
+ refImports: builders.list(),
137
+ depthParam: values.usesDepth ? 'depth = 0' : '_depth = 0',
138
+ createName,
139
+ exportName,
140
+ typeName: localTypeName,
141
+ seed: hashSeed(typeName),
142
+ valueExpression: value === CUT ? ANY : value,
143
+ };
144
+ }
145
+ class ValueBuilder {
146
+ constructor(file, currentId, currentLocalName, builders) {
147
+ this.file = file;
148
+ this.currentId = currentId;
149
+ this.currentLocalName = currentLocalName;
150
+ this.builders = builders;
151
+ this.usesDepth = false;
152
+ }
153
+ build(type, state) {
154
+ switch (type.kind) {
155
+ case 'keyword':
156
+ return this.keyword(type, state);
157
+ case 'literal':
158
+ return (0, naming_1.literal)(type.value);
159
+ case 'named':
160
+ return ANY;
161
+ case 'ref':
162
+ return this.ref(type.id, state);
163
+ case 'array':
164
+ return this.array(type.items, state);
165
+ case 'record':
166
+ return '{}';
167
+ case 'object':
168
+ return this.object(type.properties, state);
169
+ case 'union':
170
+ return this.union(type.members, state);
171
+ case 'intersection':
172
+ return this.intersection(type.members, state);
173
+ case 'widened':
174
+ return oneOf(type.values.map(naming_1.literal), false);
175
+ case 'const':
176
+ return oneOf(type.members.map((m) => m.value), false);
177
+ case 'enum': {
178
+ if (type.id !== this.currentId || type.members.length === 0)
179
+ return ANY;
180
+ return oneOf(type.members.map((m) => memberAccess(this.currentLocalName, m.key)), true);
181
+ }
182
+ }
183
+ }
184
+ keyword(type, state) {
185
+ var _a;
186
+ switch (type.name) {
187
+ case 'string':
188
+ case 'number':
189
+ return (0, mock_hints_1.scalarMock)(scalarKind(type), (_a = type.source) === null || _a === void 0 ? void 0 : _a.format, state.name, this.file.hints);
190
+ case 'boolean':
191
+ return 'faker.datatype.boolean()';
192
+ case 'null':
193
+ return 'null';
194
+ case 'undefined':
195
+ case 'void':
196
+ return 'undefined';
197
+ case 'object':
198
+ return '{}';
199
+ case 'never':
200
+ return 'undefined as never';
201
+ case 'unknown':
202
+ case 'any':
203
+ return 'faker.word.words()';
204
+ }
205
+ }
206
+ ref(id, state) {
207
+ var _a;
208
+ if (!this.file.locator.model(id))
209
+ return ANY;
210
+ if (this.file.cuts.has((0, graph_1.edgeKey)(this.currentId, id)))
211
+ return CUT;
212
+ this.usesDepth = true;
213
+ const call = `${this.builders.local(id)}(depth + 1)`;
214
+ return state.guarded ? call : `${this.depthCheck()} ? ${call} : ${(_a = state.fallback) !== null && _a !== void 0 ? _a : ANY}`;
215
+ }
216
+ depthCheck() {
217
+ return `depth < ${this.file.maxDepth}`;
218
+ }
219
+ array(items, state) {
220
+ if (this.file.arrayLength <= 0)
221
+ return '[]';
222
+ const variants = items.kind === 'union' && !isLiteralUnion(items.members) ? nonNull(items.members) : [items];
223
+ const guardList = variants.some((variant) => this.composesRef(variant));
224
+ const itemState = { indent: state.indent + 2, name: state.name, guarded: guardList };
225
+ const usedBefore = this.usesDepth;
226
+ this.usesDepth = false;
227
+ const built = [];
228
+ for (const variant of variants) {
229
+ if (built.length >= this.file.arrayLength)
230
+ break;
231
+ const value = this.build(variant, itemState);
232
+ if (value !== CUT)
233
+ built.push(value);
234
+ }
235
+ const emittedRef = guardList && this.usesDepth;
236
+ this.usesDepth || (this.usesDepth = usedBefore);
237
+ if (built.length === 0)
238
+ return '[]';
239
+ const values = Array.from({ length: this.file.arrayLength }, (_, i) => built[i % built.length]);
240
+ const list = block('[', values.map((v) => `${spaces(itemState.indent)}${v}`), ']', state.indent);
241
+ return emittedRef ? `${this.depthCheck()} ? ${list} : []` : list;
242
+ }
243
+ composesRef(type) {
244
+ if (type.kind === 'ref')
245
+ return this.file.locator.model(type.id) !== undefined && !this.file.cuts.has((0, graph_1.edgeKey)(this.currentId, type.id));
246
+ return type.kind === 'union' && type.members.some((m) => this.composesRef(m));
247
+ }
248
+ object(properties, state) {
249
+ if (properties.length === 0)
250
+ return '{}';
251
+ const entries = properties.map((p) => {
252
+ const value = this.build(p.type, { indent: state.indent + 2, name: p.name, fallback: p.optional ? 'undefined' : undefined });
253
+ return `${spaces(state.indent + 2)}${(0, naming_1.propertyKey)(p.name)}: ${value === CUT ? ANY : value}`;
254
+ });
255
+ return block('{', entries, '}', state.indent);
256
+ }
257
+ union(members, state) {
258
+ if (isLiteralUnion(members)) {
259
+ const literals = members.flatMap((m) => (m.kind === 'literal' && m.value !== null ? [(0, naming_1.literal)(m.value)] : []));
260
+ return literals.length > 0 ? oneOf(literals, true) : 'null';
261
+ }
262
+ const memberState = members.some(isNull) ? Object.assign(Object.assign({}, state), { fallback: 'null' }) : state;
263
+ for (const member of nonNull(members)) {
264
+ const value = this.build(member, memberState);
265
+ if (value !== CUT)
266
+ return value;
267
+ }
268
+ if (members.some(isNull))
269
+ return 'null';
270
+ return members.length > 0 ? this.build(members[0], state) : ANY;
271
+ }
272
+ intersection(members, state) {
273
+ if (members.every((m) => m.kind === 'object' || m.kind === 'record')) {
274
+ const openRecords = members.every((m) => m.kind !== 'record' || isTop(m.values));
275
+ const properties = members.flatMap((m) => (m.kind === 'object' ? m.properties : [])).filter((p) => openRecords || !p.optional);
276
+ return this.object(properties, state);
277
+ }
278
+ const parts = [];
279
+ for (const member of members) {
280
+ if (!this.spreadable(member))
281
+ continue;
282
+ const value = this.build(member, { indent: state.indent + 2 });
283
+ if (value !== CUT)
284
+ parts.push(`${spaces(state.indent + 2)}...${value}`);
285
+ }
286
+ if (parts.length === 0)
287
+ return ANY;
288
+ return `(${block('{', parts, '}', state.indent)} as any)`;
289
+ }
290
+ spreadable(type) {
291
+ if (type.kind === 'ref') {
292
+ const target = this.file.shapeOf(type.id);
293
+ return target !== undefined && target.kind !== 'ref' && this.spreadable(target);
294
+ }
295
+ return type.kind === 'object' || type.kind === 'record' || type.kind === 'intersection';
296
+ }
297
+ }
298
+ function scalarKind(type) {
299
+ if (type.name === 'string')
300
+ return 'string';
301
+ const source = type.source;
302
+ return (source === null || source === void 0 ? void 0 : source.type) === 'integer' || (source === null || source === void 0 ? void 0 : source.format) === 'int32' || (source === null || source === void 0 ? void 0 : source.format) === 'int64' ? 'integer' : 'number';
303
+ }
304
+ const isNull = (t) => (t.kind === 'keyword' && t.name === 'null') || (t.kind === 'literal' && t.value === null);
305
+ const isTop = (t) => t.kind === 'keyword' && (t.name === 'any' || t.name === 'unknown');
306
+ function isOnlyNull(t) {
307
+ if (t.kind === 'union')
308
+ return t.members.length > 0 && t.members.every(isOnlyNull);
309
+ return isNull(t);
310
+ }
311
+ const nonNull = (members) => members.filter((m) => !isOnlyNull(m));
312
+ const isLiteralUnion = (members) => members.length > 0 && members.every((m) => m.kind === 'literal' || isNull(m));
313
+ function oneOf(choices, asConst) {
314
+ if (choices.length === 0)
315
+ return ANY;
316
+ if (choices.length === 1)
317
+ return choices[0];
318
+ return `faker.helpers.arrayElement([${choices.join(', ')}]${asConst ? ' as const' : ''})`;
319
+ }
320
+ function memberAccess(owner, key) {
321
+ return (0, naming_1.isIdentifier)(key) ? `${owner}.${key}` : `${owner}[${key}]`;
322
+ }
323
+ function block(open, lines, close, indent) {
324
+ return `${open}\n${lines.join(',\n')},\n${spaces(indent)}${close}`;
325
+ }
326
+ function spaces(count) {
327
+ return ' '.repeat(count);
328
+ }
329
+ class BuilderImports {
330
+ constructor(file, reserved) {
331
+ this.file = file;
332
+ this.byKey = new Map();
333
+ this.locals = new Set(reserved);
334
+ }
335
+ local(id) {
336
+ const name = `create${this.file.locator.typeName(id)}Mock`;
337
+ const target = path.join(this.file.mockRoot, ...`${this.file.locator.modulePath(id)}.mock`.split('/'));
338
+ const specifier = (0, imports_1.relativeSpecifier)(this.file.fromFile, target);
339
+ const key = `${specifier}#${name}`;
340
+ const existing = this.byKey.get(key);
341
+ if (existing)
342
+ return existing.local;
343
+ const local = this.freeLocal(id);
344
+ this.locals.add(local);
345
+ this.byKey.set(key, { specifier, name, local });
346
+ return local;
347
+ }
348
+ list() {
349
+ return Array.from(this.byKey.values())
350
+ .sort((a, b) => a.name.localeCompare(b.name) || a.specifier.localeCompare(b.specifier))
351
+ .map((e) => ({ specifier: e.specifier, typeOnly: false, names: e.local === e.name ? e.name : `${e.name} as ${e.local}` }));
352
+ }
353
+ freeLocal(id) {
354
+ var _a;
355
+ var _b;
356
+ const typeName = this.file.locator.typeName(id);
357
+ const namespace = (_b = (_a = this.file.locator.model(id)) === null || _a === void 0 ? void 0 : _a.namespace) !== null && _b !== void 0 ? _b : [];
358
+ const candidates = [`create${typeName}Mock`];
359
+ for (let k = 1; k <= namespace.length; k++) {
360
+ candidates.push((0, imports_1.toIdentifier)(`create${namespace.slice(-k).map(naming_1.upperFirst).join('')}${typeName}Mock`));
361
+ }
362
+ const free = candidates.find((c) => !this.locals.has(c));
363
+ if (free)
364
+ return free;
365
+ const base = candidates[candidates.length - 1];
366
+ let i = 2;
367
+ while (this.locals.has(`${base}${i}`))
368
+ i++;
369
+ return `${base}${i}`;
370
+ }
371
+ }
@@ -0,0 +1,85 @@
1
+ import type { OutputPlugin } from '../../../core/plugin';
2
+ import type { Model } from '../../../ir/types';
3
+ import { ImportCollector, type ImportDeclaration, type ModelLocator } from '../imports';
4
+ import { type ConstMember } from '../naming';
5
+ import { type ModelsOptions } from '../options';
6
+ import { type PrintOptions } from '../type-printer';
7
+ import { type TsObject, type TsType } from '../ts-type';
8
+ export interface ImportView {
9
+ specifier: string;
10
+ typeOnly: boolean;
11
+ names: string;
12
+ }
13
+ export interface ConstView {
14
+ name: string;
15
+ asConst: boolean;
16
+ members: ConstMember[];
17
+ }
18
+ export interface PropertyView {
19
+ key: string;
20
+ optional: boolean;
21
+ type: string;
22
+ doc: string[] | null;
23
+ }
24
+ interface FileViewBase {
25
+ header: string | null;
26
+ name: string;
27
+ doc: string[] | null;
28
+ imports: ImportView[];
29
+ }
30
+ export interface ObjectModelView extends FileViewBase {
31
+ kind: 'object';
32
+ consts: ConstView[];
33
+ properties: PropertyView[];
34
+ suffix: string;
35
+ }
36
+ export interface EnumModelView extends FileViewBase {
37
+ kind: 'enum';
38
+ style: ModelsOptions['enumSchemas'];
39
+ members: ConstMember[];
40
+ union: string;
41
+ }
42
+ export interface AliasModelView extends FileViewBase {
43
+ kind: 'alias';
44
+ type: string;
45
+ }
46
+ export type ModelView = ObjectModelView | EnumModelView | AliasModelView;
47
+ export interface ExportStatementView {
48
+ keyword: string;
49
+ names: string;
50
+ specifier: string;
51
+ }
52
+ export interface IndexView {
53
+ header: string | null;
54
+ exports: ExportStatementView[];
55
+ }
56
+ export declare const modelsOutput: OutputPlugin<ModelsOptions>;
57
+ export declare function printOptionsFor(options: ModelsOptions): PrintOptions;
58
+ export type ModelShape = ObjectModelShape | EnumModelShape | AliasModelShape;
59
+ export interface ObjectModelShape {
60
+ kind: 'object';
61
+ type: TsType;
62
+ object: TsObject;
63
+ additional?: TsType;
64
+ consts: ConstView[];
65
+ }
66
+ export interface EnumModelShape {
67
+ kind: 'enum';
68
+ style: ModelsOptions['enumSchemas'];
69
+ members: ConstMember[];
70
+ type: TsType;
71
+ }
72
+ export interface AliasModelShape {
73
+ kind: 'alias';
74
+ type: TsType;
75
+ }
76
+ export declare function resolveModelShape(model: Model, locator: ModelLocator, options: ModelsOptions, imports?: ImportCollector): ModelShape;
77
+ export declare function buildModelView(model: Model, locator: ModelLocator, options: ModelsOptions, fromFile: string, seedImports?: (imports: ImportCollector) => void): ModelView;
78
+ export declare function importViews(decls: ImportDeclaration[]): ImportView[];
79
+ interface IndexEntry {
80
+ modulePath: string;
81
+ typeNames: string[];
82
+ valueNames: string[];
83
+ }
84
+ export declare function buildIndexView(entries: IndexEntry[], options: ModelsOptions, warn: (msg: string) => void): IndexView;
85
+ export {};