Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-apples-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/sandbox': minor
---

Adding support for optional `modifyHeaders` function which can modify the headers before sending the request
7 changes: 7 additions & 0 deletions packages/sandbox/src/EmbeddedSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defaultHandleRequest } from './helpers/defaultHandleRequest';
import type {
DisposableResource,
HandleRequest,
ModifyHeaders,
} from './helpers/postMessageRelayHelpers';
import { setupSandboxEmbedRelay } from './setupSandboxEmbedRelay';
import packageJSON from '../package.json';
Expand Down Expand Up @@ -59,6 +60,11 @@ export interface EmbeddableSandboxOptions {
*/
handleRequest?: HandleRequest;

/**
* optional. Function that accepts the original headers and returns the modified headers.
*/
modifyHeaders?: ModifyHeaders;

/**
* optional. If this is passed, its value will take precedence over your sandbox connection settings `includeCookies` value.
* If you pass `handleRequest`, that will override this value and its behavior.
Expand Down Expand Up @@ -128,6 +134,7 @@ export class EmbeddedSandbox {
this.disposable = setupSandboxEmbedRelay({
embeddedSandboxIFrameElement: this.embeddedSandboxIFrameElement,
handleRequest: this.handleRequest,
modifyHeaders: this.options.modifyHeaders,
__testLocal__: !!this.__testLocal__,
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sandbox/src/helpers/postMessageRelayHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export type HandleRequest = (
endpointUrl: string,
options: Omit<RequestInit, 'headers'> & { headers: Record<string, string> }
) => Promise<Response>;
export type ModifyHeaders = (
endpointUrl: string,
headers: Record<string, string> | undefined
) => Promise<Record<string, string> | undefined>;

export type SocketStatus = 'disconnected' | 'connecting' | 'connected';

Expand Down
15 changes: 13 additions & 2 deletions packages/sandbox/src/setupSandboxEmbedRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ import {
handleAuthenticationPostMessage,
HandleRequest,
IncomingEmbedMessage,
ModifyHeaders,
sendPostMessageToEmbed,
} from './helpers/postMessageRelayHelpers';
import { executeSubscription } from './helpers/subscriptionPostMessageRelayHelpers';

export function setupSandboxEmbedRelay({
handleRequest,
modifyHeaders,
embeddedSandboxIFrameElement,
__testLocal__,
}: {
handleRequest: HandleRequest;
modifyHeaders?: ModifyHeaders;
embeddedSandboxIFrameElement: HTMLIFrameElement;
__testLocal__: boolean;
}): DisposableResource {
const embedUrlOrigin = EMBEDDABLE_SANDBOX_URL_ORIGIN(__testLocal__);
// Callback definition
const onPostMessageReceived = (event: IncomingEmbedMessage) => {
const onPostMessageReceived = async (event: IncomingEmbedMessage) => {
handleAuthenticationPostMessage({
event,
embedUrlOrigin,
Expand Down Expand Up @@ -87,7 +90,7 @@ export function setupSandboxEmbedRelay({
data.operationId
) {
// Extract the operation details from the event.data object
const { operation, variables, operationName, operationId, headers } =
const { operation, variables, operationName, operationId, headers: originalHeaders } =
data;

if (isQueryOrMutation) {
Expand All @@ -97,6 +100,10 @@ export function setupSandboxEmbedRelay({
'Something went wrong, we should not have gotten here. The sandbox endpoint url was not sent.'
);
}
// If the user has provided a function to modify headers, call it
const headers = modifyHeaders
? await modifyHeaders(endpointUrl, originalHeaders)
: originalHeaders;
executeOperation({
endpointUrl,
handleRequest,
Expand All @@ -114,6 +121,10 @@ export function setupSandboxEmbedRelay({
});
} else if (isSubscription) {
const { httpMultipartParams } = data;
// If the user has provided a function to modify headers, call it
const headers = modifyHeaders
? await modifyHeaders(data.subscriptionUrl, originalHeaders)
: originalHeaders;
executeSubscription({
operation,
operationName,
Expand Down