UNPKG

7.44 kBTypeScriptView Raw
1declare namespace mockjs {
2 type N = number;
3 type S = string;
4 type B = boolean;
5
6 // Interface for global namespace 'Mockjs'
7 interface Mockjs {
8 mock: MockjsMock;
9 setup: MockjsSetup;
10 Random: MockjsRandom;
11 valid: MockjsValid;
12 toJSONSchema: MockjsToJSONSchema;
13 version: number;
14 }
15
16 interface MockjsRequestOptions {
17 url: string;
18 type: string;
19 body: any;
20 }
21
22 type templateOrFn = ((options: MockjsRequestOptions) => any) | object;
23
24 // Mockjs.mock()
25 // see https://github.com/nuysoft/Mock/wiki/Mock.mock()
26 interface MockjsMock {
27 (rurl: S | RegExp, rtype: S, template: templateOrFn): Mockjs;
28
29 (rurl: S | RegExp, template: templateOrFn): Mockjs;
30
31 (template: any): any;
32 }
33
34 interface MockjsSetupSettings {
35 timeout?: number | S | undefined;
36 }
37
38 // Mockjs.setup()
39 // see https://github.com/nuysoft/Mock/wiki/Mock.setup()
40 type MockjsSetup = (settings: MockjsSetupSettings) => void;
41
42 // Mockjs.Random - Basic
43 // see https://github.com/nuysoft/Mock/wiki/Basic
44 interface MockjsRandomBasic {
45 // Random.boolean
46 boolean(min: N, max: N, current: B): B;
47 boolean(): B;
48
49 // Random.natural
50 natural(min?: N, max?: N): N;
51
52 // Random.integer
53 integer(min?: N, max?: N): N;
54
55 // Random.float
56 float(min?: N, max?: N, dmin?: N, dmax?: N): N;
57
58 // Random.character
59 character(pool: "lower" | "upper" | "number" | "symbol"): S;
60 character(pool?: S): S;
61
62 // Random.string
63 string(pool?: S | N, min?: N, max?: N): S;
64
65 // Random.range
66 range(start?: N, stop?: N, step?: N): N[];
67 }
68
69 // Mockjs.Random - Date
70 // see https://github.com/nuysoft/Mock/wiki/Date
71 type RandomDateUtilString = "year" | "month" | "week" | "day" | "hour" | "minute" | "second" | "week";
72 interface MockjsRandomDate {
73 // Random.date
74 date(format?: S): S;
75
76 // Random.time
77 time(format?: S): S;
78
79 // Random.datetime
80 datetime(format?: S): S;
81
82 // Random.now
83 now(util: RandomDateUtilString, format?: S): S;
84 now(format?: S): S;
85 }
86
87 // Mockjs.Random - Image
88 // see https://github.com/nuysoft/Mock/wiki/Image
89 type RandomImageFormatString = "png" | "gif" | "jpg";
90 interface MockjsRandomImage {
91 // Random.image
92 image(size?: S, background?: S, foreground?: S, format?: RandomImageFormatString | S, text?: S): S;
93
94 // Random.dataImage
95 dataImage(size?: S, text?: S): S;
96 }
97
98 // Mockjs.Random - Color
99 // see https://github.com/nuysoft/Mock/wiki/Color
100 interface MockjsRandomColor {
101 // Random.color
102 color(): S;
103
104 // Random.hex
105 hex(): S;
106
107 // Random.rgb
108 rgb(): S;
109
110 // Random.rgba
111 rgba(): S;
112
113 // Random.hsl
114 hsl(): S;
115 }
116
117 // Mockjs.Random - Text
118 // see https://github.com/nuysoft/Mock/wiki/Text
119 interface MockjsRandomText {
120 // Random.paragraph
121 paragraph(min?: N, max?: N): S;
122
123 // Random.cparagraph
124 cparagraph(min?: N, max?: N): S;
125
126 // Random.sentence
127 sentence(min?: N, max?: N): S;
128
129 // Random.csentence
130 csentence(min?: N, max?: N): S;
131
132 // Random.word
133 word(min?: N, max?: N): S;
134
135 // Random.cword
136 cword(pool?: S | N, min?: N, max?: N): S;
137
138 // Random.title
139 title(min?: N, max?: N): S;
140
141 // Random.ctitle
142 ctitle(min?: N, max?: N): S;
143 }
144
145 // Mockjs.Random - Name
146 // see https://github.com/nuysoft/Mock/wiki/Name
147 interface MockjsRandomName {
148 // Random.first
149 first(): S;
150
151 // Random.last
152 last(): S;
153
154 // Random.name
155 name(middle?: B): S;
156
157 // Random.cfirst
158 cfirst(): S;
159
160 // Random.clast
161 clast(): S;
162
163 // Random.cname
164 cname(): S;
165 }
166
167 // Mockjs.Random - Web
168 // see https://github.com/nuysoft/Mock/wiki/Web
169 type RandomWebProtocal =
170 | "http"
171 | "ftp"
172 | "gopher"
173 | "mailto"
174 | "mid"
175 | "cid"
176 | "news"
177 | "nntp"
178 | "prospero"
179 | "telnet"
180 | "rlogin"
181 | "tn3270"
182 | "wais";
183 interface MockjsRandomWeb {
184 // Random.url
185 url(protocol?: S, host?: S): S;
186
187 // Random.protocol
188 protocal(): RandomWebProtocal;
189
190 // Random.domain
191 domain(): S;
192
193 // Random.tld
194 dtl(): S;
195
196 // Random.email
197 email(domain?: S): S;
198
199 // Random.ip
200 ip(): S;
201 }
202
203 // Mockjs.Random - Address
204 // see https://github.com/nuysoft/Mock/wiki/Address
205 interface MockjsRandomAddress {
206 // Random.region
207 region(): S;
208
209 // Random.province
210 province(): S;
211
212 // Random.city
213 city(prefix?: B): S;
214
215 // Random.county
216 county(prefix?: B): S;
217
218 // Random.zip
219 zip(prefix?: B): S;
220 }
221
222 // Mockjs.Random - Helper
223 // see https://github.com/nuysoft/Mock/wiki/Helper
224 interface MockjsRandomHelper {
225 // Random.capitalize
226 capitalize(word: S): S;
227
228 // Random.upper
229 upper(str: S): S;
230
231 // Random.lower
232 lower(str: S): S;
233
234 // Random.pick
235 pick(arr: any[]): any;
236
237 // Random.shuffle
238 shuffle(arr: any[]): any[];
239 }
240
241 // Mockjs.Random - Miscellaneous
242 // see https://github.com/nuysoft/Mock/wiki/Miscellaneous
243 interface MockjsRandomMiscellaneous {
244 // Random.guid
245 guid(): S;
246
247 // Random.id
248 id(): S;
249
250 // Random.increment
251 increment(step?: N): N;
252 }
253
254 interface MockjsRandomExtendOption {
255 [randomType: string]: (...args: any[]) => any;
256 }
257
258 // Mockjs.Random
259 // see https://github.com/nuysoft/Mock/wiki/Mock.Random
260 interface MockjsRandom
261 extends
262 MockjsRandomBasic,
263 MockjsRandomDate,
264 MockjsRandomImage,
265 MockjsRandomColor,
266 MockjsRandomAddress,
267 MockjsRandomHelper,
268 MockjsRandomMiscellaneous,
269 MockjsRandomName,
270 MockjsRandomText,
271 MockjsRandomWeb,
272 MockjsRandomExtendOption
273 {
274 // Random.extend
275 extend(extendOption: MockjsRandomExtendOption): MockjsRandom;
276 }
277
278 interface MockjsValidRsItem {
279 action: S;
280 actual: S;
281 expected: S;
282 message: S;
283 path: S[];
284 type: S;
285 }
286
287 // Mockjs.valid()
288 // see https://github.com/nuysoft/Mock/wiki/Mock.valid()
289 type MockjsValid = (template: any, data: any) => MockjsValidRsItem[];
290
291 interface MockjsToJSONSchemaRs {
292 name: S | undefined;
293 template: any;
294 type: S;
295 rule: object;
296 path: S[];
297 properties?: MockjsToJSONSchemaRs[] | undefined;
298 items?: MockjsToJSONSchemaRs[] | undefined;
299 }
300
301 // Mockjs.toJSONSchema()
302 // see https://github.com/nuysoft/Mock/wiki/Mock.toJSONSchema()
303 type MockjsToJSONSchema = (template: any) => MockjsToJSONSchemaRs;
304
305 let mock: MockjsMock;
306 let setup: MockjsSetup;
307 let Random: MockjsRandom;
308 let valid: MockjsValid;
309 let toJSONSchema: MockjsToJSONSchema;
310 let version: number;
311}
312
313export = mockjs;