From c777275a64871bc9934f54459efb1a3fb105e569 Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Mon, 15 May 2023 11:43:04 +0200 Subject: [PATCH] Fix SaleorCloudAPL getAll method (#240) * add debug log for not existing api url in register handler * Fix SaleoCloudApl.getAll to map data properly --- .changeset/moody-months-brush.md | 5 ++ .changeset/sour-geese-scream.md | 5 ++ src/APL/saleor-cloud-apl.test.ts | 53 ++++++++++++++++++- src/APL/saleor-cloud-apl.ts | 22 ++++++-- .../next/create-app-register-handler.ts | 4 ++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 .changeset/moody-months-brush.md create mode 100644 .changeset/sour-geese-scream.md diff --git a/.changeset/moody-months-brush.md b/.changeset/moody-months-brush.md new file mode 100644 index 0000000..678732d --- /dev/null +++ b/.changeset/moody-months-brush.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": patch +--- + +Fixed SaleorCloudAPL "getAll" method that was not mapping response from remote with AuthData interface diff --git a/.changeset/sour-geese-scream.md b/.changeset/sour-geese-scream.md new file mode 100644 index 0000000..99c11c5 --- /dev/null +++ b/.changeset/sour-geese-scream.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": patch +--- + +Added additional debug log if saleorApiUrl doesnt exist in register handler diff --git a/src/APL/saleor-cloud-apl.test.ts b/src/APL/saleor-cloud-apl.test.ts index 2579f4f..29b51fa 100644 --- a/src/APL/saleor-cloud-apl.test.ts +++ b/src/APL/saleor-cloud-apl.test.ts @@ -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", + }, + ]); + }); + }); }); }); diff --git a/src/APL/saleor-cloud-apl.ts b/src/APL/saleor-cloud-apl.ts index 6a3c479..837ae41 100644 --- a/src/APL/saleor-cloud-apl.ts +++ b/src/APL/saleor-cloud-apl.ts @@ -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); diff --git a/src/handlers/next/create-app-register-handler.ts b/src/handlers/next/create-app-register-handler.ts index 0650525..1e0da6d 100644 --- a/src/handlers/next/create-app-register-handler.ts +++ b/src/handlers/next/create-app-register-handler.ts @@ -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",