diff --git a/package.json b/package.json index 7488180..ef94410 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "MIT", "main": "lib/superagent-mock.js", "module": "es/superagent-mock.js", + "types": "types/superagent-mock.d.ts", "repository": { "type": "git", "url": "https://github.com/M6Web/superagent-mock" diff --git a/types/superagent-mock.d.ts b/types/superagent-mock.d.ts new file mode 100644 index 0000000..6c6a1af --- /dev/null +++ b/types/superagent-mock.d.ts @@ -0,0 +1,73 @@ +declare module 'superagent-mock' { + import superagent from 'superagent'; + + export type Method = 'head' | 'get' | 'post' | 'put' | 'delete'; + + export type AnyValue = + | string + | string[] + | number + | number[] + | boolean + | boolean[] + | {} + | Array<{}>; + + export type Fixture = AnyValue; + export type Response = Fixture; + + export interface Context { + method: Method; + cancel?: boolean; + delay?: number; + progress?: { + parts: number; + delay?: number; + total?: number; + lengthComputable?: boolean; + direction?: 'upload' | string; + }; + } + + type ParserMethods = { + [key in Method]?: ( + match: RegExpExecArray, + fixtures: ReturnType + ) => Response; + }; + + export type Config = ParserMethods & { + pattern: string; + + fixtures( + match: RegExpExecArray, + reqBody: {}, + headers: { [key: string]: string }, + context: Context + ): Fixture | null | undefined; + }; + + export interface Log { + matcher: string; + mocked: boolean; + url: string; + method: Method; + data: {}; + headers: { [key: string]: string }; + timestamp: number; + } + + export type Logger = (log: Log) => void; + + export interface TearDown { + unset(): void; + } + + function initialise( + sa: typeof superagent, + configs: Config[], + logger?: Logger + ): TearDown; + + export default initialise; +}