@snowtop/ent 0.0.34 → 0.0.35

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/core/base.d.ts CHANGED
@@ -108,6 +108,7 @@ declare enum privacyResult {
108
108
  export interface PrivacyResult {
109
109
  result: privacyResult;
110
110
  error?: PrivacyError;
111
+ getError?(policy: PrivacyPolicy, rule: PrivacyPolicyRule, ent?: Ent): PrivacyError;
111
112
  }
112
113
  export interface PrivacyError extends Error {
113
114
  privacyPolicy: PrivacyPolicy;
@@ -116,7 +117,7 @@ export interface PrivacyError extends Error {
116
117
  export declare function Allow(): PrivacyResult;
117
118
  export declare function Skip(): PrivacyResult;
118
119
  export declare function Deny(): PrivacyResult;
119
- export declare function DenyWithReason(e: PrivacyError): PrivacyResult;
120
+ export declare function DenyWithReason(e: PrivacyError | string): PrivacyResult;
120
121
  export interface PrivacyPolicyRule {
121
122
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
122
123
  }
package/core/base.js CHANGED
@@ -30,7 +30,23 @@ function Deny() {
30
30
  return deny;
31
31
  }
32
32
  exports.Deny = Deny;
33
+ class DenyWithReasonError extends Error {
34
+ constructor(privacyPolicy, rule, msg, ent) {
35
+ super(msg);
36
+ this.privacyPolicy = privacyPolicy;
37
+ this.privacyRule = rule;
38
+ this.ent = ent;
39
+ }
40
+ }
33
41
  function DenyWithReason(e) {
42
+ if (typeof e === "string") {
43
+ return {
44
+ result: privacyResult.Deny,
45
+ getError(policy, rule, ent) {
46
+ return new DenyWithReasonError(policy, rule, e, ent);
47
+ },
48
+ };
49
+ }
34
50
  return {
35
51
  result: privacyResult.Deny,
36
52
  error: e,
package/core/privacy.js CHANGED
@@ -464,6 +464,9 @@ async function applyPrivacyPolicyX(v, policy, ent, throwErr) {
464
464
  if (res.error) {
465
465
  throw res.error;
466
466
  }
467
+ if (res.getError) {
468
+ throw res.getError(policy, rule, ent);
469
+ }
467
470
  if (throwErr) {
468
471
  throw throwErr();
469
472
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,4 +1,4 @@
1
- import { Schema, AssocEdge, AssocEdgeGroup, Action } from "../schema";
1
+ import { Schema, Field, AssocEdge, AssocEdgeGroup, Action } from "../schema";
2
2
  import { ActionField } from "../schema/schema";
3
3
  declare enum NullableResult {
4
4
  CONTENTS = "contents",
@@ -12,10 +12,11 @@ declare type ProcessedAssocEdge = Omit<AssocEdge, "actionOnlyFields" | "edgeActi
12
12
  patternName?: string;
13
13
  edgeActions?: OutputAction[];
14
14
  };
15
- declare type ProcessedSchema = Omit<Schema, "edges" | "actions" | "edgeGroups"> & {
15
+ declare type ProcessedSchema = Omit<Schema, "edges" | "actions" | "edgeGroups" | "fields"> & {
16
16
  actions: OutputAction[];
17
17
  assocEdges: ProcessedAssocEdge[];
18
18
  assocEdgeGroups: ProcessedAssocEdgeGroup[];
19
+ fields: ProcessedField[];
19
20
  };
20
21
  declare type ProcessedAssocEdgeGroup = Omit<AssocEdgeGroup, "edgeAction"> & {
21
22
  edgeAction?: OutputAction;
@@ -29,7 +30,13 @@ interface schemasDict {
29
30
  interface ProcessedPattern {
30
31
  name: string;
31
32
  assocEdges: ProcessedAssocEdge[];
33
+ fields: ProcessedField[];
32
34
  }
35
+ declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOnCreate"> & {
36
+ hasDefaultValueOnCreate?: boolean;
37
+ hasDefaultValueOnEdit?: boolean;
38
+ patternName?: string;
39
+ };
33
40
  interface patternsDict {
34
41
  [key: string]: ProcessedPattern;
35
42
  }
@@ -1,21 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseSchema = void 0;
4
- function processFields(processedSchema, src) {
4
+ function processFields(src, patternName) {
5
+ const ret = [];
5
6
  for (const field of src) {
6
7
  let f = { ...field };
7
- f["hasDefaultValueOnCreate"] = field.defaultValueOnCreate != undefined;
8
- f["hasDefaultValueOnEdit"] = field.defaultValueOnEdit != undefined;
8
+ f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
9
+ f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
9
10
  if (field.polymorphic) {
10
11
  // convert boolean into object
11
12
  // we keep boolean as an option to keep API simple
12
13
  if (typeof field.polymorphic === "boolean") {
13
- f["polymorphic"] = {};
14
+ f.polymorphic = {};
14
15
  }
15
16
  else {
16
- f["polymorphic"] = field.polymorphic;
17
+ f.polymorphic = field.polymorphic;
17
18
  }
18
19
  }
20
+ else {
21
+ delete f.polymorphic;
22
+ }
19
23
  // convert string to object to make API consumed by go simple
20
24
  if (f.fieldEdge && f.fieldEdge.inverseEdge) {
21
25
  if (typeof f.fieldEdge.inverseEdge === "string") {
@@ -24,16 +28,22 @@ function processFields(processedSchema, src) {
24
28
  };
25
29
  }
26
30
  }
27
- processedSchema.fields.push(f);
31
+ if (patternName) {
32
+ f.patternName = patternName;
33
+ }
34
+ ret.push(f);
28
35
  }
36
+ return ret;
29
37
  }
30
- function processEdges(dst, src, patternName) {
38
+ function processEdges(src, patternName) {
39
+ const ret = [];
31
40
  for (const edge of src) {
32
41
  let edge2 = { ...edge };
33
42
  edge2.edgeActions = edge.edgeActions?.map((action) => processAction(action));
34
43
  edge2.patternName = patternName;
35
- dst.push(edge2);
44
+ ret.push(edge2);
36
45
  }
46
+ return ret;
37
47
  }
38
48
  function processEdgeGroups(processedSchema, edgeGroups) {
39
49
  // array-ify this
@@ -49,26 +59,26 @@ function processEdgeGroups(processedSchema, edgeGroups) {
49
59
  }
50
60
  }
51
61
  function processPattern(patterns, pattern, processedSchema) {
52
- // TODO kill
53
- let name = pattern.name || "node";
62
+ const name = pattern.name;
63
+ const fields = processFields(pattern.fields, pattern.name);
64
+ processedSchema.fields.push(...fields);
65
+ if (pattern.edges) {
66
+ const edges = processEdges(pattern.edges, pattern.name);
67
+ processedSchema.assocEdges.push(...edges);
68
+ }
54
69
  if (patterns[name] === undefined) {
55
- const edges = [];
56
- if (pattern.edges) {
57
- processEdges(edges, pattern.edges);
58
- }
70
+ // intentionally processing separately and not passing pattern.name
71
+ const edges = processEdges(pattern.edges || []);
59
72
  patterns[name] = {
60
73
  name: pattern.name,
61
74
  assocEdges: edges,
75
+ fields: fields,
62
76
  };
63
77
  }
64
78
  else {
65
79
  // TODO ideally we want to make sure that different patterns don't have the same name
66
80
  // can't do a deepEqual check because function calls and therefore different instances in fields
67
81
  }
68
- processFields(processedSchema, pattern.fields);
69
- if (pattern.edges) {
70
- processEdges(processedSchema.assocEdges, pattern.edges, pattern.name);
71
- }
72
82
  }
73
83
  var NullableResult;
74
84
  (function (NullableResult) {
@@ -84,6 +94,7 @@ function processAction(action) {
84
94
  let actionOnlyFields = action.actionOnlyFields.map((f) => {
85
95
  let f2 = f;
86
96
  if (!f.nullable) {
97
+ delete f2.nullable;
87
98
  return f2;
88
99
  }
89
100
  if (typeof f.nullable === "boolean") {
@@ -133,9 +144,11 @@ function parseSchema(potentialSchemas) {
133
144
  processPattern(patterns, pattern, processedSchema);
134
145
  }
135
146
  }
136
- processFields(processedSchema, schema.fields);
147
+ const fields = processFields(schema.fields);
148
+ processedSchema.fields.push(...fields);
137
149
  if (schema.edges) {
138
- processEdges(processedSchema.assocEdges, schema.edges);
150
+ const edges = processEdges(schema.edges);
151
+ processedSchema.assocEdges.push(...edges);
139
152
  }
140
153
  if (schema.edgeGroups) {
141
154
  processEdgeGroups(processedSchema, schema.edgeGroups);