Skip to content

Commit dda10ad

Browse files
committed
send bearer token and user agent in proxy calls
1 parent ffc6646 commit dda10ad

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

packages/theme/src/cli/utilities/theme-environment/proxy.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-dynamic-delete */
22
import {buildCookies} from './storefront-renderer.js'
3+
import {cleanHeader, defaultHeaders} from './storefront-utils.js'
34
import {logRequestLine} from '../log-request-line.js'
45
import {createFetchError, extractFetchErrorInfo} from '../errors.js'
56
import {renderWarning} from '@shopify/cli-kit/node/ui'
@@ -305,20 +306,24 @@ export function proxyStorefrontRequest(event: H3Event, ctx: DevServerContext): P
305306
const headers = getProxyStorefrontHeaders(event)
306307
const body = getRequestWebStream(event)
307308

309+
const finalHeaders = cleanHeader({
310+
...headers,
311+
...defaultHeaders(),
312+
Authorization: `Bearer ${ctx.session.storefrontToken}`,
313+
// Required header for CDN requests
314+
referer: url.origin,
315+
// Update the cookie with the latest session
316+
Cookie: buildCookies(ctx.session, {headers}),
317+
})
318+
308319
// eslint-disable-next-line no-restricted-globals
309320
return fetch(url, {
310321
method: event.method,
311322
body,
312323
duplex: body ? 'half' : undefined,
313324
// Important to return 3xx responses to the client
314325
redirect: 'manual',
315-
headers: {
316-
...headers,
317-
// Required header for CDN requests
318-
referer: url.origin,
319-
// Update the cookie with the latest session
320-
cookie: buildCookies(ctx.session, {headers}),
321-
},
326+
headers: finalHeaders,
322327
} as RequestInit & {duplex?: 'half'})
323328
.then((response) => patchProxiedResponseHeaders(ctx, response))
324329
.catch((error: Error) => {

packages/theme/src/cli/utilities/theme-environment/storefront-renderer.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {parseCookies, serializeCookies} from './cookies.js'
2-
import {defaultHeaders, storefrontReplaceTemplatesParams} from './storefront-utils.js'
2+
import {cleanHeader, defaultHeaders, storefrontReplaceTemplatesParams} from './storefront-utils.js'
33
import {DevServerSession, DevServerRenderContext} from './types.js'
44
import {createFetchError} from '../errors.js'
55
import {outputDebug} from '@shopify/cli-kit/node/output'
@@ -154,10 +154,3 @@ function themeAccessHeaders(session: DevServerSession) {
154154
'X-Shopify-Access-Token': session.token,
155155
}
156156
}
157-
158-
function cleanHeader(headers: {[key: string]: string}): {[key: string]: string} {
159-
// Force the use of the 'Cookie' key if consumers also provide the 'cookie' key
160-
delete headers.cookie
161-
delete headers.authorization
162-
return headers
163-
}

packages/theme/src/cli/utilities/theme-environment/storefront-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ export function defaultHeaders() {
2626
'User-Agent': `Shopify CLI; v=${CLI_KIT_VERSION}`,
2727
}
2828
}
29+
30+
export function cleanHeader(headers: {[key: string]: string}): {[key: string]: string} {
31+
// Force the use of the 'Cookie' key if consumers also provide the 'cookie' key
32+
delete headers.cookie
33+
delete headers.authorization
34+
return headers
35+
}

packages/theme/src/cli/utilities/theme-environment/theme-environment.test.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ describe('setupDevServer', () => {
8282
const localThemeExtensionFileSystem = emptyThemeExtFileSystem()
8383
const defaultServerContext: DevServerContext = {
8484
session: {
85-
storefrontToken: '',
85+
storefrontToken: 'shptka_test_token_123',
8686
token: '',
8787
storeFqdn: 'my-store.myshopify.com',
88-
sessionCookies: {},
88+
sessionCookies: {_shopify_essential: 'test-cookie-value'},
8989
},
9090
lastRequestedPath: '',
9191
localThemeFileSystem,
@@ -703,7 +703,11 @@ describe('setupDevServer', () => {
703703
expect.objectContaining({
704704
method: 'GET',
705705
redirect: 'manual',
706-
headers: {referer},
706+
headers: expect.objectContaining({
707+
referer,
708+
'User-Agent': expect.stringContaining('Shopify CLI'),
709+
Authorization: expect.stringContaining('Bearer'),
710+
}),
707711
}),
708712
)
709713

@@ -724,7 +728,11 @@ describe('setupDevServer', () => {
724728
expect.objectContaining({
725729
method: 'GET',
726730
redirect: 'manual',
727-
headers: {referer},
731+
headers: expect.objectContaining({
732+
referer,
733+
'User-Agent': expect.stringContaining('Shopify CLI'),
734+
Authorization: expect.stringContaining('Bearer'),
735+
}),
728736
}),
729737
)
730738
})
@@ -784,7 +792,11 @@ describe('setupDevServer', () => {
784792
expect.objectContaining({
785793
method: 'GET',
786794
redirect: 'manual',
787-
headers: {referer},
795+
headers: expect.objectContaining({
796+
referer,
797+
'User-Agent': expect.stringContaining('Shopify CLI'),
798+
Authorization: expect.stringContaining('Bearer'),
799+
}),
788800
}),
789801
)
790802

@@ -807,7 +819,11 @@ describe('setupDevServer', () => {
807819
expect.objectContaining({
808820
method: 'GET',
809821
redirect: 'manual',
810-
headers: {referer},
822+
headers: expect.objectContaining({
823+
referer,
824+
'User-Agent': expect.stringContaining('Shopify CLI'),
825+
Authorization: expect.stringContaining('Bearer'),
826+
}),
811827
}),
812828
)
813829

@@ -853,7 +869,11 @@ describe('setupDevServer', () => {
853869
expect.objectContaining({
854870
method: 'GET',
855871
redirect: 'manual',
856-
headers: {referer},
872+
headers: expect.objectContaining({
873+
referer,
874+
'User-Agent': expect.stringContaining('Shopify CLI'),
875+
Authorization: expect.stringContaining('Bearer'),
876+
}),
857877
}),
858878
)
859879

0 commit comments

Comments
 (0)