Fix SaleorCloudAPL getAll method (#240)
* add debug log for not existing api url in register handler * Fix SaleoCloudApl.getAll to map data properly
This commit is contained in:
parent
62e4c39335
commit
c777275a64
5 changed files with 84 additions and 5 deletions
5
.changeset/moody-months-brush.md
Normal file
5
.changeset/moody-months-brush.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@saleor/app-sdk": patch
|
||||
---
|
||||
|
||||
Fixed SaleorCloudAPL "getAll" method that was not mapping response from remote with AuthData interface
|
5
.changeset/sour-geese-scream.md
Normal file
5
.changeset/sour-geese-scream.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@saleor/app-sdk": patch
|
||||
---
|
||||
|
||||
Added additional debug log if saleorApiUrl doesnt exist in register handler
|
|
@ -1,7 +1,7 @@
|
|||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { AuthData } from "./apl";
|
||||
import { SaleorCloudAPL, SaleorCloudAPLConfig } from "./saleor-cloud-apl";
|
||||
import { GetAllAplResponseShape, SaleorCloudAPL, SaleorCloudAPLConfig } from "./saleor-cloud-apl";
|
||||
|
||||
const fetchMock = vi.fn();
|
||||
|
||||
|
@ -116,5 +116,56 @@ describe("APL", () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAll", () => {
|
||||
it("Returns mapped APL arrat", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
status: 200,
|
||||
ok: true,
|
||||
json: async () => {
|
||||
const mockData: GetAllAplResponseShape = {
|
||||
count: 2,
|
||||
results: [
|
||||
{
|
||||
domain: "example.com",
|
||||
jwks: "{}",
|
||||
token: "token1",
|
||||
saleor_api_url: "https://example.com/graphql/",
|
||||
saleor_app_id: "x",
|
||||
},
|
||||
{
|
||||
domain: "example2.com",
|
||||
jwks: "{}",
|
||||
token: "token2",
|
||||
saleor_api_url: "https://example2.com/graphql/",
|
||||
saleor_app_id: "y",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
return mockData;
|
||||
},
|
||||
});
|
||||
|
||||
const apl = new SaleorCloudAPL(aplConfig);
|
||||
|
||||
expect(await apl.getAll()).toStrictEqual([
|
||||
{
|
||||
appId: "x",
|
||||
domain: "example.com",
|
||||
jwks: "{}",
|
||||
saleorApiUrl: "https://example.com/graphql/",
|
||||
token: "token1",
|
||||
},
|
||||
{
|
||||
appId: "y",
|
||||
domain: "example2.com",
|
||||
jwks: "{}",
|
||||
saleorApiUrl: "https://example2.com/graphql/",
|
||||
token: "token2",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,6 +10,19 @@ export type SaleorCloudAPLConfig = {
|
|||
token: string;
|
||||
};
|
||||
|
||||
type CloudAPLAuthDataShape = {
|
||||
saleor_api_url: string;
|
||||
token: string;
|
||||
jwks: string;
|
||||
saleor_app_id: string;
|
||||
domain: string;
|
||||
};
|
||||
|
||||
export type GetAllAplResponseShape = {
|
||||
count: number;
|
||||
results: CloudAPLAuthDataShape[];
|
||||
};
|
||||
|
||||
const validateResponseStatus = (response: Response) => {
|
||||
if (response.status === 404) {
|
||||
debug("Auth data not found");
|
||||
|
@ -33,8 +46,7 @@ const mapAuthDataToAPIBody = (authData: AuthData) => ({
|
|||
token: authData.token,
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mapAPIResponseToAuthData = (response: any): AuthData => ({
|
||||
const mapAPIResponseToAuthData = (response: CloudAPLAuthDataShape): AuthData => ({
|
||||
appId: response.saleor_app_id,
|
||||
domain: response.domain,
|
||||
jwks: response.jwks,
|
||||
|
@ -109,7 +121,7 @@ export class SaleorCloudAPL implements APL {
|
|||
const parsedResponse = (await response.json().catch((e) => {
|
||||
debug("Failed to parse response: %s", extractErrorMessage(e));
|
||||
debug("%O", e);
|
||||
})) as unknown;
|
||||
})) as CloudAPLAuthDataShape;
|
||||
|
||||
const authData = authDataFromObject(mapAPIResponseToAuthData(parsedResponse));
|
||||
|
||||
|
@ -173,7 +185,9 @@ export class SaleorCloudAPL implements APL {
|
|||
|
||||
debug(`Get all responded with ${response.status} code`);
|
||||
|
||||
return ((await response.json()) as AuthData[]) || [];
|
||||
return ((await response.json()) as GetAllAplResponseShape).results.map(
|
||||
mapAPIResponseToAuthData
|
||||
);
|
||||
} catch (error) {
|
||||
const errorMessage = extractErrorMessage(error);
|
||||
|
||||
|
|
|
@ -154,6 +154,10 @@ export const createAppRegisterHandler = ({
|
|||
}
|
||||
}
|
||||
|
||||
if (!saleorApiUrl) {
|
||||
debug("saleorApiUrl doesnt exist in headers");
|
||||
}
|
||||
|
||||
if (!validateAllowSaleorUrls(saleorApiUrl, allowedSaleorUrls)) {
|
||||
debug(
|
||||
"Validation of URL %s against allowSaleorUrls param resolves to false, throwing",
|
||||
|
|
Loading…
Reference in a new issue