@fibery/url-utils 2.2.7 → 2.3.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/index.ts DELETED
@@ -1,168 +0,0 @@
1
- import {formsUrlPrefix, publicParamName, publicUrlPrefix, sharingParamName} from "./url-constants";
2
- import {getAuxMatch, getPageMatch, safeDecodeURIComponent} from "./parse-page-location";
3
- import {parseLocator, parseScrollLocator} from "./locator-utils";
4
-
5
- export const appendQueryFlag = (url: string, key: string) => {
6
- const [location, ...hash] = url.split("#");
7
- const prefix = location.indexOf("?") >= 0 ? "&" : "?";
8
- return [`${location}${prefix}${key}`, ...hash].join("#");
9
- };
10
-
11
- export const appendQueryParameter = (url: string, key: string, value: string | null) => {
12
- if (!value) {
13
- return url;
14
- }
15
- const [location, ...hash] = url.split("#");
16
- const prefix = location.indexOf("?") >= 0 ? "&" : "?";
17
- return [`${location}${prefix}${key}=${value}`, ...hash].join("#");
18
- };
19
-
20
- export const getFlagValue = (flag: string) => new URLSearchParams(window.location.search).get(flag);
21
-
22
- export const getSharingKey = () => {
23
- return getFlagValue(sharingParamName) || getFlagValue("sk");
24
- };
25
-
26
- export const appendSharingKey = (
27
- url?: string | null,
28
- sharingKey: undefined | string | null = getSharingKey()
29
- ): string => {
30
- if (!url) {
31
- return url || "";
32
- }
33
- return appendQueryParameter(url, sharingParamName, sharingKey);
34
- };
35
-
36
- export const appendIsPublicParam = (url: string): string => {
37
- if (isPublicSpaceMode) {
38
- //TODO: rework "appendQueryParameter" to URL class.
39
- return appendQueryParameter(url, publicParamName, "1");
40
- } else {
41
- return url;
42
- }
43
- };
44
-
45
- export const signedFileUrl = (url?: string | null) => {
46
- return url?.startsWith("/api/files/") && !url?.startsWith("/api/files/r/")
47
- ? url.replace("/api/files", "/api/files/r")
48
- : url;
49
- };
50
-
51
- export const getRefURL = (location: {href: string; origin: string} = window.location) =>
52
- location.href.replace(location.origin, "");
53
-
54
- export const isSharingMode = Boolean(getSharingKey());
55
-
56
- const decodedLocationPathName = safeDecodeURIComponent(window.location.pathname || "");
57
- export const isPublicSpaceMode =
58
- decodedLocationPathName.startsWith(publicUrlPrefix) && !decodedLocationPathName.startsWith(formsUrlPrefix);
59
-
60
- export const isPublicForm =
61
- decodedLocationPathName.startsWith(publicUrlPrefix) && decodedLocationPathName.startsWith(formsUrlPrefix);
62
-
63
- export const isPublicAccessMode = isSharingMode || isPublicSpaceMode;
64
-
65
- const validPageMatchSet = new Set([":page/app-view", ":page/specific-object-view"]);
66
-
67
- export const isRelativeUrl = (url: string) => {
68
- return url.startsWith("/");
69
- };
70
-
71
- export const isPublicUrlInPrivateMode = (absoluteUrl: string, location = window.location) => {
72
- const isPublic = location.pathname.startsWith(publicUrlPrefix);
73
- let urlObject = null;
74
- try {
75
- urlObject = new URL(absoluteUrl);
76
- } catch (ex) {
77
- // not a valid URL
78
- return false;
79
- }
80
- return !isPublic && urlObject.pathname.startsWith(publicUrlPrefix);
81
- };
82
-
83
- export const getRelativeUrlFromAbsolute = (absoluteUrl: string, location = window.location) => {
84
- return absoluteUrl.replace(location.origin, "");
85
- };
86
-
87
- const knownScrollLocatorParams = new Set(["field", "comment", "anchor"]);
88
- type Context = {contextEntityTypeName: string; contextEntityPublicId: string};
89
- export const createPureFiberyEntityUrlMatcher =
90
- ({ownHost}: {ownHost: string}) =>
91
- (
92
- originalUrl: string,
93
- matchUserAsWell = false
94
- ): null | {
95
- publicId: string;
96
- typeName: string;
97
- scrollLocator?: string | null;
98
- scrollLocatorParams?: {field?: string; comment?: string; anchor?: string};
99
- context?: Context;
100
- } => {
101
- let urlObject = null;
102
- try {
103
- urlObject = new URL(originalUrl);
104
- } catch (ex) {
105
- // not a valid URL
106
- return null;
107
- }
108
-
109
- if (urlObject.host !== ownHost) {
110
- return null;
111
- }
112
-
113
- const auxMatch = getAuxMatch(urlObject.hash, "");
114
- if (auxMatch !== null) {
115
- return null;
116
- }
117
-
118
- const pageMatch = getPageMatch(urlObject.pathname);
119
- if (pageMatch === null) {
120
- return null;
121
- }
122
-
123
- const {page, params} = pageMatch;
124
- if (!validPageMatchSet.has(page)) {
125
- return null;
126
- }
127
-
128
- let typeName = [params.appNamespace, params.typeWithoutNamespace].join("/");
129
- if (typeName === "fibery/user" && !matchUserAsWell) {
130
- return null;
131
- }
132
- let publicId = null;
133
- let context: Context | undefined = undefined;
134
- if (page === ":page/specific-object-view" && !params.viewLocator) {
135
- const objectOrViewLocator = String(params.objectLocator);
136
- publicId = parseLocator(objectOrViewLocator).publicId;
137
- } else if (params.viewLocator) {
138
- const objectOrViewLocator = String(params.viewLocator);
139
- publicId = parseLocator(objectOrViewLocator).publicId;
140
- typeName = "fibery/view";
141
- const objectLocator = String(params.objectLocator);
142
- const contextEntityPublicId = parseLocator(objectLocator).publicId;
143
- if (contextEntityPublicId && params.appNamespace && params.typeWithoutNamespace) {
144
- context = {
145
- contextEntityTypeName: `${params.appNamespace}/${params.typeWithoutNamespace}`,
146
- contextEntityPublicId: contextEntityPublicId,
147
- };
148
- }
149
- }
150
-
151
- if (publicId === null) {
152
- return null;
153
- }
154
-
155
- const scrollLocatorParams = Object.fromEntries(
156
- Object.entries(parseScrollLocator(pageMatch.params.scrollLocator as string)).filter(([key, value]) => {
157
- return knownScrollLocatorParams.has(key) && value;
158
- })
159
- );
160
-
161
- return {
162
- publicId,
163
- typeName,
164
- scrollLocator: pageMatch.params.scrollLocator as string,
165
- scrollLocatorParams: Object.keys(scrollLocatorParams).length ? scrollLocatorParams : undefined,
166
- context,
167
- };
168
- };
@@ -1 +0,0 @@
1
- export let presets: string[][];
@@ -1,4 +0,0 @@
1
- "use strict";
2
- module.exports = {
3
- presets: [["@fibery/babel-preset/typescript"]],
4
- };
@@ -1,7 +0,0 @@
1
- export let testEnvironment: string;
2
- export let coveragePathIgnorePatterns: string[];
3
- export let collectCoverageFrom: string[];
4
- export let coverageReporters: string[];
5
- export let testRunner: string;
6
- export let setupFiles: any[];
7
- export let transformIgnorePatterns: string[];
@@ -1,10 +0,0 @@
1
- "use strict";
2
- module.exports = {
3
- testEnvironment: "jsdom",
4
- coveragePathIgnorePatterns: ["/node_modules/", "<rootDir>/src/mocks", "<rootDir>/src/components/icons"],
5
- collectCoverageFrom: ["src/**/*.{js,jsx,ts,tsx}", "!src/**/*.d.ts"],
6
- coverageReporters: ["text", "cobertura"],
7
- testRunner: "jest-circus/runner",
8
- setupFiles: [require.resolve("whatwg-fetch")],
9
- transformIgnorePatterns: ["node_modules/.pnpm/(?!(screenfull|lowlight|devlop|fault)@)"],
10
- };