From 4d8c2d26cb279e8e894476648c4e75aefff12b79 Mon Sep 17 00:00:00 2001 From: Ovidiu Barabula Date: Fri, 5 Jun 2020 14:53:43 +0100 Subject: [PATCH 1/3] feat(types): add Typescript declaration file --- types/superagent-mock.d.ts | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 types/superagent-mock.d.ts diff --git a/types/superagent-mock.d.ts b/types/superagent-mock.d.ts new file mode 100644 index 0000000..c89e55f --- /dev/null +++ b/types/superagent-mock.d.ts @@ -0,0 +1,79 @@ +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[] + | ObjectLiteral + | ObjectLiteral[]; + + interface ObjectLiteral { + [key: string]: AnyValue; + [key: number]: AnyValue; + } + + export type RequestBody = ObjectLiteral; + export type Response = Fixture | ObjectLiteral; + export type Fixture = AnyValue; + + 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: RequestBody, + headers: { [key: string]: string }, + context: Context + ): Fixture | null | undefined; + }; + + export interface Log { + matcher: string; + mocked: boolean; + url: string; + method: Method; + data: ObjectLiteral; + 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; +} From 16d5d7185df0448bd803f46aaf3685121971b1d4 Mon Sep 17 00:00:00 2001 From: Ovidiu Barabula Date: Fri, 5 Jun 2020 14:54:32 +0100 Subject: [PATCH 2/3] feat(package): point to Typescript declaration file in package.json --- package.json | 1 + 1 file changed, 1 insertion(+) 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" From 6a568be4ece864955a1c8f43135ca723de761184 Mon Sep 17 00:00:00 2001 From: Ovidiu Barabula Date: Mon, 8 Jun 2020 20:54:43 +0100 Subject: [PATCH 3/3] fix(types): allow more relaxed Fixture types --- types/superagent-mock.d.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/types/superagent-mock.d.ts b/types/superagent-mock.d.ts index c89e55f..6c6a1af 100644 --- a/types/superagent-mock.d.ts +++ b/types/superagent-mock.d.ts @@ -10,17 +10,11 @@ declare module 'superagent-mock' { | number[] | boolean | boolean[] - | ObjectLiteral - | ObjectLiteral[]; + | {} + | Array<{}>; - interface ObjectLiteral { - [key: string]: AnyValue; - [key: number]: AnyValue; - } - - export type RequestBody = ObjectLiteral; - export type Response = Fixture | ObjectLiteral; export type Fixture = AnyValue; + export type Response = Fixture; export interface Context { method: Method; @@ -47,7 +41,7 @@ declare module 'superagent-mock' { fixtures( match: RegExpExecArray, - reqBody: RequestBody, + reqBody: {}, headers: { [key: string]: string }, context: Context ): Fixture | null | undefined; @@ -58,7 +52,7 @@ declare module 'superagent-mock' { mocked: boolean; url: string; method: Method; - data: ObjectLiteral; + data: {}; headers: { [key: string]: string }; timestamp: number; }