Skip to content

Commit 020df93

Browse files
malcolm-keemrlubos
authored andcommitted
feat(faker): first implementation
1 parent 1b9631b commit 020df93

125 files changed

Lines changed: 23521 additions & 2767 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎dev/typescript/presets.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const presets = {
1313
/** Just the client */
1414
plugins.clientAxios(),
1515
],
16+
fake: () => [
17+
/** Just the faker */
18+
plugins.faker(),
19+
],
1620
full: () => [
1721
/** Full kitchen sink for comprehensive testing */
1822
plugins.typescript(),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.idea
3+
logs
4+
node_modules
5+
coverage
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { faker, type Faker } from '@faker-js/faker';
4+
5+
import type { Comment, Department, Employee, Member, Org, Project, TreeNode } from '../types.gen';
6+
7+
export type Options = {
8+
faker?: Faker;
9+
/**
10+
* Whether to include optional properties, including add property to record object.
11+
* Provide a number between 0 and 1 to randomly include based on that probability.
12+
* @default true
13+
*/
14+
includeOptional?: boolean | number;
15+
/**
16+
* Whether to use schema default values instead of generating fake data.
17+
* Provide a number between 0 and 1 to randomly use defaults based on that probability.
18+
* @default false
19+
*/
20+
useDefault?: boolean | number;
21+
};
22+
23+
const MAX_CALL_DEPTH = 10;
24+
25+
const resolveCondition = (condition: boolean | number, faker: Faker): boolean => condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition });
26+
27+
export const fakeTreeNode = (options: Options = {}, _callDepth = 0): TreeNode => {
28+
const f = options?.faker ?? faker;
29+
return {
30+
id: f.string.uuid(),
31+
value: f.string.sample(),
32+
..._callDepth > MAX_CALL_DEPTH || !resolveCondition(options?.includeOptional ?? true, f) ? {} : { children: _callDepth > MAX_CALL_DEPTH ? [] : f.helpers.multiple(() => fakeTreeNode(options, _callDepth + 1)) }
33+
};
34+
};
35+
36+
export const fakeComment = (options: Options = {}, _callDepth = 0): Comment => {
37+
const f = options?.faker ?? faker;
38+
return {
39+
id: f.string.uuid(),
40+
text: f.string.sample(),
41+
parent: _callDepth > MAX_CALL_DEPTH ? null : f.datatype.boolean() ? fakeComment(options, _callDepth + 1) : null
42+
};
43+
};
44+
45+
export const fakeOrg = (options: Options = {}, _callDepth = 0): Org => {
46+
const f = options?.faker ?? faker;
47+
return {
48+
id: f.string.uuid(),
49+
name: f.string.sample(),
50+
..._callDepth > MAX_CALL_DEPTH || !resolveCondition(options?.includeOptional ?? true, f) ? {} : { members: _callDepth > MAX_CALL_DEPTH ? [] : f.helpers.multiple(() => fakeMember(options, _callDepth + 1)) }
51+
};
52+
};
53+
54+
export const fakeMember = (options: Options = {}, _callDepth = 0): Member => {
55+
const f = options?.faker ?? faker;
56+
return {
57+
id: f.string.uuid(),
58+
name: f.string.sample(),
59+
..._callDepth > MAX_CALL_DEPTH || !resolveCondition(options?.includeOptional ?? true, f) ? {} : { org: fakeOrg(options, _callDepth + 1) }
60+
};
61+
};
62+
63+
export const fakeDepartment = (options: Options = {}, _callDepth = 0): Department => {
64+
const f = options?.faker ?? faker;
65+
return {
66+
id: f.string.uuid(),
67+
name: f.string.sample(),
68+
..._callDepth > MAX_CALL_DEPTH || !resolveCondition(options?.includeOptional ?? true, f) ? {} : { employees: _callDepth > MAX_CALL_DEPTH ? [] : f.helpers.multiple(() => fakeEmployee(options, _callDepth + 1)) }
69+
};
70+
};
71+
72+
export const fakeEmployee = (options: Options = {}, _callDepth = 0): Employee => {
73+
const f = options?.faker ?? faker;
74+
return {
75+
id: f.string.uuid(),
76+
name: f.person.fullName(),
77+
projects: _callDepth > MAX_CALL_DEPTH ? [] : f.helpers.multiple(() => fakeProject(options, _callDepth + 1))
78+
};
79+
};
80+
81+
export const fakeProject = (options: Options = {}, _callDepth = 0): Project => {
82+
const f = options?.faker ?? faker;
83+
return {
84+
id: f.string.uuid(),
85+
name: f.string.sample(),
86+
..._callDepth > MAX_CALL_DEPTH || !resolveCondition(options?.includeOptional ?? true, f) ? {} : { department: fakeDepartment(options, _callDepth + 1) }
87+
};
88+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type { ClientOptions, Comment, Department, Employee, Member, Org, Project, TreeNode } from './types.gen';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type ClientOptions = {
4+
baseUrl: string;
5+
};
6+
7+
export type TreeNode = {
8+
id: string;
9+
value: string;
10+
children?: Array<TreeNode>;
11+
};
12+
13+
export type Org = {
14+
id: string;
15+
name: string;
16+
members?: Array<Member>;
17+
};
18+
19+
export type Member = {
20+
id: string;
21+
name: string;
22+
org?: Org;
23+
};
24+
25+
export type Department = {
26+
id: string;
27+
name: string;
28+
employees?: Array<Employee>;
29+
};
30+
31+
export type Employee = {
32+
id: string;
33+
name: string;
34+
projects: Array<Project>;
35+
};
36+
37+
export type Project = {
38+
id: string;
39+
name: string;
40+
department?: Department;
41+
};
42+
43+
export type Comment = {
44+
id: string;
45+
text: string;
46+
parent: Comment | null;
47+
};

0 commit comments

Comments
 (0)