Construct full API URL in case not full provided (#2738)
* Construct full API URL in case not full provided * Resolve URL from base path * Fix types
This commit is contained in:
parent
3c260557a7
commit
46b0adec0e
2 changed files with 42 additions and 23 deletions
|
@ -1,31 +1,47 @@
|
|||
import { resolveAppIframeUrl } from "@saleor/apps/urls";
|
||||
|
||||
jest.mock("@saleor/config", () => ({
|
||||
...jest.requireActual("@saleor/config"),
|
||||
getApiUrl: () => "https://shop.saleor.cloud/graphql/",
|
||||
}));
|
||||
import * as config from "@saleor/config";
|
||||
|
||||
describe("resolveAppIframeUrl", () => {
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it.each<[string, string, Record<string, unknown>, string]>([
|
||||
[
|
||||
"XyZ123",
|
||||
"https://my-app.vercel.app",
|
||||
{ param1: "param1" },
|
||||
"https://my-app.vercel.app?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=XyZ123¶m1=param1",
|
||||
],
|
||||
[
|
||||
"AbC987",
|
||||
"https://my-app.vercel.app/configuration",
|
||||
{ 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¶m1=param1¶m2=param2",
|
||||
],
|
||||
])("Generates valid URL from segments", (id, appUrl, params, expectedUrl) => {
|
||||
const result = resolveAppIframeUrl(id, appUrl, params);
|
||||
describe("For full URL provided in env", () => {
|
||||
beforeEach(() => {
|
||||
jest
|
||||
.spyOn(config, "getApiUrl")
|
||||
.mockImplementation(() => "https://shop.saleor.cloud/graphql/");
|
||||
});
|
||||
|
||||
expect(result).toEqual(expectedUrl);
|
||||
it.each<[string, string, Record<string, string>, string]>([
|
||||
[
|
||||
"XyZ123",
|
||||
"https://my-app.vercel.app",
|
||||
{ param1: "param1" },
|
||||
"https://my-app.vercel.app?domain=shop.saleor.cloud&saleorApiUrl=https%3A%2F%2Fshop.saleor.cloud%2Fgraphql%2F&id=XyZ123¶m1=param1",
|
||||
],
|
||||
[
|
||||
"AbC987",
|
||||
"https://my-app.vercel.app/configuration",
|
||||
{ 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¶m1=param1¶m2=param2",
|
||||
],
|
||||
])(
|
||||
"Generates valid URL from segments",
|
||||
(id, appUrl, params, expectedUrl) => {
|
||||
const result = resolveAppIframeUrl(id, appUrl, params);
|
||||
|
||||
expect(result).toEqual(expectedUrl);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* Test this scenario for cloud deployments where origin is computed.
|
||||
*
|
||||
* Current jest is not set up for testing location/URL
|
||||
*/
|
||||
test.todo(
|
||||
"Test if URL is valid when API_URL in env is absolute path like /graphql/",
|
||||
);
|
||||
});
|
||||
|
|
|
@ -123,8 +123,11 @@ export const resolveAppIframeUrl = (
|
|||
appUrl: string,
|
||||
params: AppDetailsUrlQueryParams,
|
||||
) => {
|
||||
const apiUrl = getApiUrl();
|
||||
const apiUrlHost = new URL(getApiUrl()).hostname;
|
||||
const apiUrl = new URL(getApiUrl(), window.location.origin).href;
|
||||
/**
|
||||
* Use host to preserve port, in case of multiple Saleors running on localhost
|
||||
*/
|
||||
const apiUrlHost = new URL(apiUrl).host;
|
||||
|
||||
const iframeContextQueryString = `?${stringifyQs(
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue