Add types for mockjs

This commit is contained in:
lavyun
2017-09-17 19:18:55 +08:00
parent d537c6b050
commit 77523b9bc3
4 changed files with 371 additions and 0 deletions

313
types/mockjs/index.d.ts vendored Normal file
View File

@@ -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;

View File

@@ -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);

View File

@@ -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"
]
}

1
types/mockjs/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }