Skip to content

Commit 15fa2b2

Browse files
committed
static to isStatic, parseSchema pass options
1 parent a3a5c54 commit 15fa2b2

File tree

7 files changed

+36
-35
lines changed

7 files changed

+36
-35
lines changed

src/cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cli
4545
const options: Options = {
4646
path: path,
4747
...userOptions,
48+
isStatic: userOptions.static,
4849
fakerLocale: userOptions.locales,
4950
includeCodes: userOptions.includeCodes
5051
? userOptions.includeCodes

src/defaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const defaultOptions: Options = {
66
baseDir: "./",
77
arrayMinLength: 1,
88
arrayMaxLength: 3,
9-
static: true,
9+
isStatic: true,
1010
handlerUrl: "*",
1111
fakerLocale: "ko",
1212
generateTarget: "api,schema",

src/generate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const generateSchema = async (options: Options) => {
4040
const specialFakers = specialFakerParser(options)
4141
return Object.entries(sampleSchemas).reduce(
4242
(acc, [schemaName, schema]) => {
43-
acc[schemaName] = parseSchema(schema, specialFakers, options.static, {}) as SchemaOutputType
43+
acc[schemaName] = parseSchema(schema, specialFakers, options, {}) as SchemaOutputType
4444
return acc
4545
},
4646
{} as Record<string, SchemaOutputType>
@@ -99,7 +99,7 @@ export const generateAPI = async (options: Options) => {
9999
// empty object return undefined
100100
return undefined
101101
}
102-
return parseSchema(schema ?? {}, specialFakers, options.static, {})
102+
return parseSchema(schema ?? {}, specialFakers, options, {})
103103
})()
104104

105105
return {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export const main = async (options: Options) => {
2727
writeSchema(generatedSchema, options)
2828
}
2929

30-
if (options.static === false) writeFaker(options)
30+
if (options.isStatic === false) writeFaker(options)
3131
}

src/parser.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { join } from "path"
2323
export const parseSchema = (
2424
schemaValue: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject,
2525
specialSchema: ReturnType<typeof specialFakerParser>,
26-
isStatic: boolean,
26+
options: Options,
2727
outputSchema: ParseSchemaType = {}
2828
): ParseSchemaType => {
2929
if (isReference(schemaValue)) {
@@ -35,45 +35,45 @@ export const parseSchema = (
3535
if (schemaValue.properties === undefined) return {}
3636
return Object.entries(schemaValue.properties).reduce(
3737
(acc, [key, field]) => {
38-
acc[key] = parseSchema(field, specialSchema, isStatic, outputSchema) as SchemaOutputType
38+
acc[key] = parseSchema(field, specialSchema, options, outputSchema) as SchemaOutputType
3939
return acc
4040
},
4141
{} as Record<string, SchemaOutputType>
4242
)
4343
} else if (schemaValue.enum !== undefined) {
4444
// enum value
45-
const enumValue = isStatic
45+
const enumValue = options.isStatic
4646
? faker.helpers.arrayElement(schemaValue.enum)
4747
: `faker.helpers.arrayElement(${toUnquotedJSON(schemaValue.enum, {
4848
depth: 0,
49-
isStatic,
49+
isStatic: options.isStatic,
5050
singleLine: true,
5151
})})`
52-
if (isStatic && typeof enumValue === "string") return enumValue + " as const"
52+
if (options.isStatic && typeof enumValue === "string") return enumValue + " as const"
5353
return enumValue
5454
} else if (schemaValue.allOf !== undefined) {
5555
// allOf value, sub model
5656
const allOfValue = schemaValue.allOf
5757
return faker.helpers.arrayElement(
5858
allOfValue.map((field) => {
59-
return parseSchema(field, specialSchema, isStatic, outputSchema)
59+
return parseSchema(field, specialSchema, options, outputSchema)
6060
})
6161
)
6262
} else if (schemaValue.anyOf !== undefined) {
6363
// anyOf value, select one or more. ex) string or null
6464
const anyOfValue = schemaValue.anyOf
65-
return isStatic
65+
return options.isStatic
6666
? faker.helpers.arrayElement(
6767
anyOfValue.map((field) => {
68-
return parseSchema(field, specialSchema, isStatic, outputSchema)
68+
return parseSchema(field, specialSchema, options, outputSchema)
6969
})
7070
)
7171
: multiLineStr(`
7272
faker.helpers.arrayElement([
7373
${anyOfValue.map((field) =>
74-
toUnquotedJSON(parseSchema(field, specialSchema, isStatic, {}), {
74+
toUnquotedJSON(parseSchema(field, specialSchema, options, {}), {
7575
depth: 0,
76-
isStatic,
76+
isStatic: options.isStatic,
7777
singleLine: true,
7878
})
7979
)}
@@ -82,18 +82,18 @@ export const parseSchema = (
8282
} else if (schemaValue.oneOf !== undefined) {
8383
// oneOf value, exactly one. Can't find example
8484
const oneOfValue = schemaValue.oneOf
85-
return isStatic
85+
return options.isStatic
8686
? faker.helpers.arrayElement(
8787
oneOfValue.map((field) => {
88-
return parseSchema(field, specialSchema, isStatic, outputSchema)
88+
return parseSchema(field, specialSchema, options, outputSchema)
8989
})
9090
)
9191
: multiLineStr(`
9292
faker.helpers.arrayElement([
9393
${oneOfValue.map((field) =>
94-
toUnquotedJSON(parseSchema(field, specialSchema, isStatic, {}), {
94+
toUnquotedJSON(parseSchema(field, specialSchema, options, {}), {
9595
depth: 0,
96-
isStatic,
96+
isStatic: options.isStatic,
9797
singleLine: true,
9898
})
9999
)}
@@ -102,11 +102,11 @@ export const parseSchema = (
102102
} else if (schemaValue.type === "array") {
103103
// array
104104
const arrayValue = schemaValue.items
105-
return getRandomLengthArray().map(() =>
106-
parseSchema(arrayValue, specialSchema, isStatic, outputSchema)
105+
return getRandomLengthArray(options.arrayMinLength, options.arrayMaxLength).map(() =>
106+
parseSchema(arrayValue, specialSchema, options, outputSchema)
107107
) as (SchemaOutputType | Record<string, SchemaOutputType>)[]
108108
}
109-
return valueGenerator(schemaValue, specialSchema, isStatic)
109+
return valueGenerator(schemaValue, specialSchema, options.isStatic)
110110
}
111111

112112
const uuidToB64 = (uuid: string) => {
@@ -317,7 +317,7 @@ export const specialFakerParser = (options: Options) => {
317317
const titleSpecial = Object.entries(titleSpecialKey).reduce(
318318
(acc, [key, value]) => {
319319
const fakerValue = getFakerValue(value, {
320-
isStatic: options.static,
320+
isStatic: options.isStatic,
321321
})
322322
acc[key] = fakerValue
323323
return acc
@@ -328,7 +328,7 @@ export const specialFakerParser = (options: Options) => {
328328
const descriptionSpecial = Object.entries(descriptionSpecialKey).reduce(
329329
(acc, [key, value]) => {
330330
const fakerValue = getFakerValue(value, {
331-
isStatic: options.static,
331+
isStatic: options.isStatic,
332332
})
333333
acc[key] = fakerValue
334334
return acc

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type Options = {
44
path: string
55
arrayMinLength?: number
66
arrayMaxLength?: number
7-
static: boolean
7+
isStatic: boolean
88
includeCodes?: number[]
99
baseDir?: string
1010
specialPath?: string

src/writer.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ export const writeResponses = async (paths: PathNormalizedType[], options: Optio
142142
]
143143
if (res.schema?.type === "ref") {
144144
const { name, value } = refSchemaParser(res.schema.value.$ref, refs)
145-
const outputSchema = parseSchema(value, specialFakers, options.static)
145+
const outputSchema = parseSchema(value, specialFakers, options)
146146
codeBaseArray.push(` // Schema is ${name}`)
147147
codeBaseArray.push(
148148
` return ${toUnquotedJSON(outputSchema, {
149149
depth: 1,
150-
isStatic: options.static,
150+
isStatic: options.isStatic,
151151
})}`
152152
)
153153
} else if (res.schema?.type === "array") {
@@ -156,36 +156,36 @@ export const writeResponses = async (paths: PathNormalizedType[], options: Optio
156156
const outputSchema = getRandomLengthArray(
157157
options.arrayMinLength,
158158
options.arrayMaxLength
159-
).map(() => parseSchema(value, specialFakers, options.static))
159+
).map(() => parseSchema(value, specialFakers, options))
160160
codeBaseArray.push(` // Schema is ${name} array`)
161161
codeBaseArray.push(
162162
` return ${toUnquotedJSON(outputSchema, {
163163
depth: 1,
164-
isStatic: options.static,
164+
isStatic: options.isStatic,
165165
})}`
166166
)
167167
} else {
168168
const outputSchema = getRandomLengthArray(
169169
options.arrayMinLength,
170170
options.arrayMaxLength
171-
).map(() => res.schema && parseSchema(res.schema.value, specialFakers, options.static))
171+
).map(() => res.schema && parseSchema(res.schema.value, specialFakers, options))
172172
codeBaseArray.push(
173173
` return ${toUnquotedJSON(outputSchema, {
174174
depth: 1,
175-
isStatic: options.static,
175+
isStatic: options.isStatic,
176176
})}`
177177
)
178178
}
179179
} else if (res.schema?.type === "anyOf") {
180180
const firstSchema = res.schema.value.anyOf?.[0]
181181
if (isReference(firstSchema)) {
182182
const { name, value } = refSchemaParser(firstSchema.$ref, refs)
183-
const outputSchema = parseSchema(value, specialFakers, options.static)
183+
const outputSchema = parseSchema(value, specialFakers, options)
184184
codeBaseArray.push(` // Schema is ${name}`)
185185
codeBaseArray.push(
186186
` return ${toUnquotedJSON(outputSchema, {
187187
depth: 1,
188-
isStatic: options.static,
188+
isStatic: options.isStatic,
189189
})}`
190190
)
191191
} else {
@@ -207,7 +207,7 @@ export const writeResponses = async (paths: PathNormalizedType[], options: Optio
207207
}
208208

209209
Object.entries(codeBasePerTag).forEach(([tag, responses]) => {
210-
const importFaker = options.static ? "" : 'import { faker } from "../fakers"\n\n'
210+
const importFaker = options.isStatic ? "" : 'import { faker } from "../fakers"\n\n'
211211

212212
const fileName = `${directory}/${tag}.ts`
213213
writeFileSync(fileName, importFaker + responses.join("\n\n"))
@@ -235,12 +235,12 @@ export const writeSchema = (schemas: Record<string, SchemaOutputType>, options:
235235
const generatedVars = Object.entries(schemas)
236236
.map(([varName, varValue]) => {
237237
return `export const ${varName}Mock = ${toUnquotedJSON(varValue, {
238-
isStatic: options.static,
238+
isStatic: options.isStatic,
239239
})}`
240240
})
241241
.join("\n\n")
242242

243-
const importFaker = options.static ? "" : 'import { faker } from "./fakers"\n\n'
243+
const importFaker = options.isStatic ? "" : 'import { faker } from "./fakers"\n\n'
244244

245245
const outputFileName = path.join(`${options.baseDir}`, "schemas.ts")
246246
writeFileSync(outputFileName, importFaker + generatedVars)

0 commit comments

Comments
 (0)