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

Add optional function to modify headers #307

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion packages/sandbox/src/EmbeddedSandbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EMBEDDABLE_SANDBOX_URL, IFRAME_DOM_ID } from './helpers/constants';
import { defaultHandleRequest } from './helpers/defaultHandleRequest';
import type { HandleRequest } from './helpers/postMessageRelayHelpers';
import type { HandleRequest, ModifyHeaders } from './helpers/postMessageRelayHelpers';
import { setupSandboxEmbedRelay } from './setupSandboxEmbedRelay';
import packageJSON from '../package.json';
import type { JSONObject } from './helpers/types';
Expand Down Expand Up @@ -53,6 +53,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 @@ -122,6 +127,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 @@ -12,22 +12,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;
}) {
const embedUrl = EMBEDDABLE_SANDBOX_URL(__testLocal__);
// Callback definition
const onPostMessageReceived = (event: IncomingEmbedMessage) => {
const onPostMessageReceived = async (event: IncomingEmbedMessage) => {
handleAuthenticationPostMessage({
event,
embedUrl,
Expand Down Expand Up @@ -85,7 +88,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 @@ -95,6 +98,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 @@ -112,6 +119,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