Skip to content

Commit

Permalink
Merge pull request #127 from Shopify/fd-dynamic-storefront-credentials
Browse files Browse the repository at this point in the history
Pass storefront credentials directly to handleRequest
  • Loading branch information
frandiox authored Oct 26, 2022
2 parents 411ea61 + 89f8cb0 commit 5ae103c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
39 changes: 25 additions & 14 deletions packages/@hydrogen/remix/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ type HydrogenHandlerParams = {
storefront: StorefrontClientProps;
};

export function createRequestHandler({
storefront,
getLoadContext,
...oxygenHandlerParams
}: Parameters<typeof createOxygenRequestHandler>[0] & HydrogenHandlerParams) {
return createOxygenRequestHandler({
...oxygenHandlerParams,
getLoadContext: async (request) => {
const context = getLoadContext ? await getLoadContext(request) : {};
// @ts-ignore
context.storefront = createStorefrontClient(storefront);
export function createRequestHandler(
oxygenHandlerParams: Parameters<typeof createOxygenRequestHandler>[0],
) {
const handleRequest = createOxygenRequestHandler(oxygenHandlerParams);

return context;
},
});
return (
request: Request,
{
storefront,
context,
...options
}: Omit<Parameters<typeof handleRequest>[1], 'loadContext'> &
HydrogenHandlerParams,
) => {
try {
return handleRequest(request, {
...options,
context: {...context, storefront: createStorefrontClient(storefront)},
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);

return new Response('Internal Error', {status: 500});
}
};
}
16 changes: 10 additions & 6 deletions packages/@remix-run/oxygen/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ export function createRequestHandler<Context = unknown>({
request: Request,
// This may be temporary unless we adjust @shopify/oxygen-workers-types
{
ctx,
env,
}: {ctx: Omit<ExecutionContext, 'passThroughOnException'>; env: any},
context,
}: {
env: any;
context: Omit<ExecutionContext, 'passThroughOnException'> & {
[key: string]: any;
};
},
) => {
try {
if (
Expand All @@ -36,14 +41,13 @@ export function createRequestHandler<Context = unknown>({
return fetch(request.url.replace(url.origin, assetBasePath), request);
}

const loadContext = await getLoadContext?.(request);

return await handleRequest(request, {
env,
...ctx,
...loadContext,
...context,
...(await getLoadContext?.(request)),
} as AppLoadContext);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);

return new Response('Internal Error', {status: 500});
Expand Down
15 changes: 8 additions & 7 deletions worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ const requestHandler = createRequestHandler({
build: remixBuild,
mode: process.env.NODE_ENV,
shouldProxyAsset: () => false,
storefront: {
publicStorefrontToken: '3b580e70970c4528da70c98e097c2fa0',
storeDomain: 'hydrogen-preview',
storefrontApiVersion: '2022-07',
},
});

export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
context: ExecutionContext,
): Promise<Response> {
try {
return await requestHandler(request, {
env,
ctx,
context,
storefront: {
publicStorefrontToken: '3b580e70970c4528da70c98e097c2fa0',
storeDomain: 'hydrogen-preview',
storefrontApiVersion: '2022-07',
},
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return new Response('An unexpected error occurred', {status: 500});
}
Expand Down

0 comments on commit 5ae103c

Please sign in to comment.