-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.d.ts
160 lines (131 loc) · 5.2 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Type definitions for factory-girl 5.0
// Project: https://github.com/aexmachina/factory-girl#readme
// Definitions by: Stack Builders <https://github.com/stackbuilders>
// Sebastián Estrella <https://github.com/sestrella>
// Luis Fernando Alvarez <https://github.com/elcuy>
// Olivier Kamers <https://github.com/OlivierKamers>
// Dan McNamara <https://github.com/DMcNamara>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
declare const factory: factory.Static;
declare namespace factory {
type Generator<T> = () => T;
type Definition<T> = T | Generator<T> | Promise<T>;
type Attributes<T> = {
[P in keyof T]: Definition<T[P]>;
};
type MaybeReadonlyArray<T> = T | ReadonlyArray<T>;
type BuildOptions = Record<string, any>;
type Initializer<T, BO = BuildOptions> =
| Attributes<T>
| ((buildOptions?: BO) => Attributes<T>)
| ((buildOptions?: BO) => Promise<Attributes<T>>);
interface Static {
factory: Static;
/**
* Associate the factory to other model
*/
assoc(name: string, key?: string, attrs?: Attributes<any>, buildOptions?: BuildOptions): any;
/**
* Associate the factory to a model that's not persisted
*/
assocAttrs(name: string, key?: string, attrs?: Attributes<any>, buildOptions?: BuildOptions): any;
/**
* Associate the factory to multiple other models
*/
assocMany(name: string, num: number, key?: string, attrs?: Attributes<any>, buildOptions?: BuildOptions): any[];
/**
* Associate the factory to multiple other models that aren't persisted
*/
assocAttrsMany(
name: string,
num: number,
key?: string,
attrs?: Attributes<any>,
buildOptions?: BuildOptions,
): any[];
/**
* Generates and returns model attributes as an object hash instead of the model instance
*/
attrs<T>(name: string, attrs?: Attributes<Partial<T>>, buildOptions?: BuildOptions): Promise<T>;
/**
* Generates and returns a collection of model attributes as an object hash instead of the model instance
*/
attrsMany<T>(
name: string,
num: number,
attrs?: MaybeReadonlyArray<Attributes<Partial<T>>>,
buildOptions?: BuildOptions | ReadonlyArray<BuildOptions>,
): Promise<T[]>;
/**
* Builds a new model instance that is not persisted
*/
build<T>(name: string, attrs?: Attributes<Partial<T>>, buildOptions?: BuildOptions): Promise<T>;
/**
* Builds an array of model instances that are not persisted
*/
buildMany<T>(
name: string,
num: number,
attrs?: MaybeReadonlyArray<Attributes<Partial<T>>>,
buildOptions?: MaybeReadonlyArray<BuildOptions>,
): Promise<T[]>;
/**
* Destroys all of the created models
*/
cleanUp(): void;
/**
* Builds a new model instance that is persisted
*/
create<T>(name: string, attrs?: Attributes<Partial<T>>, buildOptions?: BuildOptions): Promise<T>;
/**
* Builds an array of model instances that are persisted
*/
createMany<T>(
name: string,
num: number,
attrs?: MaybeReadonlyArray<Attributes<Partial<T>>>,
buildOptions?: MaybeReadonlyArray<BuildOptions>,
): Promise<T[]>;
createMany<T>(
name: string,
attrs?: ReadonlyArray<Attributes<Partial<T>>>,
buildOptions?: MaybeReadonlyArray<BuildOptions>,
): Promise<T[]>;
/**
* Define a new factory with a set of options
*/
define<T>(name: string, model: any, attrs: Initializer<Partial<T>>, options?: Options<T>): void;
/**
* Extends a factory
*/
extend(parent: string, name: string, initializer: any, options?: Options<any>): any;
/**
* Generate values sequentially inside a factory
*/
seq(name?: string): Generator<number>;
seq<T>(name: string, fn: (sequence: number) => T): Generator<T>;
seq<T>(fn: (sequence: number) => T): Generator<T>;
sequence(name?: string): Generator<number>;
sequence<T>(name: string, fn: (sequence: number) => T): Generator<T>;
sequence<T>(fn: (sequence: number) => T): Generator<T>;
/**
* Register an adapter, either as default or tied to a specific model
*/
setAdapter(adapter: any, name?: string): void;
/**
* Reset sequence generator with the given name
* or all generators if no name is given.
*/
resetSequence(name?: string): void;
resetSeq(name?: string): void;
chance(chanceMethod: string, ...options: any): any;
}
interface Options<T> {
afterBuild?: Hook<T>;
afterCreate?: Hook<T>;
}
type Hook<T> = (model: any, attrs: T[], options: any) => void;
}
export = factory;
export as namespace factory;