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(api-client): use Accept: application/json header by default #557

Merged
merged 1 commit into from
Feb 1, 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
5 changes: 5 additions & 0 deletions .changeset/loud-chicken-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/api-client": patch
---

Added `Accept: application/json` default header to get only JSON responses.
33 changes: 33 additions & 0 deletions packages/api-client-next/src/createAdminApiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,37 @@
"[ApiClientError: Failed request]",
);
});

it(`should by default include "Accept" header with "application/json" value`, async () => {
const seoUrlheadersSpy = vi.fn().mockImplementation((param: string) => {});
const app = createApp().use(
"/order",
eventHandler(async (event) => {
const headers = getHeaders(event);
seoUrlheadersSpy(headers);
return {
orderResponse: 123,
headers,
};
}),
);

const baseURL = await createPortAndGetUrl(app);

const client = createAdminAPIClient<operations, operationPaths>({
baseURL,
sessionData: {
accessToken: "Bearer my-access-token",
Dismissed Show dismissed Hide dismissed
refreshToken: "my-refresh-token",
expirationTime: Date.now() + 1000 * 60,
},
});
await client.invoke("getOrderList get /order?limit,page,query", {});

expect(seoUrlheadersSpy).toHaveBeenCalledWith(
expect.objectContaining({
accept: "application/json",
}),
);
});
});
29 changes: 28 additions & 1 deletion packages/api-client-next/src/createApiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe("createAPIClient", () => {
const app = createApp().use(
"/checkout/cart",
eventHandler(async (event) => {
setHeader;
return {
headers: event.node.req.headers,
};
Expand Down Expand Up @@ -144,4 +143,32 @@ describe("createAPIClient", () => {
"[ApiClientError: Failed request]",
);
});

it(`should by default include "Accept" header with "application/json" value`, async () => {
const seoUrlheadersSpy = vi.fn().mockImplementation((param: string) => {});
const app = createApp().use(
"/checkout/cart",
eventHandler(async (event) => {
const requestHeaders = getHeaders(event);
seoUrlheadersSpy(requestHeaders);
return {};
}),
);

const baseURL = await createPortAndGetUrl(app);

const client = createAPIClient<operations, operationPaths>({
accessToken: "123",
contextToken: "456",
baseURL,
});

await client.invoke("readCart get /checkout/cart");

expect(seoUrlheadersSpy).toHaveBeenCalledWith(
expect.objectContaining({
accept: "application/json",
}),
);
});
});
2 changes: 2 additions & 0 deletions packages/api-client-next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function createAPIClient<
}) {
const defaultHeaders: Record<string, string> = {
"sw-access-key": params.accessToken,
Accept: "application/json",
};

// protection from setting "null" or "undefined" as a token in API side
Expand Down Expand Up @@ -182,6 +183,7 @@ export function createAdminAPIClient<

const defaultHeaders = {
Authorization: createAuthorizationHeader(sessionData.accessToken),
Accept: "application/json",
};

function updateSessionData(responseData: {
Expand Down
Loading