@fibery/url-utils 2.4.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +2 -0
- package/lib/index.js +20 -2
- package/lib/locator-utils.d.ts +3 -0
- package/lib/locator-utils.js +5 -2
- package/lib/make-path.js +4 -1
- package/lib/parse-page-location.js +19 -8
- package/lib/routes.js +6 -5
- package/lib/url-constants.d.ts +2 -0
- package/lib/url-constants.js +3 -1
- package/package.json +2 -1
package/lib/index.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ export declare const isPublicSpaceMode: boolean;
|
|
|
15
15
|
export declare const isPublicForm: boolean;
|
|
16
16
|
export declare const isPublicAccessMode: boolean;
|
|
17
17
|
export declare const isRelativeUrl: (url: string) => boolean;
|
|
18
|
+
/** Resolves an href the way a user typed it: a bare host like `fibery.io` gets https, a path resolves against `origin`. */
|
|
19
|
+
export declare const createURLFromHref: (href: string, origin?: string) => URL | null;
|
|
18
20
|
export declare const isPublicUrlInPrivateMode: (absoluteUrl: string, location?: Location) => boolean;
|
|
19
21
|
export declare const getRelativeUrlFromAbsolute: (absoluteUrl: string, location?: Location) => string;
|
|
20
22
|
type Context = {
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { parseLocator, parseScrollLocator } from "./locator-utils.js";
|
|
2
2
|
import { publicParamName, publicUrlPrefix, sharingParamName } from "./url-constants.js";
|
|
3
3
|
import { getAuxMatch, getPageMatch, safeDecodeURIComponent } from "./parse-page-location.js";
|
|
4
|
+
import { isEmptyObject } from "@fibery/helpers/src/object";
|
|
4
5
|
//#region src/index.ts
|
|
5
6
|
const appendQueryFlag = (url, key) => {
|
|
6
7
|
const [location, ...hash] = url.split("#");
|
|
@@ -45,6 +46,23 @@ const validPageMatchSet = /* @__PURE__ */ new Set([":page/app-view", ":page/spec
|
|
|
45
46
|
const isRelativeUrl = (url) => {
|
|
46
47
|
return url.startsWith("/");
|
|
47
48
|
};
|
|
49
|
+
const bareHostRegex = /^[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*$/;
|
|
50
|
+
/** Resolves an href the way a user typed it: a bare host like `fibery.io` gets https, a path resolves against `origin`. */
|
|
51
|
+
const createURLFromHref = (href, origin = window.origin) => {
|
|
52
|
+
try {
|
|
53
|
+
return new URL(href);
|
|
54
|
+
} catch {
|
|
55
|
+
if (isRelativeUrl(href)) return new URL(href, origin);
|
|
56
|
+
if (bareHostRegex.test(href)) try {
|
|
57
|
+
return new URL(`https://${href}`);
|
|
58
|
+
} catch {}
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return new URL(href, origin);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
48
66
|
const isPublicUrlInPrivateMode = (absoluteUrl, location = window.location) => {
|
|
49
67
|
const isPublic = location.pathname.startsWith(publicUrlPrefix);
|
|
50
68
|
let urlObject = null;
|
|
@@ -102,9 +120,9 @@ const createPureFiberyEntityUrlMatcher = ({ ownHost }) => (originalUrl, matchUse
|
|
|
102
120
|
publicId,
|
|
103
121
|
typeName,
|
|
104
122
|
scrollLocator: pageMatch.params.scrollLocator,
|
|
105
|
-
scrollLocatorParams:
|
|
123
|
+
scrollLocatorParams: !isEmptyObject(scrollLocatorParams) ? scrollLocatorParams : void 0,
|
|
106
124
|
context
|
|
107
125
|
};
|
|
108
126
|
};
|
|
109
127
|
//#endregion
|
|
110
|
-
export { appendIsPublicParam, appendQueryFlag, appendQueryParameter, appendSharingKey, createPureFiberyEntityUrlMatcher, getFlagValue, getRefURL, getRelativeUrlFromAbsolute, getSharingKey, isPublicAccessMode, isPublicForm, isPublicSpaceMode, isPublicUrlInPrivateMode, isRelativeUrl, isSharingMode, signedFileUrl };
|
|
128
|
+
export { appendIsPublicParam, appendQueryFlag, appendQueryParameter, appendSharingKey, createPureFiberyEntityUrlMatcher, createURLFromHref, getFlagValue, getRefURL, getRelativeUrlFromAbsolute, getSharingKey, isPublicAccessMode, isPublicForm, isPublicSpaceMode, isPublicUrlInPrivateMode, isRelativeUrl, isSharingMode, signedFileUrl };
|
package/lib/locator-utils.d.ts
CHANGED
|
@@ -7,6 +7,9 @@ export declare const parseLocator: (locator: string) => {
|
|
|
7
7
|
publicId: string;
|
|
8
8
|
title: string;
|
|
9
9
|
};
|
|
10
|
+
export declare const appPathLocatorPrefix = "app-path=";
|
|
11
|
+
export declare const isAppPathLocator: (locator: string) => boolean;
|
|
12
|
+
export declare const parseAppPathLocator: (locator?: string | null) => string | null;
|
|
10
13
|
export declare const parseScrollLocator: (locator?: string | null) => Record<string, string | null>;
|
|
11
14
|
export declare const makeLocator: ({ publicId, title }: {
|
|
12
15
|
publicId: string | number | null;
|
package/lib/locator-utils.js
CHANGED
|
@@ -33,9 +33,12 @@ const normalizeLocatorTitle = (title, maxLength = TITLE_MAX_LENGTH) => {
|
|
|
33
33
|
const normalizedTitle = replaceDiactrics(title.trim());
|
|
34
34
|
return normalizedTitle.split(" ").filter(Boolean).join("-").match(threeNonUrlChars) ? "" : normalizedTitle.replace(nonUrlChars, " ").trim().replace(/\s+/g, "-").substr(0, maxLength).replace(/-+$/, "");
|
|
35
35
|
};
|
|
36
|
+
const appPathLocatorPrefix = "app-path=";
|
|
37
|
+
const isAppPathLocator = (locator) => locator.startsWith(appPathLocatorPrefix);
|
|
38
|
+
const parseAppPathLocator = (locator) => locator && isAppPathLocator(locator) ? locator.slice(9) : null;
|
|
36
39
|
const parseScrollLocator = (locator) => {
|
|
37
40
|
const defaultResult = { field: null };
|
|
38
|
-
if (locator) {
|
|
41
|
+
if (locator && !isAppPathLocator(locator)) {
|
|
39
42
|
const entries = [...new URLSearchParams(locator).entries()].map(([key, value]) => {
|
|
40
43
|
return [key, parseScrollLocatorValue(key, value)];
|
|
41
44
|
});
|
|
@@ -63,4 +66,4 @@ const parseScrollLocatorValue = (key, originalValue) => {
|
|
|
63
66
|
return originalValue;
|
|
64
67
|
};
|
|
65
68
|
//#endregion
|
|
66
|
-
export { createAnchorScrollLocatorValue, escapeRegExpChar, makeLocator, parseLocator, parseScrollLocator, parseScrollLocatorValue };
|
|
69
|
+
export { appPathLocatorPrefix, createAnchorScrollLocatorValue, escapeRegExpChar, isAppPathLocator, makeLocator, parseAppPathLocator, parseLocator, parseScrollLocator, parseScrollLocatorValue };
|
package/lib/make-path.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { appPathLocatorPrefix, isAppPathLocator } from "./locator-utils.js";
|
|
1
2
|
import { publicUrlPrefix } from "./url-constants.js";
|
|
2
3
|
import { AUX_ROUTES_PATTERNS, ROUTES_WITH_PRESERVED_UNDERSCORES_PATTERNS_LOOKUP } from "./routes.js";
|
|
3
4
|
import { isPublicSpaceMode } from "./index.js";
|
|
@@ -24,13 +25,15 @@ const makeEncodeURIComponentPretty = (spacesEncoder) => {
|
|
|
24
25
|
};
|
|
25
26
|
const makeEncodeURIComponentPrettyUnderscores = makeEncodeURIComponentPretty(encodeSpacesWithUnderscores);
|
|
26
27
|
const makeEncodeURIComponentPrettyDashes = makeEncodeURIComponentPretty(encodeSpacesWithDashes);
|
|
28
|
+
const encodeAppPathLocator = (locator) => appPathLocatorPrefix + encodeURIComponent(locator.slice(appPathLocatorPrefix.length)).replace(/%2F/g, "/");
|
|
27
29
|
const publicPrefixMaybe = (route) => {
|
|
28
30
|
if (isPublicSpaceMode && !AUX_ROUTES_PATTERNS.includes(route) && !route.startsWith("/@public/forms")) return `${publicUrlPrefix}${route}`;
|
|
29
31
|
return route;
|
|
30
32
|
};
|
|
31
33
|
function makePath(route = throwError("route arg is missing"), params = throwError("params arg is missing")) {
|
|
32
34
|
const encodeFn = ROUTES_WITH_PRESERVED_UNDERSCORES_PATTERNS_LOOKUP[route] ? makeEncodeURIComponentPrettyDashes : makeEncodeURIComponentPrettyUnderscores;
|
|
33
|
-
|
|
35
|
+
const encode = (value, token) => token.name === "scrollLocator" && isAppPathLocator(value) ? encodeAppPathLocator(value) : encodeFn(value);
|
|
36
|
+
return memoizedCompile(publicPrefixMaybe(route))(params, { encode });
|
|
34
37
|
}
|
|
35
38
|
//#endregion
|
|
36
39
|
export { makePath };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAppPathLocator } from "./locator-utils.js";
|
|
1
2
|
import { ORDERED_AUX_ROUTES, ORDERED_PAGE_ROUTES, ROUTES_WITH_PRESERVED_UNDERSCORES_LOOKUP, ROUTES_WITH_PRESERVED_UNDERSCORES_PATTERNS_LOOKUP, isNonSpaPath } from "./routes.js";
|
|
2
3
|
import { matchPath } from "react-router-dom";
|
|
3
4
|
//#region src/parse-page-location.ts
|
|
@@ -12,6 +13,10 @@ const findTruthy = (items, transform) => {
|
|
|
12
13
|
if (isTest && results.length > 1) throw new Error(`Found more than one route: ` + JSON.stringify(results, null, 2));
|
|
13
14
|
return results[0] || null;
|
|
14
15
|
};
|
|
16
|
+
const withRawAppPathLocator = (params, raw) => typeof raw?.scrollLocator === "string" && isAppPathLocator(raw.scrollLocator) ? {
|
|
17
|
+
...params,
|
|
18
|
+
scrollLocator: raw.scrollLocator
|
|
19
|
+
} : params;
|
|
15
20
|
function safeDecodeURIComponent(maybeEncodedPath) {
|
|
16
21
|
let path = maybeEncodedPath;
|
|
17
22
|
try {
|
|
@@ -25,16 +30,21 @@ function findPageMatch(maybeEncodedPath) {
|
|
|
25
30
|
const dashDecodedPath = decodeDashesToSpaces(path);
|
|
26
31
|
const processedPath = decodeUnderscoresToSpaces(dashDecodedPath);
|
|
27
32
|
return findTruthy(ORDERED_PAGE_ROUTES, ([page, route]) => {
|
|
28
|
-
const
|
|
33
|
+
const preserveUnderscores = ROUTES_WITH_PRESERVED_UNDERSCORES_PATTERNS_LOOKUP[route];
|
|
34
|
+
const match = matchPath(preserveUnderscores ? dashDecodedPath : processedPath, {
|
|
29
35
|
path: route,
|
|
30
36
|
exact: true
|
|
31
37
|
});
|
|
32
|
-
|
|
38
|
+
if (!match) return null;
|
|
39
|
+
return {
|
|
33
40
|
page,
|
|
34
|
-
params: match.params,
|
|
41
|
+
params: preserveUnderscores ? match.params : withRawAppPathLocator(match.params, matchPath(dashDecodedPath, {
|
|
42
|
+
path: route,
|
|
43
|
+
exact: true
|
|
44
|
+
})?.params),
|
|
35
45
|
isFavorite: false,
|
|
36
46
|
matchedPath: match.path
|
|
37
|
-
}
|
|
47
|
+
};
|
|
38
48
|
});
|
|
39
49
|
}
|
|
40
50
|
function findAuxMatch(path, pageAppNamespace) {
|
|
@@ -50,12 +60,13 @@ function findAuxMatch(path, pageAppNamespace) {
|
|
|
50
60
|
params: match.params,
|
|
51
61
|
pageAppNamespace
|
|
52
62
|
};
|
|
63
|
+
const nonEnumRouteMatch = matchPath(decodeUnderscoresToSpaces(decodedPath), {
|
|
64
|
+
path: route,
|
|
65
|
+
exact: true
|
|
66
|
+
});
|
|
53
67
|
return {
|
|
54
68
|
aux,
|
|
55
|
-
params:
|
|
56
|
-
path: route,
|
|
57
|
-
exact: true
|
|
58
|
-
})?.params ?? {},
|
|
69
|
+
params: withRawAppPathLocator(nonEnumRouteMatch?.params ?? {}, match.params),
|
|
59
70
|
pageAppNamespace
|
|
60
71
|
};
|
|
61
72
|
});
|
package/lib/routes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { escapeRegExpChar } from "./locator-utils.js";
|
|
1
|
+
import { appPathLocatorPrefix, escapeRegExpChar } from "./locator-utils.js";
|
|
2
2
|
import { appsUrlPrefix, formsUrlPrefix, publicAppsUrlPrefix, publicUrlPrefix } from "./url-constants.js";
|
|
3
3
|
import fromPairs from "lodash/fromPairs.js";
|
|
4
4
|
//#region src/routes.ts
|
|
@@ -16,6 +16,7 @@ const extendedCommentLocatorPattern = `parentComment=${publicIdLocatorPattern}&c
|
|
|
16
16
|
const anchorLocatorPattern = `anchor=${idLocatorPattern}`;
|
|
17
17
|
const anchorWithTitleLocatorPattern = `anchor=${titleWithGuidLocatorPattern}`;
|
|
18
18
|
const scrollLocatorPattern = `${extendedCommentLocatorPattern}|${commentLocatorPattern}|${`field=${idLocatorPattern}`}|${anchorLocatorPattern}|${anchorWithTitleLocatorPattern}|anchor=wb-[^/]+`;
|
|
19
|
+
const viewScrollLocatorPattern = `${scrollLocatorPattern}|${`${appPathLocatorPrefix}.+`}`;
|
|
19
20
|
const appIndexViewLocator = "views";
|
|
20
21
|
const userAccessViewLocator = "access";
|
|
21
22
|
const specificObjectViewsCatalogViewLocator = "views";
|
|
@@ -80,7 +81,7 @@ const orderedRoutes = [
|
|
|
80
81
|
[settingsPage, typeAccessTemplateSettingPage],
|
|
81
82
|
[settingsPage, "/fibery/settings/:subsection"],
|
|
82
83
|
[settingsPage, "/fibery/settings"],
|
|
83
|
-
[":page/app-view", `/:appNamespace/:viewLocator(${appIndexViewLocator}|${viewLocatorPattern})/:scrollLocator(${
|
|
84
|
+
[":page/app-view", `/:appNamespace/:viewLocator(${appIndexViewLocator}|${viewLocatorPattern})/:scrollLocator(${viewScrollLocatorPattern})?`],
|
|
84
85
|
[":page/favorite", `/favorites/:parts+`],
|
|
85
86
|
[":page/audit-log", "/audit-log"],
|
|
86
87
|
[":page/home", "/home"],
|
|
@@ -101,7 +102,7 @@ const orderedRoutes = [
|
|
|
101
102
|
`/:appNamespace/:typeWithoutNamespace`,
|
|
102
103
|
`:objectLocator(any|${titleWithPublicIdLocatorPattern}|${publicIdLocatorPattern})`,
|
|
103
104
|
`:viewLocator(${specificObjectViewsCatalogViewLocator}|${deprecatedSpecificObjectEditorViewLocator}|${viewLocatorPattern})?`,
|
|
104
|
-
`:scrollLocator(${
|
|
105
|
+
`:scrollLocator(${viewScrollLocatorPattern})?`
|
|
105
106
|
].join("/")],
|
|
106
107
|
[":page/public-form", `/forms/:formKey`],
|
|
107
108
|
[":page/app-root", `/:appNamespace`],
|
|
@@ -128,8 +129,8 @@ const ORDERED_COMMON_AUX_ROUTES = [
|
|
|
128
129
|
[":aux/type-object-editor", `:auxTypeNamespace/:auxTypeName/edit`],
|
|
129
130
|
[":aux/type-object-editor-implicit-namespace", `:auxTypeName/edit`],
|
|
130
131
|
[":aux/type-object-editor-for-composite-type", `:nsA/:nameA\\_:nsB/:nameB/edit`],
|
|
131
|
-
[":aux/app-view-with-context", `:appNamespace/views/:viewType/:viewLocator(${viewLocatorPattern})/context/:auxTypeNamespace/:auxTypeName/:auxLocator(${auxLocatorPattern})/:scrollLocator(${
|
|
132
|
-
[":aux/app-view", `:appNamespace/views/:viewType/:viewLocator(${viewLocatorPattern})/:scrollLocator(${
|
|
132
|
+
[":aux/app-view-with-context", `:appNamespace/views/:viewType/:viewLocator(${viewLocatorPattern})/context/:auxTypeNamespace/:auxTypeName/:auxLocator(${auxLocatorPattern})/:scrollLocator(${viewScrollLocatorPattern})?`],
|
|
133
|
+
[":aux/app-view", `:appNamespace/views/:viewType/:viewLocator(${viewLocatorPattern})/:scrollLocator(${viewScrollLocatorPattern})?`],
|
|
133
134
|
[":aux/object-editor", `:auxTypeNamespace/:auxTypeName/:auxLocator(${auxLocatorPattern})/:scrollLocator(${scrollLocatorPattern})?`],
|
|
134
135
|
[":aux/object-editor-implicit-namespace", `:auxTypeName/:auxLocator(${auxLocatorPattern})/:scrollLocator(${scrollLocatorPattern})?`],
|
|
135
136
|
[":aux/object-editor-for-composite-type", `:nsA/:nameA\\_:nsB/:nameB/:auxLocator(${auxLocatorPattern})/:scrollLocator(${scrollLocatorPattern})?`],
|
package/lib/url-constants.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export declare const formsUrlPrefix = "/@public/forms";
|
|
|
6
6
|
export declare const formKeyParamName = "form-key";
|
|
7
7
|
export declare const appsUrlPrefix = "/apps";
|
|
8
8
|
export declare const publicAppsUrlPrefix = "/@public/apps";
|
|
9
|
+
export declare const entityIdParamName = "entity-id";
|
|
10
|
+
export declare const entityTypeParamName = "entity-type";
|
|
9
11
|
export declare const containerTypeParamName = "container-type";
|
|
10
12
|
export declare const documentContainerTypes: readonly ["entity", "comment", "view", "canvas-comment"];
|
|
11
13
|
export type DocumentContainerType = (typeof documentContainerTypes)[number];
|
package/lib/url-constants.js
CHANGED
|
@@ -6,6 +6,8 @@ const formsUrlPrefix = "/@public/forms";
|
|
|
6
6
|
const formKeyParamName = "form-key";
|
|
7
7
|
const appsUrlPrefix = "/apps";
|
|
8
8
|
const publicAppsUrlPrefix = `${publicUrlPrefix}${appsUrlPrefix}`;
|
|
9
|
+
const entityIdParamName = "entity-id";
|
|
10
|
+
const entityTypeParamName = "entity-type";
|
|
9
11
|
const containerTypeParamName = "container-type";
|
|
10
12
|
const documentContainerTypes = [
|
|
11
13
|
"entity",
|
|
@@ -15,4 +17,4 @@ const documentContainerTypes = [
|
|
|
15
17
|
];
|
|
16
18
|
const anyDocumentContainerType = documentContainerTypes.join(",");
|
|
17
19
|
//#endregion
|
|
18
|
-
export { anyDocumentContainerType, appsUrlPrefix, containerTypeParamName, documentContainerTypes, formKeyParamName, formsUrlPrefix, publicAppsUrlPrefix, publicParamName, publicUrlPrefix, sharingParamName };
|
|
20
|
+
export { anyDocumentContainerType, appsUrlPrefix, containerTypeParamName, documentContainerTypes, entityIdParamName, entityTypeParamName, formKeyParamName, formsUrlPrefix, publicAppsUrlPrefix, publicParamName, publicUrlPrefix, sharingParamName };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fibery/url-utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
+
"@fibery/helpers": "6.0.0",
|
|
23
24
|
"lodash": "4.18.1",
|
|
24
25
|
"path-to-regexp": "3.0.0",
|
|
25
26
|
"react-router-dom": "5.3.4"
|