Skip to content

Commit 3ab0d1d

Browse files
authored
chore: Improve types (#428)
1 parent bef3ee3 commit 3ab0d1d

File tree

9 files changed

+178
-23
lines changed

9 files changed

+178
-23
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import Adapter from '@pollyjs/adapter';
22

3-
export default class FetchAdapter extends Adapter {}
3+
export default class FetchAdapter extends Adapter<{
4+
context?: any;
5+
}> {}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import Adapter from '@pollyjs/adapter';
22

3-
export default class PuppeteerAdapter extends Adapter {}
3+
export default class PuppeteerAdapter extends Adapter<{
4+
page: any;
5+
requestResourceTypes?: string[];
6+
}> {}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import Adapter from '@pollyjs/adapter';
22

3-
export default class XHRAdapter extends Adapter {}
3+
export default class XHRAdapter extends Adapter<{
4+
context?: any;
5+
}> {}
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1-
import { Headers, Request } from '@pollyjs/core';
1+
import { Polly, Headers, Request, Interceptor } from '@pollyjs/core';
22

3-
export default class Adapter {
3+
export default class Adapter<
4+
TOptions extends {} = {},
5+
TRequestArguments = any
6+
> {
47
static readonly id: string;
58
static readonly type: string;
6-
readonly options: { [key: string]: any };
7-
connect(): void;
8-
disconnect(): void;
9+
polly: Polly;
10+
isConnected: boolean;
11+
readonly defaultOptions: TOptions;
12+
readonly options: TOptions;
13+
persister: Polly['persister'];
14+
connect: () => void;
15+
onConnect: () => void;
16+
disconnect: () => void;
17+
onDisconnect: () => void;
18+
timeout: (request: Request, options: { time: number }) => Promise<void>;
19+
handleRequest: (request: {
20+
url: string;
21+
method: string;
22+
headers: Headers;
23+
body: string;
24+
requestArguments?: TRequestArguments;
25+
}) => Promise<Request>;
26+
passthrough: (request: Request) => Promise<void>;
27+
onIntercept: (request: Request, interceptor: Interceptor) => Promise<void>;
28+
intercept: (request: Request, interceptor: Interceptor) => Promise<void>;
29+
onRecord: (request: Request) => Promise<void>;
30+
record: (request: Request) => Promise<void>;
31+
onReplay: (request: Request) => Promise<void>;
32+
replay: (request: Request) => Promise<void>;
33+
assert: (message: string, condition?: boolean) => void;
34+
onPassthrough: (request: Request) => Promise<void>;
935
passthroughRequest(pollyRequest: Request): Promise<{
1036
statusCode: number;
1137
headers: Headers;
1238
body: string;
39+
encoding?: string;
1340
}>;
41+
respondToRequest: (request: Request, error?: Error) => Promise<void>;
42+
onIdentifyRequest: (request: Request) => Promise<void>;
43+
onRequest: (request: Request) => Promise<void>;
44+
onRequestFinished: (request: Request) => Promise<void>;
45+
onRequestFailed: (request: Request) => Promise<void>;
1446
}

packages/@pollyjs/core/types.d.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Adapter from '@pollyjs/adapter';
22
import Persister from '@pollyjs/persister';
3-
import { LogLevelDesc } from 'loglevel';
3+
import { Logger, LogLevelDesc } from 'loglevel';
44

55
export type MODE = 'record' | 'replay' | 'passthrough' | 'stopped';
66
export type ACTION = 'record' | 'replay' | 'intercept' | 'passthrough';
@@ -98,6 +98,8 @@ export interface HTTPBase {
9898
jsonBody(): any;
9999
}
100100

101+
export type RequestEvent = 'identify';
102+
101103
export interface Request extends HTTPBase {
102104
method: string;
103105
url: string;
@@ -109,29 +111,35 @@ export interface Request extends HTTPBase {
109111
hash: string;
110112
query: { [key: string]: string | string[] };
111113
readonly params: { [key: string]: string };
114+
readonly log: Logger;
115+
readonly requestArguments: any;
112116
recordingName: string;
117+
recordingId: string;
113118
responseTime?: number | undefined;
114119
timestamp?: string | undefined;
115120
didRespond: boolean;
116121
id?: string | undefined;
117122
order?: number | undefined;
118123
action: ACTION | null;
124+
aborted: boolean;
125+
promise: Promise<void>;
126+
configure(config: Partial<PollyConfig>): void;
127+
overrideRecordingName(recordingName: string): void;
128+
on(event: RequestEvent, listener: RequestEventListener): this;
129+
once(event: RequestEvent, listener: RequestEventListener): this;
130+
off(event: RequestEvent, listener?: RequestEventListener): this;
119131
}
120132
export interface Response extends HTTPBase {
121133
statusCode: number;
122-
isBinary: boolean;
134+
encoding: string | undefined;
123135
readonly statusText: string;
124136
readonly ok: boolean;
125137

126138
status(status: number): this;
127139
sendStatus(status: number): this;
128140
end(): Readonly<this>;
129141
}
130-
export interface Interceptor {
131-
abort(): void;
132-
passthrough(): void;
133-
stopPropagation(): void;
134-
}
142+
135143
export type RequestRouteEvent = 'request';
136144
export type RecordingRouteEvent = 'beforeReplay' | 'beforePersist';
137145
export type ResponseRouteEvent = 'beforeResponse' | 'response';
@@ -142,6 +150,10 @@ export interface ListenerEvent {
142150
readonly type: string;
143151
stopPropagation: () => void;
144152
}
153+
export interface Interceptor extends ListenerEvent {
154+
abort(): void;
155+
passthrough(): void;
156+
}
145157
export type ErrorEventListener = (
146158
req: Request,
147159
error: any,
@@ -205,7 +217,7 @@ export class RouteHandler {
205217
passthrough(value?: boolean): RouteHandler;
206218
intercept(fn: InterceptHandler): RouteHandler;
207219
recordingName(recordingName?: string): RouteHandler;
208-
configure(config: PollyConfig): RouteHandler;
220+
configure(config: Partial<PollyConfig>): RouteHandler;
209221
times(n?: number): RouteHandler;
210222
}
211223
export class PollyServer {
@@ -222,6 +234,15 @@ export class PollyServer {
222234
host(host: string, callback: () => void): void;
223235
namespace(path: string, callback: () => void): void;
224236
}
237+
export class PollyLogger {
238+
polly: Polly;
239+
log: Logger;
240+
connect: () => void;
241+
disconnect: () => void;
242+
logRequest: (request: Request) => void;
243+
logRequestResponse: (request: Request) => void;
244+
logRequestError: (request: Request, error: Error) => void;
245+
}
225246
export type PollyEvent = 'create' | 'stop' | 'register';
226247
export type PollyEventListener = (poll: Polly) => void;
227248
export class Polly {
@@ -241,6 +262,9 @@ export class Polly {
241262
persister: Persister | null;
242263
adapters: Map<string, Adapter>;
243264
config: PollyConfig;
265+
logger: PollyLogger;
266+
267+
private _requests: Request[];
244268

245269
pause(): void;
246270
play(): void;
@@ -249,7 +273,7 @@ export class Polly {
249273
passthrough(): void;
250274
stop(): Promise<void>;
251275
flush(): Promise<void>;
252-
configure(config: PollyConfig): void;
276+
configure(config: Partial<PollyConfig>): void;
253277
connectTo(name: string | typeof Adapter): void;
254278
disconnectFrom(name: string | typeof Adapter): void;
255279
disconnect(): void;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import Persister from '@pollyjs/persister';
22

3-
export default class FSPersister extends Persister {}
3+
export default class FSPersister extends Persister<{
4+
recordingsDir?: string;
5+
}> {}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import Persister from '@pollyjs/persister';
22

3-
export default class LocalStoragePersister extends Persister {}
3+
export default class LocalStoragePersister extends Persister<{
4+
context?: any;
5+
key?: string;
6+
}> {}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import Persister from '@pollyjs/persister';
22

3-
export default class RESTPersister extends Persister {}
3+
export default class RESTPersister extends Persister<{
4+
host?: string;
5+
apiNamespace?: string;
6+
}> {}
Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,90 @@
1-
export default class Persister {
1+
import { Polly, Request } from '@pollyjs/core';
2+
import { Cookie } from 'set-cookie-parser';
3+
4+
type NVObject = { name: string; value: string };
5+
6+
export interface HarRequest {
7+
httpVersion: string;
8+
url: string;
9+
method: string;
10+
headers: NVObject[];
11+
headersSize: number;
12+
queryString: NVObject[];
13+
cookies: Cookie[];
14+
postData?: {
15+
mimeType: string;
16+
params: [];
17+
text?: string;
18+
};
19+
bodySize?: number;
20+
}
21+
22+
export interface HarResponse {
23+
httpVersion: string;
24+
status: number;
25+
statusText: string;
26+
headers: NVObject[];
27+
headersSize: number;
28+
cookies: Cookie[];
29+
content: {
30+
mimeType: string;
31+
text?: string;
32+
encoding?: string;
33+
size?: number;
34+
};
35+
bodySize?: number;
36+
}
37+
38+
export interface HarEntry {
39+
_id: string;
40+
_order: number;
41+
startedDateTime: string;
42+
requests: HarRequest;
43+
response: HarResponse;
44+
cache: {};
45+
timings: {
46+
blocked: number;
47+
dns: number;
48+
connect: number;
49+
send: number;
50+
wait: number;
51+
receive: number;
52+
ssl: number;
53+
};
54+
time: number;
55+
}
56+
57+
export interface HarLog {
58+
version: string;
59+
browser: string;
60+
entries: HarEntry[];
61+
pages: [];
62+
addEntries: (entries: HarEntry[]) => void;
63+
sortEntries: () => void;
64+
}
65+
66+
export interface Har {
67+
log: HarLog;
68+
}
69+
70+
export default class Persister<TOptions extends {} = {}> {
271
static readonly id: string;
372
static readonly type: string;
4-
readonly options: { [key: string]: any };
5-
persist(): Promise<void>;
73+
readonly defaultOptions: TOptions;
74+
readonly options: TOptions;
75+
private pending: Map<string, { name: string; recordings: Request[] }>;
76+
private _cache: Map<string, Har>;
77+
polly: Polly;
78+
hasPending: boolean;
79+
persist: () => Promise<void>;
80+
recordRequest: (request: Request) => void;
81+
find: (recordingId: string) => Promise<Har | null>;
82+
save: (recordingId: string, har: Har) => Promise<void>;
83+
delete: (recordingId: string) => Promise<void>;
84+
findEntry: (request: Request) => Promise<HarEntry | null>;
85+
stringify: (value: any) => string;
86+
assert: (message: string, condition?: boolean) => void;
87+
findRecording: (recordingId: string) => Promise<Har | null>;
88+
saveRecording: (recordingId: string, har: Har) => Promise<void>;
89+
deleteRecording: (recordingId: string) => Promise<void>;
690
}

0 commit comments

Comments
 (0)