@nocobase/shared 3.0.0-alpha.3 → 3.0.0-alpha.5

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 CHANGED
@@ -7,4 +7,5 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  export * from './getValuesByPath';
10
+ export * from './ui-operation';
10
11
  export * from './variable-usage';
package/lib/index.js CHANGED
@@ -24,9 +24,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
24
24
  var src_exports = {};
25
25
  module.exports = __toCommonJS(src_exports);
26
26
  __reExport(src_exports, require("./getValuesByPath"), module.exports);
27
+ __reExport(src_exports, require("./ui-operation"), module.exports);
27
28
  __reExport(src_exports, require("./variable-usage"), module.exports);
28
29
  // Annotate the CommonJS export names for ESM import in node:
29
30
  0 && (module.exports = {
30
31
  ...require("./getValuesByPath"),
32
+ ...require("./ui-operation"),
31
33
  ...require("./variable-usage")
32
34
  });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export declare const UI_OPERATION_QUERY_KEY = "_operation_";
10
+ export declare const UI_OPERATION_VERSION = 1;
11
+ export declare const MAX_UI_OPERATION_ENCODED_LENGTH: number;
12
+ export interface UIOperation<TOperationId extends string = string, TParams extends Record<string, unknown> = Record<string, unknown>> {
13
+ v: typeof UI_OPERATION_VERSION;
14
+ operationId: TOperationId;
15
+ params?: TParams;
16
+ }
17
+ export interface UIOperationCodec {
18
+ encode(value: string): string;
19
+ decode(value: string): string | undefined;
20
+ }
21
+ export declare function isUIOperation(value: unknown): value is UIOperation;
22
+ export declare function encodeUIOperation(operation: UIOperation, codec: UIOperationCodec): string;
23
+ export declare function decodeUIOperation(encoded: string, codec: UIOperationCodec): UIOperation | undefined;
24
+ export declare function parseUIOperation(search: string, codec: UIOperationCodec): UIOperation | undefined;
25
+ export declare function removeUIOperation(search: string): string;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var ui_operation_exports = {};
29
+ __export(ui_operation_exports, {
30
+ MAX_UI_OPERATION_ENCODED_LENGTH: () => MAX_UI_OPERATION_ENCODED_LENGTH,
31
+ UI_OPERATION_QUERY_KEY: () => UI_OPERATION_QUERY_KEY,
32
+ UI_OPERATION_VERSION: () => UI_OPERATION_VERSION,
33
+ decodeUIOperation: () => decodeUIOperation,
34
+ encodeUIOperation: () => encodeUIOperation,
35
+ isUIOperation: () => isUIOperation,
36
+ parseUIOperation: () => parseUIOperation,
37
+ removeUIOperation: () => removeUIOperation
38
+ });
39
+ module.exports = __toCommonJS(ui_operation_exports);
40
+ const UI_OPERATION_QUERY_KEY = "_operation_";
41
+ const UI_OPERATION_VERSION = 1;
42
+ const MAX_UI_OPERATION_ENCODED_LENGTH = 8 * 1024;
43
+ function isPlainObject(value) {
44
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
45
+ return false;
46
+ }
47
+ const prototype = Object.getPrototypeOf(value);
48
+ return prototype === Object.prototype || prototype === null;
49
+ }
50
+ __name(isPlainObject, "isPlainObject");
51
+ function isUIOperation(value) {
52
+ if (!isPlainObject(value) || value.v !== UI_OPERATION_VERSION || typeof value.operationId !== "string" || !value.operationId) {
53
+ return false;
54
+ }
55
+ return value.params === void 0 || isPlainObject(value.params);
56
+ }
57
+ __name(isUIOperation, "isUIOperation");
58
+ function encodeUIOperation(operation, codec) {
59
+ if (!isUIOperation(operation)) {
60
+ throw new TypeError("Invalid UI operation.");
61
+ }
62
+ const encoded = codec.encode(JSON.stringify(operation));
63
+ if (encoded.length > MAX_UI_OPERATION_ENCODED_LENGTH) {
64
+ throw new RangeError(`UI operation exceeds the ${MAX_UI_OPERATION_ENCODED_LENGTH}-byte encoded length limit.`);
65
+ }
66
+ return encoded;
67
+ }
68
+ __name(encodeUIOperation, "encodeUIOperation");
69
+ function decodeUIOperation(encoded, codec) {
70
+ if (encoded.length > MAX_UI_OPERATION_ENCODED_LENGTH) {
71
+ return void 0;
72
+ }
73
+ try {
74
+ const decoded = codec.decode(encoded);
75
+ if (decoded === void 0) {
76
+ return void 0;
77
+ }
78
+ const parsed = JSON.parse(decoded);
79
+ return isUIOperation(parsed) ? parsed : void 0;
80
+ } catch {
81
+ return void 0;
82
+ }
83
+ }
84
+ __name(decodeUIOperation, "decodeUIOperation");
85
+ function parseUIOperation(search, codec) {
86
+ try {
87
+ const values = new URLSearchParams(search).getAll(UI_OPERATION_QUERY_KEY);
88
+ if (values.length !== 1) {
89
+ return void 0;
90
+ }
91
+ return decodeUIOperation(values[0], codec);
92
+ } catch {
93
+ return void 0;
94
+ }
95
+ }
96
+ __name(parseUIOperation, "parseUIOperation");
97
+ function removeUIOperation(search) {
98
+ const params = new URLSearchParams(search);
99
+ params.delete(UI_OPERATION_QUERY_KEY);
100
+ const remaining = params.toString();
101
+ return remaining ? `?${remaining}` : "";
102
+ }
103
+ __name(removeUIOperation, "removeUIOperation");
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ MAX_UI_OPERATION_ENCODED_LENGTH,
107
+ UI_OPERATION_QUERY_KEY,
108
+ UI_OPERATION_VERSION,
109
+ decodeUIOperation,
110
+ encodeUIOperation,
111
+ isUIOperation,
112
+ parseUIOperation,
113
+ removeUIOperation
114
+ });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@nocobase/shared",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "Apache-2.0",
7
- "gitHead": "9899cd799c070c7bd85140148ea5b100b046bdc7"
7
+ "gitHead": "82c7ed1cdb2f247c190582e34ee37f154cac0968"
8
8
  }
package/src/index.ts CHANGED
@@ -8,4 +8,5 @@
8
8
  */
9
9
 
10
10
  export * from './getValuesByPath';
11
+ export * from './ui-operation';
11
12
  export * from './variable-usage';
@@ -0,0 +1,99 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ export const UI_OPERATION_QUERY_KEY = '_operation_';
11
+ export const UI_OPERATION_VERSION = 1;
12
+ export const MAX_UI_OPERATION_ENCODED_LENGTH = 8 * 1024;
13
+
14
+ export interface UIOperation<
15
+ TOperationId extends string = string,
16
+ TParams extends Record<string, unknown> = Record<string, unknown>,
17
+ > {
18
+ v: typeof UI_OPERATION_VERSION;
19
+ operationId: TOperationId;
20
+ params?: TParams;
21
+ }
22
+
23
+ export interface UIOperationCodec {
24
+ encode(value: string): string;
25
+ decode(value: string): string | undefined;
26
+ }
27
+
28
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
29
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
30
+ return false;
31
+ }
32
+
33
+ const prototype = Object.getPrototypeOf(value);
34
+ return prototype === Object.prototype || prototype === null;
35
+ }
36
+
37
+ export function isUIOperation(value: unknown): value is UIOperation {
38
+ if (
39
+ !isPlainObject(value) ||
40
+ value.v !== UI_OPERATION_VERSION ||
41
+ typeof value.operationId !== 'string' ||
42
+ !value.operationId
43
+ ) {
44
+ return false;
45
+ }
46
+
47
+ return value.params === undefined || isPlainObject(value.params);
48
+ }
49
+
50
+ export function encodeUIOperation(operation: UIOperation, codec: UIOperationCodec): string {
51
+ if (!isUIOperation(operation)) {
52
+ throw new TypeError('Invalid UI operation.');
53
+ }
54
+
55
+ const encoded = codec.encode(JSON.stringify(operation));
56
+ if (encoded.length > MAX_UI_OPERATION_ENCODED_LENGTH) {
57
+ throw new RangeError(`UI operation exceeds the ${MAX_UI_OPERATION_ENCODED_LENGTH}-byte encoded length limit.`);
58
+ }
59
+
60
+ return encoded;
61
+ }
62
+
63
+ export function decodeUIOperation(encoded: string, codec: UIOperationCodec): UIOperation | undefined {
64
+ if (encoded.length > MAX_UI_OPERATION_ENCODED_LENGTH) {
65
+ return undefined;
66
+ }
67
+
68
+ try {
69
+ const decoded = codec.decode(encoded);
70
+ if (decoded === undefined) {
71
+ return undefined;
72
+ }
73
+
74
+ const parsed: unknown = JSON.parse(decoded);
75
+ return isUIOperation(parsed) ? parsed : undefined;
76
+ } catch {
77
+ return undefined;
78
+ }
79
+ }
80
+
81
+ export function parseUIOperation(search: string, codec: UIOperationCodec): UIOperation | undefined {
82
+ try {
83
+ const values = new URLSearchParams(search).getAll(UI_OPERATION_QUERY_KEY);
84
+ if (values.length !== 1) {
85
+ return undefined;
86
+ }
87
+
88
+ return decodeUIOperation(values[0], codec);
89
+ } catch {
90
+ return undefined;
91
+ }
92
+ }
93
+
94
+ export function removeUIOperation(search: string): string {
95
+ const params = new URLSearchParams(search);
96
+ params.delete(UI_OPERATION_QUERY_KEY);
97
+ const remaining = params.toString();
98
+ return remaining ? `?${remaining}` : '';
99
+ }