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(shopify-api): 301 redirect follows with initial method #1999

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/wicked-bananas-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': patch
---

fix redirect method for custom shop domains
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ const tests: Record<string | number, Test> = {
}),
}),
},
301: {
expectedRequest: initTestRequest({method: 'post'}),
testResponse: initTestResponse({
statusCode: 301,
headers: {
location: '/url/path/301resolved',
},
}),
},
'301resolved': {
expectedRequest: initTestRequest({method: 'post'}),
testResponse: initTestResponse({
body: 'followed redirect',
statusCode: 204,
}),
},
400: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
Expand All @@ -98,6 +114,13 @@ const tests: Record<string | number, Test> = {
body: JSON.stringify({}),
}),
},
405: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
statusCode: 405,
statusText: 'Wrong method',
}),
},
417: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
Expand Down Expand Up @@ -162,6 +185,12 @@ const server = createServer((req: IncomingMessage, res: ServerResponse) => {
const expectedRequest = test.expectedRequest;
let testResponse = test.testResponse;

if (code.startsWith('301')) {
if (expectedRequest?.method !== req.method?.toLowerCase()) {
testResponse = tests['405'].testResponse;
}
}

if (matchHeaders(receivedHeaders as Headers, expectedRequest.headers)) {
if (code === 'retries' && retryCount < 2) {
testResponse = tests['429'].testResponse;
Expand Down
12 changes: 12 additions & 0 deletions packages/apps/shopify-api/adapters/__e2etests__/test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ export const testSuite = [
expectedResponse: initTestResponse(),
},
},
{
name: 'gracefully handles 301 redirect',
config: {
testRequest: initTestRequest({
url: '/url/path/301',
type: TestType.Graphql,
}),
expectedResponse: initTestResponse({
statusCode: 204,
}),
},
},
{
name: 'gracefully handles 403 error',
config: {
Expand Down
24 changes: 22 additions & 2 deletions packages/apps/shopify-api/adapters/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@ import {
nodeRuntimeString,
} from './adapter';

// For the purposes of this package, fetch correctly implements everything we need
setAbstractFetchFunc(fetch as any as AbstractFetchFunc);
// node-fetch redirects post requests as get requests on 301, 302 or 303 status codes
// this does not work for graphql requests, so we need to manually handle redirects
const fetchWithRedirect: AbstractFetchFunc = async (url, init) => {
const fetchOptions = {
...init,
redirect: 'manual',
} satisfies RequestInit;

const response = await (fetch as any as AbstractFetchFunc)(url, fetchOptions);

if (
(response.status === 301 || response.status === 302) &&
response.headers.has('location')
) {
const location = response.headers.get('location')!;
return fetchWithRedirect(location, init);
}

return response;
};

setAbstractFetchFunc(fetchWithRedirect);
setAbstractConvertRequestFunc(nodeConvertRequest);
setAbstractConvertIncomingResponseFunc(nodeConvertIncomingResponse);
setAbstractConvertResponseFunc(nodeConvertAndSendResponse);
Expand Down
Loading