2022-11-17 14:10:33 +00:00
|
|
|
import { getApiUrl } from "@saleor/config";
|
2021-08-09 15:27:13 +00:00
|
|
|
import { stringifyQs } from "@saleor/utils/urls";
|
2020-07-22 10:54:15 +00:00
|
|
|
import urlJoin from "url-join";
|
|
|
|
|
|
|
|
import { ActiveTab, Dialog, Pagination, SingleAction } from "../types";
|
|
|
|
|
|
|
|
export const MANIFEST_ATTR = "manifestUrl";
|
|
|
|
|
2022-05-31 15:18:15 +00:00
|
|
|
export type AppListUrlDialog =
|
|
|
|
| "remove"
|
|
|
|
| "remove-app"
|
|
|
|
| "app-activate"
|
|
|
|
| "app-deactivate";
|
2020-07-22 10:54:15 +00:00
|
|
|
|
|
|
|
export type AppDetailsUrlDialog = "app-activate" | "app-deactivate";
|
|
|
|
|
|
|
|
export type AppListUrlQueryParams = ActiveTab &
|
|
|
|
Dialog<AppListUrlDialog> &
|
|
|
|
SingleAction &
|
|
|
|
Pagination;
|
|
|
|
|
2022-06-24 12:17:58 +00:00
|
|
|
export interface AppDetailsUrlMountQueryParams {
|
|
|
|
productId?: string;
|
2022-10-26 11:19:20 +00:00
|
|
|
productIds?: string[];
|
2022-06-24 12:17:58 +00:00
|
|
|
orderId?: string;
|
2022-10-26 11:19:20 +00:00
|
|
|
customerId?: string;
|
|
|
|
customerIds?: string[];
|
2022-06-24 12:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 10:54:15 +00:00
|
|
|
export type AppDetailsUrlQueryParams = Dialog<AppDetailsUrlDialog> &
|
2022-06-24 12:17:58 +00:00
|
|
|
SingleAction &
|
|
|
|
AppDetailsUrlMountQueryParams;
|
2020-07-22 10:54:15 +00:00
|
|
|
|
|
|
|
export type AppInstallUrlQueryParams = Partial<{ [MANIFEST_ATTR]: string }>;
|
|
|
|
|
|
|
|
export enum AppListUrlSortField {
|
|
|
|
name = "name",
|
2022-06-21 09:36:55 +00:00
|
|
|
active = "active",
|
2020-07-22 10:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const appsSection = "/apps/";
|
|
|
|
export const appsListPath = appsSection;
|
|
|
|
|
2022-02-02 15:30:34 +00:00
|
|
|
export const appDetailsPath = (id: string) => urlJoin(appsSection, id);
|
|
|
|
export const appPath = (id: string) => urlJoin(appsSection, id, "app");
|
|
|
|
export const appDeepPath = (id: string, subPath: string) =>
|
|
|
|
urlJoin(appPath(id), subPath);
|
2020-07-22 10:54:15 +00:00
|
|
|
export const appInstallPath = urlJoin(appsSection, "install");
|
2022-10-13 15:13:08 +00:00
|
|
|
export const createAppInstallUrl = (manifestUrl: string) =>
|
|
|
|
`${appInstallPath}?manifestUrl=${manifestUrl}`;
|
2020-07-22 10:54:15 +00:00
|
|
|
|
2022-02-02 15:30:34 +00:00
|
|
|
export const appDetailsUrl = (id: string, params?: AppDetailsUrlQueryParams) =>
|
|
|
|
appDetailsPath(encodeURIComponent(id)) + "?" + stringifyQs(params);
|
2020-07-22 10:54:15 +00:00
|
|
|
|
2022-02-02 15:30:34 +00:00
|
|
|
export const appUrl = (id: string, params?: AppDetailsUrlQueryParams) =>
|
|
|
|
appPath(encodeURIComponent(id)) + "?" + stringifyQs(params);
|
|
|
|
export const appDeepUrl = (
|
|
|
|
id: string,
|
|
|
|
subPath: string,
|
2022-06-21 09:36:55 +00:00
|
|
|
params?: AppDetailsUrlQueryParams,
|
2022-02-02 15:30:34 +00:00
|
|
|
) => appDeepPath(encodeURIComponent(id), subPath) + "?" + stringifyQs(params);
|
|
|
|
|
2022-07-05 08:39:58 +00:00
|
|
|
export const getAppDeepPathFromDashboardUrl = (
|
|
|
|
dashboardUrl: string,
|
|
|
|
appId: string,
|
|
|
|
) => {
|
|
|
|
const deepSubPath = dashboardUrl.replace(
|
|
|
|
appPath(encodeURIComponent(appId)),
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
return deepSubPath || "/";
|
|
|
|
};
|
2022-02-02 15:30:34 +00:00
|
|
|
export const getAppCompleteUrlFromDashboardUrl = (
|
|
|
|
dashboardUrl: string,
|
|
|
|
appUrl?: string,
|
2022-06-21 09:36:55 +00:00
|
|
|
appId?: string,
|
2022-02-02 15:30:34 +00:00
|
|
|
) => {
|
|
|
|
if (!appUrl || !appId) {
|
|
|
|
return appUrl;
|
|
|
|
}
|
|
|
|
const deepSubPath = dashboardUrl.replace(
|
|
|
|
appPath(encodeURIComponent(appId)),
|
2022-06-21 09:36:55 +00:00
|
|
|
"",
|
2022-02-02 15:30:34 +00:00
|
|
|
);
|
|
|
|
const appCompleteUrl = urlJoin(appUrl, deepSubPath);
|
|
|
|
return appCompleteUrl;
|
|
|
|
};
|
|
|
|
export const getDashboardUrFromAppCompleteUrl = (
|
|
|
|
appCompleteUrl: string,
|
|
|
|
appUrl?: string,
|
2022-06-21 09:36:55 +00:00
|
|
|
appId?: string,
|
2022-02-02 15:30:34 +00:00
|
|
|
) => {
|
|
|
|
if (!appUrl || !appId) {
|
|
|
|
return appUrl;
|
|
|
|
}
|
|
|
|
const deepSubPath = appCompleteUrl.replace(appUrl, "");
|
|
|
|
const dashboardUrl = urlJoin(appPath(encodeURIComponent(appId)), deepSubPath);
|
|
|
|
return dashboardUrl;
|
|
|
|
};
|
2020-07-22 10:54:15 +00:00
|
|
|
|
|
|
|
export const appsListUrl = (params?: AppListUrlQueryParams) =>
|
|
|
|
appsListPath + "?" + stringifyQs(params);
|
2022-10-26 11:19:20 +00:00
|
|
|
|
2022-11-17 14:10:33 +00:00
|
|
|
export const resolveAppIframeUrl = (
|
2022-10-26 11:19:20 +00:00
|
|
|
appId: string,
|
|
|
|
appUrl: string,
|
|
|
|
params: AppDetailsUrlQueryParams,
|
|
|
|
) => {
|
2022-11-29 10:45:51 +00:00
|
|
|
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;
|
2022-11-28 11:15:48 +00:00
|
|
|
|
2022-10-26 11:19:20 +00:00
|
|
|
const iframeContextQueryString = `?${stringifyQs(
|
2022-11-17 14:10:33 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @deprecated - domain will be removed in favor of saleorApiUrl.
|
|
|
|
* Current hostname (used as domain) can be extracted from full URL
|
|
|
|
*
|
|
|
|
* Difference will be:
|
|
|
|
* shop.saleor.cloud -> https://shop.saleor.cloud/graphql/
|
|
|
|
*/
|
2022-11-28 11:15:48 +00:00
|
|
|
domain: apiUrlHost,
|
|
|
|
saleorApiUrl: apiUrl,
|
2022-11-17 14:10:33 +00:00
|
|
|
id: appId,
|
|
|
|
...params,
|
|
|
|
},
|
2022-10-26 11:19:20 +00:00
|
|
|
"comma",
|
|
|
|
)}`;
|
2022-11-17 14:10:33 +00:00
|
|
|
|
2022-10-26 11:19:20 +00:00
|
|
|
return urlJoin(appUrl, window.location.search, iframeContextQueryString);
|
|
|
|
};
|