@forge/react 11.2.9 → 11.3.0-experimental-6de6b19

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.
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePermissions = void 0;
4
+ const react_1 = require("react");
5
+ const bridge_1 = require("@forge/bridge");
6
+ /**
7
+ * Helper function to check if a URL matches any of the allowed patterns
8
+ * Supports wildcards and basic pattern matching
9
+ */
10
+ const matchesAllowedUrl = (url, allowedUrls) => {
11
+ return allowedUrls.some((allowedUrl) => {
12
+ // Exact match
13
+ if (allowedUrl === url)
14
+ return true;
15
+ // Wildcard pattern matching
16
+ if (allowedUrl.includes('*')) {
17
+ const pattern = allowedUrl
18
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape regex special chars
19
+ .replace(/\*/g, '.*'); // Convert * to .*
20
+ const regex = new RegExp(`^${pattern}`);
21
+ return regex.test(url);
22
+ }
23
+ return false;
24
+ });
25
+ };
26
+ /**
27
+ * Hook for checking permissions in Forge apps
28
+ *
29
+ * @param requiredPermissions - The permissions required for the component
30
+ * @returns Object containing permission state and loading status
31
+ *
32
+ * @example
33
+ * ```tsx
34
+ * const MyComponent: React.FC = () => {
35
+ * const { hasPermission, isLoading, missingPermissions } = usePermissions({
36
+ * scopes: ['write:confluence-content'],
37
+ * external: {
38
+ * fetch: {
39
+ * backend: ['https://api.example.com']
40
+ * }
41
+ * }
42
+ * });
43
+ *
44
+ * if (isLoading) return <LoadingSpinner />;
45
+ *
46
+ * if (!hasPermission) {
47
+ * return <PermissionDenied missingPermissions={missingPermissions} />;
48
+ * }
49
+ *
50
+ * return <ProtectedFeature />;
51
+ * };
52
+ * ```
53
+ */
54
+ const usePermissions = (requiredPermissions) => {
55
+ const [context, setContext] = (0, react_1.useState)();
56
+ const [isLoading, setIsLoading] = (0, react_1.useState)(true);
57
+ const [error, setError] = (0, react_1.useState)(null);
58
+ // Load context on mount
59
+ (0, react_1.useEffect)(() => {
60
+ const loadContext = async () => {
61
+ try {
62
+ setIsLoading(true);
63
+ setError(null);
64
+ const contextData = await bridge_1.view.getContext();
65
+ setContext(contextData);
66
+ }
67
+ catch (err) {
68
+ setError(err instanceof Error ? err : new Error('Failed to load context'));
69
+ }
70
+ finally {
71
+ setIsLoading(false);
72
+ }
73
+ };
74
+ void loadContext();
75
+ }, []);
76
+ // Permission checking utilities
77
+ const permissionUtils = (0, react_1.useMemo)(() => {
78
+ if (!context?.permissions)
79
+ return null;
80
+ const { scopes, external = {} } = context.permissions;
81
+ const scopeArray = Array.isArray(scopes) ? scopes : Object.keys(scopes || {});
82
+ return {
83
+ hasScope: (scope) => scopeArray.includes(scope),
84
+ canFetchFrom: (type, url) => {
85
+ const fetchUrls = external.fetch?.[type];
86
+ if (!fetchUrls?.length)
87
+ return false;
88
+ const allowedUrls = fetchUrls
89
+ .map((item) => typeof item === 'string' ? item : 'address' in item ? item.address : item.remote)
90
+ .filter((url) => typeof url === 'string');
91
+ return matchesAllowedUrl(url, allowedUrls);
92
+ },
93
+ canLoadResource: (type, url) => {
94
+ const resourceUrls = external[type];
95
+ if (!resourceUrls?.length)
96
+ return false;
97
+ const stringUrls = resourceUrls.filter((item) => typeof item === 'string');
98
+ return matchesAllowedUrl(url, stringUrls);
99
+ },
100
+ getScopes: () => scopeArray,
101
+ getExternalPermissions: () => external,
102
+ hasAnyPermissions: () => scopeArray.length > 0 || Object.keys(external).length > 0
103
+ };
104
+ }, [context?.permissions, isLoading, error]);
105
+ // Check permissions
106
+ const permissionResult = (0, react_1.useMemo)(() => {
107
+ if (!requiredPermissions) {
108
+ return { granted: false, missing: null };
109
+ }
110
+ if (!permissionUtils) {
111
+ // If still loading or there's an error, return null for missing permissions
112
+ if (isLoading || error) {
113
+ return { granted: false, missing: null };
114
+ }
115
+ // No permissions available - return all required permissions as missing
116
+ return { granted: false, missing: requiredPermissions };
117
+ }
118
+ const missing = {};
119
+ let hasAllPermissions = true;
120
+ // Check scopes
121
+ if (requiredPermissions.scopes?.length) {
122
+ const missingScopes = requiredPermissions.scopes.filter((scope) => !permissionUtils.hasScope(scope));
123
+ if (missingScopes.length > 0) {
124
+ missing.scopes = missingScopes;
125
+ hasAllPermissions = false;
126
+ }
127
+ }
128
+ // Check external permissions
129
+ if (requiredPermissions.external) {
130
+ const missingExternal = {};
131
+ // Check fetch permissions
132
+ if (requiredPermissions.external.fetch) {
133
+ const missingFetch = {};
134
+ ['backend', 'client'].forEach((type) => {
135
+ const requiredUrls = requiredPermissions.external?.fetch?.[type];
136
+ if (requiredUrls?.length) {
137
+ const missingUrls = requiredUrls.filter((url) => !permissionUtils.canFetchFrom(type, url));
138
+ if (missingUrls.length > 0) {
139
+ missingFetch[type] = missingUrls;
140
+ hasAllPermissions = false;
141
+ }
142
+ }
143
+ });
144
+ if (Object.keys(missingFetch).length > 0) {
145
+ missingExternal.fetch = missingFetch;
146
+ }
147
+ }
148
+ // Check resource permissions
149
+ const resourceTypes = ['fonts', 'styles', 'frames', 'images', 'media', 'scripts'];
150
+ resourceTypes.forEach((type) => {
151
+ const requiredUrls = requiredPermissions.external?.[type];
152
+ if (requiredUrls?.length) {
153
+ const missingUrls = requiredUrls.filter((url) => !permissionUtils.canLoadResource(type, url));
154
+ if (missingUrls.length > 0) {
155
+ missingExternal[type] = missingUrls;
156
+ hasAllPermissions = false;
157
+ }
158
+ }
159
+ });
160
+ if (Object.keys(missingExternal).length > 0) {
161
+ missing.external = missingExternal;
162
+ }
163
+ }
164
+ // Note: Content permissions are not supported in the current RuntimePermissions type
165
+ return {
166
+ granted: hasAllPermissions,
167
+ missing: hasAllPermissions ? null : missing
168
+ };
169
+ }, [permissionUtils, requiredPermissions]);
170
+ return {
171
+ hasPermission: permissionResult.granted,
172
+ isLoading,
173
+ missingPermissions: permissionResult.missing,
174
+ error
175
+ };
176
+ };
177
+ exports.usePermissions = usePermissions;
@@ -1,3 +1,3 @@
1
- import type { FullContext } from '@forge/bridge/out/types';
1
+ import type { FullContext } from '@forge/bridge';
2
2
  export declare const useProductContext: () => FullContext | undefined;
3
3
  //# sourceMappingURL=useProductContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useProductContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useProductContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAG3D,eAAO,MAAM,iBAAiB,+BAa7B,CAAC"}
1
+ {"version":3,"file":"useProductContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useProductContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAGjD,eAAO,MAAM,iBAAiB,+BAa7B,CAAC"}
package/out/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { useProductContext } from './hooks/useProductContext';
2
2
  export { useConfig } from './hooks/useConfig';
3
+ export { usePermissions, type PermissionRequirements, type MissingPermissions, type PermissionCheckResult } from './hooks/usePermissions';
3
4
  export { ForgeReconciler as default } from './reconciler';
4
5
  export * from './components';
5
6
  export { useContentProperty } from './hooks/useContentProperty';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,EAAE,eAAe,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAE1D,cAAc,cAAc,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtE,OAAO,EAAE,+BAA+B,EAAE,MAAM,oDAAoD,CAAC;AACrG,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EACL,cAAc,EACd,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC3B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,eAAe,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAE1D,cAAc,cAAc,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtE,OAAO,EAAE,+BAA+B,EAAE,MAAM,oDAAoD,CAAC;AACrG,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,cAAc,iBAAiB,CAAC"}
package/out/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useForm = exports.replaceUnsupportedDocumentNodes = exports.I18nProvider = exports.useTranslation = exports.useIssueProperty = exports.useSpaceProperty = exports.useContentProperty = exports.default = exports.useConfig = exports.useProductContext = void 0;
3
+ exports.useForm = exports.replaceUnsupportedDocumentNodes = exports.I18nProvider = exports.useTranslation = exports.useIssueProperty = exports.useSpaceProperty = exports.useContentProperty = exports.default = exports.usePermissions = exports.useConfig = exports.useProductContext = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  var useProductContext_1 = require("./hooks/useProductContext");
6
6
  Object.defineProperty(exports, "useProductContext", { enumerable: true, get: function () { return useProductContext_1.useProductContext; } });
7
7
  var useConfig_1 = require("./hooks/useConfig");
8
8
  Object.defineProperty(exports, "useConfig", { enumerable: true, get: function () { return useConfig_1.useConfig; } });
9
+ var usePermissions_1 = require("./hooks/usePermissions");
10
+ Object.defineProperty(exports, "usePermissions", { enumerable: true, get: function () { return usePermissions_1.usePermissions; } });
9
11
  var reconciler_1 = require("./reconciler");
10
12
  Object.defineProperty(exports, "default", { enumerable: true, get: function () { return reconciler_1.ForgeReconciler; } });
11
13
  tslib_1.__exportStar(require("./components"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forge/react",
3
- "version": "11.2.9",
3
+ "version": "11.3.0-experimental-6de6b19",
4
4
  "description": "Forge React reconciler",
5
5
  "author": "Atlassian",
6
6
  "license": "SEE LICENSE IN LICENSE.txt",
@@ -15,14 +15,20 @@
15
15
  "add-component-jsdoc": "ts-node ./build/add-component-jsdoc.ts"
16
16
  },
17
17
  "exports": {
18
- ".": "./out/index.js",
19
- "./jira": "./out/components/jira/index.js"
18
+ ".": {
19
+ "types": "./out/index.d.ts",
20
+ "default": "./out/index.js"
21
+ },
22
+ "./jira": {
23
+ "types": "./out/components/jira/index.d.ts",
24
+ "default": "./out/components/jira/index.js"
25
+ }
20
26
  },
21
27
  "dependencies": {
22
28
  "@atlaskit/adf-schema": "^48.0.0",
23
29
  "@atlaskit/adf-utils": "^19.19.0",
24
30
  "@atlaskit/forge-react-types": "^0.42.10",
25
- "@forge/bridge": "^5.4.1",
31
+ "@forge/bridge": "^5.5.0-experimental-6de6b19",
26
32
  "@forge/i18n": "0.0.7",
27
33
  "@types/react": "^18.2.64",
28
34
  "@types/react-reconciler": "^0.28.8",