Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SetupApi): validate given request handlers #1460

Merged
merged 1 commit into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SetupApi.ts
Expand Up @@ -22,7 +22,7 @@ export abstract class SetupApi<EventsMap extends EventMapType> {

public readonly events: LifeCycleEventEmitter<EventsMap>

constructor(initialHandlers: Array<RequestHandler>) {
constructor(...initialHandlers: Array<RequestHandler>) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the accepted initialHandlers were never spread, they always arrive as an Array of handlers, regardless of how they were provided to the setupServer/setupWorker function calls.

Preserving the argument spread enables the correct validation.

this.validateHandlers(initialHandlers)

this.initialHandlers = toReadonlyArray(initialHandlers)
Expand Down
2 changes: 1 addition & 1 deletion src/native/index.ts
Expand Up @@ -12,5 +12,5 @@ export function setupServer(
): SetupServerApi {
// Provision request interception via patching the `XMLHttpRequest` class only
// in React Native. There is no `http`/`https` modules in that environment.
return new SetupServerApi([XMLHttpRequestInterceptor], handlers)
return new SetupServerApi([XMLHttpRequestInterceptor], ...handlers)
}
4 changes: 2 additions & 2 deletions src/node/SetupServerApi.ts
Expand Up @@ -39,9 +39,9 @@ export class SetupServerApi extends SetupApi<ServerLifecycleEventsMap> {
interceptors: Array<{
new (): Interceptor<HttpRequestEventMap>
}>,
handlers: Array<RequestHandler>,
...handlers: Array<RequestHandler>
) {
super(handlers)
super(...handlers)

this.interceptor = new BatchInterceptor({
name: 'setup-server',
Expand Down
2 changes: 1 addition & 1 deletion src/node/setupServer.ts
Expand Up @@ -13,6 +13,6 @@ export const setupServer = (
): SetupServerApi => {
return new SetupServerApi(
[ClientRequestInterceptor, XMLHttpRequestInterceptor],
handlers,
...handlers,
)
}
6 changes: 3 additions & 3 deletions src/setupWorker/setupWorker.ts
Expand Up @@ -32,8 +32,8 @@ export class SetupWorkerApi extends SetupApi<WorkerLifecycleEventsMap> {
private stopHandler: StopHandler = null as any
private listeners: Array<Listener>

constructor(handlers: Array<RequestHandler>) {
super(handlers)
constructor(...handlers: Array<RequestHandler>) {
super(...handlers)

invariant(
!isNodeProcess(),
Expand Down Expand Up @@ -224,5 +224,5 @@ export class SetupWorkerApi extends SetupApi<WorkerLifecycleEventsMap> {
export function setupWorker(
...handlers: Array<RequestHandler>
): SetupWorkerApi {
return new SetupWorkerApi(handlers)
return new SetupWorkerApi(...handlers)
}