|
| 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 | +}; |
0 commit comments