@zapier/policy-context 0.10.0 → 1.0.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.d.ts CHANGED
@@ -38,7 +38,7 @@ declare function buildUrlString(parsed: ParsedUrl): string;
38
38
  */
39
39
  declare const HttpRequestContextSchema: z.ZodObject<{
40
40
  request_type: z.ZodLiteral<"http_request">;
41
- method: z.ZodString;
41
+ method: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
42
42
  url: z.ZodObject<{
43
43
  scheme: z.ZodString;
44
44
  host: z.ZodString;
@@ -54,26 +54,84 @@ declare const HttpRequestContextSchema: z.ZodObject<{
54
54
  }, z.core.$strip>;
55
55
  type HttpRequestContext = z.infer<typeof HttpRequestContextSchema>;
56
56
  /**
57
- * Action run context for approval/policy flows.
58
- * Sent by sdkapi when an action run is denied, stored by approvalsapi,
59
- * and used to build policy conditions.
57
+ * Canonical Zapier action types.
58
+ *
59
+ * Mirrors the `action_type` enum in sdkapi's OpenAPI spec
60
+ * (`packages/openapi-spec/openapi.d.ts` `action_type`). Single source of
61
+ * truth for any code that branches on or filters by action_type — keeps
62
+ * `ActionRunContextSchema`, `BuildActionRunContextOptionsSchema`, and the
63
+ * `READ_ACTION_TYPES` allowlist in `connection-policy.ts` aligned.
64
+ */
65
+ declare const ActionTypeSchema: z.ZodEnum<{
66
+ filter: "filter";
67
+ read: "read";
68
+ read_bulk: "read_bulk";
69
+ run: "run";
70
+ search: "search";
71
+ search_and_write: "search_and_write";
72
+ search_or_write: "search_or_write";
73
+ write: "write";
74
+ }>;
75
+ type ActionType = z.infer<typeof ActionTypeSchema>;
76
+ /**
77
+ * Parse an action resource string of the form
78
+ * `action/{selected_api}/{action_type}/{action_key}` into its parts.
79
+ * Throws if the string doesn't match the canonical 4-segment shape or
80
+ * if its `action_type` segment isn't one of the canonical 8 — every
81
+ * action_run context that has been through `ActionRunContextSchema` is
82
+ * guaranteed to parse cleanly, so a thrown error here means upstream
83
+ * validation was bypassed.
60
84
  */
61
- declare const ActionRunContextSchema: z.ZodObject<{
85
+ declare function parseActionResource(resource: string): {
86
+ selected_api: string;
87
+ action_type: ActionType;
88
+ action_key: string;
89
+ };
90
+ declare const ActionRunContextSchema: z.ZodPipe<z.ZodObject<{
62
91
  request_type: z.ZodLiteral<"action_run">;
63
- selected_api: z.ZodString;
64
- action_type: z.ZodString;
65
- action_key: z.ZodString;
92
+ resource: z.ZodOptional<z.ZodString>;
93
+ selected_api: z.ZodOptional<z.ZodString>;
94
+ action_type: z.ZodOptional<z.ZodEnum<{
95
+ filter: "filter";
96
+ read: "read";
97
+ read_bulk: "read_bulk";
98
+ run: "run";
99
+ search: "search";
100
+ search_and_write: "search_and_write";
101
+ search_or_write: "search_or_write";
102
+ write: "write";
103
+ }>>;
104
+ action_key: z.ZodOptional<z.ZodString>;
66
105
  connection_id: z.ZodOptional<z.ZodString>;
67
106
  inputs: z.ZodOptional<z.ZodType<_zapier_policy_schema.JsonValue, unknown, z.core.$ZodTypeInternals<_zapier_policy_schema.JsonValue, unknown>>>;
68
107
  reason: z.ZodOptional<z.ZodString>;
69
- }, z.core.$strip>;
108
+ }, z.core.$strip>, z.ZodTransform<{
109
+ reason?: string | undefined;
110
+ inputs?: _zapier_policy_schema.JsonValue | undefined;
111
+ connection_id?: string | undefined;
112
+ request_type: "action_run";
113
+ resource: string;
114
+ }, {
115
+ request_type: "action_run";
116
+ resource?: string | undefined;
117
+ selected_api?: string | undefined;
118
+ action_type?: "filter" | "read" | "read_bulk" | "run" | "search" | "search_and_write" | "search_or_write" | "write" | undefined;
119
+ action_key?: string | undefined;
120
+ connection_id?: string | undefined;
121
+ inputs?: _zapier_policy_schema.JsonValue | undefined;
122
+ reason?: string | undefined;
123
+ }>>;
70
124
  type ActionRunContext = z.infer<typeof ActionRunContextSchema>;
71
125
  /**
72
- * Discriminated union of all request context types.
126
+ * Union of all request context types, keyed on `request_type`.
127
+ *
128
+ * Plain `z.union` (not `z.discriminatedUnion`) because
129
+ * `ActionRunContextSchema` carries a `.transform` and discriminated
130
+ * unions can't accept a `ZodEffects` member.
73
131
  */
74
- declare const RequestContextSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
132
+ declare const RequestContextSchema: z.ZodUnion<readonly [z.ZodObject<{
75
133
  request_type: z.ZodLiteral<"http_request">;
76
- method: z.ZodString;
134
+ method: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
77
135
  url: z.ZodObject<{
78
136
  scheme: z.ZodString;
79
137
  host: z.ZodString;
@@ -86,15 +144,40 @@ declare const RequestContextSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
86
144
  body_json: z.ZodNullable<z.ZodType<_zapier_policy_schema.JsonValue, unknown, z.core.$ZodTypeInternals<_zapier_policy_schema.JsonValue, unknown>>>;
87
145
  connection_id: z.ZodOptional<z.ZodString>;
88
146
  reason: z.ZodOptional<z.ZodString>;
89
- }, z.core.$strip>, z.ZodObject<{
147
+ }, z.core.$strip>, z.ZodPipe<z.ZodObject<{
90
148
  request_type: z.ZodLiteral<"action_run">;
91
- selected_api: z.ZodString;
92
- action_type: z.ZodString;
93
- action_key: z.ZodString;
149
+ resource: z.ZodOptional<z.ZodString>;
150
+ selected_api: z.ZodOptional<z.ZodString>;
151
+ action_type: z.ZodOptional<z.ZodEnum<{
152
+ filter: "filter";
153
+ read: "read";
154
+ read_bulk: "read_bulk";
155
+ run: "run";
156
+ search: "search";
157
+ search_and_write: "search_and_write";
158
+ search_or_write: "search_or_write";
159
+ write: "write";
160
+ }>>;
161
+ action_key: z.ZodOptional<z.ZodString>;
94
162
  connection_id: z.ZodOptional<z.ZodString>;
95
163
  inputs: z.ZodOptional<z.ZodType<_zapier_policy_schema.JsonValue, unknown, z.core.$ZodTypeInternals<_zapier_policy_schema.JsonValue, unknown>>>;
96
164
  reason: z.ZodOptional<z.ZodString>;
97
- }, z.core.$strip>], "request_type">;
165
+ }, z.core.$strip>, z.ZodTransform<{
166
+ reason?: string | undefined;
167
+ inputs?: _zapier_policy_schema.JsonValue | undefined;
168
+ connection_id?: string | undefined;
169
+ request_type: "action_run";
170
+ resource: string;
171
+ }, {
172
+ request_type: "action_run";
173
+ resource?: string | undefined;
174
+ selected_api?: string | undefined;
175
+ action_type?: "filter" | "read" | "read_bulk" | "run" | "search" | "search_and_write" | "search_or_write" | "write" | undefined;
176
+ action_key?: string | undefined;
177
+ connection_id?: string | undefined;
178
+ inputs?: _zapier_policy_schema.JsonValue | undefined;
179
+ reason?: string | undefined;
180
+ }>>]>;
98
181
  type RequestContext = z.infer<typeof RequestContextSchema>;
99
182
 
100
183
  /**
@@ -133,14 +216,28 @@ type BuildHttpRequestContextOptions = z.infer<typeof BuildHttpRequestContextOpti
133
216
  declare function buildHttpRequestContext(options: BuildHttpRequestContextOptions): HttpRequestContext;
134
217
  declare const BuildActionRunContextOptionsSchema: z.ZodObject<{
135
218
  selected_api: z.ZodString;
136
- action_type: z.ZodString;
219
+ action_type: z.ZodEnum<{
220
+ filter: "filter";
221
+ read: "read";
222
+ read_bulk: "read_bulk";
223
+ run: "run";
224
+ search: "search";
225
+ search_and_write: "search_and_write";
226
+ search_or_write: "search_or_write";
227
+ write: "write";
228
+ }>;
137
229
  action_key: z.ZodString;
138
230
  connection_id: z.ZodOptional<z.ZodString>;
139
231
  inputs: z.ZodOptional<z.ZodType<_zapier_policy_schema.JsonValue, unknown, z.core.$ZodTypeInternals<_zapier_policy_schema.JsonValue, unknown>>>;
232
+ reason: z.ZodOptional<z.ZodString>;
140
233
  }, z.core.$strip>;
141
234
  type BuildActionRunContextOptions = z.infer<typeof BuildActionRunContextOptionsSchema>;
142
235
  /**
143
236
  * Build a canonical ActionRunContext from raw action parts.
237
+ *
238
+ * Callers pass the three identity fields separately for readability;
239
+ * this validates them and collapses them into the canonical `resource`
240
+ * string the schema stores.
144
241
  */
145
242
  declare function buildActionRunContext(options: BuildActionRunContextOptions): ActionRunContext;
146
243
 
@@ -167,16 +264,27 @@ declare function buildDomainHttpConditions(context: HttpRequestContext): Conditi
167
264
  * @returns Condition[] for an action permission statement
168
265
  */
169
266
  declare function buildExactActionConditions(context: ActionRunContext): Condition[];
267
+ /**
268
+ * The three identity fields that compose an action resource string.
269
+ */
270
+ interface ActionResourceParts {
271
+ selected_api: string;
272
+ action_type: ActionType;
273
+ action_key: string;
274
+ }
170
275
  /**
171
276
  * Build the resource string for an action.
172
277
  * Format: action/{selected_api}/{action_type}/{action_key}
173
278
  */
174
- declare function buildActionResource(context: ActionRunContext): string;
279
+ declare function buildActionResource(parts: ActionResourceParts): string;
175
280
  /**
176
- * Build a broad action resource that matches all actions for an app.
177
- * Format: action/{selected_api}/{wildcard}/{wildcard}
281
+ * Build a broad action resource that matches all actions of the same type
282
+ * for an app (e.g. all reads, all writes). Scopes on `action_type` to
283
+ * avoid silently granting cross-type access.
284
+ *
285
+ * Format: action/{selected_api}/{action_type}/{wildcard}
178
286
  */
179
- declare function buildBroadActionResource(context: ActionRunContext): string;
287
+ declare function buildBroadActionResource(parts: Pick<ActionResourceParts, "selected_api" | "action_type">): string;
180
288
 
181
289
  /**
182
290
  * Build an exact-match policy from any supported request context.
@@ -191,13 +299,35 @@ declare const AccessLevelSchema: z.ZodEnum<{
191
299
  read_write: "read_write";
192
300
  }>;
193
301
  type AccessLevel = z.infer<typeof AccessLevelSchema>;
302
+ /** HTTP methods that count as side-effect-free reads for connection-level policies. */
303
+ declare const READ_HTTP_METHODS: readonly ["GET", "HEAD", "OPTIONS"];
304
+ /**
305
+ * Action types that count as side-effect-free reads for connection-level
306
+ * policies. `read_bulk` is the paginated/bulk variant of `read` — same
307
+ * external API semantics, no mutations. Widely used by major apps (Slack,
308
+ * Gmail, Jira, Salesforce, etc.) via the public `bulkReads` schema key.
309
+ * `filter` and `run` are excluded: neither has a runtime executor for
310
+ * external apps, and `filter` actions don't carry a connection_id anyway,
311
+ * so this allowlist has no effect on them.
312
+ */
313
+ declare const READ_ACTION_TYPES: readonly ["read", "read_bulk", "search"];
314
+ /**
315
+ * Returns true when the request needs write access (i.e. it is NOT a
316
+ * side-effect-free read). Mirrors the logic encoded in `buildConnectionPolicy`
317
+ * so UI surfaces that gate the read-vs-read_write choice stay consistent
318
+ * with the policies they generate.
319
+ */
320
+ declare function isWriteRequest(context: RequestContext): boolean;
194
321
  /**
195
322
  * Build a connection-level policy that covers both HTTP and action access.
196
323
  *
197
324
  * Always produces three statements:
198
325
  * 1. `can_use` for the authentication
199
326
  * 2. `can_execute` on `http` scoped by connection_id (+ method filter for read)
200
- * 3. `can_execute` on `action/{selectedApi}/**` scoped by connection_id (+ action_type filter for read)
327
+ * 3. `can_execute` on `action/{selectedApi}/{action_type}/*` per allowed type,
328
+ * scoped by connection_id. For read access, the resource list expands to
329
+ * one entry per read action_type so the engine matches via the resource
330
+ * string rather than a separate `path: ["action_type"]` condition.
201
331
  */
202
332
  declare function buildConnectionPolicy(options: {
203
333
  connectionId: string;
@@ -205,4 +335,4 @@ declare function buildConnectionPolicy(options: {
205
335
  accessLevel: AccessLevel;
206
336
  }): Policy;
207
337
 
208
- export { type AccessLevel, AccessLevelSchema, type ActionRunContext, ActionRunContextSchema, type BuildActionRunContextOptions, BuildActionRunContextOptionsSchema, type BuildHttpRequestContextOptions, BuildHttpRequestContextOptionsSchema, DEFAULT_PORTS, type HttpRequestContext, HttpRequestContextSchema, IGNORED_HEADERS, type ParsedUrl, ParsedUrlSchema, type RequestContext, RequestContextSchema, buildActionResource, buildActionRunContext, buildBroadActionResource, buildConnectionPolicy, buildDomainHttpConditions, buildExactActionConditions, buildExactHttpConditions, buildHttpRequestContext, buildUrlString, parseUrl, toExactPolicy };
338
+ export { type AccessLevel, AccessLevelSchema, type ActionRunContext, ActionRunContextSchema, type ActionType, ActionTypeSchema, type BuildActionRunContextOptions, BuildActionRunContextOptionsSchema, type BuildHttpRequestContextOptions, BuildHttpRequestContextOptionsSchema, DEFAULT_PORTS, type HttpRequestContext, HttpRequestContextSchema, IGNORED_HEADERS, type ParsedUrl, ParsedUrlSchema, READ_ACTION_TYPES, READ_HTTP_METHODS, type RequestContext, RequestContextSchema, buildActionResource, buildActionRunContext, buildBroadActionResource, buildConnectionPolicy, buildDomainHttpConditions, buildExactActionConditions, buildExactHttpConditions, buildHttpRequestContext, buildUrlString, isWriteRequest, parseActionResource, parseUrl, toExactPolicy };