[@pollyjs/core] Update types for v4.0.0 (#41769)

This commit is contained in:
Offir Golan
2020-01-24 16:56:24 -08:00
committed by Ben Lichtman
parent bc6cbcd0ea
commit 60c8a720cc
3 changed files with 59 additions and 35 deletions
+23 -21
View File
@@ -1,4 +1,4 @@
// Type definitions for @pollyjs/core 3.0
// Type definitions for @pollyjs/core 4.0
// Project: https://github.com/netflix/pollyjs/tree/master/packages/@pollyjs/core
// Definitions by: feinoujc <https://github.com/feinoujc>
// Borui Gu <https://github.com/BoruiGu>
@@ -18,7 +18,7 @@ export const Timing: {
relative(ratio: number): (ms: number) => Promise<void>;
};
export type MatchBy<T = string, R = T> = (input: T) => R;
export type MatchBy<T = string, R = T> = (input: T, req: Request) => R;
export type Headers = Record<string, string | string[]>;
export interface PollyConfig {
mode?: MODE;
@@ -26,7 +26,7 @@ export interface PollyConfig {
adapters?: Array<string | typeof Adapter>;
adapterOptions?: {
fetch?: { context?: any };
puppeteer?: { page?: any };
puppeteer?: { page?: any; requestResourceTypes?: string[] };
xhr?: { context?: any };
[key: string]: any;
};
@@ -43,8 +43,6 @@ export interface PollyConfig {
logging?: boolean;
recordIfMissing?: boolean;
/** @deprecated use expiryStrategy */
recordIfExpired?: boolean;
recordFailedRequests?: boolean;
expiryStrategy?: EXPIRY_STRATEGY;
@@ -57,16 +55,19 @@ export interface PollyConfig {
body?: boolean | MatchBy<any>;
order?: boolean;
url?: {
protocol?: boolean | MatchBy;
username?: boolean | MatchBy;
password?: boolean | MatchBy;
hostname?: boolean | MatchBy;
port?: boolean | MatchBy<number>;
pathname?: boolean | MatchBy;
query?: boolean | MatchBy<any>;
hash?: boolean | MatchBy;
};
url?:
| boolean
| MatchBy
| {
protocol?: boolean | MatchBy;
username?: boolean | MatchBy;
password?: boolean | MatchBy;
hostname?: boolean | MatchBy;
port?: boolean | MatchBy<number>;
pathname?: boolean | MatchBy;
query?: boolean | MatchBy<{ [key: string]: any }>;
hash?: boolean | MatchBy;
};
};
}
export interface HTTPBase {
@@ -185,12 +186,13 @@ export class Polly {
adapters: Map<string, Adapter>;
config: PollyConfig;
pause: () => void;
play: () => void;
replay: () => void;
record: () => void;
stop: () => Promise<void>;
flush: () => Promise<void>;
pause(): void;
play(): void;
replay(): void;
record(): void;
passthrough(): void;
stop(): Promise<void>;
flush(): Promise<void>;
configure(config: PollyConfig): void;
connectTo(name: string | typeof Adapter): void;
disconnectFrom(name: string | typeof Adapter): void;
+35 -13
View File
@@ -39,6 +39,10 @@ new Polly('test recording', {
fetch: {
context: {},
},
puppeteer: {
page: {},
requestResourceTypes: ['fetch', 'xhr'],
},
foo: {
bar: true,
},
@@ -55,17 +59,17 @@ new Polly('test recording', {
},
timing: Timing.relative(3),
matchRequestsBy: {
method(method) {
method(method, _req) {
return method.toLowerCase();
},
headers:
1 === 1
? { exclude: ['X-Auth'] }
: headers => {
: (headers, _req) => {
delete headers['X-Auth'];
return headers;
},
body(body) {
body(body, _req) {
const json = JSON.parse(body);
delete json.email;
@@ -73,34 +77,48 @@ new Polly('test recording', {
},
url: {
protocol(protocol) {
protocol(protocol, _req) {
return protocol === 'http' ? 'https:' : protocol;
},
username(username) {
username(username, _req) {
return username === 'johndoe' ? 'username' : username;
},
password(password) {
password(password, _req) {
return password || 'password';
},
hostname(hostname) {
hostname(hostname, _req) {
return hostname.replace('.com', '.net');
},
port(port) {
port(port, _req) {
return port > 80 ? 3000 : 433;
},
pathname(pathname) {
pathname(pathname, _req) {
return pathname.replace('/api/v1', '/api');
},
query(query) {
query(query, _req) {
return { ...query, token: '' };
},
hash(hash) {
hash(hash, _req) {
return hash.replace(/token=[0-9]+/, '');
},
},
},
});
polly.configure({
matchRequestsBy: {
url: false,
},
});
polly.configure({
matchRequestsBy: {
url(url, _req) {
return url.replace('https', 'http');
},
},
});
function log(_: string) {
// no op
}
@@ -110,6 +128,12 @@ async function test() {
polly.pause();
polly.play();
polly.record();
polly.replay();
polly.passthrough();
await polly.flush();
await polly.stop();
const { server } = polly;
server.get('/session').on('request', req => {
req.headers['X-AUTH'] = '<ACCESS_TOKEN>';
@@ -192,8 +216,6 @@ async function test() {
req.removeHeaders(['Content-Type', 'Content-Length']);
log(req.pathname + JSON.stringify(error));
});
await polly.flush();
}
setupMocha();
@@ -1,7 +1,7 @@
import { setupPolly } from 'setup-polly-jest';
setupPolly();
setupPolly({ recordIfExpired: true });
setupPolly({ recordIfMissing: true });
setupPolly({ adapters: ['xhr'] });
const context = setupPolly();