@burojs/context 0.2.0 → 0.4.0
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/dist/index.cjs +289 -334
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +98 -83
- package/dist/index.d.ts +98 -83
- package/dist/index.js +288 -332
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/types/permissions.d.ts
|
|
1
2
|
type PermissionStatus = 'loading' | 'ready' | 'error';
|
|
2
3
|
/**
|
|
3
4
|
* Permission resolver returned by user's permissions slice.
|
|
@@ -7,39 +8,44 @@ type PermissionStatus = 'loading' | 'ready' | 'error';
|
|
|
7
8
|
* ready: normal evaluation
|
|
8
9
|
*/
|
|
9
10
|
interface PermissionMap {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
status: PermissionStatus;
|
|
12
|
+
can: (action: string, resource: string, record?: unknown) => boolean;
|
|
13
|
+
reason: (action: string, resource: string, record?: unknown) => string | undefined;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/types/slice.d.ts
|
|
15
17
|
type SliceKind = 'static' | 'fromAuth' | 'picker' | 'async';
|
|
16
18
|
/**
|
|
17
19
|
* Common shape for all slice definitions.
|
|
18
20
|
* Specific kinds extend this with kind-specific fields (defined in slices/*.ts).
|
|
19
21
|
*/
|
|
20
22
|
interface SliceDefinition<TName extends string = string, TValue = unknown> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
23
|
+
name: TName;
|
|
24
|
+
kind: SliceKind;
|
|
25
|
+
deps: string[];
|
|
26
|
+
/** Phantom — for type inference of slice value types. */
|
|
27
|
+
__valueType?: TValue;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/types/store.d.ts
|
|
28
31
|
interface Store<TState extends Record<string, unknown>> {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
snapshot: () => TState;
|
|
33
|
+
setSlice: <K extends keyof TState>(key: K, value: TState[K]) => void;
|
|
34
|
+
subscribe: (fn: () => void, selector?: (state: TState) => unknown) => () => void;
|
|
32
35
|
}
|
|
33
|
-
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/atom.d.ts
|
|
34
38
|
interface Atom<T> {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
get: () => T;
|
|
40
|
+
set: (value: T) => void;
|
|
41
|
+
subscribe: (fn: () => void) => () => void;
|
|
38
42
|
}
|
|
39
43
|
declare function createAtom<T>(initial: T): Atom<T>;
|
|
40
|
-
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/store.d.ts
|
|
41
46
|
declare function createStore<TState extends Record<string, unknown>>(initial: TState): Store<TState>;
|
|
42
|
-
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/tracked-deps.d.ts
|
|
43
49
|
/**
|
|
44
50
|
* Proxy-based read tracking. The selector function reads from a Proxy
|
|
45
51
|
* wrapping the target; every property access is recorded with dotted-path keys.
|
|
@@ -48,42 +54,46 @@ declare function createStore<TState extends Record<string, unknown>>(initial: TS
|
|
|
48
54
|
* enabling point-wise re-invocation only when relevant slices change.
|
|
49
55
|
*/
|
|
50
56
|
interface TrackedReadResult<T> {
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
result: T;
|
|
58
|
+
deps: Set<string>;
|
|
53
59
|
}
|
|
54
60
|
declare function trackReads<TCtx, TResult>(ctx: TCtx, selector: (c: TCtx) => TResult): TrackedReadResult<TResult>;
|
|
55
|
-
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/dag.d.ts
|
|
56
63
|
interface DagNode {
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
name: string;
|
|
65
|
+
deps: string[];
|
|
59
66
|
}
|
|
60
67
|
/**
|
|
61
68
|
* Kahn-style topological sort with cycle detection.
|
|
62
69
|
* Throws on cycles (with path) and on missing dependencies.
|
|
63
70
|
*/
|
|
64
71
|
declare function topoSort<N extends DagNode>(nodes: N[]): N[];
|
|
65
|
-
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/slices/static.d.ts
|
|
66
74
|
interface StaticSliceDefinition<TName extends string, TValue> extends SliceDefinition<TName, TValue> {
|
|
67
|
-
|
|
68
|
-
|
|
75
|
+
kind: 'static';
|
|
76
|
+
value: () => TValue;
|
|
69
77
|
}
|
|
70
78
|
interface StaticSliceConfig<TValue> {
|
|
71
|
-
|
|
79
|
+
value: TValue | (() => TValue);
|
|
72
80
|
}
|
|
73
81
|
declare function defineStaticSlice<TName extends string, TValue>(name: TName, config: StaticSliceConfig<TValue>): StaticSliceDefinition<TName, TValue>;
|
|
74
|
-
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/slices/from-auth.d.ts
|
|
75
84
|
type AuthInvalidationEvent = 'login' | 'logout' | 'refresh';
|
|
76
85
|
interface FromAuthSliceDefinition<TName extends string, TValue> extends SliceDefinition<TName, TValue> {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
kind: 'fromAuth';
|
|
87
|
+
extract: (rawAuthUser: unknown) => TValue;
|
|
88
|
+
invalidateOn: AuthInvalidationEvent[];
|
|
80
89
|
}
|
|
81
90
|
interface FromAuthSliceConfig<TValue> {
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
extract: (rawAuthUser: never) => TValue;
|
|
92
|
+
invalidateOn?: AuthInvalidationEvent[];
|
|
84
93
|
}
|
|
85
94
|
declare function defineFromAuthSlice<TName extends string, TValue>(name: TName, config: FromAuthSliceConfig<TValue>): FromAuthSliceDefinition<TName, TValue>;
|
|
86
|
-
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/slices/picker.d.ts
|
|
87
97
|
type PickerPersistMode = 'localStorage' | 'sessionStorage' | 'url';
|
|
88
98
|
/**
|
|
89
99
|
* Picker target — minimal contract. Real ResourceDef from @burojs/core
|
|
@@ -102,62 +112,64 @@ type PickerPersistMode = 'localStorage' | 'sessionStorage' | 'url';
|
|
|
102
112
|
* Resolver emits a dev-warning if target has those set.
|
|
103
113
|
*/
|
|
104
114
|
interface PickerSliceDefinition<TName extends string, TValue> extends SliceDefinition<TName, TValue> {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
115
|
+
kind: 'picker';
|
|
116
|
+
resource: () => unknown;
|
|
117
|
+
filter?: (ctx: never) => Record<string, unknown>;
|
|
118
|
+
resetWhen?: string[];
|
|
119
|
+
persist?: PickerPersistMode;
|
|
120
|
+
required: boolean;
|
|
111
121
|
}
|
|
112
122
|
interface PickerSliceConfig {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
123
|
+
resource: () => unknown;
|
|
124
|
+
dependsOn?: string[];
|
|
125
|
+
filter?: (ctx: never) => Record<string, unknown>;
|
|
126
|
+
resetWhen?: string[];
|
|
127
|
+
persist?: PickerPersistMode;
|
|
128
|
+
required?: boolean;
|
|
119
129
|
}
|
|
120
130
|
declare function definePickerSlice<TName extends string, TValue = unknown>(name: TName, config: PickerSliceConfig): PickerSliceDefinition<TName, TValue>;
|
|
121
|
-
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/slices/async.d.ts
|
|
122
133
|
interface AsyncSliceDefinition<TName extends string, TValue> extends SliceDefinition<TName, TValue> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
134
|
+
kind: 'async';
|
|
135
|
+
fetch: (ctx: never) => Promise<TValue>;
|
|
136
|
+
cacheKey?: (ctx: never) => string;
|
|
137
|
+
staleTime: number;
|
|
127
138
|
}
|
|
128
139
|
interface AsyncSliceConfig<TValue> {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
dependsOn?: string[];
|
|
141
|
+
fetch: (ctx: never) => Promise<TValue>;
|
|
142
|
+
cacheKey?: (ctx: never) => string;
|
|
143
|
+
/** ms; 0 = always refetch on invalidation */
|
|
144
|
+
staleTime?: number;
|
|
134
145
|
}
|
|
135
146
|
declare function defineAsyncSlice<TName extends string, TValue>(name: TName, config: AsyncSliceConfig<TValue>): AsyncSliceDefinition<TName, TValue>;
|
|
136
|
-
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/define-context-slices.d.ts
|
|
137
149
|
interface SliceBuilder {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
static: <TValue>(config: StaticSliceConfig<TValue>) => StaticSliceDefinition<string, TValue>;
|
|
151
|
+
fromAuth: <TValue>(config: FromAuthSliceConfig<TValue>) => FromAuthSliceDefinition<string, TValue>;
|
|
152
|
+
picker: <TValue = unknown>(config: PickerSliceConfig) => PickerSliceDefinition<string, TValue>;
|
|
153
|
+
async: <TValue>(config: AsyncSliceConfig<TValue>) => AsyncSliceDefinition<string, TValue>;
|
|
142
154
|
}
|
|
143
155
|
declare function defineContextSlices<T extends Record<string, SliceDefinition>>(factory: (s: SliceBuilder) => T): T;
|
|
144
|
-
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/resolver.d.ts
|
|
145
158
|
interface Resolver<TSlices extends Record<string, SliceDefinition>> {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
feedAuth: (rawAuthUser: unknown) => void;
|
|
153
|
-
flushAsync: () => Promise<void>;
|
|
159
|
+
boot: () => Promise<void>;
|
|
160
|
+
snapshot: () => { [K in keyof TSlices]: unknown; };
|
|
161
|
+
setSlice: <K extends keyof TSlices>(key: K, value: unknown) => void;
|
|
162
|
+
subscribe: (fn: () => void, selector?: (s: never) => unknown) => () => void;
|
|
163
|
+
feedAuth: (rawAuthUser: unknown) => void;
|
|
164
|
+
flushAsync: () => Promise<void>;
|
|
154
165
|
}
|
|
155
166
|
declare function createResolver<TSlices extends Record<string, SliceDefinition>>(slices: TSlices): Resolver<TSlices>;
|
|
156
|
-
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/scope.d.ts
|
|
157
169
|
type Scope = Record<string, unknown> | null;
|
|
158
170
|
interface ScopeChainEntry {
|
|
159
|
-
|
|
160
|
-
|
|
171
|
+
name: string;
|
|
172
|
+
scope: (ctx: never) => Scope;
|
|
161
173
|
}
|
|
162
174
|
/**
|
|
163
175
|
* Shallow merge two scope objects. Patch wins on conflict.
|
|
@@ -171,27 +183,30 @@ declare function mergeScope(base: Scope, patch: Scope): Scope;
|
|
|
171
183
|
*/
|
|
172
184
|
declare function cascadeScope(chain: ScopeChainEntry[], ctx: never): Scope;
|
|
173
185
|
interface RequireScopeOk {
|
|
174
|
-
|
|
186
|
+
ok: true;
|
|
175
187
|
}
|
|
176
188
|
interface RequireScopeFail {
|
|
177
|
-
|
|
178
|
-
|
|
189
|
+
ok: false;
|
|
190
|
+
missing: string[];
|
|
179
191
|
}
|
|
180
192
|
type RequireScopeResult = RequireScopeOk | RequireScopeFail;
|
|
181
193
|
/**
|
|
182
194
|
* Verify all required keys are present (and not undefined) in scope.
|
|
183
195
|
*/
|
|
184
196
|
declare function checkRequireScope(scope: Scope, required: string[]): RequireScopeResult;
|
|
185
|
-
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/permissions.d.ts
|
|
186
199
|
declare function createPendingPermissions(): PermissionMap;
|
|
187
200
|
declare function createErrorPermissions(error: unknown): PermissionMap;
|
|
188
201
|
interface ReadyPermissionsConfig {
|
|
189
|
-
|
|
190
|
-
|
|
202
|
+
can: (action: string, resource: string, record?: unknown) => boolean;
|
|
203
|
+
reason?: (action: string, resource: string, record?: unknown) => string | undefined;
|
|
191
204
|
}
|
|
192
205
|
declare function createReadyPermissions(config: ReadyPermissionsConfig): PermissionMap;
|
|
193
|
-
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/index.d.ts
|
|
194
208
|
declare const PACKAGE_NAME: "@burojs/context";
|
|
195
209
|
declare const PACKAGE_VERSION: "0.0.0";
|
|
196
|
-
|
|
210
|
+
//#endregion
|
|
197
211
|
export { type AsyncSliceConfig, type AsyncSliceDefinition, type Atom, type AuthInvalidationEvent, type DagNode, type FromAuthSliceConfig, type FromAuthSliceDefinition, PACKAGE_NAME, PACKAGE_VERSION, type PermissionMap, type PermissionStatus, type PickerPersistMode, type PickerSliceConfig, type PickerSliceDefinition, type ReadyPermissionsConfig, type RequireScopeFail, type RequireScopeOk, type RequireScopeResult, type Resolver, type Scope, type ScopeChainEntry, type SliceBuilder, type SliceDefinition, type SliceKind, type StaticSliceConfig, type StaticSliceDefinition, type Store, type TrackedReadResult, cascadeScope, checkRequireScope, createAtom, createErrorPermissions, createPendingPermissions, createReadyPermissions, createResolver, createStore, defineAsyncSlice, defineContextSlices, defineFromAuthSlice, definePickerSlice, defineStaticSlice, mergeScope, topoSort, trackReads };
|
|
212
|
+
//# sourceMappingURL=index.d.ts.map
|