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: use redirect manual in fetch #24

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
40 changes: 38 additions & 2 deletions src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("HTTP Client", async () => {
expect(mockFetch.mock.lastCall?.[0]).toBe("test/api/test");
expect(mockFetch.mock.lastCall?.[1]).toEqual({
body: form,
redirect: "error",
redirect: "manual",
headers: {
Authorization: "Bearer test",
accept: "application/json",
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("HTTP Client", async () => {
expect(mockFetch.mock.lastCall?.[0]).toBe("test/api/test");
expect(mockFetch.mock.lastCall?.[1]).toEqual({
body: JSON.stringify(form),
redirect: "error",
redirect: "manual",
headers: {
Authorization: "Bearer test",
accept: "application/json",
Expand Down Expand Up @@ -180,4 +180,40 @@ describe("HTTP Client", async () => {

mockFetch.mockRestore();
});

test("test authentification gets redirect", async () => {
shyim marked this conversation as resolved.
Show resolved Hide resolved
const mockFetch = spyOn(global, "fetch").mockImplementationOnce(() =>
Promise.resolve(new Response("", { status: 301 })),
);

const client = new HttpClient(new SimpleShop("blaa", "test", "test"));

expect(client.get("/test")).rejects.toThrowError(
"Request failed with error: Got a redirect response from the URL, the URL should point to the Shop without redirect for shop with id: blaa",
);

mockFetch.mockRestore();
});

test("gets redirect on a api route", async () => {
const mockFetch = spyOn(global, "fetch").mockImplementation(
(input, init) => {
if (input === "test/api/oauth/token") {
return Promise.resolve(
new Response('{"access_token": "test", "expires_in": 5000}'),
);
}

return Promise.resolve(new Response("", { status: 301 }));
},
);

const client = new HttpClient(new SimpleShop("blaa", "test", "test"));

expect(client.post("/test", {})).rejects.toThrowError(
"Request failed with error: Got a redirect response from the URL, the URL should point to the Shop without redirect for shop with id: blaa",
);

mockFetch.mockRestore();
});
});
46 changes: 44 additions & 2 deletions src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class HttpClient {
headers: Record<string, string> = {},
): Promise<HttpClientResponse<ResponseType>> {
const f = await globalThis.fetch(`${this.shop.getShopUrl()}/api${url}`, {
redirect: "error",
redirect: "manual",
body,
headers: Object.assign(
{
Expand All @@ -108,6 +108,27 @@ export class HttpClient {
method,
});

if (f.status === 301 || f.status === 302) {
throw new ApiClientRequestFailed(
this.shop.getShopId(),
new HttpClientResponse<ShopwareErrorResponse>(
f.status,
{
errors: [
{
code: "301",
status: "301",
title: "301",
detail:
"Got a redirect response from the URL, the URL should point to the Shop without redirect",
},
],
},
f.headers,
),
);
}

// Obtain new token
if (!f.ok && f.status === 401) {
this.storage.expiresIn = null;
Expand Down Expand Up @@ -141,7 +162,7 @@ export class HttpClient {
`${this.shop.getShopUrl()}/api/oauth/token`,
{
method: "POST",
redirect: "error",
redirect: "manual",
headers: {
"content-type": "application/json",
},
Expand All @@ -153,6 +174,27 @@ export class HttpClient {
},
);

if (auth.status === 301 || auth.status === 302) {
throw new ApiClientRequestFailed(
this.shop.getShopId(),
new HttpClientResponse<ShopwareErrorResponse>(
auth.status,
{
errors: [
{
code: "301",
status: "301",
title: "301",
detail:
"Got a redirect response from the URL, the URL should point to the Shop without redirect",
},
],
},
auth.headers,
),
);
}

if (!auth.ok) {
const contentType = auth.headers.get("content-type") || "text/plain";
let body = "";
Expand Down