fauxy 0.0.1-alpha.4 → 0.0.2

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/dist/index.d.ts CHANGED
@@ -15,114 +15,46 @@ type Count =
15
15
  /**
16
16
  * Mock config
17
17
  */
18
- type FakerType =
19
- | 'uuid'
20
- | 'boolean'
21
- | 'words'
22
- | 'number'
23
- | 'url'
24
- | 'fullName'
25
- | 'date'
26
- | 'personalNumber'
27
- | 'rank'
28
- | 'arrayElement'
29
- | 'arrayElements'
30
- | 'callback'
31
- | 'array'
32
- | 'object'
18
+ type FakerWrapperType = 'callback' | 'array' | 'object' | 'instance'
33
19
 
34
- interface BaseFakerConfig {
35
- type: FakerType
20
+ interface BaseFakerWrapperConfig {
21
+ type: FakerWrapperType
36
22
  }
37
23
 
38
- interface UuidFakerConfig extends BaseFakerConfig {
39
- type: 'uuid'
40
- }
41
-
42
- interface BooleanFakerConfig extends BaseFakerConfig {
43
- type: 'boolean'
44
- }
45
-
46
- interface WordsFakerConfig extends BaseFakerConfig {
47
- type: 'words'
48
- count?: Count
49
- }
50
-
51
- interface NumberFakerConfig extends BaseFakerConfig {
52
- type: 'number'
53
- min?: number
54
- max?: number
55
- }
56
-
57
- interface UrlFakerConfig extends BaseFakerConfig {
58
- type: 'url'
59
- }
60
-
61
- interface DateFakerConfig extends BaseFakerConfig {
62
- type: 'date'
63
- }
64
-
65
- interface FullNameFakerConfig extends BaseFakerConfig {
66
- type: 'fullName'
67
- }
68
-
69
- interface PersonalNumberFakerConfig extends BaseFakerConfig {
70
- type: 'personalNumber'
71
- }
72
-
73
- interface RankFakerConfig extends BaseFakerConfig {
74
- type: 'rank'
75
- }
76
-
77
- interface ArrayElementFakerConfig extends BaseFakerConfig {
78
- type: 'arrayElement'
79
- items: unknown[]
80
- }
81
-
82
- interface ArrayElementsFakerConfig extends BaseFakerConfig {
83
- type: 'arrayElements'
84
- items: unknown[]
85
- count?: Count
86
- }
87
-
88
- interface CallbackFakerConfig extends BaseFakerConfig {
24
+ interface CallbackFakerWrapperConfig extends BaseFakerWrapperConfig {
89
25
  type: 'callback'
90
26
  callback: (...args: unknown[]) => any
91
27
  }
92
28
 
93
- interface ArrayFakerConfig extends BaseFakerConfig {
29
+ interface ArrayFakerWrapperConfig extends BaseFakerWrapperConfig {
94
30
  type: 'array'
95
- items: Faker<any>
31
+ items: FakerWrapper<any>
96
32
  count: Count
97
33
  }
98
34
 
99
- interface ObjectFakerConfig<O extends Record<string, any> = Record<string, any>>
100
- extends BaseFakerConfig {
35
+ interface ObjectFakerWrapperConfig<O extends Record<string, any> = Record<string, any>>
36
+ extends BaseFakerWrapperConfig {
101
37
  type: 'object'
102
38
  properties: {
103
- [_ in keyof O]: Faker<any> | any
39
+ [_ in keyof O]: FakerWrapper<any> | any
104
40
  }
105
41
  }
106
42
 
43
+ interface InstanceFakerWrapperConfig extends BaseFakerWrapperConfig {
44
+ type: 'instance'
45
+ callback: () => unknown
46
+ args: unknown[]
47
+ }
48
+
107
49
  type FakerConfig =
108
- | UuidFakerConfig
109
- | BooleanFakerConfig
110
- | WordsFakerConfig
111
- | NumberFakerConfig
112
- | UrlFakerConfig
113
- | FullNameFakerConfig
114
- | DateFakerConfig
115
- | PersonalNumberFakerConfig
116
- | RankFakerConfig
117
- | ArrayElementFakerConfig
118
- | ArrayElementsFakerConfig
119
- | CallbackFakerConfig
120
- | ArrayFakerConfig
121
- | ObjectFakerConfig
50
+ | CallbackFakerWrapperConfig
51
+ | ArrayFakerWrapperConfig
52
+ | ObjectFakerWrapperConfig
53
+ | InstanceFakerWrapperConfig
122
54
 
123
- type ExtractFakerConfig<T extends FakerType> = Extract<FakerConfig, { type: T }>
55
+ type ExtractFakerConfig<T extends FakerWrapperType> = Extract<FakerConfig, { type: T }>
124
56
 
125
- declare class Faker<T extends FakerType> {
57
+ declare class FakerWrapper<T extends FakerWrapperType> {
126
58
  private readonly params: {
127
59
  config: ExtractFakerConfig<T>
128
60
  nullable: boolean
@@ -137,100 +69,41 @@ declare class Faker<T extends FakerType> {
137
69
  }
138
70
  }
139
71
 
140
- public static uuid() {
141
- return new Faker<'uuid'>({
142
- type: 'uuid',
143
- })
144
- }
145
-
146
- public static boolean() {
147
- return new Faker<'boolean'>({
148
- type: 'boolean',
149
- })
150
- }
151
-
152
- public static words(params: Omit<WordsFakerConfig, 'type'> = {}) {
153
- return new Faker<'words'>({
154
- type: 'words',
155
- count: params?.count,
156
- })
157
- }
158
-
159
- public static number(params: Omit<NumberFakerConfig, 'type'> = {}) {
160
- return new Faker<'number'>({
161
- type: 'number',
162
- min: params?.min ?? 0,
163
- max: params?.max ?? 1000,
164
- })
165
- }
166
-
167
- public static url() {
168
- return new Faker<'url'>({
169
- type: 'url',
170
- })
171
- }
172
-
173
- public static date() {
174
- return new Faker<'date'>({
175
- type: 'date',
176
- })
177
- }
178
-
179
- public static fullName() {
180
- return new Faker<'fullName'>({
181
- type: 'fullName',
182
- })
183
- }
184
-
185
- public static personalNumber() {
186
- return new Faker<'personalNumber'>({
187
- type: 'personalNumber',
188
- })
189
- }
190
-
191
- public static rank() {
192
- return new Faker<'rank'>({
193
- type: 'rank',
194
- })
195
- }
196
-
197
- public static arrayElement(params: Omit<ArrayElementFakerConfig, 'type'>) {
198
- return new Faker<'arrayElement'>({
199
- type: 'arrayElement',
200
- items: params.items,
201
- })
202
- }
203
-
204
- public static arrayElements(params: Omit<ArrayElementsFakerConfig, 'type'>) {
205
- return new Faker<'arrayElements'>({
206
- type: 'arrayElements',
207
- items: params.items,
208
- count: params?.count,
209
- })
210
- }
211
-
212
- public static callback(callback: CallbackFakerConfig['callback']) {
213
- return new Faker<'callback'>({
72
+ public static callback(callback: CallbackFakerWrapperConfig['callback']) {
73
+ return new FakerWrapper<'callback'>({
214
74
  type: 'callback',
215
75
  callback,
216
76
  })
217
77
  }
218
78
 
219
- public static array(params: Omit<ArrayFakerConfig, 'type'>) {
220
- return new Faker<'array'>({
79
+ public static array(params: Omit<ArrayFakerWrapperConfig, 'type'>) {
80
+ return new FakerWrapper<'array'>({
221
81
  type: 'array',
222
82
  items: params.items,
223
83
  count: params.count,
224
84
  })
225
85
  }
226
86
 
227
- public static object<O extends object = object>(params: Omit<ObjectFakerConfig<O>, 'type'>) {
228
- return new Faker<'object'>({
87
+ public static object<O extends object = object>(
88
+ params: Omit<ObjectFakerWrapperConfig<O>, 'type'>,
89
+ ) {
90
+ return new FakerWrapper<'object'>({
229
91
  type: 'object',
230
92
  properties: params.properties,
231
93
  })
232
94
  }
233
95
 
96
+ public static instance<Function extends (...args: any[]) => any>(
97
+ callback: Function,
98
+ ...args: Parameters<Function>
99
+ ) {
100
+ return new FakerWrapper<'instance'>({
101
+ type: 'instance',
102
+ callback,
103
+ args,
104
+ })
105
+ }
106
+
234
107
  // modifiers
235
108
  public nullable(probability: number = 0.5) {
236
109
  this.params.nullable = true
@@ -265,6 +138,6 @@ declare class Faker<T extends FakerType> {
265
138
 
266
139
  declare const mockApi: (handlers: RequestHandler[]) => SetupWorkerApi
267
140
 
268
- declare function faker(): typeof Faker
141
+ declare function fake(): typeof FakerWrapper
269
142
 
270
- export { faker, mockApi };
143
+ export { fake, mockApi };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fauxy",
3
- "version": "0.0.1-alpha.4",
3
+ "version": "0.0.2",
4
4
  "description": "A package for mocking data and API requests",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/heavenlybilly/fauxy",
package/readme.md CHANGED
@@ -20,26 +20,88 @@ npx fauxy init
20
20
  ```
21
21
 
22
22
  To ensure MSW knows where to find the service worker, add the following section to your package.json:
23
+
23
24
  ```json
24
25
  "msw": {
25
- "workerDirectory": [
26
+ "workerDirectory": [
26
27
  "public"
27
- ]
28
+ ]
28
29
  }
29
30
  ```
30
31
 
31
32
  An example of starting the mock service worker:
33
+
32
34
  ```ts
35
+ // @mocks/index.ts
33
36
  import { mockApi } from 'fauxy'
37
+ import someHandlers from '@/mocks/handlers/some-handlers'
38
+
39
+ mockApi.start([...someHandlers])
40
+ ```
41
+
42
+ ```ts
43
+ // @mocks/handlers/some-handlers.ts
44
+ import { HttpResponse, delay, http, fake, fakerModules as fm } from 'fauxy'
45
+
46
+ export default [
47
+ http.post('/api/persons/select', async () => {
48
+ await delay()
49
+
50
+ const items = fake().array({
51
+ count: 10,
52
+ items: fake().object({
53
+ properties: {
54
+ id: fake(fm.string.uuid),
55
+ title: fake(fm.person.fullName),
56
+ },
57
+ }),
58
+ })
34
59
 
35
- mockApi.start()
60
+ return HttpResponse.json(items)
61
+ }),
62
+ ]
36
63
  ```
37
64
 
38
- ## How to use ![WIP](https://img.shields.io/badge/status-WIP-yellow)
65
+ ## How to use
66
+
67
+
68
+ > How to use with [Laravel Mix](docs/laravel-mix.md)
39
69
 
40
- - [Laravel Mix](docs/laravel-mix.md)
70
+ The following example demonstrates how to use `fake` and `fakerModules` to generate mock objects and arrays. It also
71
+ shows how to set the locale for Faker.
72
+
73
+ ```ts
74
+ import { fake, fakerModules as fm, setLocale } from 'fauxy'
75
+
76
+ interface Person {
77
+ id: string
78
+ name: string
79
+ age: number
80
+ }
81
+
82
+ // set the Faker locale
83
+ setLocale('ru')
84
+
85
+ // generate an array of Person objects
86
+ const persons = fake()
87
+ .array({
88
+ count: { min: 2, max: 4 }, // number of items in the array
89
+ items: fake().object<Person>({
90
+ properties: {
91
+ id: fake(fm.string.uuid), // generate a UUID for the id
92
+ name: fake(fm.person.fullName), // generate a full name
93
+ age: fake(fm.number.int, { min: 18, max: 35 }), // generate a random integer between 18 and 35
94
+ },
95
+ }),
96
+ })
97
+ .create<Person[]>()
98
+
99
+ // generate a random date between two specific dates
100
+ const date = fake(fm.date.between, { from: '2025-11-01', to: '2025-11-10' }).create()
101
+ ```
41
102
 
42
103
  ## Notes
104
+
43
105
  - Fauxy uses MSW directly from its package, so you can access all MSW features as usual.
44
106
  - Faker is only available via Fauxy’s wrapper, which adds convenience methods and some custom enhancements.
45
107
  - For more advanced usage and configuration, check out the official MSW repository.
@@ -1,9 +1,9 @@
1
1
  import { HttpResponse, RequestHandler, delay, http, passthrough } from 'msw'
2
2
  import { SetupWorkerApi } from 'msw/browser'
3
- import { Faker } from '@/faker/faker'
3
+ import { FakerWrapper } from '@/faker/fakerWrapper'
4
4
 
5
5
  export { HttpResponse, RequestHandler, http, delay, passthrough }
6
6
 
7
7
  export declare const mockApi: (handlers: RequestHandler[]) => SetupWorkerApi
8
8
 
9
- export declare function faker(): typeof Faker
9
+ export declare function fake(): typeof FakerWrapper
@@ -0,0 +1,90 @@
1
+ import { generate } from './generators'
2
+ import {
3
+ ArrayFakerWrapperConfig,
4
+ CallbackFakerWrapperConfig,
5
+ ExtractFakerConfig,
6
+ FakerWrapperType,
7
+ ObjectFakerWrapperConfig,
8
+ } from './types'
9
+
10
+ export class FakerWrapper<T extends FakerWrapperType> {
11
+ private readonly params: {
12
+ config: ExtractFakerConfig<T>
13
+ nullable: boolean
14
+ nullableProbability: number
15
+ }
16
+
17
+ private constructor(config: ExtractFakerConfig<T>) {
18
+ this.params = {
19
+ config,
20
+ nullable: false,
21
+ nullableProbability: 0,
22
+ }
23
+ }
24
+
25
+ public static callback(callback: CallbackFakerWrapperConfig['callback']) {
26
+ return new FakerWrapper<'callback'>({
27
+ type: 'callback',
28
+ callback,
29
+ })
30
+ }
31
+
32
+ public static array(params: Omit<ArrayFakerWrapperConfig, 'type'>) {
33
+ return new FakerWrapper<'array'>({
34
+ type: 'array',
35
+ items: params.items,
36
+ count: params.count,
37
+ })
38
+ }
39
+
40
+ public static object<O extends object = object>(
41
+ params: Omit<ObjectFakerWrapperConfig<O>, 'type'>,
42
+ ) {
43
+ return new FakerWrapper<'object'>({
44
+ type: 'object',
45
+ properties: params.properties,
46
+ })
47
+ }
48
+
49
+ public static instance<Function extends (...args: any[]) => any>(
50
+ callback: Function,
51
+ ...args: Parameters<Function>
52
+ ) {
53
+ return new FakerWrapper<'instance'>({
54
+ type: 'instance',
55
+ callback,
56
+ args,
57
+ })
58
+ }
59
+
60
+ // modifiers
61
+ public nullable(probability: number = 0.5) {
62
+ this.params.nullable = true
63
+ this.params.nullableProbability = probability
64
+
65
+ return this
66
+ }
67
+
68
+ // create methods
69
+ private createSingle<U>(): U {
70
+ return generate(this.params) as U
71
+ }
72
+
73
+ private createMultiple<U>(count: number): U[] {
74
+ return Array(count)
75
+ .fill(null)
76
+ .map(() => this.createSingle<U>())
77
+ }
78
+
79
+ public create<U>(): U
80
+
81
+ public create<U>(count: number): U[]
82
+
83
+ public create<U>(count?: number): U | U[] {
84
+ if (count === undefined) {
85
+ return this.createSingle<U>()
86
+ }
87
+
88
+ return this.createMultiple<U>(count)
89
+ }
90
+ }
@@ -1,7 +1,7 @@
1
1
  import { makeCount } from '@/faker/utils'
2
- import { ArrayFakerConfig } from '../types'
2
+ import { ArrayFakerWrapperConfig } from '../types'
3
3
 
4
- export const generateArray = (config: ArrayFakerConfig) => {
4
+ export const generateArray = (config: ArrayFakerWrapperConfig) => {
5
5
  return Array(makeCount(config.count))
6
6
  .fill(null)
7
7
  .map(() => {
@@ -1,5 +1,5 @@
1
- import { CallbackFakerConfig } from '../types'
1
+ import { CallbackFakerWrapperConfig } from '../types'
2
2
 
3
- export const generateCallback = (config: CallbackFakerConfig) => {
3
+ export const generateUsingCallback = (config: CallbackFakerWrapperConfig) => {
4
4
  return config.callback()
5
5
  }
@@ -1,18 +1,8 @@
1
+ import { generateUsingInstance } from '@/faker/generators/instance'
1
2
  import { FakerConfig } from '../types'
2
3
  import { generateArray } from './array'
3
- import { generateArrayElement } from './array-element'
4
- import { generateArrayElements } from './array-elements'
5
- import { generateBoolean } from './boolean'
6
- import { generateCallback } from './callback'
7
- import { generateDate } from './date'
8
- import { generateFullName } from './full-name'
9
- import { generateNumber } from './number'
4
+ import { generateUsingCallback } from './callback'
10
5
  import { generateObject } from './object'
11
- import { generatePersonalNumber } from './personal-number'
12
- import { generateRank } from './rank'
13
- import { generateUrl } from './url'
14
- import { generateUuid } from './uuid'
15
- import { generateWords } from './words'
16
6
 
17
7
  const random = (probability: number) => {
18
8
  return Math.random() < probability
@@ -30,34 +20,14 @@ export const generate = (params: {
30
20
  }
31
21
 
32
22
  switch (type) {
33
- case 'uuid':
34
- return generateUuid(params.config)
35
- case 'boolean':
36
- return generateBoolean(params.config)
37
- case 'words':
38
- return generateWords(params.config)
39
- case 'number':
40
- return generateNumber(params.config)
41
- case 'url':
42
- return generateUrl(params.config)
43
- case 'date':
44
- return generateDate(params.config)
45
- case 'personalNumber':
46
- return generatePersonalNumber(params.config)
47
- case 'fullName':
48
- return generateFullName(params.config)
49
- case 'rank':
50
- return generateRank(params.config)
51
- case 'arrayElement':
52
- return generateArrayElement(params.config)
53
- case 'arrayElements':
54
- return generateArrayElements(params.config)
55
23
  case 'callback':
56
- return generateCallback(params.config)
24
+ return generateUsingCallback(params.config)
57
25
  case 'object':
58
26
  return generateObject(params.config)
59
27
  case 'array':
60
28
  return generateArray(params.config)
29
+ case 'instance':
30
+ return generateUsingInstance(params.config)
61
31
  default: {
62
32
  const _: never = type
63
33
  throw new Error(`Unsupported type ${_}`)
@@ -0,0 +1,6 @@
1
+ import { InstanceFakerWrapperConfig } from '../types'
2
+
3
+ export const generateUsingInstance = (config: InstanceFakerWrapperConfig) => {
4
+ // @ts-ignore
5
+ return config.callback(...config.args)
6
+ }
@@ -1,11 +1,11 @@
1
- import { Faker } from '@/faker/faker'
2
- import { ObjectFakerConfig } from '../types'
1
+ import { FakerWrapper } from '@/faker/fakerWrapper'
2
+ import { ObjectFakerWrapperConfig } from '../types'
3
3
 
4
- export const generateObject = (config: ObjectFakerConfig) => {
4
+ export const generateObject = (config: ObjectFakerWrapperConfig) => {
5
5
  return Object.keys(config.properties).reduce((carry, propertyName) => {
6
6
  const propertyValue = config.properties[propertyName]
7
7
 
8
- const value = propertyValue instanceof Faker ? propertyValue.create() : propertyValue
8
+ const value = propertyValue instanceof FakerWrapper ? propertyValue.create() : propertyValue
9
9
 
10
10
  return {
11
11
  ...carry,
@@ -1,5 +1,64 @@
1
- import { Faker } from '@/faker/faker'
1
+ import { Faker } from '@faker-js/faker'
2
+ import { FakerWrapper } from '@/faker/fakerWrapper'
3
+ import { Locale, locales } from '@/faker/locales'
2
4
 
3
- export const faker = () => {
4
- return Faker
5
+ const initialInstance = new Faker({ locale: [locales.en, locales.base] })
6
+
7
+ export const fakerModules = {
8
+ airline: initialInstance.airline,
9
+ animal: initialInstance.animal,
10
+ book: initialInstance.book,
11
+ color: initialInstance.color,
12
+ commerce: initialInstance.commerce,
13
+ company: initialInstance.company,
14
+ database: initialInstance.database,
15
+ datatype: initialInstance.datatype,
16
+ date: initialInstance.date,
17
+ finance: initialInstance.finance,
18
+ food: initialInstance.food,
19
+ git: initialInstance.git,
20
+ hacker: initialInstance.hacker,
21
+ helpers: initialInstance.helpers,
22
+ image: initialInstance.image,
23
+ internet: initialInstance.internet,
24
+ location: initialInstance.location,
25
+ lorem: initialInstance.lorem,
26
+ music: initialInstance.music,
27
+ number: initialInstance.number,
28
+ person: initialInstance.person,
29
+ phone: initialInstance.phone,
30
+ science: initialInstance.science,
31
+ string: initialInstance.string,
32
+ system: initialInstance.system,
33
+ vehicle: initialInstance.vehicle,
34
+ word: initialInstance.word,
35
+ }
36
+
37
+ const buildModules = (locale: Locale) => {
38
+ const newInstance = new Faker({ locale: [locales[locale], locales.en, locales.base] })
39
+
40
+ const modules = Object.keys(fakerModules) as (keyof typeof fakerModules)[]
41
+
42
+ modules.forEach((moduleName) => {
43
+ // @ts-ignore
44
+ fakerModules[moduleName] = newInstance[moduleName]
45
+ })
46
+ }
47
+
48
+ export function setLocale(locale: Locale) {
49
+ buildModules(locale)
50
+ }
51
+
52
+ export function fake(): typeof FakerWrapper
53
+ export function fake<T extends (...p: any[]) => any>(
54
+ fn: T,
55
+ ...args: Parameters<T>
56
+ ): FakerWrapper<'instance'>
57
+
58
+ export function fake<T extends (...p: any[]) => any>(fn?: T, ...args: Parameters<T>) {
59
+ if (fn) {
60
+ return FakerWrapper.instance(fn, ...args)
61
+ }
62
+
63
+ return FakerWrapper
5
64
  }