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

Commit

Permalink
fix: consolidate types
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent 7592a8b commit 38bbfa2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 61 deletions.
45 changes: 3 additions & 42 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,15 @@

import * as _ from 'lodash'

export type PluginBuilder<I, A1 = undefined, A2 = undefined, A3 = undefined, A4 = undefined> = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<I>
import * as Types from './types'

export interface Plugin<I> {
run?(context: I): any
init?(context: I): any
finally?(context: I): any
catch?(context: I): any
}

export interface Context {
test: (typeof it | typeof it.skip)
plugins: {[k: string]: PluginBuilder<any, any, any, any, any>}
expectation?: string
chain: Plugin<any>[]
error?: Error & {code?: string}
}

const context: Context = {
const context: Types.Context = {
test: it,
plugins: {},
chain: [],
}

export interface PluginDef {
output: object
a1: any
a2: any
a3: any
a4: any
}

export interface Plugins {[k: string]: PluginDef}

export type Base<I extends Context, T extends Plugins> = {
it: {
(expectation: string, cb?: (context: I) => any): void
(cb?: (context: I) => any): void
}
end: {
(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): 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): Base<I, {}> => {
const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
const end = (arg1: any, cb: any) => {
context = assignWithProps({}, context)
if (_.isFunction(arg1)) {
Expand Down
8 changes: 1 addition & 7 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {Plugin} from './base'

export interface EnvOptions {
clear?: boolean
}

export interface Env extends Plugin<{envs: (typeof process.env)[]}> {}
import {Env, EnvOptions} from './types'

export default (env: {[k: string]: string | undefined}, opts: EnvOptions = {}) => ({
run(ctx) {
Expand Down
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base, {Base, Context, Plugin} from './base'
import base from './base'
import _catch from './catch'
import {expect} from './chai'
import env, {EnvOptions} from './env'
import env from './env'
import nock, {NockScope} from './nock'
import {stderr, stdout} from './stdmock'
import stub from './stub'
Expand All @@ -19,12 +19,8 @@ export const fancy = base
export type Fancy = typeof fancy

export {
expect,
FancyTypes,
Base,
Context,
EnvOptions,
NockScope,
Plugin,
expect,
}
export default fancy
4 changes: 2 additions & 2 deletions src/nock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Nock from 'nock'

export {Scope as NockScope} from 'nock'

export default (host: string, cb: (nock: Nock.Scope) => any) => {
const nock: typeof Nock = require('nock')
const intercepter = nock(host)
Expand All @@ -16,5 +18,3 @@ export default (host: string, cb: (nock: Nock.Scope) => any) => {
},
}
}

export {Scope as NockScope} from 'nock'
49 changes: 46 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
export {Base, Context, Plugin} from './base'
export {EnvOptions} from './env'
export {NockScope} from './nock'
export type PluginBuilder<I, A1 = undefined, A2 = undefined, A3 = undefined, A4 = undefined> = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<I>

export interface Context {
test: (typeof it | typeof it.skip)
plugins: {[k: string]: PluginBuilder<any, any, any, any, any>}
expectation?: string
chain: Plugin<any>[]
error?: Error & {code?: string}
}

export interface Plugin<I> {
run?(context: I): any
init?(context: I): any
finally?(context: I): any
catch?(context: I): any
}

export interface PluginDef {
output: object
a1: any
a2: any
a3: any
a4: any
}

export interface Plugins {[k: string]: PluginDef}

export type Base<I extends Context, T extends Plugins> = {
it: {
(expectation: string, cb?: (context: I) => any): void
(cb?: (context: I) => any): void
}
end: {
(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): 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>}

export interface EnvOptions {
clear?: boolean
}

export interface Env extends Plugin<{envs: (typeof process.env)[]}> {}

0 comments on commit 38bbfa2

Please sign in to comment.