request-mocking-protocol
Version:
A protocol for declarative mocking of HTTP requests
24 lines (19 loc) • 836 B
text/typescript
/**
* Global fetch interceptor.
*/
import { matchSchemas } from '../request-matcher/utils';
import { ResponseBuilder } from '../response-builder';
import { extractMockSchemas, GetHeaders } from '../transport';
export function setupFetchInterceptor(getIncomingHeaders: GetHeaders) {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const request = new Request(input, init);
const mockSchemas = await extractMockSchemas(getIncomingHeaders);
const matchResult = await matchSchemas(request, mockSchemas);
if (!matchResult) return originalFetch(input, init);
const { body, headers, status, statusText } = await new ResponseBuilder(matchResult, {
bypass: (req) => originalFetch(req),
}).build();
return new Response(body, { status, statusText, headers });
};
}