@volo/abp-react-app-config 10.3.0-rc.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.
Files changed (4) hide show
  1. package/README.md +28 -0
  2. package/index.d.ts +44 -0
  3. package/index.js +68 -0
  4. package/package.json +37 -0
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ ## Description
2
+
3
+ `@volo/abp-react-app-config` provides React hooks over `@volo/abp-app-config`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @volo/abp-react-app-config @volo/abp-app-config
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { createAbpReactAppConfig } from '@volo/abp-react-app-config'
15
+
16
+ export const appConfig = createAbpReactAppConfig({
17
+ baseUrl: () => window.location.origin,
18
+ })
19
+
20
+ await appConfig.fetchConfig()
21
+ ```
22
+
23
+ ```tsx
24
+ function Component() {
25
+ const { isGranted } = appConfig.usePolicies()
26
+ return <>{isGranted('AbpIdentity.Users') ? 'yes' : 'no'}</>
27
+ }
28
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ import type { AbpAppConfigClient, AbpAppConfigOptions, AbpAppConfigSnapshot } from '@volo/abp-app-config'
2
+
3
+ export interface AbpReactAppConfigClient<TConfig = Record<string, unknown>>
4
+ extends AbpAppConfigClient<TConfig> {
5
+ useSnapshot: () => Readonly<AbpAppConfigSnapshot<TConfig>>
6
+ usePolicies: () => {
7
+ policies: Record<string, boolean>
8
+ isGranted: (policyExpression: string) => boolean
9
+ isLoading: boolean
10
+ isLoaded: boolean
11
+ refetch: () => Promise<TConfig>
12
+ clear: () => void
13
+ fetchConfig: (
14
+ token?: string | null,
15
+ requestOptions?: RequestInit
16
+ ) => Promise<TConfig>
17
+ }
18
+ useTranslation: (defaultResource?: string | null) => {
19
+ localization: AbpAppConfigSnapshot<TConfig>['localization']
20
+ currentCulture: string
21
+ isLoading: boolean
22
+ isLoaded: boolean
23
+ refetch: (
24
+ culture?: string,
25
+ token?: string | null,
26
+ requestOptions?: RequestInit
27
+ ) => Promise<unknown>
28
+ t: (key: string, resourceName?: string | null) => string
29
+ tWithFallback: (key: string, resourceName?: string | null) => string
30
+ }
31
+ }
32
+
33
+ export interface AbpReactAppConfigOptions<TConfig = Record<string, unknown>>
34
+ extends AbpAppConfigOptions {
35
+ client?: AbpAppConfigClient<TConfig>
36
+ }
37
+
38
+ export type AbpReactAppConfigCreateOptions<TConfig = Record<string, unknown>> =
39
+ | AbpReactAppConfigOptions<TConfig>
40
+ | { client: AbpAppConfigClient<TConfig> }
41
+
42
+ export declare function createAbpReactAppConfig<TConfig = Record<string, unknown>>(
43
+ options?: AbpReactAppConfigCreateOptions<TConfig>
44
+ ): AbpReactAppConfigClient<TConfig>
package/index.js ADDED
@@ -0,0 +1,68 @@
1
+ import { useMemo, useSyncExternalStore } from 'react'
2
+ import { createAbpAppConfig } from '@volo/abp-app-config'
3
+
4
+ function evaluatePolicy(policies, policyExpression) {
5
+ const hasPolicy = (policy) => policies[policy.trim()] === true
6
+
7
+ if (policyExpression.includes('||')) {
8
+ return policyExpression.split('||').some((part) => hasPolicy(part))
9
+ }
10
+
11
+ if (policyExpression.includes('&&')) {
12
+ return policyExpression.split('&&').every((part) => hasPolicy(part))
13
+ }
14
+
15
+ return hasPolicy(policyExpression)
16
+ }
17
+
18
+ export function createAbpReactAppConfig(options = {}) {
19
+ const client = options.client || createAbpAppConfig(options)
20
+
21
+ function useSnapshot() {
22
+ return useSyncExternalStore(client.subscribe, client.getSnapshot, client.getSnapshot)
23
+ }
24
+
25
+ function usePolicies() {
26
+ const snapshot = useSnapshot()
27
+ const policies =
28
+ snapshot.sections.auth?.grantedPolicies ||
29
+ snapshot.sections.auth?.policies ||
30
+ {}
31
+
32
+ const isGranted = useMemo(() => {
33
+ return (policyExpression) => evaluatePolicy(policies, policyExpression)
34
+ }, [policies])
35
+
36
+ return {
37
+ policies,
38
+ isGranted,
39
+ isLoading: snapshot.loading,
40
+ isLoaded: snapshot.initialized,
41
+ refetch: client.refetch,
42
+ clear: client.clear,
43
+ fetchConfig: client.fetchConfig,
44
+ }
45
+ }
46
+
47
+ function useTranslation(defaultResource = null) {
48
+ const snapshot = useSnapshot()
49
+
50
+ return {
51
+ localization: snapshot.localization,
52
+ currentCulture: snapshot.currentCulture,
53
+ isLoading: snapshot.localizationLoading,
54
+ isLoaded: snapshot.localizationInitialized,
55
+ refetch: client.fetchLocalization,
56
+ t: (key, resourceName = defaultResource) => client.t(key, resourceName),
57
+ tWithFallback: (key, resourceName = defaultResource) =>
58
+ client.tWithFallback(key, resourceName),
59
+ }
60
+ }
61
+
62
+ return {
63
+ ...client,
64
+ useSnapshot,
65
+ usePolicies,
66
+ useTranslation,
67
+ }
68
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "version": "10.3.0-rc.1",
3
+ "name": "@volo/abp-react-app-config",
4
+ "description": "React adapter helpers for @volo/abp-app-config.",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "types": "./index.d.ts",
11
+ "files": [
12
+ "index.js",
13
+ "index.d.ts",
14
+ "README.md"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "dependencies": {
20
+ "@volo/abp-app-config": "~10.3.0-rc.1"
21
+ },
22
+ "peerDependencies": {
23
+ "react": ">=18"
24
+ },
25
+ "homepage": "https://abp.io",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/volosoft/volo.git"
29
+ },
30
+ "license": "LGPL-3.0",
31
+ "keywords": [
32
+ "abp",
33
+ "application-configuration",
34
+ "react",
35
+ "adapter"
36
+ ]
37
+ }