diff --git a/types/six-runtime/index.d.ts b/types/six-runtime/index.d.ts new file mode 100644 index 0000000000..e31a526666 --- /dev/null +++ b/types/six-runtime/index.d.ts @@ -0,0 +1,219 @@ +// Type definitions for six-runtime 0.1 +// Project: https://github.com/493636333/six +// Definitions by: liyong +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 + +declare const six: Six; + +interface Six { + /** + * process.env.NODE_ENV + */ + readonly NODE_ENV: string; + + /** + * NODE_ENV的缩短版 + * + * production、prod => prod + * + * dev、develop、development => dev + */ + readonly ENV: string; + + /** + * Six的rootPath + */ + readonly ROOT_PATH: string; + + /** + * Six的plugin的map + */ + readonly plugins: Six.PluginsMap; + + /** + * 针对这次请求生效的数据缓存 set + */ + set(key: string, value: any): void; + + /** + * 针对这次请求生效的数据缓存 get + */ + get(key: string): any; + + /** + * DataProvider类,所有的data-provider必须继承这个类 + */ + DataProvider: Six.plugins.dataProvider.DataProvider; + + /** + * 执行某个data-provider,并返回执行之后的值 + */ + dp(name: string): any; + + /** + * 发送请求的工具方法,会将用户的cookie和traceId透传过去 + */ + http: Six.plugins.http; + + /** + * 打日志方法 + */ + log: Six.plugins.log; + + /** + * rpc调用工具方法,返回ThriftPool实例 + */ + rpc: Six.plugins.rpc; + + /** + * 时间统计工具 + */ + time: Six.plugins.time.timmer; + + /** + * 渲染函数 + */ + render: Six.plugins.view.render; +} + +declare namespace Six { + interface PluginsMap { + sso: plugins.sso.ssoFactory; + upm: plugins.upm.upmFactory; + uac: plugins.uac.uacFactory; + static: plugins.static.staticFactory; + } + + type next = () => any; + + type middleware = (ctx: object, next: next) => any; + + namespace plugins { + type whiteList = (ctx: object) => boolean; + + namespace sso { + type ssoFactory = (conf: ssoConf) => any; + + interface ssoConf { + whiteList: string|RegExp|string[]|whiteList; + ssoConf: ssoClientConf; + enableCache: boolean; + hasLoginCacheMaxAge: number; + adapter(user: object): object; + } + interface ssoClientConf { + clientId: string; + secret: string; + env: string; + token: token; + protocol: string; + errorHook(error: Error): void; + callbackUrl: string; + } + + interface token { + name: string; + path: string; + httpOnly: boolean; + expiredTime: number; + } + } + + namespace upm { + type upmFactory = (conf: upmConf) => any; + + interface upmConf { + whiteList: string|RegExp|string[]|whiteList; + enableMock: boolean; + domain: string; + path: string; + clientId: string; + clientSecret: string; + skipOffline: boolean; + failHook(err: Error, ctx: object, next: next): any; + getPath(ctx: object): string; + } + } + + namespace uac { + type uacFactory = (conf: uacConf) => any; + interface uacConf { + whiteList: string|RegExp|string[]|whiteList; + enableMock: boolean; + client_id: string; + client_secret: string; + skipOffline: boolean; + failHook(err: Error, ctx: object, next: next): any; + getPath(ctx: object): string; + } + } + + namespace dataProvider { + abstract class DataProvider { + static deps: string[]; + static timeout: number; + static retry: number; + prepare(ctx: object, depsRes: object): any; + abstract execute(ctx: object, args: any): any; + } + } + + interface http { + proxy(options: object): middleware; + get(url: any, options?: object): Promise; + post(url: any, options?: object): Promise; + put(url: any, options?: object): Promise; + patch(url: any, options?: object): Promise; + del(url: any, options?: object): Promise; + head(url: any, options?: object): Promise; + } + + interface log { + debug(msg: string, ...restOfVar: any[]): void; + info(msg: string, ...restOfVar: any[]): void; + warn(msg: string, ...restOfVar: any[]): void; + error(msg: string|Error, ...restOfVar: any[]): void; + } + + type rpc = (config: object) => object; + + namespace static { + type staticFactory = (conf: staticConf) => middleware; + interface staticConf { + rootPath: string; + cacheConf: cacheConf; + } + + interface cacheConf { + defer: boolean; + cache: boolean; + maxAge: number; + index: string; + gzip: boolean; + extensions: boolean|string[]; + } + } + + namespace time { + interface timmer { + now(): any; + start(key: string): void; + end(key: string): number; + serialize(between?: string, end?: string, filter?: filter): string; + } + + type filter = (key: string) => boolean; + } + + namespace view { + type render = (name: string, runtimeConf?: runtimeConf|null, data?: object) => Promise; + + interface runtimeConf { + codeKey: string; + msgKey: string; + dataKey: string; + } + } + } +} diff --git a/types/six-runtime/six-runtime-tests.ts b/types/six-runtime/six-runtime-tests.ts new file mode 100644 index 0000000000..906ddbd233 --- /dev/null +++ b/types/six-runtime/six-runtime-tests.ts @@ -0,0 +1 @@ +six.log.warn('xxx'); diff --git a/types/six-runtime/tsconfig.json b/types/six-runtime/tsconfig.json new file mode 100644 index 0000000000..60a8cb89af --- /dev/null +++ b/types/six-runtime/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "six-runtime-tests.ts" + ] +} \ No newline at end of file diff --git a/types/six-runtime/tslint.json b/types/six-runtime/tslint.json new file mode 100644 index 0000000000..30a1bdde2e --- /dev/null +++ b/types/six-runtime/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file