forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosie.d.ts
201 lines (183 loc) · 6.46 KB
/
rosie.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Type definitions for rosie
// Project: https://github.com/rosiejs/rosie
// Definitions by: Abner Oliveira <https://github.com/abner/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module rosie {
interface IFactoryStatic {
/**
* Defines a factory by name and constructor function. Call #attr and #option
* on the result to define the properties of this factory.
*
* @param {!string} name
* @param {function(object): *=} constructor
* @return {Factory}
*/
define(name: String, constructor?: Function): IFactory;
/**
* Locates a factory by name and calls #build on it.
*
* @param {string} name
* @param {object=} attributes
* @param {object=} options
* @return {*}
*/
build(name: string, attributes?: any, options?: Object): Object;
/**
* Builds a collection of objects using the named factory.
*
* @param {string} name
* @param {number} size
* @param {object=} attributes
* @param {object=} options
* @return {Array.<*>}
*/
buildList(name: string, size: number, attributes?: any, options?: Object): Object[];
/**
* Locates a factory by name and calls #attributes on it.
*
* @param {string} name
* @param {object} attributes
* @param {object} options
* @return {object}
*/
attributes(name: string, attributes: Object, options?: Object): Object;
}
interface IFactory {
/**
* Define an attribute on this factory. Attributes can optionally define a
* default value, either as a value (e.g. a string or number) or as a builder
* function. For example:
*
* // no default value for age
* Factory.define('Person').attr('age')
*
* // static default value for age
* Factory.define('Person').attr('age', 18)
*
* // dynamic default value for age
* Factory.define('Person').attr('age', function() {
* return Math.random() * 100;
* })
*
* Attributes with dynamic default values can depend on options or other
* attributes:
*
* Factory.define('Person').attr('age', ['name'], function(name) {
* return name === 'Brian' ? 30 : 18;
* });
*
* By default if the consumer of your factory provides a value for an
* attribute your builder function will not be called. You can override this
* behavior by declaring that your attribute depends on itself:
*
* Factory.define('Person').attr('spouse', ['spouse'], function(spouse) {
* return Factory.build('Person', spouse);
* });
*
* As in the example above, this can be a useful way to fill in
* partially-specified child objects.
*
* @param {string} attr
* @param {Array.<string>=} | any dependenciesOrValue
* @param any
* @return {Factory}
*/
attr(name: string, dependenciesOrValue: any | string[], value?: any): IFactory;
/**
* Define an option for this factory. Options are values that may inform
* dynamic attribute behavior but are not included in objects built by the
* factory. Like attributes, options may have dependencies. Unlike
* attributes, options may only depend on other options.
*
* Factory.define('Person')
* .option('includeRelationships', false)
* .attr(
* 'spouse',
* ['spouse', 'includeRelationships'],
* function(spouse, includeRelationships) {
* return includeRelationships ?
* Factory.build('Person', spouse) :
* null;
* });
*
* Factory.build('Person', null, { includeRelationships: true });
*
* Options may have either static or dynamic default values, just like
* attributes. Options without default values must have a value specified
* when building.
*
* @param {string} opt
* @param {Array.<string>=} | any dependencies or value
* @param {*=} value
* @return {Factory}
*/
option(name: string, dependenciesOrValue: any | string[], value?: any): IFactory;
/**
* Defines an attribute that, by default, simply has an auto-incrementing
* numeric value starting at 1. You can provide your own builder function
* that accepts the number of the sequence and returns whatever value you'd
* like it to be.
*
* Sequence values are inherited such that a factory derived from another
* with a sequence will share the state of that sequence and they will never
* conflict.
*
* Factory.define('Person').sequence('id');
*
* @param {string} attr
* @param {Array.<string>=} dependencies
* @param {function(number): *=} builder
* @return {Factory}
*/
sequence(name: string, dependencies?: string[], builder?: Function) : IFactory;
/**
* Sets a post-processor callback that will receive built objects and the
* options for the build just before they are returned from the #build
* function.
*
* @param {function(object, ?object)} callback
* @return {Factory}
*/
after(functionArg: Function): IFactory;
/**
* Sets the constructor for this factory to be another factory. This can be
* used to create more specific sub-types of factories.
*
* @param {Factory} parentFactory
* @return {Factory}
*/
inherits(functionArg: Function): IFactory;
/**
* Builds a plain object containing values for each of the declared
* attributes. The result of this is the same as the result when using #build
* when there is no constructor registered.
*
* @param {object=} attributes
* @param {object=} options
* @return {object}
*/
attributes(attributes:Object, options: Object): Object;
/**
* Generates values for all the registered options using the values given.
*
* @private
* @param {object} options
* @return {object}
*/
options(options: Object): Object;
/**
* Builds objects by getting values for all attributes and optionally passing
* the result to a constructor function.
*
* @param {object=} attributes
* @param {object=} options
* @return {*}
*/
build(attributes: Object, options: Object): Object;
buildList(size: number, attributes: Object, options: Object): Object[];
}
}
declare var rosie: { Factory: rosie.IFactoryStatic };
declare module 'rosie' {
export = rosie;
}