local-lite 0.0.15 → 0.0.16
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/adapters/remote/supabase.d.ts +1 -5
- package/dist/adapters/remote/supabase.js +2 -2
- package/dist/auth/google.d.ts +2 -1
- package/dist/auth/google.js +7 -0
- package/dist/auth/microsoft.d.ts +3 -3
- package/dist/auth/microsoft.js +12 -3
- package/dist/backups/cloud-storage.d.ts +22 -6
- package/dist/backups/cloud-storage.js +111 -16
- package/dist/backups/index.d.ts +3 -15
- package/dist/backups/index.js +9 -26
- package/dist/backups/onedrive.d.ts +18 -4
- package/dist/backups/onedrive.js +69 -12
- package/dist/backups/operations.d.ts +39 -0
- package/dist/backups/operations.js +74 -0
- package/dist/config/resolve-remote.js +0 -1
- package/dist/core/client.d.ts +20 -0
- package/dist/core/client.js +146 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/react/SyncInspector.d.ts +0 -2
- package/dist/react/SyncInspector.js +0 -2
- package/dist/react/auth.d.ts +0 -2
- package/dist/react/auth.js +0 -2
- package/dist/react/backups.d.ts +28 -0
- package/dist/react/backups.js +265 -0
- package/dist/react/index.d.ts +3 -2
- package/dist/react/index.js +2 -2
- package/package.json +1 -11
- package/dist/access/client.d.ts +0 -9
- package/dist/access/client.js +0 -56
- package/dist/access/engine.d.ts +0 -2
- package/dist/access/engine.js +0 -46
- package/dist/access/errors.d.ts +0 -14
- package/dist/access/errors.js +0 -23
- package/dist/access/guard.d.ts +0 -24
- package/dist/access/guard.js +0 -104
- package/dist/access/index.d.ts +0 -5
- package/dist/access/index.js +0 -21
- package/dist/access/react/index.d.ts +0 -32
- package/dist/access/react/index.js +0 -42
- package/dist/access/types.d.ts +0 -26
- package/dist/access/types.js +0 -2
package/dist/react/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.SyncInspector = void 0;
|
|
18
18
|
exports.clearLiveQueryCache = clearLiveQueryCache;
|
|
19
19
|
exports.useLiveQuery = useLiveQuery;
|
|
20
20
|
exports.useLiveQueries = useLiveQueries;
|
|
@@ -644,5 +644,5 @@ __exportStar(require("./theme.js"), exports);
|
|
|
644
644
|
__exportStar(require("./BenchmarkModal.js"), exports);
|
|
645
645
|
var SyncInspector_js_1 = require("./SyncInspector.js");
|
|
646
646
|
Object.defineProperty(exports, "SyncInspector", { enumerable: true, get: function () { return SyncInspector_js_1.SyncInspector; } });
|
|
647
|
-
Object.defineProperty(exports, "SyncIndicator", { enumerable: true, get: function () { return SyncInspector_js_1.SyncIndicator; } });
|
|
648
647
|
__exportStar(require("./auth.js"), exports);
|
|
648
|
+
__exportStar(require("./backups.js"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "local-lite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "Never let a lost connection break a promise. Local-first sync that faithfully keeps data safe until you're back online.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -100,16 +100,6 @@
|
|
|
100
100
|
"types": "./dist/core/index.d.ts",
|
|
101
101
|
"import": "./dist/core/index.js",
|
|
102
102
|
"default": "./dist/core/index.js"
|
|
103
|
-
},
|
|
104
|
-
"./access": {
|
|
105
|
-
"types": "./dist/access/index.d.ts",
|
|
106
|
-
"import": "./dist/access/index.js",
|
|
107
|
-
"default": "./dist/access/index.js"
|
|
108
|
-
},
|
|
109
|
-
"./access/react": {
|
|
110
|
-
"types": "./dist/access/react/index.d.ts",
|
|
111
|
-
"import": "./dist/access/react/index.js",
|
|
112
|
-
"default": "./dist/access/react/index.js"
|
|
113
103
|
}
|
|
114
104
|
},
|
|
115
105
|
"scripts": {
|
package/dist/access/client.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { AccessGuard } from './guard.js';
|
|
2
|
-
/**
|
|
3
|
-
* Wraps a LocalFirstClient with an AccessGuard.
|
|
4
|
-
* Intercepts CRUD queries and mutations, asserting permissions before writes and
|
|
5
|
-
* filtering unauthorized rows or restricted fields on reads.
|
|
6
|
-
*/
|
|
7
|
-
export declare function withAccess<T extends {
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
}>(client: T, guard: AccessGuard): T;
|
package/dist/access/client.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withAccess = withAccess;
|
|
4
|
-
/**
|
|
5
|
-
* Wraps a LocalFirstClient with an AccessGuard.
|
|
6
|
-
* Intercepts CRUD queries and mutations, asserting permissions before writes and
|
|
7
|
-
* filtering unauthorized rows or restricted fields on reads.
|
|
8
|
-
*/
|
|
9
|
-
function withAccess(client, guard) {
|
|
10
|
-
return new Proxy(client, {
|
|
11
|
-
get(target, prop, receiver) {
|
|
12
|
-
if (prop === 'insert') {
|
|
13
|
-
return async (tableName, rowIdOrPayload, payloadOrOptions, options) => {
|
|
14
|
-
const payload = typeof rowIdOrPayload === 'string'
|
|
15
|
-
? payloadOrOptions || {}
|
|
16
|
-
: rowIdOrPayload || {};
|
|
17
|
-
guard.assertCan('create', tableName, payload);
|
|
18
|
-
return target.insert(tableName, rowIdOrPayload, payloadOrOptions, options);
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
if (prop === 'update') {
|
|
22
|
-
return async (tableName, rowId, payload, options) => {
|
|
23
|
-
guard.assertCan('update', tableName, { id: rowId, ...payload });
|
|
24
|
-
return target.update(tableName, rowId, payload, options);
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
if (prop === 'delete') {
|
|
28
|
-
return async (tableName, rowId, options) => {
|
|
29
|
-
guard.assertCan('delete', tableName, { id: rowId });
|
|
30
|
-
return target.delete(tableName, rowId, options);
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if (prop === 'findById') {
|
|
34
|
-
return async (tableName, id, options) => {
|
|
35
|
-
const record = await target.findById(tableName, id, options);
|
|
36
|
-
if (!record)
|
|
37
|
-
return null;
|
|
38
|
-
if (!guard.can('read', tableName, record)) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
return guard.filterAllowedFields('read', tableName, record, record);
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
if (prop === 'findMany') {
|
|
45
|
-
return async (tableName, options) => {
|
|
46
|
-
const records = await target.findMany(tableName, options);
|
|
47
|
-
return guard.filterRows('read', tableName, records);
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
if (prop === 'accessGuard') {
|
|
51
|
-
return guard;
|
|
52
|
-
}
|
|
53
|
-
return Reflect.get(target, prop, receiver);
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
package/dist/access/engine.d.ts
DELETED
package/dist/access/engine.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defineAccessRules = defineAccessRules;
|
|
4
|
-
const guard_js_1 = require("./guard.js");
|
|
5
|
-
class RuleBuilderImpl {
|
|
6
|
-
rules = [];
|
|
7
|
-
addRule(inverted, actions, subjects, arg3, arg4) {
|
|
8
|
-
const actionList = Array.isArray(actions) ? actions : [actions];
|
|
9
|
-
const subjectList = Array.isArray(subjects) ? subjects : [subjects];
|
|
10
|
-
let options;
|
|
11
|
-
let predicate;
|
|
12
|
-
if (typeof arg3 === 'function') {
|
|
13
|
-
predicate = arg3;
|
|
14
|
-
}
|
|
15
|
-
else if (arg3 && typeof arg3 === 'object') {
|
|
16
|
-
options = arg3;
|
|
17
|
-
predicate = arg4;
|
|
18
|
-
}
|
|
19
|
-
this.rules.push({
|
|
20
|
-
inverted,
|
|
21
|
-
actions: actionList,
|
|
22
|
-
subjects: subjectList,
|
|
23
|
-
predicate,
|
|
24
|
-
fields: options?.fields,
|
|
25
|
-
description: options?.description,
|
|
26
|
-
});
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
can(action, subject, arg3, arg4) {
|
|
30
|
-
return this.addRule(false, action, subject, arg3, arg4);
|
|
31
|
-
}
|
|
32
|
-
cannot(action, subject, arg3, arg4) {
|
|
33
|
-
return this.addRule(true, action, subject, arg3, arg4);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function defineAccessRules(policy) {
|
|
37
|
-
const createGuard = (user) => {
|
|
38
|
-
const builder = new RuleBuilderImpl();
|
|
39
|
-
policy(builder, user);
|
|
40
|
-
return new guard_js_1.AccessGuard(user, builder.rules);
|
|
41
|
-
};
|
|
42
|
-
return {
|
|
43
|
-
createGuard,
|
|
44
|
-
for: createGuard,
|
|
45
|
-
};
|
|
46
|
-
}
|
package/dist/access/errors.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare class AccessDeniedError extends Error {
|
|
2
|
-
readonly action: string;
|
|
3
|
-
readonly subject: string;
|
|
4
|
-
readonly fields?: string[];
|
|
5
|
-
readonly record?: any;
|
|
6
|
-
readonly reason?: string;
|
|
7
|
-
constructor(options: {
|
|
8
|
-
action: string;
|
|
9
|
-
subject: string;
|
|
10
|
-
fields?: string[];
|
|
11
|
-
record?: any;
|
|
12
|
-
reason?: string;
|
|
13
|
-
});
|
|
14
|
-
}
|
package/dist/access/errors.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AccessDeniedError = void 0;
|
|
4
|
-
class AccessDeniedError extends Error {
|
|
5
|
-
action;
|
|
6
|
-
subject;
|
|
7
|
-
fields;
|
|
8
|
-
record;
|
|
9
|
-
reason;
|
|
10
|
-
constructor(options) {
|
|
11
|
-
const fieldsText = options.fields && options.fields.length > 0 ? ` (fields: ${options.fields.join(', ')})` : '';
|
|
12
|
-
const message = options.reason ||
|
|
13
|
-
`Access denied: cannot perform action '${options.action}' on subject '${options.subject}'${fieldsText}.`;
|
|
14
|
-
super(message);
|
|
15
|
-
this.name = 'AccessDeniedError';
|
|
16
|
-
this.action = options.action;
|
|
17
|
-
this.subject = options.subject;
|
|
18
|
-
this.fields = options.fields;
|
|
19
|
-
this.record = options.record;
|
|
20
|
-
this.reason = options.reason;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.AccessDeniedError = AccessDeniedError;
|
package/dist/access/guard.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { AccessAction, AccessRule, AccessSubject } from './types.js';
|
|
2
|
-
export declare class AccessGuard<TUser = any> {
|
|
3
|
-
readonly user: TUser | null;
|
|
4
|
-
readonly rules: AccessRule<TUser>[];
|
|
5
|
-
constructor(user: TUser | null, rules: AccessRule<TUser>[]);
|
|
6
|
-
/**
|
|
7
|
-
* Evaluates if the current user has permission to perform an action on a subject.
|
|
8
|
-
*/
|
|
9
|
-
can(action: AccessAction, subject: AccessSubject, record?: any): boolean;
|
|
10
|
-
cannot(action: AccessAction, subject: AccessSubject, record?: any): boolean;
|
|
11
|
-
assertCan(action: AccessAction, subject: AccessSubject, record?: any): void;
|
|
12
|
-
/**
|
|
13
|
-
* Check if a specific field is accessible for an action.
|
|
14
|
-
*/
|
|
15
|
-
canAccessField(action: AccessAction, subject: AccessSubject, field: string, record?: any): boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Strips restricted fields from an object based on rules.
|
|
18
|
-
*/
|
|
19
|
-
filterAllowedFields<T extends Record<string, any>>(action: AccessAction, subject: AccessSubject, data: T, record?: any): Partial<T>;
|
|
20
|
-
/**
|
|
21
|
-
* Filters an array of rows, removing unauthorized records and stripping forbidden fields.
|
|
22
|
-
*/
|
|
23
|
-
filterRows<T extends Record<string, any>>(action: AccessAction, subject: AccessSubject, rows: T[]): T[];
|
|
24
|
-
}
|
package/dist/access/guard.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AccessGuard = void 0;
|
|
4
|
-
const errors_js_1 = require("./errors.js");
|
|
5
|
-
class AccessGuard {
|
|
6
|
-
user;
|
|
7
|
-
rules;
|
|
8
|
-
constructor(user, rules) {
|
|
9
|
-
this.user = user;
|
|
10
|
-
this.rules = rules;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Evaluates if the current user has permission to perform an action on a subject.
|
|
14
|
-
*/
|
|
15
|
-
can(action, subject, record) {
|
|
16
|
-
let allowed = false;
|
|
17
|
-
for (const rule of this.rules) {
|
|
18
|
-
const actionMatches = rule.actions.includes('manage') ||
|
|
19
|
-
rule.actions.includes('all') ||
|
|
20
|
-
rule.actions.includes(action);
|
|
21
|
-
const subjectMatches = rule.subjects.includes('all') ||
|
|
22
|
-
rule.subjects.includes(subject);
|
|
23
|
-
if (!actionMatches || !subjectMatches) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (rule.predicate) {
|
|
27
|
-
try {
|
|
28
|
-
if (!rule.predicate(record, this.user)) {
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
// If cannot rule matches (with no field filter), reject immediately
|
|
37
|
-
if (rule.inverted && (!rule.fields || rule.fields.length === 0)) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
// If can rule matches, mark allowed
|
|
41
|
-
if (!rule.inverted) {
|
|
42
|
-
allowed = true;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return allowed;
|
|
46
|
-
}
|
|
47
|
-
cannot(action, subject, record) {
|
|
48
|
-
return !this.can(action, subject, record);
|
|
49
|
-
}
|
|
50
|
-
assertCan(action, subject, record) {
|
|
51
|
-
if (!this.can(action, subject, record)) {
|
|
52
|
-
throw new errors_js_1.AccessDeniedError({ action, subject, record });
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Check if a specific field is accessible for an action.
|
|
57
|
-
*/
|
|
58
|
-
canAccessField(action, subject, field, record) {
|
|
59
|
-
if (!this.can(action, subject, record)) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
for (const rule of this.rules) {
|
|
63
|
-
if (!rule.inverted || !rule.fields || !rule.fields.includes(field)) {
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
const actionMatches = rule.actions.includes('manage') ||
|
|
67
|
-
rule.actions.includes('all') ||
|
|
68
|
-
rule.actions.includes(action);
|
|
69
|
-
const subjectMatches = rule.subjects.includes('all') ||
|
|
70
|
-
rule.subjects.includes(subject);
|
|
71
|
-
if (actionMatches && subjectMatches) {
|
|
72
|
-
if (!rule.predicate || rule.predicate(record, this.user)) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Strips restricted fields from an object based on rules.
|
|
81
|
-
*/
|
|
82
|
-
filterAllowedFields(action, subject, data, record) {
|
|
83
|
-
if (!data || typeof data !== 'object')
|
|
84
|
-
return data;
|
|
85
|
-
const result = {};
|
|
86
|
-
for (const [key, value] of Object.entries(data)) {
|
|
87
|
-
if (this.canAccessField(action, subject, key, record || data)) {
|
|
88
|
-
result[key] = value;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Filters an array of rows, removing unauthorized records and stripping forbidden fields.
|
|
95
|
-
*/
|
|
96
|
-
filterRows(action, subject, rows) {
|
|
97
|
-
if (!Array.isArray(rows))
|
|
98
|
-
return [];
|
|
99
|
-
return rows
|
|
100
|
-
.filter((row) => this.can(action, subject, row))
|
|
101
|
-
.map((row) => this.filterAllowedFields(action, subject, row, row));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
exports.AccessGuard = AccessGuard;
|
package/dist/access/index.d.ts
DELETED
package/dist/access/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types.js"), exports);
|
|
18
|
-
__exportStar(require("./errors.js"), exports);
|
|
19
|
-
__exportStar(require("./guard.js"), exports);
|
|
20
|
-
__exportStar(require("./engine.js"), exports);
|
|
21
|
-
__exportStar(require("./client.js"), exports);
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import React, { ReactNode } from 'react';
|
|
2
|
-
import { AccessGuard } from '../guard.js';
|
|
3
|
-
import { AccessAction, AccessSubject } from '../types.js';
|
|
4
|
-
export interface AccessContextValue<TUser = any> {
|
|
5
|
-
guard: AccessGuard<TUser> | null;
|
|
6
|
-
user: TUser | null;
|
|
7
|
-
can: (action: AccessAction, subject: AccessSubject, record?: any) => boolean;
|
|
8
|
-
cannot: (action: AccessAction, subject: AccessSubject, record?: any) => boolean;
|
|
9
|
-
assertCan: (action: AccessAction, subject: AccessSubject, record?: any) => void;
|
|
10
|
-
}
|
|
11
|
-
export interface AccessProviderProps<TUser = any> {
|
|
12
|
-
guard: AccessGuard<TUser>;
|
|
13
|
-
children: ReactNode;
|
|
14
|
-
}
|
|
15
|
-
export declare function AccessProvider<TUser = any>({ guard, children, }: AccessProviderProps<TUser>): React.JSX.Element;
|
|
16
|
-
export declare function useAccess<TUser = any>(): AccessContextValue<TUser>;
|
|
17
|
-
export interface CanProps {
|
|
18
|
-
I: AccessAction;
|
|
19
|
-
a: AccessSubject;
|
|
20
|
-
this?: any;
|
|
21
|
-
fallback?: ReactNode;
|
|
22
|
-
children: ReactNode;
|
|
23
|
-
}
|
|
24
|
-
export declare function Can({ I, a, this: record, fallback, children }: CanProps): React.JSX.Element;
|
|
25
|
-
export interface CannotProps {
|
|
26
|
-
I: AccessAction;
|
|
27
|
-
a: AccessSubject;
|
|
28
|
-
this?: any;
|
|
29
|
-
fallback?: ReactNode;
|
|
30
|
-
children: ReactNode;
|
|
31
|
-
}
|
|
32
|
-
export declare function Cannot({ I, a, this: record, fallback, children }: CannotProps): React.JSX.Element;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AccessProvider = AccessProvider;
|
|
4
|
-
exports.useAccess = useAccess;
|
|
5
|
-
exports.Can = Can;
|
|
6
|
-
exports.Cannot = Cannot;
|
|
7
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
-
const react_1 = require("react");
|
|
9
|
-
const AccessContext = (0, react_1.createContext)(null);
|
|
10
|
-
function AccessProvider({ guard, children, }) {
|
|
11
|
-
const value = (0, react_1.useMemo)(() => {
|
|
12
|
-
return {
|
|
13
|
-
guard,
|
|
14
|
-
user: guard.user,
|
|
15
|
-
can: (action, subject, record) => guard.can(action, subject, record),
|
|
16
|
-
cannot: (action, subject, record) => guard.cannot(action, subject, record),
|
|
17
|
-
assertCan: (action, subject, record) => guard.assertCan(action, subject, record),
|
|
18
|
-
};
|
|
19
|
-
}, [guard]);
|
|
20
|
-
return (0, jsx_runtime_1.jsx)(AccessContext.Provider, { value: value, children: children });
|
|
21
|
-
}
|
|
22
|
-
function useAccess() {
|
|
23
|
-
const context = (0, react_1.useContext)(AccessContext);
|
|
24
|
-
if (!context) {
|
|
25
|
-
throw new Error('useAccess must be used within an <AccessProvider>');
|
|
26
|
-
}
|
|
27
|
-
return context;
|
|
28
|
-
}
|
|
29
|
-
function Can({ I, a, this: record, fallback = null, children }) {
|
|
30
|
-
const { can } = useAccess();
|
|
31
|
-
if (can(I, a, record)) {
|
|
32
|
-
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
|
|
33
|
-
}
|
|
34
|
-
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: fallback });
|
|
35
|
-
}
|
|
36
|
-
function Cannot({ I, a, this: record, fallback = null, children }) {
|
|
37
|
-
const { cannot } = useAccess();
|
|
38
|
-
if (cannot(I, a, record)) {
|
|
39
|
-
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
|
|
40
|
-
}
|
|
41
|
-
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: fallback });
|
|
42
|
-
}
|
package/dist/access/types.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export type AccessAction = 'create' | 'read' | 'update' | 'delete' | 'manage' | (string & {});
|
|
2
|
-
export type AccessSubject = 'all' | (string & {});
|
|
3
|
-
export type AccessPredicate<TRecord = any, TUser = any> = (record?: TRecord, user?: TUser | null) => boolean;
|
|
4
|
-
export interface RuleOptions {
|
|
5
|
-
fields?: string[];
|
|
6
|
-
description?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface AccessRule<TUser = any> {
|
|
9
|
-
inverted: boolean;
|
|
10
|
-
actions: string[];
|
|
11
|
-
subjects: string[];
|
|
12
|
-
predicate?: AccessPredicate<any, TUser>;
|
|
13
|
-
fields?: string[];
|
|
14
|
-
description?: string;
|
|
15
|
-
}
|
|
16
|
-
export interface AccessRuleBuilder<TUser = any> {
|
|
17
|
-
can(action: AccessAction | AccessAction[], subject: AccessSubject | AccessSubject[], predicate?: AccessPredicate<any, TUser>): this;
|
|
18
|
-
can(action: AccessAction | AccessAction[], subject: AccessSubject | AccessSubject[], options: RuleOptions, predicate?: AccessPredicate<any, TUser>): this;
|
|
19
|
-
cannot(action: AccessAction | AccessAction[], subject: AccessSubject | AccessSubject[], predicate?: AccessPredicate<any, TUser>): this;
|
|
20
|
-
cannot(action: AccessAction | AccessAction[], subject: AccessSubject | AccessSubject[], options: RuleOptions, predicate?: AccessPredicate<any, TUser>): this;
|
|
21
|
-
}
|
|
22
|
-
export type AccessPolicyDefinition<TUser = any> = (builder: AccessRuleBuilder<TUser>, user: TUser | null) => void;
|
|
23
|
-
export interface AccessEngine<TUser = any> {
|
|
24
|
-
createGuard(user: TUser | null): any;
|
|
25
|
-
for(user: TUser | null): any;
|
|
26
|
-
}
|
package/dist/access/types.js
DELETED