Memo app bridge & improved registerHandler error message (#237)

* Add token exp validaiton

* revert lock

* Improved registerHandler error message and wrap useAuthenticatedFetch with memo
This commit is contained in:
Lukasz Ostrowski 2023-05-08 09:29:47 +02:00 committed by GitHub
parent da351faae9
commit 62e4c39335
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---
Registed Handler now prints saleorApiUrl in error response, so its available in dashboard toast

View file

@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---
Wrapped useAuthenticatedFetch with useMemo to avoid re-renders

View file

@ -1,3 +1,5 @@
import { useMemo } from "react";
import {
SALEOR_API_URL_HEADER,
SALEOR_AUTHORIZATION_BEARER_HEADER,
@ -17,6 +19,7 @@ export const createAuthenticatedFetch =
const { token, domain, saleorApiUrl } = appBridge.getState();
const headers = new Headers(init?.headers);
headers.set(SALEOR_DOMAIN_HEADER, domain);
headers.set(SALEOR_AUTHORIZATION_BEARER_HEADER, token ?? "");
headers.set(SALEOR_API_URL_HEADER, saleorApiUrl ?? "");
@ -39,5 +42,5 @@ export const useAuthenticatedFetch = (fetch = window.fetch): typeof window.fetch
throw new Error("useAuthenticatedFetch can be used only in browser context");
}
return createAuthenticatedFetch(appBridge, fetch);
return useMemo(() => createAuthenticatedFetch(appBridge, fetch), [appBridge, fetch]);
};

View file

@ -190,8 +190,8 @@ export const createAppRegisterHandler = ({
return new Response(
createRegisterHandlerResponseBody(false, {
code: "UNKNOWN_APP_ID",
message:
"The auth data given during registration request could not be used to fetch app ID. This usually means that App could not connect to Saleor during installation",
message: `The auth data given during registration request could not be used to fetch app ID.
This usually means that App could not connect to Saleor during installation. Saleor URL that App tried to connect: ${saleorApiUrl}`,
}),
{
status: 401,