5d1a4086fa8cf37bf768d6e2ca78ef48bd2d6a8c8638a7d9a8c547333054cdb54f231b62ebec107c6b136a8d11a9feb511048c492c8c45b5853fb30fbca4a2 7.3 KB

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