@sanoma-ai/workflows 0.0.0-stage → 0.1.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/LICENSE +105 -0
- package/README.md +493 -2
- package/dist/approvals.d.ts +46 -0
- package/dist/approvals.js +111 -0
- package/dist/approvals.js.map +1 -0
- package/dist/ast.d.ts +57 -0
- package/dist/ast.js +81 -0
- package/dist/ast.js.map +1 -0
- package/dist/call.d.ts +33 -0
- package/dist/call.js +402 -0
- package/dist/call.js.map +1 -0
- package/dist/client.d.ts +167 -0
- package/dist/client.js +400 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +126 -0
- package/dist/config.js +247 -0
- package/dist/config.js.map +1 -0
- package/dist/credentials.d.ts +30 -0
- package/dist/credentials.js +193 -0
- package/dist/credentials.js.map +1 -0
- package/dist/cucumber.d.ts +64 -0
- package/dist/cucumber.js +158 -0
- package/dist/cucumber.js.map +1 -0
- package/dist/datafile.d.ts +62 -0
- package/dist/datafile.js +326 -0
- package/dist/datafile.js.map +1 -0
- package/dist/db.d.ts +58 -0
- package/dist/db.js +179 -0
- package/dist/db.js.map +1 -0
- package/dist/define.d.ts +205 -0
- package/dist/define.js +103 -0
- package/dist/define.js.map +1 -0
- package/dist/describe.d.ts +135 -0
- package/dist/describe.js +127 -0
- package/dist/describe.js.map +1 -0
- package/dist/drift.d.ts +75 -0
- package/dist/drift.js +90 -0
- package/dist/drift.js.map +1 -0
- package/dist/env.d.ts +49 -0
- package/dist/env.js +83 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +88 -0
- package/dist/errors.js +148 -0
- package/dist/errors.js.map +1 -0
- package/dist/expectations.d.ts +26 -0
- package/dist/expectations.js +137 -0
- package/dist/expectations.js.map +1 -0
- package/dist/fake.d.ts +55 -0
- package/dist/fake.js +87 -0
- package/dist/fake.js.map +1 -0
- package/dist/fill.d.ts +9 -0
- package/dist/fill.js +17 -0
- package/dist/fill.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/ledger.d.ts +217 -0
- package/dist/ledger.js +251 -0
- package/dist/ledger.js.map +1 -0
- package/dist/lint.d.ts +32 -0
- package/dist/lint.js +168 -0
- package/dist/lint.js.map +1 -0
- package/dist/log.d.ts +24 -0
- package/dist/log.js +83 -0
- package/dist/log.js.map +1 -0
- package/dist/op.d.ts +257 -0
- package/dist/op.js +188 -0
- package/dist/op.js.map +1 -0
- package/dist/outline.d.ts +179 -0
- package/dist/outline.js +506 -0
- package/dist/outline.js.map +1 -0
- package/dist/paths.d.ts +11 -0
- package/dist/paths.js +30 -0
- package/dist/paths.js.map +1 -0
- package/dist/plugin.d.ts +28 -0
- package/dist/plugin.js +25 -0
- package/dist/plugin.js.map +1 -0
- package/dist/policy.d.ts +114 -0
- package/dist/policy.js +60 -0
- package/dist/policy.js.map +1 -0
- package/dist/resource.d.ts +212 -0
- package/dist/resource.js +285 -0
- package/dist/resource.js.map +1 -0
- package/dist/resources.d.ts +62 -0
- package/dist/resources.js +197 -0
- package/dist/resources.js.map +1 -0
- package/dist/run.d.ts +87 -0
- package/dist/run.js +2 -0
- package/dist/run.js.map +1 -0
- package/dist/sandbox.d.ts +33 -0
- package/dist/sandbox.js +123 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/scenario.d.ts +212 -0
- package/dist/scenario.js +1079 -0
- package/dist/scenario.js.map +1 -0
- package/dist/scenarios.d.ts +55 -0
- package/dist/scenarios.js +71 -0
- package/dist/scenarios.js.map +1 -0
- package/dist/shared.d.ts +61 -0
- package/dist/shared.js +77 -0
- package/dist/shared.js.map +1 -0
- package/dist/steps.d.ts +76 -0
- package/dist/steps.js +23 -0
- package/dist/steps.js.map +1 -0
- package/dist/version.d.ts +29 -0
- package/dist/version.js +76 -0
- package/dist/version.js.map +1 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +219 -0
- package/dist/worker.js.map +1 -0
- package/oxlint.json +128 -0
- package/package.json +91 -3
- package/src/approvals.ts +157 -0
- package/src/ast.ts +115 -0
- package/src/call.ts +468 -0
- package/src/client.ts +534 -0
- package/src/config.ts +381 -0
- package/src/credentials.ts +211 -0
- package/src/cucumber.ts +230 -0
- package/src/datafile.ts +409 -0
- package/src/db.ts +197 -0
- package/src/define.ts +285 -0
- package/src/describe.ts +262 -0
- package/src/drift.ts +149 -0
- package/src/env.ts +122 -0
- package/src/errors.ts +202 -0
- package/src/expectations.ts +165 -0
- package/src/fake.ts +156 -0
- package/src/fill.ts +24 -0
- package/src/index.ts +120 -0
- package/src/ledger.ts +419 -0
- package/src/lint.ts +208 -0
- package/src/log.ts +82 -0
- package/src/op.ts +399 -0
- package/src/outline.ts +620 -0
- package/src/paths.ts +32 -0
- package/src/plugin.ts +37 -0
- package/src/policy.ts +139 -0
- package/src/resource.ts +447 -0
- package/src/resources.ts +256 -0
- package/src/run.ts +91 -0
- package/src/sandbox.ts +157 -0
- package/src/scenario.ts +1328 -0
- package/src/scenarios.ts +111 -0
- package/src/shared.ts +116 -0
- package/src/steps.ts +90 -0
- package/src/version.ts +86 -0
- package/src/worker.ts +253 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAY7C,MAAM,MAAM,GAAG;IACb,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;IACxB,KAAK,EAAE;QACL,WAAW,EAAE;YACX,IAAI,EAAE;gBACJ,IAAI,EAAE,SAAS;gBACf,IAAI,EAAE;oBACJ,WAAW,EACT,iJAAiJ;iBACpJ;aACF;YACD,MAAM,CAAC,OAAgB;gBACrB,OAAO;oBACL,OAAO,CAAC,OAAgB;wBACtB,KAAK,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC;4BACvF,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;wBAC7D,CAAC;oBACH,CAAC;iBACF,CAAC;YACJ,CAAC;SACF;KACF;CACF,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
package/dist/policy.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type ApprovalState, Approver, type Principal } from "./define.ts";
|
|
3
|
+
import type { Effect, Op } from "./op.ts";
|
|
4
|
+
/** An operation as a policy sees it: plain data, without its schemas. */
|
|
5
|
+
export type PolicyOp = Pick<Op, "id" | "vendor" | "resource" | "name" | "effect">;
|
|
6
|
+
/**
|
|
7
|
+
* An operation as a policy sees it, for a `PolicyCall`: what the runtime passes, so a policy
|
|
8
|
+
* test can build a call from the connector, `{ op: policyOpOf(shop.order.refund), ... }`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const policyOpOf: ({ id, vendor, resource, name, effect }: Op) => PolicyOp;
|
|
11
|
+
/** What a policy sees for each operation a run calls. Plain data, so a test can build one by hand. */
|
|
12
|
+
export interface PolicyCall {
|
|
13
|
+
op: PolicyOp;
|
|
14
|
+
/** The same as `op.effect`. */
|
|
15
|
+
effect: Effect;
|
|
16
|
+
/** The resource instance the call acts on, from the operation's `target`, when it declares one. */
|
|
17
|
+
target?: string;
|
|
18
|
+
/** The operation's input, already checked against its schema. */
|
|
19
|
+
input: unknown;
|
|
20
|
+
/** Who started the run. */
|
|
21
|
+
actor: Principal;
|
|
22
|
+
/**
|
|
23
|
+
* The run so far: a copy of its approvals, including those the policy asked for earlier.
|
|
24
|
+
* A policy request has `requestedBy: "policy"` and the `op` it held.
|
|
25
|
+
*/
|
|
26
|
+
run: {
|
|
27
|
+
id: string;
|
|
28
|
+
workflow: string;
|
|
29
|
+
approvals: readonly ApprovalState[];
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A policy's answer. `reasons` say why, for whoever reads the ledger; they change nothing
|
|
34
|
+
* about what happens to the call.
|
|
35
|
+
*/
|
|
36
|
+
export type Decision = {
|
|
37
|
+
kind: "allow";
|
|
38
|
+
reasons?: string[];
|
|
39
|
+
} | {
|
|
40
|
+
kind: "deny";
|
|
41
|
+
reason: string;
|
|
42
|
+
reasons?: string[];
|
|
43
|
+
} | ApproveDecision;
|
|
44
|
+
/**
|
|
45
|
+
* Hold the call until the approver decides: a person's id, or `{ group }` for anyone in it.
|
|
46
|
+
* The approval covers the held operation, and `covers` adds others, so a policy can let their
|
|
47
|
+
* later calls through once it is approved.
|
|
48
|
+
*/
|
|
49
|
+
interface ApproveDecision {
|
|
50
|
+
kind: "approve";
|
|
51
|
+
approver: Approver;
|
|
52
|
+
title?: string;
|
|
53
|
+
covers?: Op[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Checks a policy's answer. Parsing also copies it, so nothing else the policy returned is kept,
|
|
57
|
+
* and turns `covers` into op ids.
|
|
58
|
+
*/
|
|
59
|
+
export declare const DecisionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
60
|
+
kind: z.ZodLiteral<"allow">;
|
|
61
|
+
reasons: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
62
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
63
|
+
kind: z.ZodLiteral<"deny">;
|
|
64
|
+
reason: z.ZodString;
|
|
65
|
+
reasons: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
66
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
67
|
+
kind: z.ZodLiteral<"approve">;
|
|
68
|
+
approver: z.ZodType<Approver, unknown, z.core.$ZodTypeInternals<Approver, unknown>>;
|
|
69
|
+
title: z.ZodOptional<z.ZodString>;
|
|
70
|
+
covers: z.ZodOptional<z.ZodArray<z.ZodPipe<z.ZodObject<{
|
|
71
|
+
id: z.ZodString;
|
|
72
|
+
}, z.core.$strip>, z.ZodTransform<string, {
|
|
73
|
+
id: string;
|
|
74
|
+
}>>>>;
|
|
75
|
+
}, z.core.$strip>], "kind">;
|
|
76
|
+
/**
|
|
77
|
+
* A decision as checked and as the ledger records it: `covers` by op id, and the `version` of
|
|
78
|
+
* the policy that made it, when it has one.
|
|
79
|
+
*/
|
|
80
|
+
export type RecordedDecision = z.output<typeof DecisionSchema> & {
|
|
81
|
+
policyVersion?: string;
|
|
82
|
+
};
|
|
83
|
+
export declare const allow: (reasons?: string[]) => Decision;
|
|
84
|
+
export declare const deny: (reason: string, reasons?: string[]) => Decision;
|
|
85
|
+
/** Holds the call for `approver`. `covers` lists other operations the approval also stands for. */
|
|
86
|
+
export declare function approve(approver: Approver, { title, covers }?: {
|
|
87
|
+
title?: string;
|
|
88
|
+
covers?: Op[];
|
|
89
|
+
}): Decision;
|
|
90
|
+
/**
|
|
91
|
+
* Decides whether a run may make an operation call: allow it, deny it (the run fails),
|
|
92
|
+
* or hold it until someone approves. Runs are replayed after a restart, so a policy must
|
|
93
|
+
* decide the same way every time from the call alone: no clock, randomness or network.
|
|
94
|
+
* `lintWorkflow` checks a policy file for these too. Each decision is recorded once made,
|
|
95
|
+
* and a replay reuses it, but a call interrupted before that is decided again. A policy
|
|
96
|
+
* must not have side effects.
|
|
97
|
+
*/
|
|
98
|
+
export type Policy = ((call: PolicyCall) => Decision | Promise<Decision>) & {
|
|
99
|
+
/** Names this version of the policy, from `definePolicy(fn, { version })`. */
|
|
100
|
+
readonly version?: string;
|
|
101
|
+
};
|
|
102
|
+
/** A policy, optionally named with a version so a reader can tell which rules decided a call. */
|
|
103
|
+
export declare function definePolicy(policy: (call: PolicyCall) => Decision | Promise<Decision>, options?: {
|
|
104
|
+
version?: string;
|
|
105
|
+
}): Policy;
|
|
106
|
+
/** Allows every operation call. A config says so explicitly: `policy: allowAll`. */
|
|
107
|
+
export declare const allowAll: Policy;
|
|
108
|
+
/**
|
|
109
|
+
* True when an approval in the run was approved, covers the operation, and was addressed to
|
|
110
|
+
* `approver`. Check all three: a workflow can request an approval covering any operation from
|
|
111
|
+
* anyone it names, so an approval's `covers` alone does not say the right person approved.
|
|
112
|
+
*/
|
|
113
|
+
export declare function approvedFor(approvals: readonly ApprovalState[], opId: string, approver: Approver): boolean;
|
|
114
|
+
export {};
|
package/dist/policy.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Approver, Covers, sameApprover } from "./define.js";
|
|
3
|
+
/**
|
|
4
|
+
* An operation as a policy sees it, for a `PolicyCall`: what the runtime passes, so a policy
|
|
5
|
+
* test can build a call from the connector, `{ op: policyOpOf(shop.order.refund), ... }`.
|
|
6
|
+
*/
|
|
7
|
+
export const policyOpOf = ({ id, vendor, resource, name, effect }) => ({
|
|
8
|
+
id,
|
|
9
|
+
vendor,
|
|
10
|
+
resource,
|
|
11
|
+
name,
|
|
12
|
+
effect,
|
|
13
|
+
});
|
|
14
|
+
const ReasonList = z.array(z.string(), { error: "reasons must be a list of strings" }).optional();
|
|
15
|
+
/**
|
|
16
|
+
* Checks a policy's answer. Parsing also copies it, so nothing else the policy returned is kept,
|
|
17
|
+
* and turns `covers` into op ids.
|
|
18
|
+
*/
|
|
19
|
+
export const DecisionSchema = z.discriminatedUnion("kind", [
|
|
20
|
+
z.object({ kind: z.literal("allow"), reasons: ReasonList }),
|
|
21
|
+
z.object({ kind: z.literal("deny"), reason: z.string({ error: "deny needs a reason" }), reasons: ReasonList }),
|
|
22
|
+
z.object({
|
|
23
|
+
kind: z.literal("approve"),
|
|
24
|
+
approver: Approver,
|
|
25
|
+
title: z.string({ error: "the title must be a string" }).optional(),
|
|
26
|
+
covers: Covers,
|
|
27
|
+
}),
|
|
28
|
+
], { error: "not a decision" });
|
|
29
|
+
export const allow = (reasons) => reasons === undefined ? { kind: "allow" } : { kind: "allow", reasons };
|
|
30
|
+
export const deny = (reason, reasons) => reasons === undefined ? { kind: "deny", reason } : { kind: "deny", reason, reasons };
|
|
31
|
+
/** Holds the call for `approver`. `covers` lists other operations the approval also stands for. */
|
|
32
|
+
export function approve(approver, { title, covers } = {}) {
|
|
33
|
+
return {
|
|
34
|
+
kind: "approve",
|
|
35
|
+
approver,
|
|
36
|
+
...(title === undefined ? {} : { title }),
|
|
37
|
+
...(covers === undefined ? {} : { covers }),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** A policy, optionally named with a version so a reader can tell which rules decided a call. */
|
|
41
|
+
export function definePolicy(policy, options = {}) {
|
|
42
|
+
if (typeof policy !== "function")
|
|
43
|
+
throw new Error("definePolicy expects a function of the call");
|
|
44
|
+
if (options.version !== undefined && (typeof options.version !== "string" || !options.version.trim())) {
|
|
45
|
+
throw new Error("definePolicy: `version` must be a non-empty string");
|
|
46
|
+
}
|
|
47
|
+
const defined = (call) => policy(call);
|
|
48
|
+
return Object.freeze(options.version === undefined ? defined : Object.assign(defined, { version: options.version }));
|
|
49
|
+
}
|
|
50
|
+
/** Allows every operation call. A config says so explicitly: `policy: allowAll`. */
|
|
51
|
+
export const allowAll = definePolicy(() => allow());
|
|
52
|
+
/**
|
|
53
|
+
* True when an approval in the run was approved, covers the operation, and was addressed to
|
|
54
|
+
* `approver`. Check all three: a workflow can request an approval covering any operation from
|
|
55
|
+
* anyone it names, so an approval's `covers` alone does not say the right person approved.
|
|
56
|
+
*/
|
|
57
|
+
export function approvedFor(approvals, opId, approver) {
|
|
58
|
+
return approvals.some((a) => a.status === "approved" && a.covers.includes(opId) && sameApprover(a.approver, approver));
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAsB,QAAQ,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AAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;AACvH,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,MAAM,QAAQ,GAAW,YAAY,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC;AAE5D;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,SAAmC,EAAE,IAAY,EAAE,QAAkB;IAC/F,OAAO,SAAS,CAAC,IAAI,CACnB,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,UAAU,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,CAAC,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAChG,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type Connector, type OpSpec, RESOURCE } from "./op.ts";
|
|
3
|
+
/** Which fields of a resource are flagged, by dotted path (`pages.html_url`; a list's items have no index). */
|
|
4
|
+
export interface ResourceFields {
|
|
5
|
+
/** Changing it replaces the object. */
|
|
6
|
+
readonly immutable: readonly string[];
|
|
7
|
+
/** Set by the vendor only: never drift, and a data file may not declare it. */
|
|
8
|
+
readonly vendorOwned: readonly string[];
|
|
9
|
+
/** Sensitive or write-only: never read back, never compared, never in an operation's output. */
|
|
10
|
+
readonly writeOnly: readonly string[];
|
|
11
|
+
/** Lists whose order is the vendor's (a set): compared as multisets, in no order. */
|
|
12
|
+
readonly unordered?: readonly string[];
|
|
13
|
+
/**
|
|
14
|
+
* Fields that name another declared resource, by dotted path: the `<vendor>.<type>` it is, and
|
|
15
|
+
* how the vendor spells it (`by`): `{ repository_id: { type: "github.repository", by: ["name",
|
|
16
|
+
* "node_id"] } }`. A data file gives that resource there (`repository_id: site`), which stands
|
|
17
|
+
* for its `name`; anywhere else a resource is refused.
|
|
18
|
+
*/
|
|
19
|
+
readonly references?: References;
|
|
20
|
+
}
|
|
21
|
+
/** Where a field names another declared resource: see `ResourceFields.references`. */
|
|
22
|
+
export interface FieldReference {
|
|
23
|
+
/** The `<vendor>.<type>` of the resource it names. */
|
|
24
|
+
readonly type: string;
|
|
25
|
+
/**
|
|
26
|
+
* The fields of that resource's state the vendor may hold here instead of its `name`, such as
|
|
27
|
+
* GitHub's `node_id`: a drift check takes any of their values, read in the same run, as the
|
|
28
|
+
* declared one. Without it, the field holds the resource's `name`, and is compared as it is.
|
|
29
|
+
*/
|
|
30
|
+
readonly by?: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
/** A resource type's `fields.references`: by dotted path, the resource each names. */
|
|
33
|
+
export type References = {
|
|
34
|
+
readonly [path: string]: FieldReference;
|
|
35
|
+
};
|
|
36
|
+
type State = Record<string, unknown>;
|
|
37
|
+
/** A resource type, as its connector declares it with `defineResource`. */
|
|
38
|
+
export interface ResourceSpec<S extends z.ZodObject = z.ZodObject> {
|
|
39
|
+
/** The connector's vendor id, such as `github`; `defineConnector` refuses a type of another vendor. */
|
|
40
|
+
vendor: string;
|
|
41
|
+
/** The type's name within the vendor, such as `repository`: its operations are `<vendor>.<type>.read` and `.import`. */
|
|
42
|
+
type: string;
|
|
43
|
+
/** What people call it, such as "Repository". */
|
|
44
|
+
title: string;
|
|
45
|
+
/**
|
|
46
|
+
* How `find` makes the import id, for people: `name`, `repository_id:pattern`. The fields it
|
|
47
|
+
* names stay declarable even when the vendor sets them (Stripe's `id`).
|
|
48
|
+
*/
|
|
49
|
+
identity: string;
|
|
50
|
+
/** The resource's fields as a read returns them; a data file declares them, less the vendor-owned ones. */
|
|
51
|
+
schema: S;
|
|
52
|
+
fields: ResourceFields;
|
|
53
|
+
/** The vendor's id for a declared resource (the import id), from its declared fields. */
|
|
54
|
+
find: (desired: z.input<S>) => string;
|
|
55
|
+
/**
|
|
56
|
+
* The part of a state that a drift check compares, given what was declared. The check
|
|
57
|
+
* compares `normalize(actual, desired)` with `normalize(desired, desired)`. Default:
|
|
58
|
+
* `compareDeclared`, which keeps the declared fields only.
|
|
59
|
+
*/
|
|
60
|
+
normalize?: (state: State, desired: z.input<S>) => State;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Where a declared resource keeps its brand, its type's `<vendor>.<type>`: a symbol, so a field
|
|
64
|
+
* holding a resource is told from data; a registered one (`Symbol.for`), so a copy of this
|
|
65
|
+
* package reads what another copy declared, as `VENDOR`.
|
|
66
|
+
*/
|
|
67
|
+
export declare const DECLARED: unique symbol;
|
|
68
|
+
/** A resource a data file declares: `github.repository({ name: "sanoma" })`. */
|
|
69
|
+
export interface Declared<Id extends string = string, T = State> {
|
|
70
|
+
readonly [DECLARED]: Id;
|
|
71
|
+
readonly vendor: string;
|
|
72
|
+
readonly type: string;
|
|
73
|
+
/** The vendor's id for it: what `find` made of `desired`. */
|
|
74
|
+
readonly name: string;
|
|
75
|
+
/**
|
|
76
|
+
* Its fields, each resource it names replaced by that resource's `name`: what its type
|
|
77
|
+
* checked, and what a drift check compares.
|
|
78
|
+
*/
|
|
79
|
+
readonly desired: T;
|
|
80
|
+
/**
|
|
81
|
+
* Where it names another resource: the field's dotted path (a list item's with its index,
|
|
82
|
+
* `teams.0`) to that resource's `<vendor>.<type>:<name>`.
|
|
83
|
+
*/
|
|
84
|
+
readonly refs: Readonly<Record<string, string>>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* What a resource type's constructor takes: its fields, where `R` (its `fields.references`)
|
|
88
|
+
* names a path, a declared resource of that type instead of a value (`repository_id: site`).
|
|
89
|
+
*/
|
|
90
|
+
export type Declaring<T, R extends References = {}> = [keyof R] extends [never] ? T : DeclaringAt<T, R, "">;
|
|
91
|
+
type DeclaringAt<T, R extends References, P extends string> = T extends readonly (infer I)[] ? DeclaringAt<I, R, P>[] : T extends object ? {
|
|
92
|
+
[K in keyof T]: K extends string ? DeclaringField<T[K], R, P extends "" ? K : `${P}.${K}`> : T[K];
|
|
93
|
+
} : T;
|
|
94
|
+
type DeclaringField<V, R extends References, P extends string> = P extends keyof R ? V | DeclaredOf<R[P]["type"]> : [Extract<keyof R, `${P}.${string}`>] extends [never] ? V : DeclaringAt<V, R, P>;
|
|
95
|
+
type DeclaredOf<Id> = Id extends string ? Declared<Id> : never;
|
|
96
|
+
declare const opsOf: <S extends z.ZodObject>(schema: S) => {
|
|
97
|
+
import: {
|
|
98
|
+
input: z.ZodObject<{
|
|
99
|
+
id: z.ZodString;
|
|
100
|
+
}, z.core.$strip>;
|
|
101
|
+
output: z.ZodObject<{
|
|
102
|
+
id: z.ZodString;
|
|
103
|
+
gone: z.ZodBoolean;
|
|
104
|
+
state: z.ZodOptional<S>;
|
|
105
|
+
handle: z.ZodOptional<z.ZodString>;
|
|
106
|
+
}, z.core.$strip>;
|
|
107
|
+
};
|
|
108
|
+
read: {
|
|
109
|
+
input: z.ZodObject<{
|
|
110
|
+
id: z.ZodString;
|
|
111
|
+
state: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
112
|
+
handle: z.ZodOptional<z.ZodString>;
|
|
113
|
+
}, z.core.$strip>;
|
|
114
|
+
output: z.ZodObject<{
|
|
115
|
+
id: z.ZodString;
|
|
116
|
+
gone: z.ZodBoolean;
|
|
117
|
+
state: z.ZodOptional<S>;
|
|
118
|
+
handle: z.ZodOptional<z.ZodString>;
|
|
119
|
+
}, z.core.$strip>;
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
type OpsOf<S extends z.ZodObject> = ReturnType<typeof opsOf<S>>;
|
|
123
|
+
/** A resource type's operation specs: `read` and `import`. */
|
|
124
|
+
export type ResourceOps<S extends z.ZodObject> = {
|
|
125
|
+
[N in keyof OpsOf<S>]: OpSpec<OpsOf<S>[N]["input"], OpsOf<S>[N]["output"]>;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* A resource type: call it to declare a resource in a data file, and give it to
|
|
129
|
+
* `defineConnector` as a group, under its `type`: `defineConnector("github", { repository })`.
|
|
130
|
+
*/
|
|
131
|
+
export interface Resource<S extends z.ZodObject = z.ZodObject, Id extends string = string, R extends References = {}> extends Readonly<Required<ResourceSpec<S>>> {
|
|
132
|
+
(desired: Declaring<z.input<S>, R>): Declared<Id, z.input<S>>;
|
|
133
|
+
/** `read` and `import`, each effect `read`, idempotent, with the import id as `target`. */
|
|
134
|
+
readonly ops: ResourceOps<S>;
|
|
135
|
+
readonly [RESOURCE]: true;
|
|
136
|
+
}
|
|
137
|
+
/** True for a plain object: not null, not a list. */
|
|
138
|
+
export declare const isObject: (v: unknown) => v is State;
|
|
139
|
+
/** `path.name`, or `name` at the top: a field's dotted path. */
|
|
140
|
+
export declare const joinPath: (path: string, name: string) => string;
|
|
141
|
+
/**
|
|
142
|
+
* The fields of `state` that `desired` declares, minus vendor-owned and write-only ones: only
|
|
143
|
+
* declared fields are compared, as CloudFormation does. An object is picked field by field, a
|
|
144
|
+
* list item by item against the declared item at its index, and an `unordered` list (a set)
|
|
145
|
+
* item by item against what its declared items declare, then sorted, so its order never
|
|
146
|
+
* drifts. Anything else is kept whole.
|
|
147
|
+
*
|
|
148
|
+
* So a field the data file leaves out is never drift, whatever the vendor holds. That covers
|
|
149
|
+
* attributes the provider marks `computed` and `optional`: they are the user's to set, not
|
|
150
|
+
* vendor-owned, but the vendor fills them when nobody does (GitHub's `etag`, `topics`,
|
|
151
|
+
* `visibility`), so they are compared only when declared.
|
|
152
|
+
*/
|
|
153
|
+
export declare function compareDeclared(fields: ResourceFields, state: State, desired: State): State;
|
|
154
|
+
/** A declared field the vendor holds another value in. */
|
|
155
|
+
export interface DriftField {
|
|
156
|
+
/** Its dotted path, a list item's with its index: `required_pull_request_reviews.0.dismiss_stale_reviews`. */
|
|
157
|
+
path: string;
|
|
158
|
+
/** What the data file declares. */
|
|
159
|
+
desired: unknown;
|
|
160
|
+
/** What the vendor holds, `null` for nothing. */
|
|
161
|
+
actual: unknown;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Where `state` differs from what `desired` declares: the type's `normalize` of each (by default
|
|
165
|
+
* `compareDeclared`: declared fields only, never vendor-owned or write-only ones, `unordered`
|
|
166
|
+
* lists as sets), then leaf by leaf: objects field by field, lists of one length item by item,
|
|
167
|
+
* and anything else (a list whose length changed, an `unordered` one) whole.
|
|
168
|
+
*
|
|
169
|
+
* `accept` is, by a reference's path as `refs` has it (`repository_id`, `teams.0`), the values
|
|
170
|
+
* the vendor may hold there for the resource it names (its `node_id`, say: see
|
|
171
|
+
* `FieldReference.by`): any of them counts as the declared value.
|
|
172
|
+
*/
|
|
173
|
+
export declare function diffDeclared(resource: {
|
|
174
|
+
readonly fields: ResourceFields;
|
|
175
|
+
readonly normalize: (state: State, desired: any) => State;
|
|
176
|
+
}, state: State, desired: State, accept?: Readonly<Record<string, readonly unknown[]>>): DriftField[];
|
|
177
|
+
/**
|
|
178
|
+
* `desired` without the fields the type marks write-only (a webhook's `secret`): what a run may
|
|
179
|
+
* record of a declaration. A list's items lose them too.
|
|
180
|
+
*/
|
|
181
|
+
export declare function withoutWriteOnly(fields: ResourceFields, desired: State): State;
|
|
182
|
+
/**
|
|
183
|
+
* Declares a resource type: its schema, flagged fields, identity and how it is compared, and
|
|
184
|
+
* derives its operations, `<vendor>.<type>.read` and `<vendor>.<type>.import`, both effect
|
|
185
|
+
* `read` and idempotent, with the import id as the policy's `target`:
|
|
186
|
+
*
|
|
187
|
+
* - `import` takes `{ id }`, the import id, and returns `{ gone: false, state }`, the object's
|
|
188
|
+
* state (with the driver's opaque `handle` when it keeps one), or `{ gone: true }` when the
|
|
189
|
+
* vendor has no such object.
|
|
190
|
+
* - `read` takes `{ id, state?, handle? }`, a state from an earlier `import` or `read`, and
|
|
191
|
+
* returns the same: `{ gone: true }` when the object no longer exists, else its fresh
|
|
192
|
+
* `state`. A driver may import first when it is given no state.
|
|
193
|
+
*
|
|
194
|
+
* The ledger records a `handle` as `"<handle>"`, never its bytes (the operations' `opaque`).
|
|
195
|
+
*
|
|
196
|
+
* Calling the result declares one resource, for a data file: `repository({ name: "sanoma" })`
|
|
197
|
+
* is a `Declared`, `{ vendor, type, name: "sanoma", desired, refs: {} }`. It refuses fields the
|
|
198
|
+
* schema does not have, fields the vendor owns (unless the identity names them), values the
|
|
199
|
+
* schema rejects, and an empty identity. Where `fields.references` allows it, a field may name
|
|
200
|
+
* another declared resource of that type (`repository_id: site`): `desired` holds, and the
|
|
201
|
+
* schema and `find` read, that resource's `name`, and `refs` says where it was named.
|
|
202
|
+
*/
|
|
203
|
+
export declare function defineResource<S extends z.ZodObject, const V extends string, const T extends string, const R extends References = {}>(spec: ResourceSpec<S> & {
|
|
204
|
+
vendor: V;
|
|
205
|
+
type: T;
|
|
206
|
+
fields: {
|
|
207
|
+
references?: R;
|
|
208
|
+
};
|
|
209
|
+
}): Resource<S, `${V}.${T}`, R>;
|
|
210
|
+
/** Every resource type the connectors declare, by `<vendor>.<type>`. */
|
|
211
|
+
export declare function resourceTypesOf(connectors: readonly Connector<any, any>[]): Map<string, Resource>;
|
|
212
|
+
export {};
|