UNPKG

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