Skip to content

Commit 7e60b89

Browse files
committed
feat(class-mock): add fakerjs, start write mock decorator
1 parent cb30319 commit 7e60b89

File tree

5 files changed

+89
-11
lines changed

5 files changed

+89
-11
lines changed

packages/class-mock/api.drawio.png

44.5 KB
Loading

packages/class-mock/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@
6565
"typescript": "4.6.3",
6666
"vite": "^2.9.5",
6767
"vitest": "^0.9.3"
68+
},
69+
"dependencies": {
70+
"@faker-js/faker": "^6.2.0"
6871
}
6972
}

packages/class-mock/src/index.ts

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import 'reflect-metadata'
3+
import faker from '@faker-js/faker'
4+
import {PropertyDecoratorConfig, ClassDecoratorConfig} from './utils/types-helper'
25

3-
@Reflect.metadata('type', 'class')
4-
class A {
5-
constructor(public name: string, public age: number) {}
6-
@Reflect.metadata(undefined, undefined)
7-
method(): boolean {
8-
return true
6+
const METADATA_KEY_MOCK = 'mock'
7+
8+
function FakerDecorator<T extends (...args: any[]) => any>(
9+
fakerFn: T,
10+
...fakerParams: Parameters<T>
11+
): PropertyDecorator & {
12+
config(config?: PropertyDecoratorConfig): PropertyDecorator
13+
} {
14+
function decorator(target: Object, propertyKey: string | symbol, config: PropertyDecoratorConfig = {}) {
15+
const metadata: {
16+
fakerFn: T
17+
fakerParams: Parameters<T>
18+
config: PropertyDecoratorConfig
19+
} = {
20+
fakerFn,
21+
fakerParams,
22+
config
23+
}
24+
let classMetadata = Reflect.getOwnMetadata(METADATA_KEY_MOCK, target)
25+
if (!classMetadata) classMetadata = {}
26+
if (!classMetadata.property) classMetadata.property = {}
27+
classMetadata.property[propertyKey] = metadata
28+
Reflect.defineMetadata(METADATA_KEY_MOCK, classMetadata, target)
29+
Reflect.defineMetadata(propertyKey, metadata, target, propertyKey)
930
}
31+
32+
decorator.config = (config: PropertyDecoratorConfig) => (target: Object, propertyKey: string | symbol) =>
33+
decorator(target, propertyKey, config)
34+
35+
return decorator
1036
}
1137

12-
const t1 = Reflect.getMetadata('design:paramtypes', A)
13-
const t2 = Reflect.getMetadata('design:returntype', A.prototype, 'method')
14-
const t3 = Reflect.getMetadata('design:type', A.prototype, 'method')
38+
function Mock(config: ClassDecoratorConfig = {}): ClassDecorator {
39+
function decorator<TFunction extends Function>(target: TFunction): TFunction | void {
40+
const metadata: {
41+
config: PropertyDecoratorConfig
42+
} = {
43+
config
44+
}
45+
Reflect.defineMetadata(METADATA_KEY_MOCK, metadata, target)
46+
}
47+
return decorator
48+
}
1549

16-
console.log(...t1, t2, t3)
50+
interface MockClass<T = any> {
51+
new (...args: any[]): T
52+
}
53+
54+
function createMock<T = any>(target: MockClass<T>): T {
55+
const classMetadata = Reflect.getOwnMetadata(METADATA_KEY_MOCK, target)
56+
const metadataKeys = Reflect.getMetadataKeys(target)
57+
58+
return '' as any as T
59+
}
60+
61+
@Mock()
62+
class User {
63+
@FakerDecorator(faker.random.number, {min: 0, max: 100}).config({
64+
alwaysRandom: true
65+
})
66+
age!: number
67+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type Nil = null | undefined
2+
3+
export interface PropertyDecoratorConfig {
4+
partial?: boolean | Nil
5+
alwaysRandom?: boolean | Nil
6+
groups?: string[] | Nil
7+
array?: boolean
8+
length?: number | Nil
9+
max?: number | Nil
10+
min?: number | Nil
11+
}
12+
13+
export interface ClassDecoratorConfig {
14+
partial?: boolean | Nil
15+
alwaysRandom?: boolean | Nil
16+
}

pnpm-lock.yaml

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)