@kubb/plugin-faker 5.0.0-beta.10 → 5.0.0-beta.100

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.
package/src/utils.ts DELETED
@@ -1,268 +0,0 @@
1
- import { posix } from 'node:path'
2
- import { ast } from '@kubb/core'
3
- import type { ResolverFaker } from './types.ts'
4
-
5
- /**
6
- * Returns the `@faker-js/faker` named export for a locale code.
7
- *
8
- * Without a locale, returns `'faker'` for the default English instance.
9
- * With a locale, the language code is converted to upper case and joined with any region suffix.
10
- *
11
- * @example Default
12
- * `localeToFakerImport() // 'faker'`
13
- *
14
- * @example Simple locale
15
- * `localeToFakerImport('de') // 'fakerDE'`
16
- *
17
- * @example Compound locale
18
- * `localeToFakerImport('de_AT') // 'fakerDE_AT'`
19
- */
20
- export function localeToFakerImport(locale?: string): string {
21
- if (!locale) {
22
- return 'faker'
23
- }
24
-
25
- const parts = locale.split('_')
26
- parts[0] = parts[0]!.toUpperCase()
27
- return `faker${parts.join('_')}`
28
- }
29
-
30
- /**
31
- * Determines if a schema node can be overridden during faker generation.
32
- */
33
- export function canOverrideSchema(node: ast.SchemaNode): boolean {
34
- return new Set<ast.SchemaNode['type']>([
35
- 'array',
36
- 'tuple',
37
- 'object',
38
- 'intersection',
39
- 'union',
40
- 'enum',
41
- 'ref',
42
- 'string',
43
- 'email',
44
- 'url',
45
- 'uuid',
46
- 'number',
47
- 'integer',
48
- 'bigint',
49
- 'boolean',
50
- 'date',
51
- 'time',
52
- 'datetime',
53
- 'blob',
54
- ]).has(node.type)
55
- }
56
-
57
- /**
58
- * Resolves a schema reference by looking up the referenced schema in the provided array.
59
- * Returns the original node if it's not a reference.
60
- */
61
- export function resolveSchemaRef(node: ast.SchemaNode, schemas: Array<ast.SchemaNode>): ast.SchemaNode {
62
- if (node.type !== 'ref') {
63
- return node
64
- }
65
-
66
- return schemas.find((schema) => schema.name === node.name && schema.type !== 'ref') ?? node
67
- }
68
-
69
- /**
70
- * Resolves a parameter name based on its location (path, query, header, etc.) using the provided resolver.
71
- */
72
- export function resolveParamNameByLocation(
73
- resolver: Pick<ResolverFaker, 'resolvePathParamsName' | 'resolveQueryParamsName' | 'resolveHeaderParamsName' | 'resolveParamName'>,
74
- node: ast.OperationNode,
75
- param: ast.ParameterNode,
76
- ): string {
77
- switch (param.in) {
78
- case 'path':
79
- return resolver.resolvePathParamsName(node, param)
80
- case 'query':
81
- return resolver.resolveQueryParamsName(node, param)
82
- case 'header':
83
- return resolver.resolveHeaderParamsName(node, param)
84
- default:
85
- return resolver.resolveParamName(node, param)
86
- }
87
- }
88
-
89
- function shouldInlineSingleResponseSchema(schema: ast.SchemaNode): boolean {
90
- return new Set<ast.SchemaNode['type']>([
91
- 'any',
92
- 'unknown',
93
- 'void',
94
- 'null',
95
- 'array',
96
- 'tuple',
97
- 'string',
98
- 'email',
99
- 'url',
100
- 'uuid',
101
- 'number',
102
- 'integer',
103
- 'bigint',
104
- 'boolean',
105
- 'date',
106
- 'time',
107
- 'datetime',
108
- 'blob',
109
- 'enum',
110
- 'union',
111
- ]).has(schema.type)
112
- }
113
-
114
- /**
115
- * Builds a response schema as a union of all response statuses.
116
- * Returns null if no responses are provided, or embeds single simple responses inline.
117
- */
118
- export function buildResponseUnionSchema(node: ast.OperationNode, resolver: ResolverFaker): ast.SchemaNode | null {
119
- const responses = node.responses.filter((response) => response.schema)
120
-
121
- if (!responses.length) {
122
- return null
123
- }
124
-
125
- if (responses.length === 1) {
126
- if (shouldInlineSingleResponseSchema(responses[0]!.schema)) {
127
- return responses[0]!.schema
128
- }
129
-
130
- return ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, responses[0]!.statusCode) })
131
- }
132
-
133
- return ast.createSchema({
134
- type: 'union',
135
- members: responses.map((response) => ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, response.statusCode) })),
136
- })
137
- }
138
-
139
- const SCALAR_TYPES = new Set<ast.SchemaNode['type']>([
140
- 'string',
141
- 'email',
142
- 'url',
143
- 'uuid',
144
- 'number',
145
- 'integer',
146
- 'bigint',
147
- 'boolean',
148
- 'date',
149
- 'time',
150
- 'datetime',
151
- 'blob',
152
- 'enum',
153
- ])
154
- const ARRAY_TYPES = new Set<ast.SchemaNode['type']>(['array'])
155
-
156
- function toRelativeImportPath(from: string, to: string): string {
157
- const relativePath = posix.relative(posix.dirname(from), to)
158
- return relativePath.startsWith('../') ? relativePath : `./${relativePath}`
159
- }
160
-
161
- /**
162
- * Resolves a type reference, determining if it needs an import statement or inline type reference.
163
- * Takes into account whether the type can be overridden and the file paths.
164
- */
165
- export function resolveTypeReference({
166
- node,
167
- canOverride,
168
- name,
169
- typeName,
170
- filePath,
171
- typeFilePath,
172
- }: {
173
- node: ast.SchemaNode
174
- canOverride: boolean
175
- name: string
176
- typeName: string
177
- filePath: string
178
- typeFilePath: string
179
- }): { importPath?: string; typeName: string } {
180
- const { usesTypeName } = resolveFakerTypeUsage(node, typeName, canOverride)
181
-
182
- if (!usesTypeName) {
183
- return { typeName }
184
- }
185
-
186
- if (name === typeName) {
187
- return {
188
- typeName: `import('${toRelativeImportPath(filePath, typeFilePath)}').${typeName}`,
189
- }
190
- }
191
-
192
- return {
193
- importPath: typeFilePath,
194
- typeName,
195
- }
196
- }
197
-
198
- /**
199
- * Maps a schema node type to its corresponding scalar type representation.
200
- * Returns the type name for enums or the base type (string, number, etc.) for primitives.
201
- */
202
- export function getScalarType(node: ast.SchemaNode, typeName: string): string {
203
- switch (node.type) {
204
- case 'string':
205
- case 'email':
206
- case 'url':
207
- case 'uuid':
208
- return 'string'
209
- case 'number':
210
- case 'integer':
211
- return 'number'
212
- case 'bigint':
213
- return 'bigint'
214
- case 'boolean':
215
- return 'boolean'
216
- case 'date':
217
- case 'time':
218
- return node.representation === 'date' ? 'Date' : 'string'
219
- case 'datetime':
220
- return 'string'
221
- case 'blob':
222
- return 'Blob'
223
- case 'enum':
224
- return typeName
225
- default:
226
- return typeName
227
- }
228
- }
229
-
230
- /**
231
- * Resolves faker type usage information for a schema.
232
- * Determines the data type, return type, and whether it uses the type name.
233
- */
234
- export function resolveFakerTypeUsage(
235
- node: ast.SchemaNode,
236
- typeName: string,
237
- canOverride: boolean,
238
- ): {
239
- dataType: string
240
- returnType: string | undefined
241
- usesTypeName: boolean
242
- } {
243
- const isArray = ARRAY_TYPES.has(node.type)
244
- const isTuple = node.type === 'tuple'
245
- const isScalar = SCALAR_TYPES.has(node.type)
246
-
247
- let dataType = `Partial<${typeName}>`
248
-
249
- if (isArray || isTuple || node.type === 'union' || node.type === 'enum') {
250
- dataType = typeName
251
- }
252
-
253
- if (isScalar) {
254
- dataType = getScalarType(node, typeName)
255
- }
256
-
257
- let returnType = canOverride ? typeName : undefined
258
-
259
- if (isScalar) {
260
- returnType = getScalarType(node, typeName)
261
- }
262
-
263
- return {
264
- dataType,
265
- returnType,
266
- usesTypeName: dataType.includes(typeName) || Boolean(returnType?.includes(typeName)),
267
- }
268
- }