From 77523b9bc308dbcfa4dada434ff7d90d5de26aa8 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sun, 17 Sep 2017 19:18:55 +0800 Subject: [PATCH 1/6] Add types for mockjs --- types/mockjs/index.d.ts | 313 +++++++++++++++++++++++++++++++++++ types/mockjs/mockjs-tests.ts | 34 ++++ types/mockjs/tsconfig.json | 23 +++ types/mockjs/tslint.json | 1 + 4 files changed, 371 insertions(+) create mode 100644 types/mockjs/index.d.ts create mode 100644 types/mockjs/mockjs-tests.ts create mode 100644 types/mockjs/tsconfig.json create mode 100644 types/mockjs/tslint.json diff --git a/types/mockjs/index.d.ts b/types/mockjs/index.d.ts new file mode 100644 index 0000000000..8890823cc1 --- /dev/null +++ b/types/mockjs/index.d.ts @@ -0,0 +1,313 @@ +// Type definitions for mockjs 1.0.1-beta3 +// Project: http://mockjs.com/ +// Definitions by: lavyun http://github.com/lavyun +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare namespace mockjs { + type N = number; + type S = string; + type B = boolean; + + // Interface for global namespace 'Mockjs' + interface Mockjs { + mock: MockjsMock; + setup: MockjsSetup; + Random: MockjsRandom; + valid: MockjsValid; + toJSONSchema: MockjsToJSONSchema; + version: number; + } + + interface MockjsMockCbOptions { + url: S; + type: S; + body: S | null; + } + + // Mockjs.mock() + // see https://github.com/nuysoft/Mock/wiki/Mock.mock() + interface MockjsMock { + (rurl: S | RegExp, rtype: S, template: any): Mockjs; + + (rurl: S | RegExp, rtype: S, callback: (options: MockjsMockCbOptions) => any): Mockjs; + + (rurl: S | RegExp, template: any): Mockjs; + + (rurl: S | RegExp, callback: (options: MockjsMockCbOptions) => any): Mockjs; + + (template: any): any; + } + + interface MockjsSetupSettings { + timeout?: number | S; + } + + // Mockjs.setup() + // see https://github.com/nuysoft/Mock/wiki/Mock.setup() + interface MockjsSetup { + (settings: MockjsSetupSettings): void; + } + + // Mockjs.Random - Basic + // see https://github.com/nuysoft/Mock/wiki/Basic + interface MockjsRandomBasic { + // Random.boolean + boolean(min: N, max: N, current: B): B; + boolean(): B; + + // Random.natural + natural(min?: N, max?: N): N; + + // Random.integer + integer(min?: N, max?: N): N; + + // Random.float + float(min?: N, max?: N, dmin?: N, dmax?: N): N; + + // Random.character + character(pool: 'lower' | 'upper' | 'number' | 'symbol'): S; + character(pool?: S): S; + + // Random.string + string(pool: S, min: N, max: N): S; + string(pool: S, length: N): S; + string(min: N, max: N): S; + string(length?: N): S; + + // Random.range + range(start?: N, stop?: N, step?: N): N; + } + + // Mockjs.Random - Date + // see https://github.com/nuysoft/Mock/wiki/Date + type RandomDateUtilString = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'week'; + interface MockjsRandomDate { + // Random.date + date(format?: S): S; + + // Random.time + time(format?: S): S; + + // Random.datetime + datetime(format?: S): S; + + // Random.now + now(util: RandomDateUtilString, format?: S): S; + mow(format?: S): S; + } + + // Mockjs.Random - Image + // see https://github.com/nuysoft/Mock/wiki/Image + type RandomImageFormatString = 'png' | 'gif' | 'jpg'; + interface MockjsRandomImage { + // Random.image + image(size: S, background: S, foreground: S, format: RandomImageFormatString, text: S): S; + image(size: S, background: S, foreground: S, text: S): S; + image(size?: S, background?: S, text?: S): S; + + // Random.dataImage + dataImage(size?: S, text?: S): S; + } + + // Mockjs.Random - Color + // see https://github.com/nuysoft/Mock/wiki/Color + interface MockjsRandomColor { + // Random.color + color(): S; + + // Random.hex + hex(): S; + + // Random.rgb + rgb(): S; + + // Random.rgba + rgba(): S; + + // Random.hsl + hsl(): S; + } + + // Mockjs.Random - Text + // see https://github.com/nuysoft/Mock/wiki/Text + interface MockjsRandomText { + // Random.paragraph + paragraph(min: N, max: N): S; + paragraph(length?: N): S; + + // Random.cparagraph + cparagraph(min: N, max: N): S; + cparagraph(length?: N): S; + + // Random.sentence + sentence(min: N, max: N): S; + sentence(length?: N): S; + + // Random.csentence + csentence(min: N, max: N): S; + csentence(length?: N): S; + + // Random.word + word(min: N, max: N): S; + word(length?: N): S; + + // Random.cword + cword(pool: S, min: N, max: N): S; + cword(pool: S, length: N): S; + cword(min: N, max: N): S; + cword(pool: S): S; + cword(length?: N): S; + + // Random.title + title(min: N, max: N): S; + title(length?: N): S; + + // Random.ctitle + ctitle(min: N, max: N): S; + ctitle(length?: N): S; + } + + // Mockjs.Random - Name + // see https://github.com/nuysoft/Mock/wiki/Name + interface MockjsRandomName { + // Random.first + first(): S; + + // Random.last + last(): S; + + // Random.name + name(middle?: B): S; + + // Random.cfirst + cfirst(): S; + + // Random.clast + clast(): S; + + // Random.cname + cname(): S; + } + + // Mockjs.Random - Web + // see https://github.com/nuysoft/Mock/wiki/Web + type RandomWebProtocal = 'http' | 'ftp' | 'gopher' | 'mailto' | 'mid' | 'cid' | 'news' | 'nntp' | 'prospero' | 'telnet' | 'rlogin' | 'tn3270' | 'wais'; + interface MockjsRandomWeb { + // Random.url + url(protocol?: S, host?: S): S; + + // Random.protocol + protocal(): RandomWebProtocal; + + // Random.domain + domain(): S; + + // Random.tld + dtl(): S; + + // Random.email + email(domain?: S): S; + + // Random.ip + ip(): S; + } + + // Mockjs.Random - Address + // see https://github.com/nuysoft/Mock/wiki/Address + interface MockjsRandomAddress { + // Random.region + region(): S; + + // Random.province + province(): S; + + // Random.city + city(prefix?: B): S; + + // Random.county + country(prefix?: B): S; + + // Random.zip + zip(prefix?: B): S; + } + + // Mockjs.Random - Helper + // see https://github.com/nuysoft/Mock/wiki/Helper + interface MockjsRandomHelper { + // Random.capitalize + capitalize(word: S): S; + + // Random.upper + upper(str: S): S; + + // Random.lower + lower(str: S): S; + + // Random.pick + pick(arr: any[]): any; + + // Random.shuffle + shuffle(arr: any[]): any[]; + } + + // Mockjs.Random - Miscellaneous + // see https://github.com/nuysoft/Mock/wiki/Miscellaneous + interface MockjsRandomMiscellaneous { + // Random.guid + guid(): S; + + // Random.id + id(): S; + + // Random.increment + increment(step?: N): N; + } + + // Mockjs.Random + // see https://github.com/nuysoft/Mock/wiki/Mock.Random + interface MockjsRandom extends MockjsRandomBasic, MockjsRandomDate, + MockjsRandomImage, MockjsRandomColor, MockjsRandomAddress, + MockjsRandomHelper, MockjsRandomMiscellaneous, MockjsRandomName, + MockjsRandomText, MockjsRandomWeb { } + + interface MockjsValidRsItem { + action: S; + actual: S; + expected: S; + message: S; + path: S[]; + type: S; + } + + // Mockjs.valid() + // see https://github.com/nuysoft/Mock/wiki/Mock.valid() + interface MockjsValid { + (template: any, data: any): MockjsValidRsItem[]; + } + + interface MockjsToJSONSchemaRs { + name: S | undefined; + template: any; + type: S; + rule: any; + path: S[]; + properties?: MockjsToJSONSchemaRs[]; + items?: MockjsToJSONSchemaRs[]; + } + + // Mockjs.toJSONSchema() + // see https://github.com/nuysoft/Mock/wiki/Mock.toJSONSchema() + interface MockjsToJSONSchema { + (template: any): MockjsToJSONSchemaRs; + } + + let mock: MockjsMock; + let setup: MockjsSetup; + let Random: MockjsRandom; + let valid: MockjsValid; + let toJSONSchema: MockjsToJSONSchema; + let version: number; +} + +export = mockjs; diff --git a/types/mockjs/mockjs-tests.ts b/types/mockjs/mockjs-tests.ts new file mode 100644 index 0000000000..3cedd1528e --- /dev/null +++ b/types/mockjs/mockjs-tests.ts @@ -0,0 +1,34 @@ +import * as Mock from 'mockjs' + +Mock.mock('/test', 'get', { + name: 'mockjs' +}) +.mock('/login', 'post', { + status: 0 +}); +// When request '/test' will response {name: 'mockjs'} +// When request '/login' will response {status: 0} + +const Random = Mock.Random; +console.log(Random.cfirst()); // get one chinese word +console.log(Random.string('hello', 1, 3)); // get 1-3 words from 'hello' +console.log(Random.email()); // get a string like email + +Mock.setup({ + timeout: '100-400' +}); +// When do some request, there were 100-400ms delay until get response + +const template = { + 'age|10-40': 1 +}; + +const data = { + age: 30 +}; + +console.log(Mock.valid(template, data)); + +console.log(Mock.toJSONSchema(template)); + +console.log(Mock.version); \ No newline at end of file diff --git a/types/mockjs/tsconfig.json b/types/mockjs/tsconfig.json new file mode 100644 index 0000000000..59b6876702 --- /dev/null +++ b/types/mockjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mockjs-tests.ts" + ] +} \ No newline at end of file diff --git a/types/mockjs/tslint.json b/types/mockjs/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/mockjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file From f35589aa0d1d06a9af719264bed8f94ce009e588 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sun, 17 Sep 2017 19:25:53 +0800 Subject: [PATCH 2/6] modify types --- types/mockjs/index.d.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/mockjs/index.d.ts b/types/mockjs/index.d.ts index 8890823cc1..24b250b975 100644 --- a/types/mockjs/index.d.ts +++ b/types/mockjs/index.d.ts @@ -2,7 +2,6 @@ // Project: http://mockjs.com/ // Definitions by: lavyun http://github.com/lavyun // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 declare namespace mockjs { type N = number; @@ -290,7 +289,7 @@ declare namespace mockjs { name: S | undefined; template: any; type: S; - rule: any; + rule: object; path: S[]; properties?: MockjsToJSONSchemaRs[]; items?: MockjsToJSONSchemaRs[]; From eeb4b8754e4f759dccb5099954c5ad49b9a11ee6 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sun, 17 Sep 2017 19:30:26 +0800 Subject: [PATCH 3/6] modify strictNullChecks to true --- types/mockjs/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mockjs/tsconfig.json b/types/mockjs/tsconfig.json index 59b6876702..7a8b41e142 100644 --- a/types/mockjs/tsconfig.json +++ b/types/mockjs/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" From 9becc2180147dd6a92d610269c64576809d39e30 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sun, 17 Sep 2017 19:48:22 +0800 Subject: [PATCH 4/6] strictNullChecks is false --- types/mockjs/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mockjs/tsconfig.json b/types/mockjs/tsconfig.json index 7a8b41e142..59b6876702 100644 --- a/types/mockjs/tsconfig.json +++ b/types/mockjs/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": true, + "strictNullChecks": false, "baseUrl": "../", "typeRoots": [ "../" From f4335d7b22b050d7e7ce6db4b4a2852db4633267 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sun, 17 Sep 2017 20:55:14 +0800 Subject: [PATCH 5/6] fixed --- types/mockjs/index.d.ts | 65 ++++++++++-------------------------- types/mockjs/mockjs-tests.ts | 4 +-- types/mockjs/tslint.json | 9 ++++- 3 files changed, 27 insertions(+), 51 deletions(-) diff --git a/types/mockjs/index.d.ts b/types/mockjs/index.d.ts index 24b250b975..a0ca673b0a 100644 --- a/types/mockjs/index.d.ts +++ b/types/mockjs/index.d.ts @@ -1,7 +1,8 @@ -// Type definitions for mockjs 1.0.1-beta3 +// Type definitions for mockjs 1.0 // Project: http://mockjs.com/ -// Definitions by: lavyun http://github.com/lavyun +// Definitions by: lavyun // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 declare namespace mockjs { type N = number; @@ -18,23 +19,13 @@ declare namespace mockjs { version: number; } - interface MockjsMockCbOptions { - url: S; - type: S; - body: S | null; - } - // Mockjs.mock() // see https://github.com/nuysoft/Mock/wiki/Mock.mock() interface MockjsMock { (rurl: S | RegExp, rtype: S, template: any): Mockjs; - (rurl: S | RegExp, rtype: S, callback: (options: MockjsMockCbOptions) => any): Mockjs; - (rurl: S | RegExp, template: any): Mockjs; - (rurl: S | RegExp, callback: (options: MockjsMockCbOptions) => any): Mockjs; - (template: any): any; } @@ -44,9 +35,7 @@ declare namespace mockjs { // Mockjs.setup() // see https://github.com/nuysoft/Mock/wiki/Mock.setup() - interface MockjsSetup { - (settings: MockjsSetupSettings): void; - } + type MockjsSetup = (settings: MockjsSetupSettings) => void; // Mockjs.Random - Basic // see https://github.com/nuysoft/Mock/wiki/Basic @@ -69,10 +58,7 @@ declare namespace mockjs { character(pool?: S): S; // Random.string - string(pool: S, min: N, max: N): S; - string(pool: S, length: N): S; - string(min: N, max: N): S; - string(length?: N): S; + string(pool: S | N, min?: N, max?: N): S; // Random.range range(start?: N, stop?: N, step?: N): N; @@ -101,9 +87,7 @@ declare namespace mockjs { type RandomImageFormatString = 'png' | 'gif' | 'jpg'; interface MockjsRandomImage { // Random.image - image(size: S, background: S, foreground: S, format: RandomImageFormatString, text: S): S; - image(size: S, background: S, foreground: S, text: S): S; - image(size?: S, background?: S, text?: S): S; + image(size?: S, background?: S, foreground?: S, format?: RandomImageFormatString | S, text?: S): S; // Random.dataImage dataImage(size?: S, text?: S): S; @@ -132,39 +116,28 @@ declare namespace mockjs { // see https://github.com/nuysoft/Mock/wiki/Text interface MockjsRandomText { // Random.paragraph - paragraph(min: N, max: N): S; - paragraph(length?: N): S; + paragraph(min?: N, max?: N): S; // Random.cparagraph - cparagraph(min: N, max: N): S; - cparagraph(length?: N): S; + cparagraph(min?: N, max?: N): S; // Random.sentence - sentence(min: N, max: N): S; - sentence(length?: N): S; + sentence(min?: N, max?: N): S; // Random.csentence - csentence(min: N, max: N): S; - csentence(length?: N): S; + csentence(min?: N, max?: N): S; // Random.word - word(min: N, max: N): S; - word(length?: N): S; + word(min?: N, max?: N): S; // Random.cword - cword(pool: S, min: N, max: N): S; - cword(pool: S, length: N): S; - cword(min: N, max: N): S; - cword(pool: S): S; - cword(length?: N): S; + cword(pool?: S | N, min?: N, max?: N): S; // Random.title - title(min: N, max: N): S; - title(length?: N): S; + title(min?: N, max?: N): S; // Random.ctitle - ctitle(min: N, max: N): S; - ctitle(length?: N): S; + ctitle(min?: N, max?: N): S; } // Mockjs.Random - Name @@ -281,9 +254,7 @@ declare namespace mockjs { // Mockjs.valid() // see https://github.com/nuysoft/Mock/wiki/Mock.valid() - interface MockjsValid { - (template: any, data: any): MockjsValidRsItem[]; - } + type MockjsValid = (template: any, data: any) => MockjsValidRsItem[]; interface MockjsToJSONSchemaRs { name: S | undefined; @@ -297,10 +268,8 @@ declare namespace mockjs { // Mockjs.toJSONSchema() // see https://github.com/nuysoft/Mock/wiki/Mock.toJSONSchema() - interface MockjsToJSONSchema { - (template: any): MockjsToJSONSchemaRs; - } - + type MockjsToJSONSchema = (template: any) => MockjsToJSONSchemaRs; + let mock: MockjsMock; let setup: MockjsSetup; let Random: MockjsRandom; diff --git a/types/mockjs/mockjs-tests.ts b/types/mockjs/mockjs-tests.ts index 3cedd1528e..5c891066fe 100644 --- a/types/mockjs/mockjs-tests.ts +++ b/types/mockjs/mockjs-tests.ts @@ -1,4 +1,4 @@ -import * as Mock from 'mockjs' +import * as Mock from 'mockjs'; Mock.mock('/test', 'get', { name: 'mockjs' @@ -31,4 +31,4 @@ console.log(Mock.valid(template, data)); console.log(Mock.toJSONSchema(template)); -console.log(Mock.version); \ No newline at end of file +console.log(Mock.version); diff --git a/types/mockjs/tslint.json b/types/mockjs/tslint.json index 2750cc0197..faeaedf2e5 100644 --- a/types/mockjs/tslint.json +++ b/types/mockjs/tslint.json @@ -1 +1,8 @@ -{ "extends": "dtslint/dt.json" } \ No newline at end of file +{ + "extends": "dtslint/dt.json", + "rules": { + "dt-header": false, + "export-just-namespace": false, + "no-bad-reference": true + } +} \ No newline at end of file From af4c13d92b4f8b0b54b8a79b4100c2a0e9c56265 Mon Sep 17 00:00:00 2001 From: lavyun Date: Sat, 23 Sep 2017 10:06:33 +0800 Subject: [PATCH 6/6] update module import --- types/mockjs/mockjs-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mockjs/mockjs-tests.ts b/types/mockjs/mockjs-tests.ts index 5c891066fe..dd8b876087 100644 --- a/types/mockjs/mockjs-tests.ts +++ b/types/mockjs/mockjs-tests.ts @@ -1,4 +1,4 @@ -import * as Mock from 'mockjs'; +import Mock = require('mockjs'); Mock.mock('/test', 'get', { name: 'mockjs'