Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: simplify exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent 120ae47 commit 01401df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
22 changes: 15 additions & 7 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<I extends Context, T extends Plugins> = {
export type Base<I extends Context, T extends Plugins> = {
it: {
(expectation: string, cb?: (context: I) => any): void
(cb?: (context: I) => any): void
Expand All @@ -37,12 +45,12 @@ export type Fancy<I extends Context, T extends Plugins> = {
(expectation: string, cb?: (context: I) => any): void
(cb?: (context: I) => any): void
}
add<K extends string, O>(key: K, cb: (context: I) => Promise<O> | O): Fancy<I & {[P in K]: O}, T>
do(cb: (context: I) => any): Fancy<I, T>
register<K extends string, O, A1, A2, A3, A4>(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<O & I>): Fancy<I, T & {[P in K]: [O, A1, A2, A3, A4]}>
} & {[P in keyof T]: (arg1?: T[P][1], arg2?: T[P][2], arg3?: T[P][3], arg4?: T[P][4]) => Fancy<T[P][0] & I, T>}
add<K extends string, O>(key: K, cb: (context: I) => Promise<O> | O): Base<I & {[P in K]: O}, T>
do(cb: (context: I) => any): Base<I, T>
register<K extends string, O, A1, A2, A3, A4>(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<O & I>): Base<I, T & {[P in K]: {output: O, a1: A1, a2: A2, a3: A3, a4: A4}}>
} & {[P in keyof T]: (arg1?: T[P]['a1'], arg2?: T[P]['a2'], arg3?: T[P]['a3'], arg4?: T[P]['a4']) => Base<T[P]['output'] & I, T>}

const base = <I extends Context>(context: I): Fancy<I, {}> => {
const base = <I extends Context>(context: I): Base<I, {}> => {
const end = (arg1: any, cb: any) => {
context = assignWithProps({}, context)
if (_.isFunction(arg1)) {
Expand Down
10 changes: 7 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down

0 comments on commit 01401df

Please sign in to comment.