@zapier/policy-context 0.8.0 → 0.8.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/README.md +80 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @zapier/policy-context
|
|
2
|
+
|
|
3
|
+
Shared request context types, builders, and policy condition generators — the contract between [sdkapi](https://gitlab.com/zapier/zapier-sdk/sdkapi) and [approvalsapi](https://gitlab.com/zapier/zapier-sdk/approvalsapi) for describing requests in approval flows and policy evaluation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @zapier/policy-context zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`zod` (v4+) is a peer dependency.
|
|
12
|
+
|
|
13
|
+
## What it provides
|
|
14
|
+
|
|
15
|
+
Three layers, each building on the previous:
|
|
16
|
+
|
|
17
|
+
1. **Types** — Zod schemas for `HttpRequestContext`, `ActionRunContext`, and the `RequestContext` discriminated union. These are the canonical shapes stored in approvalsapi and sent by sdkapi.
|
|
18
|
+
2. **Builders** — Normalize raw request parts (URL parsing, header filtering, body coercion) into validated contexts.
|
|
19
|
+
3. **Conditions / Policies** — Convert stored contexts into [`@zapier/policy-schema`](https://www.npmjs.com/package/@zapier/policy-schema) `Condition[]` and full `Policy` documents.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Build a request context
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { buildHttpRequestContext } from "@zapier/policy-context";
|
|
27
|
+
|
|
28
|
+
const context = buildHttpRequestContext({
|
|
29
|
+
method: "POST",
|
|
30
|
+
url: "https://api.example.com/v1/messages",
|
|
31
|
+
headers: { "content-type": "application/json", authorization: "Bearer …" },
|
|
32
|
+
body: '{"text":"hello"}',
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`buildHttpRequestContext` normalizes the URL, filters headers through `IGNORED_HEADERS` (auth, cookies, etc.), and validates the result with Zod.
|
|
37
|
+
|
|
38
|
+
### Generate policy conditions
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import {
|
|
42
|
+
buildExactHttpConditions,
|
|
43
|
+
buildDomainHttpConditions,
|
|
44
|
+
} from "@zapier/policy-context";
|
|
45
|
+
|
|
46
|
+
const exact = buildExactHttpConditions(context); // matches this exact request
|
|
47
|
+
const domain = buildDomainHttpConditions(context); // matches any request to the same host
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Build a complete "allow this exact request" policy
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { toExactPolicy } from "@zapier/policy-context";
|
|
54
|
+
|
|
55
|
+
const policy = toExactPolicy(context);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Connection-scoped policies
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { buildConnectionPolicy } from "@zapier/policy-context";
|
|
62
|
+
|
|
63
|
+
const policy = buildConnectionPolicy({
|
|
64
|
+
connectionId: "conn_123",
|
|
65
|
+
accessLevel: "read_only",
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Subpath imports
|
|
70
|
+
|
|
71
|
+
In addition to the package root, every module is importable directly:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { buildHttpRequestContext } from "@zapier/policy-context/builders";
|
|
75
|
+
import { HttpRequestContextSchema } from "@zapier/policy-context/types";
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
UNLICENSED — internal Zapier use.
|