Resolve saleor host/domain from full saleor api url (#2613)

This commit is contained in:
Lukasz Ostrowski 2022-11-28 12:15:48 +01:00 committed by GitHub
parent 231a32d115
commit 17e3c4c8f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 14 deletions

View file

@ -102,7 +102,7 @@ export const AppFrame: React.FC<Props> = ({
return (
<iframe
ref={frameRef}
src={resolveAppIframeUrl(appId, src, shop.domain.host, params)}
src={resolveAppIframeUrl(appId, src, params)}
onError={onError}
onLoad={handleLoad}
className={clsx(classes.iframe, className)}

View file

@ -10,27 +10,22 @@ describe("resolveAppIframeUrl", () => {
jest.clearAllMocks();
});
it.each<[string, string, string, Record<string, unknown>, string]>([
it.each<[string, string, Record<string, unknown>, string]>([
[
"XyZ123",
"https://my-app.vercel.app",
"shop.saleor.cloud",
{ param1: "param1" },
"https://my-app.vercel.app?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=XyZ123&param1=param1",
],
[
"AbC987",
"https://my-app.vercel.app/configuration",
"shop.saleor.cloud",
{ param1: "param1", param2: "param2" },
"https://my-app.vercel.app/configuration?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=AbC987&param1=param1&param2=param2",
],
])(
"Generates valid URL from segments",
(id, appUrl, shopHostname, params, expectedUrl) => {
const result = resolveAppIframeUrl(id, appUrl, shopHostname, params);
])("Generates valid URL from segments", (id, appUrl, params, expectedUrl) => {
const result = resolveAppIframeUrl(id, appUrl, params);
expect(result).toEqual(expectedUrl);
},
);
expect(result).toEqual(expectedUrl);
});
});

View file

@ -121,9 +121,11 @@ export const appsListUrl = (params?: AppListUrlQueryParams) =>
export const resolveAppIframeUrl = (
appId: string,
appUrl: string,
shopDomainHost: string,
params: AppDetailsUrlQueryParams,
) => {
const apiUrl = getApiUrl();
const apiUrlHost = new URL(getApiUrl()).hostname;
const iframeContextQueryString = `?${stringifyQs(
{
/**
@ -133,8 +135,8 @@ export const resolveAppIframeUrl = (
* Difference will be:
* shop.saleor.cloud -> https://shop.saleor.cloud/graphql/
*/
domain: shopDomainHost,
saleorApiUrl: getApiUrl(),
domain: apiUrlHost,
saleorApiUrl: apiUrl,
id: appId,
...params,
},