diff --git a/src/base.ts b/src/base.ts index f5d40d0..f9d32a2 100644 --- a/src/base.ts +++ b/src/base.ts @@ -26,9 +26,17 @@ const context: Context = { chain: [], } -export interface Plugins {[k: string]: [object, any, any, any, any]} +export interface PluginDef { + output: object + a1: any + a2: any + a3: any + a4: any +} + +export interface Plugins {[k: string]: PluginDef} -export type Fancy = { +export type Base = { it: { (expectation: string, cb?: (context: I) => any): void (cb?: (context: I) => any): void @@ -37,12 +45,12 @@ export type Fancy = { (expectation: string, cb?: (context: I) => any): void (cb?: (context: I) => any): void } - add(key: K, cb: (context: I) => Promise | O): Fancy - do(cb: (context: I) => any): Fancy - register(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin): Fancy -} & {[P in keyof T]: (arg1?: T[P][1], arg2?: T[P][2], arg3?: T[P][3], arg4?: T[P][4]) => Fancy} + add(key: K, cb: (context: I) => Promise | O): Base + do(cb: (context: I) => any): Base + register(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin): Base +} & {[P in keyof T]: (arg1?: T[P]['a1'], arg2?: T[P]['a2'], arg3?: T[P]['a3'], arg4?: T[P]['a4']) => Base} -const base = (context: I): Fancy => { +const base = (context: I): Base => { const end = (arg1: any, cb: any) => { context = assignWithProps({}, context) if (_.isFunction(arg1)) { diff --git a/src/env.ts b/src/env.ts index e5a3600..7025aeb 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,16 +1,20 @@ +import {Plugin} from './base' + export interface EnvOptions { clear?: boolean } +export interface Env extends Plugin<{envs: (typeof process.env)[]}> {} + export default (env: {[k: string]: string | undefined}, opts: EnvOptions = {}) => ({ - run(ctx: {envs: (typeof process.env)[]}) { + run(ctx) { ctx.envs = ctx.envs || [] ctx.envs.push(process.env) if (opts.clear) process.env = {...env} else process.env = {...process.env, ...env} }, - finally(ctx: {envs: (typeof process.env)[]}) { + finally(ctx) { const env = ctx.envs.pop() if (env) process.env = env }, -}) +}) as Env diff --git a/src/index.ts b/src/index.ts index e5ce13e..d3c5aed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import base, {Context, Fancy, Plugin} from './base' +import base, {Base, Context, Plugin} from './base' import _catch from './catch' import env, {EnvOptions} from './env' import nock, {NockScope} from './nock' @@ -13,9 +13,11 @@ export const fancy = base .register('stderr', stderr) .register('stdout', stdout) +export type Fancy = typeof fancy + export { Context, - Fancy, + Base, Plugin, EnvOptions, NockScope,