-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to intercept fetch requests
- Loading branch information
Showing
9 changed files
with
354 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Interceptor } from './Interceptor'; | ||
|
||
/** | ||
* A batch interceptor that exposes a single interface | ||
* to apply and operate with multiple interceptors at once. | ||
*/ | ||
export class BatchInterceptor { | ||
private interceptors: Interceptor[]; | ||
private subscriptions: Array<() => void> = []; | ||
|
||
constructor(interceptors: Interceptor[]) { | ||
this.interceptors = interceptors; | ||
} | ||
|
||
public setup() { | ||
for (const interceptor of this.interceptors) { | ||
interceptor.setup(); | ||
|
||
this.subscriptions.push(() => interceptor.teardown()); | ||
} | ||
} | ||
|
||
public on(event: string, listener: (...args: any[]) => void): void { | ||
for (const interceptor of this.interceptors) { | ||
interceptor.on(event, listener); | ||
} | ||
} | ||
|
||
public teardown() { | ||
for (const unsubscribe of this.subscriptions) { | ||
unsubscribe(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import crypto from 'crypto'; | ||
|
||
import { IsomorphicRequest } from './utils/IsomorphicRequest'; | ||
import { IsomorphicResponse } from './utils/IsomorphicResponse'; | ||
import { isInterceptable } from './utils/isInterceptable'; | ||
import { Interceptor, NodeRequestInterceptorOptions } from './Interceptor'; | ||
|
||
export class FetchInterceptor extends Interceptor { | ||
constructor(options?: NodeRequestInterceptorOptions) { | ||
super(options); | ||
} | ||
|
||
public static checkEnvironment() { | ||
return ( | ||
typeof globalThis !== 'undefined' && | ||
typeof globalThis.fetch !== 'undefined' | ||
); | ||
} | ||
|
||
public setup() { | ||
const pureFetch = globalThis.fetch; | ||
|
||
globalThis.fetch = async (input, init) => { | ||
const requestId = crypto.randomUUID(); | ||
const request = new Request(input, init); | ||
const _isInterceptable = isInterceptable({ | ||
url: new URL(request.url), | ||
ignoredDomains: this.options.ignoredDomains ?? [], | ||
baseUrl: this.options.baseUrl ?? '', | ||
allowLocalUrls: this.options.allowLocalUrls ?? false | ||
}); | ||
|
||
if (_isInterceptable) { | ||
const isomorphicRequest = await IsomorphicRequest.fromFetchRequest( | ||
request | ||
); | ||
this.emitter.emit('request', isomorphicRequest, requestId); | ||
} | ||
|
||
return pureFetch(request).then(async (response) => { | ||
if (_isInterceptable) { | ||
const isomorphicResponse = await IsomorphicResponse.fromFetchResponse( | ||
response | ||
); | ||
this.emitter.emit('response', isomorphicResponse, requestId); | ||
} | ||
return response; | ||
}); | ||
}; | ||
|
||
this.subscriptions.push(() => { | ||
globalThis.fetch = pureFetch; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
export interface NodeRequestInterceptorOptions { | ||
ignoredDomains?: string[]; | ||
allowLocalUrls?: boolean; | ||
baseUrl?: string; | ||
} | ||
|
||
export class Interceptor { | ||
protected emitter: EventEmitter; | ||
protected options: NodeRequestInterceptorOptions; | ||
protected subscriptions: Array<() => void> = []; | ||
|
||
constructor(options?: NodeRequestInterceptorOptions) { | ||
this.emitter = new EventEmitter(); | ||
this.options = options ?? {}; | ||
} | ||
|
||
public setup(): void { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public teardown(): void { | ||
for (const unsubscribe of this.subscriptions) { | ||
unsubscribe(); | ||
} | ||
this.emitter.removeAllListeners(); | ||
} | ||
|
||
public on(event: string, listener: (...args: any[]) => void): void { | ||
this.emitter.on(event, listener); | ||
} | ||
|
||
public static checkEnvironment() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.