@kubb/plugin-faker 0.0.0-canary-20241104172400

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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/dist/chunk-FCIZHQ4X.cjs +339 -0
  4. package/dist/chunk-FCIZHQ4X.cjs.map +1 -0
  5. package/dist/chunk-OOL6ZKDZ.cjs +115 -0
  6. package/dist/chunk-OOL6ZKDZ.cjs.map +1 -0
  7. package/dist/chunk-PLVSF3YP.js +333 -0
  8. package/dist/chunk-PLVSF3YP.js.map +1 -0
  9. package/dist/chunk-ZOJMDISS.js +113 -0
  10. package/dist/chunk-ZOJMDISS.js.map +1 -0
  11. package/dist/components.cjs +12 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +20 -0
  14. package/dist/components.d.ts +20 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +13 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +8 -0
  20. package/dist/generators.d.ts +8 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +115 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +9 -0
  26. package/dist/index.d.ts +9 -0
  27. package/dist/index.js +108 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-Cq8z4Gto.d.cts +95 -0
  30. package/dist/types-Cq8z4Gto.d.ts +95 -0
  31. package/package.json +98 -0
  32. package/src/components/Faker.tsx +82 -0
  33. package/src/components/index.ts +1 -0
  34. package/src/generators/__snapshots__/createPet.ts +26 -0
  35. package/src/generators/__snapshots__/createPetSeed.ts +30 -0
  36. package/src/generators/__snapshots__/createPetUnknownTypeAny.ts +26 -0
  37. package/src/generators/__snapshots__/deletePet.ts +3 -0
  38. package/src/generators/__snapshots__/enumNames.ts +5 -0
  39. package/src/generators/__snapshots__/enumVarNames.ts +5 -0
  40. package/src/generators/__snapshots__/getPets.ts +26 -0
  41. package/src/generators/__snapshots__/pet.ts +8 -0
  42. package/src/generators/__snapshots__/petWithDateString.ts +8 -0
  43. package/src/generators/__snapshots__/petWithDayjs.ts +9 -0
  44. package/src/generators/__snapshots__/petWithMapper.ts +8 -0
  45. package/src/generators/__snapshots__/petWithRandExp.ts +9 -0
  46. package/src/generators/__snapshots__/pets.ts +8 -0
  47. package/src/generators/__snapshots__/showPetById.ts +26 -0
  48. package/src/generators/fakerGenerator.tsx +140 -0
  49. package/src/generators/index.ts +1 -0
  50. package/src/index.ts +2 -0
  51. package/src/parser/index.ts +351 -0
  52. package/src/plugin.ts +128 -0
  53. package/src/types.ts +92 -0
@@ -0,0 +1,26 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ /**
4
+ * @description Null response
5
+ */
6
+ export function createPets201() {
7
+ return unknown;
8
+ }
9
+
10
+ /**
11
+ * @description unexpected error
12
+ */
13
+ export function createPetsError() {
14
+ return error();
15
+ }
16
+
17
+ export function createPetsMutationRequest(data?: Partial<CreatePetsMutationRequest>) {
18
+ return {
19
+ ...{ "name": faker.string.alpha(), "tag": faker.string.alpha() },
20
+ ...data || {}
21
+ };
22
+ }
23
+
24
+ export function createPetsMutationResponse(data?: Partial<CreatePetsMutationResponse>) {
25
+ return faker.helpers.arrayElement<any>([createPets201()]) || data;
26
+ }
@@ -0,0 +1,30 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ /**
4
+ * @description Null response
5
+ */
6
+ export function createPets201() {
7
+ faker.seed([222]);
8
+ return unknown;
9
+ }
10
+
11
+ /**
12
+ * @description unexpected error
13
+ */
14
+ export function createPetsError() {
15
+ faker.seed([222]);
16
+ return error();
17
+ }
18
+
19
+ export function createPetsMutationRequest(data?: Partial<CreatePetsMutationRequest>) {
20
+ faker.seed([222]);
21
+ return {
22
+ ...{ "name": faker.string.alpha(), "tag": faker.string.alpha() },
23
+ ...data || {}
24
+ };
25
+ }
26
+
27
+ export function createPetsMutationResponse(data?: Partial<CreatePetsMutationResponse>) {
28
+ faker.seed([222]);
29
+ return faker.helpers.arrayElement<any>([createPets201()]) || data;
30
+ }
@@ -0,0 +1,26 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ /**
4
+ * @description Null response
5
+ */
6
+ export function createPets201() {
7
+ return undefined;
8
+ }
9
+
10
+ /**
11
+ * @description unexpected error
12
+ */
13
+ export function createPetsError() {
14
+ return error();
15
+ }
16
+
17
+ export function createPetsMutationRequest(data?: Partial<CreatePetsMutationRequest>) {
18
+ return {
19
+ ...{ "name": faker.string.alpha(), "tag": faker.string.alpha() },
20
+ ...data || {}
21
+ };
22
+ }
23
+
24
+ export function createPetsMutationResponse(data?: Partial<CreatePetsMutationResponse>) {
25
+ return faker.helpers.arrayElement<any>([createPets201()]) || data;
26
+ }
@@ -0,0 +1,3 @@
1
+ export function deletePetsPetidMutationResponse() {
2
+ return unknown;
3
+ }
@@ -0,0 +1,5 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function enumNamesType() {
4
+ return faker.helpers.arrayElement<any>(["Pending", "Received"]);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function enumVarNamesType() {
4
+ return faker.helpers.arrayElement<any>(["Pending", "Received"]);
5
+ }
@@ -0,0 +1,26 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function listPetsQueryParams(data?: Partial<ListPetsQueryParams>) {
4
+ return {
5
+ ...{ "limit": faker.string.alpha() },
6
+ ...data || {}
7
+ };
8
+ }
9
+
10
+ /**
11
+ * @description A paged array of pets
12
+ */
13
+ export function listPets200() {
14
+ return pets();
15
+ }
16
+
17
+ /**
18
+ * @description unexpected error
19
+ */
20
+ export function listPetsError() {
21
+ return error();
22
+ }
23
+
24
+ export function listPetsQueryResponse(data?: Partial<ListPetsQueryResponse>) {
25
+ return faker.helpers.arrayElement<any>([listPets200()]) || data;
26
+ }
@@ -0,0 +1,8 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function pet(data?: Partial<Pet>) {
4
+ return {
5
+ ...{ "id": faker.number.int(), "name": faker.string.alpha(), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), faker.helpers.fromRegExp(new RegExp("\\b[1-9]\\b"))]), "shipDate": faker.date.anytime(), "shipTime": faker.date.anytime() },
6
+ ...data || {}
7
+ };
8
+ }
@@ -0,0 +1,8 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function pet(data?: Partial<Pet>) {
4
+ return {
5
+ ...{ "id": faker.number.int(), "name": faker.string.alpha(), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), faker.helpers.fromRegExp(new RegExp("\\b[1-9]\\b"))]), "shipDate": faker.date.anytime().toString(), "shipTime": faker.date.anytime().toString() },
6
+ ...data || {}
7
+ };
8
+ }
@@ -0,0 +1,9 @@
1
+ import dayjs from "dayjs";
2
+ import { faker } from "@faker-js/faker";
3
+
4
+ export function pet(data?: Partial<Pet>) {
5
+ return {
6
+ ...{ "id": faker.number.int(), "name": faker.string.alpha(), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), faker.helpers.fromRegExp(new RegExp("\\b[1-9]\\b"))]), "shipDate": dayjs(faker.date.anytime()).format("YYYY-MM-DD"), "shipTime": dayjs(faker.date.anytime()).format("HH:mm:ss") },
7
+ ...data || {}
8
+ };
9
+ }
@@ -0,0 +1,8 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function pet(data?: Partial<Pet>) {
4
+ return {
5
+ ...{ "id": faker.string.fromCharacters("abc"), "name": faker.string.alpha({ casing: "lower" }), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), faker.helpers.fromRegExp(new RegExp("\\b[1-9]\\b"))]), "shipDate": faker.date.anytime(), "shipTime": faker.date.anytime() },
6
+ ...data || {}
7
+ };
8
+ }
@@ -0,0 +1,9 @@
1
+ import RandExp from "randexp";
2
+ import { faker } from "@faker-js/faker";
3
+
4
+ export function pet(data?: Partial<Pet>) {
5
+ return {
6
+ ...{ "id": faker.number.int(), "name": faker.string.alpha(), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), new RandExp("\\b[1-9]\\b").gen()]), "shipDate": faker.date.anytime(), "shipTime": faker.date.anytime() },
7
+ ...data || {}
8
+ };
9
+ }
@@ -0,0 +1,8 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function pets(data?: Partial<Pets>) {
4
+ return [
5
+ ...faker.helpers.multiple(() => (pet())) as any,
6
+ ...data || []
7
+ ];
8
+ }
@@ -0,0 +1,26 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ export function showPetByIdPathParams(data?: Partial<ShowPetByIdPathParams>) {
4
+ return {
5
+ ...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
6
+ ...data || {}
7
+ };
8
+ }
9
+
10
+ /**
11
+ * @description Expected response to a valid request
12
+ */
13
+ export function showPetById200() {
14
+ return pet();
15
+ }
16
+
17
+ /**
18
+ * @description unexpected error
19
+ */
20
+ export function showPetByIdError() {
21
+ return error();
22
+ }
23
+
24
+ export function showPetByIdQueryResponse(data?: Partial<ShowPetByIdQueryResponse>) {
25
+ return faker.helpers.arrayElement<any>([showPetById200()]) || data;
26
+ }
@@ -0,0 +1,140 @@
1
+ import { type OperationSchema as OperationSchemaType, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
2
+ import { Oas } from '@kubb/plugin-oas/components'
3
+ import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
4
+ import { pluginTsName } from '@kubb/plugin-ts'
5
+ import { File, useApp } from '@kubb/react'
6
+ import { Faker } from '../components'
7
+ import type { PluginFaker } from '../types'
8
+
9
+ export const fakerGenerator = createReactGenerator<PluginFaker>({
10
+ name: 'faker',
11
+ Operation({ operation, options }) {
12
+ const { dateParser, regexGenerator, seed, mapper } = options
13
+
14
+ const { plugin, pluginManager, mode } = useApp<PluginFaker>()
15
+ const oas = useOas()
16
+ const { getSchemas, getFile } = useOperationManager()
17
+ const schemaManager = useSchemaManager()
18
+
19
+ const file = getFile(operation)
20
+ const schemas = getSchemas(operation)
21
+ const schemaGenerator = new SchemaGenerator(options, {
22
+ oas,
23
+ plugin,
24
+ pluginManager,
25
+ mode,
26
+ override: options.override,
27
+ })
28
+
29
+ const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
30
+ .flat()
31
+ .filter(Boolean)
32
+
33
+ const mapOperationSchema = ({ name, schema, description, ...options }: OperationSchemaType, i: number) => {
34
+ const tree = schemaGenerator.parse({ schema, name })
35
+ const imports = schemaManager.getImports(tree)
36
+
37
+ const faker = {
38
+ name: schemaManager.getName(name, { type: 'function' }),
39
+ file: schemaManager.getFile(name),
40
+ }
41
+
42
+ const type = {
43
+ name: schemaManager.getName(name, { type: 'type', pluginKey: [pluginTsName] }),
44
+ file: schemaManager.getFile(options.operationName || name, { pluginKey: [pluginTsName], tag: options.operation?.getTags()[0]?.name }),
45
+ }
46
+
47
+ const canOverride = tree.some(
48
+ ({ keyword }) =>
49
+ keyword === schemaKeywords.array ||
50
+ keyword === schemaKeywords.and ||
51
+ keyword === schemaKeywords.object ||
52
+ keyword === schemaKeywords.union ||
53
+ keyword === schemaKeywords.tuple,
54
+ )
55
+
56
+ return (
57
+ <Oas.Schema key={i} name={name} value={schema} tree={tree}>
58
+ {canOverride && <File.Import isTypeOnly root={file.path} path={type.file.path} name={[type.name]} />}
59
+ {imports.map((imp, index) => (
60
+ <File.Import key={index} root={file.path} path={imp.path} name={imp.name} />
61
+ ))}
62
+ <Faker
63
+ name={faker.name}
64
+ typeName={type.name}
65
+ description={description}
66
+ tree={tree}
67
+ regexGenerator={regexGenerator}
68
+ dateParser={dateParser}
69
+ mapper={mapper}
70
+ seed={seed}
71
+ canOverride={canOverride}
72
+ />
73
+ </Oas.Schema>
74
+ )
75
+ }
76
+
77
+ return (
78
+ <File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
79
+ <File.Import name={['faker']} path="@faker-js/faker" />
80
+ {regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
81
+ {dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
82
+ {operationSchemas.map(mapOperationSchema)}
83
+ </File>
84
+ )
85
+ },
86
+ Schema({ schema, options }) {
87
+ const { dateParser, regexGenerator, seed, mapper } = options
88
+
89
+ const { getName, getFile, getImports } = useSchemaManager()
90
+ const {
91
+ plugin: {
92
+ options: { output },
93
+ },
94
+ } = useApp<PluginFaker>()
95
+ const imports = getImports(schema.tree)
96
+
97
+ const faker = {
98
+ name: getName(schema.name, { type: 'function' }),
99
+ file: getFile(schema.name),
100
+ }
101
+
102
+ const type = {
103
+ name: getName(schema.name, { type: 'type', pluginKey: [pluginTsName] }),
104
+ file: getFile(schema.name, { pluginKey: [pluginTsName] }),
105
+ }
106
+
107
+ const canOverride = schema.tree.some(
108
+ ({ keyword }) =>
109
+ keyword === schemaKeywords.array ||
110
+ keyword === schemaKeywords.and ||
111
+ keyword === schemaKeywords.object ||
112
+ keyword === schemaKeywords.union ||
113
+ keyword === schemaKeywords.tuple,
114
+ )
115
+
116
+ return (
117
+ <File baseName={faker.file.baseName} path={faker.file.path} meta={faker.file.meta} banner={output?.banner} footer={output?.footer}>
118
+ <File.Import name={['faker']} path="@faker-js/faker" />
119
+ {regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
120
+ {dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
121
+ <File.Import isTypeOnly root={faker.file.path} path={type.file.path} name={[type.name]} />
122
+ {imports.map((imp, index) => (
123
+ <File.Import key={index} root={faker.file.path} path={imp.path} name={imp.name} />
124
+ ))}
125
+
126
+ <Faker
127
+ name={faker.name}
128
+ typeName={type.name}
129
+ description={schema.value.description}
130
+ tree={schema.tree}
131
+ regexGenerator={regexGenerator}
132
+ dateParser={dateParser}
133
+ mapper={mapper}
134
+ seed={seed}
135
+ canOverride={canOverride}
136
+ />
137
+ </File>
138
+ )
139
+ },
140
+ })
@@ -0,0 +1 @@
1
+ export { fakerGenerator } from './fakerGenerator.tsx'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { pluginFaker, pluginFakerName } from './plugin.ts'
2
+ export type { PluginFaker } from './types.ts'