monorise 1.14.1 → 2.0.0-dev.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.
- package/dist/base/index.d.ts +227 -1
- package/dist/base/index.js +383 -1
- package/dist/base/index.js.map +1 -1
- package/dist/cli/cli.js +295 -122
- package/dist/cli/cli.js.map +1 -1
- package/dist/cli/templates/files/api-keys.ts +12 -0
- package/dist/cli/templates/files/monorise.config.ts +2 -0
- package/dist/cli/templates/files/proxy-route.ts +12 -22
- package/dist/cli/templates/files/sst.config.ts +4 -1
- package/dist/cli/templates/files/ws-ticket-route.ts +21 -0
- package/dist/core/index.d.ts +63 -36
- package/dist/core/index.js +783 -208
- package/dist/core/index.js.map +1 -1
- package/dist/index.d.ts +24 -7
- package/dist/proxy/index.d.ts +275 -2
- package/dist/proxy/index.js +696 -37
- package/dist/proxy/index.js.map +1 -1
- package/dist/react/index.d.ts +2 -164
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +46 -188
- package/dist/react/index.js.map +1 -1
- package/dist/react/lib/api.d.ts +1 -2
- package/dist/react/lib/api.d.ts.map +1 -1
- package/dist/react/store/monorise.store.d.ts +0 -54
- package/dist/react/store/monorise.store.d.ts.map +1 -1
- package/dist/sst/components/monorise-core.d.ts.map +1 -1
- package/dist/sst/index.js +4 -1
- package/dist/sst/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/cli/templates/files/proxy-request.ts +0 -50
- package/dist/react/actions/auth.action.d.ts +0 -24
- package/dist/react/actions/auth.action.d.ts.map +0 -1
- package/dist/react/services/auth.service.d.ts +0 -21
- package/dist/react/services/auth.service.d.ts.map +0 -1
package/dist/base/index.d.ts
CHANGED
|
@@ -590,4 +590,230 @@ interface MonoriseEntityConfig<T extends Entity = Entity, B extends z.ZodRawShap
|
|
|
590
590
|
allowLegacyWhere?: boolean;
|
|
591
591
|
}
|
|
592
592
|
|
|
593
|
-
|
|
593
|
+
/**
|
|
594
|
+
* @description An access level used as shorthand.
|
|
595
|
+
* - On a policy, it applies to every entity and every relationship.
|
|
596
|
+
* - On an entity, it grants every field, every tag, listing and, by default,
|
|
597
|
+
* every relationship from that entity at that level.
|
|
598
|
+
* - On a relationship, it grants every `mutualData` field at that level.
|
|
599
|
+
*
|
|
600
|
+
* `'write'` means read, create, update and delete.
|
|
601
|
+
*/
|
|
602
|
+
type ApiKeyAccessLevel = 'none' | 'read' | 'write';
|
|
603
|
+
/**
|
|
604
|
+
* @description System fields a create payload may set only when the create
|
|
605
|
+
* list names them: `$entityId` for a caller-chosen `entityId`, `$createdAt`
|
|
606
|
+
* for a backdated `createdAt`.
|
|
607
|
+
*/
|
|
608
|
+
type ApiKeySystemField = '$entityId' | '$createdAt';
|
|
609
|
+
/** `'*'` for every field, present and future, or an allowlist. */
|
|
610
|
+
type ApiKeyFieldList<F extends string = string> = '*' | F[];
|
|
611
|
+
type EntityField<T extends Entity> = Extract<keyof DraftEntity<T>, string>;
|
|
612
|
+
/**
|
|
613
|
+
* @description Each entity type's tag names. The CLI augments this from the
|
|
614
|
+
* entity configs, so `tags` in a policy accepts only real tag names.
|
|
615
|
+
*/
|
|
616
|
+
interface ApiKeyEntityTags {
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* @description The `mutualData` field names of each direction whose pair has
|
|
620
|
+
* a mutual config. The CLI augments this from the entity configs, so a
|
|
621
|
+
* relationship grant's field lists accept only real `mutualData` fields.
|
|
622
|
+
*/
|
|
623
|
+
interface ApiKeyMutualDataFields {
|
|
624
|
+
}
|
|
625
|
+
type TagName<T extends Entity> = T extends keyof ApiKeyEntityTags ? Extract<ApiKeyEntityTags[T], string> : string;
|
|
626
|
+
type MutualDataField<A extends Entity, B extends Entity> = A extends keyof ApiKeyMutualDataFields ? B extends keyof ApiKeyMutualDataFields[A] ? Extract<ApiKeyMutualDataFields[A][B], string> : string : string;
|
|
627
|
+
/**
|
|
628
|
+
* @description Access to one direction of a relationship, e.g. learner→course
|
|
629
|
+
* (`/mutual/learner/:id/course`). Field lists name `mutualData` fields.
|
|
630
|
+
*/
|
|
631
|
+
type ApiKeyMutualGrant<A extends Entity = Entity, B extends Entity = Entity> = ApiKeyAccessLevel | {
|
|
632
|
+
read?: ApiKeyFieldList<MutualDataField<A, B>>;
|
|
633
|
+
create?: ApiKeyFieldList<MutualDataField<A, B>>;
|
|
634
|
+
update?: ApiKeyFieldList<MutualDataField<A, B>>;
|
|
635
|
+
/** Same as `create` and `update` with this list. */
|
|
636
|
+
write?: ApiKeyFieldList<MutualDataField<A, B>>;
|
|
637
|
+
delete?: boolean;
|
|
638
|
+
};
|
|
639
|
+
/**
|
|
640
|
+
* @description Access to one entity type.
|
|
641
|
+
*
|
|
642
|
+
* A shorthand string is broad: every field, every tag and every relationship
|
|
643
|
+
* from this entity at that level. An object is exact: only what it lists.
|
|
644
|
+
* Any write grant implies read on the same fields.
|
|
645
|
+
*/
|
|
646
|
+
type ApiKeyEntityGrant<T extends Entity = Entity> = ApiKeyAccessLevel | {
|
|
647
|
+
/** Fields the key can read: get by ID or unique field, and every response's masking. */
|
|
648
|
+
read?: ApiKeyFieldList<EntityField<T>>;
|
|
649
|
+
/**
|
|
650
|
+
* Also list the whole type: `GET /entity/:type`, search and tag
|
|
651
|
+
* queries. Get only needs a reference to a record; listing needs none,
|
|
652
|
+
* so an object grant lists only when it says so.
|
|
653
|
+
*/
|
|
654
|
+
list?: boolean;
|
|
655
|
+
/** Must include every field `createSchema` requires. */
|
|
656
|
+
create?: ApiKeyFieldList<EntityField<T> | ApiKeySystemField>;
|
|
657
|
+
update?: ApiKeyFieldList<EntityField<T>>;
|
|
658
|
+
/** Same as `create` and `update` with this list. */
|
|
659
|
+
write?: ApiKeyFieldList<EntityField<T> | ApiKeySystemField>;
|
|
660
|
+
delete?: boolean;
|
|
661
|
+
/** Defaults to every tag when `read` is `'*'`, otherwise to none. */
|
|
662
|
+
tags?: ApiKeyFieldList<TagName<T>>;
|
|
663
|
+
/**
|
|
664
|
+
* Relationships from this entity, keyed by the target entity type. An
|
|
665
|
+
* entry covers one direction only: `[Entity.COURSE]` under learner is
|
|
666
|
+
* learner→course and says nothing about course→learner. `'*'` sets the
|
|
667
|
+
* level for every direction without an entry; it defaults to `'none'`.
|
|
668
|
+
*/
|
|
669
|
+
mutuals?: {
|
|
670
|
+
'*'?: ApiKeyAccessLevel;
|
|
671
|
+
} & {
|
|
672
|
+
[O in Entity]?: ApiKeyMutualGrant<T, O>;
|
|
673
|
+
};
|
|
674
|
+
};
|
|
675
|
+
/**
|
|
676
|
+
* @description A policy. The shorthands `'read'` and `'write'` apply to every
|
|
677
|
+
* entity and relationship, and grant no custom routes.
|
|
678
|
+
*/
|
|
679
|
+
type ApiKeyPolicy = 'read' | 'write' | {
|
|
680
|
+
entities?: {
|
|
681
|
+
'*'?: ApiKeyAccessLevel;
|
|
682
|
+
} & {
|
|
683
|
+
[T in Entity]?: ApiKeyEntityGrant<T>;
|
|
684
|
+
};
|
|
685
|
+
/**
|
|
686
|
+
* Custom route permissions this policy grants: the names custom routes
|
|
687
|
+
* declare with `requires(...)`. A custom route without `requires(...)`
|
|
688
|
+
* is callable only by `full` keys.
|
|
689
|
+
*/
|
|
690
|
+
routes?: string[];
|
|
691
|
+
};
|
|
692
|
+
/**
|
|
693
|
+
* @description Every custom route permission the project's policies grant.
|
|
694
|
+
* The CLI augments it from the policies, so `requires()` accepts only names
|
|
695
|
+
* some policy grants.
|
|
696
|
+
*/
|
|
697
|
+
interface ApiKeyRoutePermissions {
|
|
698
|
+
}
|
|
699
|
+
/** A custom route permission name; any string until the CLI augments `ApiKeyRoutePermissions`. */
|
|
700
|
+
type ApiKeyRoutePermission = keyof ApiKeyRoutePermissions extends never ? string : Extract<keyof ApiKeyRoutePermissions, string>;
|
|
701
|
+
/**
|
|
702
|
+
* @description Policies keyed by name. `API_KEYS` maps these names to keys.
|
|
703
|
+
* `full` is built in and cannot be redefined.
|
|
704
|
+
*/
|
|
705
|
+
type ApiKeyPolicies = Record<string, ApiKeyPolicy>;
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* @description Built-in policy: read, write and delete on every entity and
|
|
709
|
+
* relationship, plus every custom route. Every key in the legacy array form of
|
|
710
|
+
* `API_KEYS` maps to it.
|
|
711
|
+
*/
|
|
712
|
+
declare const FULL_API_KEY_POLICY = "full";
|
|
713
|
+
/**
|
|
714
|
+
* @description Built-in policy for keys whose scope will be narrowed later.
|
|
715
|
+
* Until the project defines a policy named `default`, keys under it can do
|
|
716
|
+
* everything `full` can. Once it does, they get exactly what that policy
|
|
717
|
+
* grants, and the keys themselves never change. New projects' keys start here.
|
|
718
|
+
*/
|
|
719
|
+
declare const DEFAULT_API_KEY_POLICY = "default";
|
|
720
|
+
/** `'*'` for every field, or the allowed fields. */
|
|
721
|
+
type ApiKeyFieldAccess = '*' | ReadonlySet<string>;
|
|
722
|
+
/** What a policy allows on one entity type. `null` means not granted. */
|
|
723
|
+
type ApiKeyEntityAccess = {
|
|
724
|
+
/**
|
|
725
|
+
* Whether the key can see the type at all: the policy grants it anything
|
|
726
|
+
* but `'none'`, by name or through its `'*'` entry. A type the key can't
|
|
727
|
+
* see answers 404, as if it didn't exist.
|
|
728
|
+
*/
|
|
729
|
+
visible: boolean;
|
|
730
|
+
/** Fields the key can read, by ID or unique field, and in every response. */
|
|
731
|
+
read: ApiKeyFieldAccess | null;
|
|
732
|
+
/** Whether the key can list the whole type: listing, search and tag queries. */
|
|
733
|
+
list: boolean;
|
|
734
|
+
create: ApiKeyFieldAccess | null;
|
|
735
|
+
update: ApiKeyFieldAccess | null;
|
|
736
|
+
delete: boolean;
|
|
737
|
+
tags: ApiKeyFieldAccess | null;
|
|
738
|
+
};
|
|
739
|
+
/** What a policy allows on one direction of a relationship. Fields are `mutualData` fields. */
|
|
740
|
+
type ApiKeyMutualAccess = {
|
|
741
|
+
read: ApiKeyFieldAccess | null;
|
|
742
|
+
create: ApiKeyFieldAccess | null;
|
|
743
|
+
update: ApiKeyFieldAccess | null;
|
|
744
|
+
delete: boolean;
|
|
745
|
+
};
|
|
746
|
+
type CompiledApiKeyPolicy = {
|
|
747
|
+
name: string;
|
|
748
|
+
/** True for the built-in `full` policy, and for `default` until the project defines it. */
|
|
749
|
+
full: boolean;
|
|
750
|
+
entity(entityType: string): ApiKeyEntityAccess;
|
|
751
|
+
/**
|
|
752
|
+
* Access to the direction `byEntityType`→`entityType`, as in
|
|
753
|
+
* `/mutual/:byEntityType/:byEntityId/:entityType`. `null` when either end
|
|
754
|
+
* type isn't visible.
|
|
755
|
+
*/
|
|
756
|
+
mutual(byEntityType: string, entityType: string): ApiKeyMutualAccess | null;
|
|
757
|
+
/** Whether the policy grants a custom route permission. `full` grants every one. */
|
|
758
|
+
allowsRoute(permission: string): boolean;
|
|
759
|
+
};
|
|
760
|
+
/**
|
|
761
|
+
* @description Compiles a policy into the access it grants. Compiled entity
|
|
762
|
+
* grants are cached, so compile once per policy and reuse the result.
|
|
763
|
+
*
|
|
764
|
+
* `name === 'full'` compiles the built-in policy and ignores `policy`.
|
|
765
|
+
* `name === 'default'` without a policy compiles as `full` too.
|
|
766
|
+
*/
|
|
767
|
+
declare const compileApiKeyPolicy: (name: string, policy?: ApiKeyPolicy) => CompiledApiKeyPolicy;
|
|
768
|
+
/**
|
|
769
|
+
* @description The parts of an entity config that policy validation reads.
|
|
770
|
+
* Any config built by `createEntityConfig` satisfies it. Configs are keyed by
|
|
771
|
+
* entity type.
|
|
772
|
+
*/
|
|
773
|
+
type ApiKeyPolicyEntityConfig = {
|
|
774
|
+
baseSchema: z.AnyZodObject;
|
|
775
|
+
createSchema?: z.AnyZodObject;
|
|
776
|
+
tags?: {
|
|
777
|
+
name: string;
|
|
778
|
+
}[];
|
|
779
|
+
mutual?: {
|
|
780
|
+
mutualSchema?: z.AnyZodObject;
|
|
781
|
+
createMutualSchema?: z.AnyZodObject;
|
|
782
|
+
mutualFields?: Record<string, {
|
|
783
|
+
entityType: string | Entity;
|
|
784
|
+
mutual?: {
|
|
785
|
+
mutualDataSchema?: z.AnyZodObject;
|
|
786
|
+
asEntity?: {
|
|
787
|
+
name: string | Entity;
|
|
788
|
+
};
|
|
789
|
+
};
|
|
790
|
+
}>;
|
|
791
|
+
};
|
|
792
|
+
};
|
|
793
|
+
/**
|
|
794
|
+
* @description Checks policies against the entity configs. Returns one message
|
|
795
|
+
* per problem; an empty array means the policies are valid. The CLI runs this
|
|
796
|
+
* at build time and the API handler at cold start.
|
|
797
|
+
*/
|
|
798
|
+
declare const validateApiKeyPolicies: (policies: unknown, entityConfigs: Record<string, ApiKeyPolicyEntityConfig>) => string[];
|
|
799
|
+
/**
|
|
800
|
+
* @description Declares the API key policies. Export the result as the default
|
|
801
|
+
* export of the file `apiKeys` points to in `monorise.config.ts`; `API_KEYS`
|
|
802
|
+
* then maps each policy name to its keys. `full` is built in.
|
|
803
|
+
*
|
|
804
|
+
* @example
|
|
805
|
+
* ```ts
|
|
806
|
+
* export default defineApiKeyPolicies({
|
|
807
|
+
* 'agent-read': 'read',
|
|
808
|
+
* client: {
|
|
809
|
+
* entities: {
|
|
810
|
+
* [Entity.ORDER]: { read: '*', create: ['customerId', 'items'], update: ['status'] },
|
|
811
|
+
* [Entity.PRODUCT]: 'read',
|
|
812
|
+
* },
|
|
813
|
+
* },
|
|
814
|
+
* });
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
declare const defineApiKeyPolicies: (policies: ApiKeyPolicies) => ApiKeyPolicies;
|
|
818
|
+
|
|
819
|
+
export { type AdjustmentCondition, type AdjustmentConditionFn, type ApiKeyAccessLevel, type ApiKeyEntityAccess, type ApiKeyEntityGrant, type ApiKeyEntityTags, type ApiKeyFieldAccess, type ApiKeyFieldList, type ApiKeyMutualAccess, type ApiKeyMutualDataFields, type ApiKeyMutualGrant, type ApiKeyPolicies, type ApiKeyPolicy, type ApiKeyPolicyEntityConfig, type ApiKeyRoutePermission, type ApiKeyRoutePermissions, type ApiKeySystemField, type CompiledApiKeyPolicy, type CreatedEntity, DEFAULT_API_KEY_POLICY, type DraftEntity, Entity, type EntitySchemaMap, FULL_API_KEY_POLICY, type MonoriseEntityConfig, type MutualConfig, type MutualConfigInput, type NumericFields, type UpdateCondition, type UpdateConditionFn, type WhereClause, type WhereConditions, type WhereOperator, compileApiKeyPolicy, createEntityConfig, createMutualConfig, defineApiKeyPolicies, resolveEffectiveMutualSchema, validateApiKeyPolicies };
|
package/dist/base/index.js
CHANGED
|
@@ -17,6 +17,18 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
20
32
|
|
|
21
33
|
// types/monorise.type.ts
|
|
22
34
|
var Entity = /* @__PURE__ */ ((Entity2) => {
|
|
@@ -77,10 +89,380 @@ var createMutualConfig = (config) => {
|
|
|
77
89
|
mutualDataSchema: config.asEntity.finalSchema
|
|
78
90
|
});
|
|
79
91
|
};
|
|
92
|
+
|
|
93
|
+
// utils/api-key-policy.ts
|
|
94
|
+
var FULL_API_KEY_POLICY = "full";
|
|
95
|
+
var DEFAULT_API_KEY_POLICY = "default";
|
|
96
|
+
var ACCESS_LEVELS = ["none", "read", "write"];
|
|
97
|
+
var SYSTEM_FIELDS = ["$entityId", "$createdAt"];
|
|
98
|
+
var ENTITY_GRANT_KEYS = [
|
|
99
|
+
"read",
|
|
100
|
+
"list",
|
|
101
|
+
"create",
|
|
102
|
+
"update",
|
|
103
|
+
"write",
|
|
104
|
+
"delete",
|
|
105
|
+
"tags",
|
|
106
|
+
"mutuals"
|
|
107
|
+
];
|
|
108
|
+
var MUTUAL_GRANT_KEYS = ["read", "create", "update", "write", "delete"];
|
|
109
|
+
var POLICY_KEYS = ["entities", "routes"];
|
|
110
|
+
var hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
111
|
+
var toAccess = (list) => list === void 0 ? null : list === "*" ? "*" : new Set(list);
|
|
112
|
+
var union = (...accesses) => {
|
|
113
|
+
let fields = null;
|
|
114
|
+
for (const access of accesses) {
|
|
115
|
+
if (access === null) continue;
|
|
116
|
+
if (access === "*") return "*";
|
|
117
|
+
fields = /* @__PURE__ */ new Set([...fields != null ? fields : [], ...access]);
|
|
118
|
+
}
|
|
119
|
+
return fields;
|
|
120
|
+
};
|
|
121
|
+
var withoutSystemFields = (access) => access === null || access === "*" ? access : new Set([...access].filter((field) => !SYSTEM_FIELDS.includes(field)));
|
|
122
|
+
var HIDDEN = {
|
|
123
|
+
access: {
|
|
124
|
+
visible: false,
|
|
125
|
+
read: null,
|
|
126
|
+
list: false,
|
|
127
|
+
create: null,
|
|
128
|
+
update: null,
|
|
129
|
+
delete: false,
|
|
130
|
+
tags: null
|
|
131
|
+
},
|
|
132
|
+
mutualDefault: "none",
|
|
133
|
+
mutuals: /* @__PURE__ */ new Map()
|
|
134
|
+
};
|
|
135
|
+
var compileEntityGrant = (grant) => {
|
|
136
|
+
var _a;
|
|
137
|
+
if (grant === void 0 || grant === "none") return HIDDEN;
|
|
138
|
+
if (grant === "read" || grant === "write") {
|
|
139
|
+
const write = grant === "write";
|
|
140
|
+
return {
|
|
141
|
+
access: {
|
|
142
|
+
visible: true,
|
|
143
|
+
read: "*",
|
|
144
|
+
list: true,
|
|
145
|
+
create: write ? "*" : null,
|
|
146
|
+
update: write ? "*" : null,
|
|
147
|
+
delete: write,
|
|
148
|
+
tags: "*"
|
|
149
|
+
},
|
|
150
|
+
mutualDefault: grant,
|
|
151
|
+
mutuals: /* @__PURE__ */ new Map()
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const create = union(toAccess(grant.create), toAccess(grant.write));
|
|
155
|
+
const update = union(toAccess(grant.update), toAccess(grant.write));
|
|
156
|
+
const read = union(toAccess(grant.read), withoutSystemFields(create), update);
|
|
157
|
+
const _b = (_a = grant.mutuals) != null ? _a : {}, { "*": mutualDefault = "none" } = _b, mutuals = __objRest(_b, ["*"]);
|
|
158
|
+
return {
|
|
159
|
+
access: {
|
|
160
|
+
visible: true,
|
|
161
|
+
read,
|
|
162
|
+
list: grant.list === true,
|
|
163
|
+
create,
|
|
164
|
+
update,
|
|
165
|
+
delete: grant.delete === true,
|
|
166
|
+
tags: grant.tags !== void 0 ? toAccess(grant.tags) : read === "*" ? "*" : null
|
|
167
|
+
},
|
|
168
|
+
mutualDefault,
|
|
169
|
+
mutuals: new Map(Object.entries(mutuals))
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
var compileMutualGrant = (grant) => {
|
|
173
|
+
if (grant === "none" || grant === "read" || grant === "write") {
|
|
174
|
+
const write = grant === "write";
|
|
175
|
+
return {
|
|
176
|
+
read: grant === "none" ? null : "*",
|
|
177
|
+
create: write ? "*" : null,
|
|
178
|
+
update: write ? "*" : null,
|
|
179
|
+
delete: write
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const create = union(toAccess(grant.create), toAccess(grant.write));
|
|
183
|
+
const update = union(toAccess(grant.update), toAccess(grant.write));
|
|
184
|
+
return {
|
|
185
|
+
read: union(toAccess(grant.read), create, update),
|
|
186
|
+
create,
|
|
187
|
+
update,
|
|
188
|
+
delete: grant.delete === true
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
var compileApiKeyPolicy = (name, policy) => {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
const full = name === FULL_API_KEY_POLICY || name === DEFAULT_API_KEY_POLICY && policy === void 0;
|
|
194
|
+
const entities = full ? { "*": "write" } : policy === "read" || policy === "write" ? { "*": policy } : (_a = policy == null ? void 0 : policy.entities) != null ? _a : {};
|
|
195
|
+
const routes = new Set(
|
|
196
|
+
!full && typeof policy === "object" ? (_b = policy.routes) != null ? _b : [] : []
|
|
197
|
+
);
|
|
198
|
+
const cache = /* @__PURE__ */ new Map();
|
|
199
|
+
const compiled = (entityType) => {
|
|
200
|
+
const grantKey = hasOwn(entities, entityType) ? entityType : "*";
|
|
201
|
+
let entry = cache.get(grantKey);
|
|
202
|
+
if (!entry) {
|
|
203
|
+
entry = compileEntityGrant(
|
|
204
|
+
hasOwn(entities, grantKey) ? entities[grantKey] : void 0
|
|
205
|
+
);
|
|
206
|
+
cache.set(grantKey, entry);
|
|
207
|
+
}
|
|
208
|
+
return entry;
|
|
209
|
+
};
|
|
210
|
+
return {
|
|
211
|
+
name,
|
|
212
|
+
full,
|
|
213
|
+
entity: (entityType) => compiled(entityType).access,
|
|
214
|
+
mutual: (byEntityType, entityType) => {
|
|
215
|
+
var _a2;
|
|
216
|
+
const from = compiled(byEntityType);
|
|
217
|
+
if (!from.access.visible || !compiled(entityType).access.visible) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return compileMutualGrant(
|
|
221
|
+
(_a2 = from.mutuals.get(entityType)) != null ? _a2 : from.mutualDefault
|
|
222
|
+
);
|
|
223
|
+
},
|
|
224
|
+
allowsRoute: (permission) => full || routes.has(permission)
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
228
|
+
var quoteList = (items) => [...items].map((item) => `'${item}'`).join(", ");
|
|
229
|
+
var validateApiKeyPolicies = (policies, entityConfigs) => {
|
|
230
|
+
var _a, _b, _c, _d;
|
|
231
|
+
const errors = [];
|
|
232
|
+
if (!isPlainObject(policies)) {
|
|
233
|
+
return ["API key policies must be an object keyed by policy name"];
|
|
234
|
+
}
|
|
235
|
+
const entityNames = Object.keys(entityConfigs);
|
|
236
|
+
const isEntityType = (name) => hasOwn(entityConfigs, name);
|
|
237
|
+
const mutualFieldNames = (config) => {
|
|
238
|
+
var _a2, _b2, _c2, _d2, _e, _f, _g, _h;
|
|
239
|
+
return /* @__PURE__ */ new Set([
|
|
240
|
+
...Object.keys((_b2 = (_a2 = config.mutual) == null ? void 0 : _a2.mutualFields) != null ? _b2 : {}),
|
|
241
|
+
...Object.keys((_e = (_d2 = (_c2 = config.mutual) == null ? void 0 : _c2.mutualSchema) == null ? void 0 : _d2.shape) != null ? _e : {}),
|
|
242
|
+
...Object.keys((_h = (_g = (_f = config.mutual) == null ? void 0 : _f.createMutualSchema) == null ? void 0 : _g.shape) != null ? _h : {})
|
|
243
|
+
]);
|
|
244
|
+
};
|
|
245
|
+
const ownFields = (config) => {
|
|
246
|
+
var _a2, _b2, _c2, _d2;
|
|
247
|
+
return /* @__PURE__ */ new Set([
|
|
248
|
+
...Object.keys((_b2 = (_a2 = config.baseSchema) == null ? void 0 : _a2.shape) != null ? _b2 : {}),
|
|
249
|
+
...Object.keys((_d2 = (_c2 = config.createSchema) == null ? void 0 : _c2.shape) != null ? _d2 : {})
|
|
250
|
+
]);
|
|
251
|
+
};
|
|
252
|
+
const mutualConfig = (byEntityType, entityType) => {
|
|
253
|
+
var _a2;
|
|
254
|
+
const declared = (from, to) => {
|
|
255
|
+
var _a3, _b2, _c2, _d2;
|
|
256
|
+
return (_d2 = Object.values((_c2 = (_b2 = (_a3 = entityConfigs[from]) == null ? void 0 : _a3.mutual) == null ? void 0 : _b2.mutualFields) != null ? _c2 : {}).find(
|
|
257
|
+
(field) => field.entityType === to && field.mutual
|
|
258
|
+
)) == null ? void 0 : _d2.mutual;
|
|
259
|
+
};
|
|
260
|
+
return (_a2 = declared(byEntityType, entityType)) != null ? _a2 : declared(entityType, byEntityType);
|
|
261
|
+
};
|
|
262
|
+
const checkFieldList = (where, list, allowed, describe) => {
|
|
263
|
+
if (list === "*") return;
|
|
264
|
+
if (!Array.isArray(list) || !list.every((field) => typeof field === "string")) {
|
|
265
|
+
errors.push(`${where} must be '*' or an array of field names`);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
for (const field of list) {
|
|
269
|
+
const problem = describe(field);
|
|
270
|
+
if (problem) errors.push(`${where}: ${problem}`);
|
|
271
|
+
else if (allowed && !allowed.has(field)) {
|
|
272
|
+
errors.push(`${where}: unknown field '${field}'`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const checkUnknownKeys = (where, value, known) => {
|
|
277
|
+
for (const key of Object.keys(value)) {
|
|
278
|
+
if (!known.includes(key)) {
|
|
279
|
+
errors.push(
|
|
280
|
+
`${where}: unknown key '${key}' (expected ${quoteList(known)})`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
const checkLevel = (where, value) => {
|
|
286
|
+
if (!ACCESS_LEVELS.includes(value)) {
|
|
287
|
+
errors.push(`${where} must be one of ${quoteList(ACCESS_LEVELS)}`);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const checkMutualGrant = (where, grant, byEntityType, entityType) => {
|
|
291
|
+
var _a2, _b2;
|
|
292
|
+
if (typeof grant === "string") return checkLevel(where, grant);
|
|
293
|
+
if (!isPlainObject(grant)) {
|
|
294
|
+
errors.push(`${where} must be an access level or an object`);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
checkUnknownKeys(where, grant, MUTUAL_GRANT_KEYS);
|
|
298
|
+
const dataFields = (_b2 = (_a2 = mutualConfig(byEntityType, entityType)) == null ? void 0 : _a2.mutualDataSchema) == null ? void 0 : _b2.shape;
|
|
299
|
+
const allowed = dataFields ? new Set(Object.keys(dataFields)) : void 0;
|
|
300
|
+
for (const key of ["read", "create", "update", "write"]) {
|
|
301
|
+
if (grant[key] === void 0) continue;
|
|
302
|
+
checkFieldList(`${where}.${key}`, grant[key], allowed, () => void 0);
|
|
303
|
+
}
|
|
304
|
+
if (grant.delete !== void 0 && typeof grant.delete !== "boolean") {
|
|
305
|
+
errors.push(`${where}.delete must be a boolean`);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const checkEntityGrant = (where, grant, entityType) => {
|
|
309
|
+
var _a2;
|
|
310
|
+
const config = entityConfigs[entityType];
|
|
311
|
+
if (typeof grant === "string") return checkLevel(where, grant);
|
|
312
|
+
if (!isPlainObject(grant)) {
|
|
313
|
+
errors.push(`${where} must be an access level or an object`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
checkUnknownKeys(where, grant, ENTITY_GRANT_KEYS);
|
|
317
|
+
const fields = ownFields(config);
|
|
318
|
+
const mutualFields = mutualFieldNames(config);
|
|
319
|
+
const fieldListErrors = errors.length;
|
|
320
|
+
for (const key of ["read", "create", "update", "write"]) {
|
|
321
|
+
if (grant[key] === void 0) continue;
|
|
322
|
+
const allowsSystemFields = key === "create" || key === "write";
|
|
323
|
+
const allowed = allowsSystemFields ? /* @__PURE__ */ new Set([...fields, ...SYSTEM_FIELDS]) : fields;
|
|
324
|
+
checkFieldList(`${where}.${key}`, grant[key], allowed, (field) => {
|
|
325
|
+
if (SYSTEM_FIELDS.includes(field)) {
|
|
326
|
+
return allowsSystemFields ? void 0 : `'${field}' can only be granted in 'create' or 'write'`;
|
|
327
|
+
}
|
|
328
|
+
if (mutualFields.has(field) && !fields.has(field)) {
|
|
329
|
+
return `'${field}' is a relationship field; grant it under 'mutuals' instead`;
|
|
330
|
+
}
|
|
331
|
+
return void 0;
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (grant.delete !== void 0 && typeof grant.delete !== "boolean") {
|
|
335
|
+
errors.push(`${where}.delete must be a boolean`);
|
|
336
|
+
}
|
|
337
|
+
if (grant.list !== void 0 && typeof grant.list !== "boolean") {
|
|
338
|
+
errors.push(`${where}.list must be a boolean`);
|
|
339
|
+
} else if (grant.list === true && errors.length === fieldListErrors) {
|
|
340
|
+
const { read } = compileEntityGrant(grant).access;
|
|
341
|
+
if (read === null || read !== "*" && read.size === 0) {
|
|
342
|
+
errors.push(
|
|
343
|
+
`${where}.list needs fields to read; a listing would return no data`
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (grant.tags !== void 0) {
|
|
348
|
+
checkFieldList(
|
|
349
|
+
`${where}.tags`,
|
|
350
|
+
grant.tags,
|
|
351
|
+
new Set(((_a2 = config.tags) != null ? _a2 : []).map((tag) => tag.name)),
|
|
352
|
+
() => void 0
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
if (grant.mutuals !== void 0) {
|
|
356
|
+
if (!isPlainObject(grant.mutuals)) {
|
|
357
|
+
errors.push(`${where}.mutuals must be an object`);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
for (const [target, mutualGrant] of Object.entries(grant.mutuals)) {
|
|
361
|
+
const mutualWhere = `${where}.mutuals.${target}`;
|
|
362
|
+
if (target === "*") {
|
|
363
|
+
checkLevel(mutualWhere, mutualGrant);
|
|
364
|
+
} else if (!isEntityType(target)) {
|
|
365
|
+
errors.push(`${mutualWhere}: unknown entity type '${target}'`);
|
|
366
|
+
} else {
|
|
367
|
+
checkMutualGrant(mutualWhere, mutualGrant, entityType, target);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
const checkCreateIsSatisfiable = (where, policy, entityType) => {
|
|
373
|
+
var _a2, _b2, _c2, _d2, _e, _f, _g, _h, _i;
|
|
374
|
+
const config = entityConfigs[entityType];
|
|
375
|
+
const { create } = policy.entity(entityType);
|
|
376
|
+
if (create === null) return;
|
|
377
|
+
if (create !== "*") {
|
|
378
|
+
const shape = __spreadValues(__spreadValues({}, (_a2 = config.baseSchema) == null ? void 0 : _a2.shape), (_b2 = config.createSchema) == null ? void 0 : _b2.shape);
|
|
379
|
+
const missing = Object.entries(shape).filter(([field, schema]) => !schema.isOptional() && !create.has(field)).map(([field]) => field);
|
|
380
|
+
if (missing.length) {
|
|
381
|
+
errors.push(
|
|
382
|
+
`${where}: create omits required field(s) ${quoteList(missing)}, so a key with this policy could never create one`
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const requiredMutuals = (_e = (_d2 = (_c2 = config.mutual) == null ? void 0 : _c2.createMutualSchema) == null ? void 0 : _d2.shape) != null ? _e : {};
|
|
387
|
+
for (const [field, schema] of Object.entries(
|
|
388
|
+
requiredMutuals
|
|
389
|
+
)) {
|
|
390
|
+
const target = (_h = (_g = (_f = config.mutual) == null ? void 0 : _f.mutualFields) == null ? void 0 : _g[field]) == null ? void 0 : _h.entityType;
|
|
391
|
+
if (schema.isOptional() || !target) continue;
|
|
392
|
+
if (!((_i = policy.mutual(entityType, target)) == null ? void 0 : _i.create)) {
|
|
393
|
+
errors.push(
|
|
394
|
+
`${where}: create needs create on ${entityType}\u2192${target}, because createMutualSchema requires '${field}'`
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
for (const [name, policy] of Object.entries(policies)) {
|
|
400
|
+
const where = `policy '${name}'`;
|
|
401
|
+
const errorsBefore = errors.length;
|
|
402
|
+
if (name === FULL_API_KEY_POLICY) {
|
|
403
|
+
errors.push(`${where} is built in and cannot be redefined`);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (policy === "read" || policy === "write") continue;
|
|
407
|
+
if (!isPlainObject(policy)) {
|
|
408
|
+
errors.push(`${where} must be 'read', 'write' or an object`);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
checkUnknownKeys(where, policy, POLICY_KEYS);
|
|
412
|
+
if (policy.routes !== void 0 && (!Array.isArray(policy.routes) || !policy.routes.every(
|
|
413
|
+
(permission) => typeof permission === "string" && permission.length
|
|
414
|
+
))) {
|
|
415
|
+
errors.push(`${where}.routes must be an array of permission names`);
|
|
416
|
+
}
|
|
417
|
+
const entities = (_a = policy.entities) != null ? _a : {};
|
|
418
|
+
if (!isPlainObject(entities)) {
|
|
419
|
+
errors.push(`${where}.entities must be an object`);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
for (const [entityType, grant] of Object.entries(entities)) {
|
|
423
|
+
const entityWhere = `${where}.entities.${entityType}`;
|
|
424
|
+
if (entityType === "*") {
|
|
425
|
+
checkLevel(entityWhere, grant);
|
|
426
|
+
} else if (!isEntityType(entityType)) {
|
|
427
|
+
errors.push(`${entityWhere}: unknown entity type '${entityType}'`);
|
|
428
|
+
} else {
|
|
429
|
+
checkEntityGrant(entityWhere, grant, entityType);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (errors.length > errorsBefore) continue;
|
|
433
|
+
const compiled = compileApiKeyPolicy(name, policy);
|
|
434
|
+
for (const entityType of entityNames) {
|
|
435
|
+
const entityWhere = `${where}.entities.${entityType}`;
|
|
436
|
+
checkCreateIsSatisfiable(entityWhere, compiled, entityType);
|
|
437
|
+
for (const target of entityNames) {
|
|
438
|
+
const asEntity = (_c = (_b = mutualConfig(entityType, target)) == null ? void 0 : _b.asEntity) == null ? void 0 : _c.name;
|
|
439
|
+
if (asEntity && ((_d = compiled.mutual(entityType, target)) == null ? void 0 : _d.create) && !compiled.entity(asEntity).create) {
|
|
440
|
+
errors.push(
|
|
441
|
+
`${entityWhere}: create on ${entityType}\u2192${target} also creates a '${asEntity}' entity, so it needs create on '${asEntity}'`
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return errors;
|
|
448
|
+
};
|
|
449
|
+
var VALIDATE_API_KEY_POLICIES = /* @__PURE__ */ Symbol.for("monorise.validateApiKeyPolicies");
|
|
450
|
+
var defineApiKeyPolicies = (policies) => {
|
|
451
|
+
Object.defineProperty(policies, VALIDATE_API_KEY_POLICIES, {
|
|
452
|
+
value: (entityConfigs) => validateApiKeyPolicies(policies, entityConfigs),
|
|
453
|
+
enumerable: false
|
|
454
|
+
});
|
|
455
|
+
return policies;
|
|
456
|
+
};
|
|
80
457
|
export {
|
|
458
|
+
DEFAULT_API_KEY_POLICY,
|
|
81
459
|
Entity,
|
|
460
|
+
FULL_API_KEY_POLICY,
|
|
461
|
+
compileApiKeyPolicy,
|
|
82
462
|
createEntityConfig,
|
|
83
463
|
createMutualConfig,
|
|
84
|
-
|
|
464
|
+
defineApiKeyPolicies,
|
|
465
|
+
resolveEffectiveMutualSchema,
|
|
466
|
+
validateApiKeyPolicies
|
|
85
467
|
};
|
|
86
468
|
//# sourceMappingURL=index.js.map
|