alchemy 2.0.0-beta.48 → 2.0.0-beta.49
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/bin/exec.js +28 -5
- package/bin/exec.js.map +1 -1
- package/lib/AdoptPolicy.js +1 -1
- package/lib/AdoptPolicy.js.map +1 -1
- package/lib/Binding.d.ts +5 -1
- package/lib/Binding.d.ts.map +1 -1
- package/lib/Binding.js.map +1 -1
- package/lib/Cloudflare/AiGateway/AiGateway.d.ts +1 -1
- package/lib/Cloudflare/ApiToken/Common.d.ts +10 -1
- package/lib/Cloudflare/ApiToken/Common.d.ts.map +1 -1
- package/lib/Cloudflare/ApiToken/Common.js.map +1 -1
- package/lib/Cloudflare/Dns/DnsBinding.d.ts +52 -0
- package/lib/Cloudflare/Dns/DnsBinding.d.ts.map +1 -0
- package/lib/Cloudflare/Dns/DnsBinding.js +59 -0
- package/lib/Cloudflare/Dns/DnsBinding.js.map +1 -0
- package/lib/Cloudflare/Dns/DnsRead.d.ts +80 -0
- package/lib/Cloudflare/Dns/DnsRead.d.ts.map +1 -0
- package/lib/Cloudflare/Dns/DnsRead.js +70 -0
- package/lib/Cloudflare/Dns/DnsRead.js.map +1 -0
- package/lib/Cloudflare/Dns/DnsReadWrite.d.ts +79 -0
- package/lib/Cloudflare/Dns/DnsReadWrite.d.ts.map +1 -0
- package/lib/Cloudflare/Dns/DnsReadWrite.js +72 -0
- package/lib/Cloudflare/Dns/DnsReadWrite.js.map +1 -0
- package/lib/Cloudflare/Dns/DnsWrite.d.ts +113 -0
- package/lib/Cloudflare/Dns/DnsWrite.d.ts.map +1 -0
- package/lib/Cloudflare/Dns/DnsWrite.js +100 -0
- package/lib/Cloudflare/Dns/DnsWrite.js.map +1 -0
- package/lib/Cloudflare/Dns/index.d.ts +5 -0
- package/lib/Cloudflare/Dns/index.d.ts.map +1 -0
- package/lib/Cloudflare/Dns/index.js +5 -0
- package/lib/Cloudflare/Dns/index.js.map +1 -0
- package/lib/Cloudflare/Hyperdrive/Hyperdrive.d.ts +1 -1
- package/lib/Cloudflare/KV/KVNamespace.d.ts +1 -1
- package/lib/Cloudflare/Providers.d.ts.map +1 -1
- package/lib/Cloudflare/Providers.js +5 -1
- package/lib/Cloudflare/Providers.js.map +1 -1
- package/lib/Cloudflare/Queue/Queue.d.ts +1 -1
- package/lib/Cloudflare/R2/R2Bucket.d.ts +1 -1
- package/lib/Cloudflare/SecretsStore/Secret.d.ts +1 -1
- package/lib/Cloudflare/StateStore/Api.d.ts +1 -1
- package/lib/Cloudflare/Vectorize/VectorizeIndex.d.ts +1 -1
- package/lib/Cloudflare/Workers/WorkerBinding.d.ts +2 -2
- package/lib/Cloudflare/index.d.ts +1 -0
- package/lib/Cloudflare/index.d.ts.map +1 -1
- package/lib/Cloudflare/index.js +1 -0
- package/lib/Cloudflare/index.js.map +1 -1
- package/lib/Output.d.ts +13 -1
- package/lib/Output.d.ts.map +1 -1
- package/lib/Output.js +43 -11
- package/lib/Output.js.map +1 -1
- package/lib/Plan.d.ts.map +1 -1
- package/lib/Plan.js +8 -0
- package/lib/Plan.js.map +1 -1
- package/lib/Platform.d.ts.map +1 -1
- package/lib/Platform.js +5 -2
- package/lib/Platform.js.map +1 -1
- package/lib/Resource.d.ts +1 -1
- package/lib/Resource.d.ts.map +1 -1
- package/lib/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/AdoptPolicy.ts +1 -1
- package/src/Binding.ts +5 -1
- package/src/Cloudflare/ApiToken/Common.ts +13 -3
- package/src/Cloudflare/Dns/DnsBinding.ts +123 -0
- package/src/Cloudflare/Dns/DnsRead.ts +129 -0
- package/src/Cloudflare/Dns/DnsReadWrite.ts +99 -0
- package/src/Cloudflare/Dns/DnsWrite.ts +209 -0
- package/src/Cloudflare/Dns/index.ts +4 -0
- package/src/Cloudflare/Providers.ts +7 -0
- package/src/Cloudflare/index.ts +1 -0
- package/src/Output.ts +67 -11
- package/src/Plan.ts +9 -0
- package/src/Platform.ts +11 -3
- package/src/Resource.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alchemy",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
4
4
|
"homepage": "https://alchemy.run",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Sam Goodwin <sam@alchemy.run>",
|
|
@@ -291,15 +291,15 @@
|
|
|
291
291
|
"@alchemy.run/node-utils": "0.0.4",
|
|
292
292
|
"@aws-sdk/credential-providers": "^3.0.0",
|
|
293
293
|
"@clack/prompts": "^0.11.0",
|
|
294
|
-
"@distilled.cloud/aws": "^0.22.
|
|
295
|
-
"@distilled.cloud/axiom": "^0.22.
|
|
296
|
-
"@distilled.cloud/cloudflare": "^0.22.
|
|
294
|
+
"@distilled.cloud/aws": "^0.22.4",
|
|
295
|
+
"@distilled.cloud/axiom": "^0.22.4",
|
|
296
|
+
"@distilled.cloud/cloudflare": "^0.22.4",
|
|
297
297
|
"@distilled.cloud/cloudflare-rolldown-plugin": "0.10.1",
|
|
298
298
|
"@distilled.cloud/cloudflare-runtime": "0.10.1",
|
|
299
299
|
"@distilled.cloud/cloudflare-vite-plugin": "0.10.1",
|
|
300
|
-
"@distilled.cloud/core": "^0.22.
|
|
301
|
-
"@distilled.cloud/neon": "^0.22.
|
|
302
|
-
"@distilled.cloud/planetscale": "^0.22.
|
|
300
|
+
"@distilled.cloud/core": "^0.22.4",
|
|
301
|
+
"@distilled.cloud/neon": "^0.22.4",
|
|
302
|
+
"@distilled.cloud/planetscale": "^0.22.4",
|
|
303
303
|
"@effect/vitest": ">=4.0.0-beta.74 || >=4.0.0",
|
|
304
304
|
"@libsql/client": "^0.17.0",
|
|
305
305
|
"@octokit/rest": "^22.0.1",
|
package/src/AdoptPolicy.ts
CHANGED
|
@@ -139,7 +139,7 @@ export const adopt: {
|
|
|
139
139
|
): <A, E, R2 = never>(
|
|
140
140
|
effect: Effect.Effect<A, E, R2>,
|
|
141
141
|
) => Effect.Effect<A, E, R1 | R2>;
|
|
142
|
-
} = ((enabled: boolean | Effect.Effect<boolean, never, any>) =>
|
|
142
|
+
} = ((enabled: boolean | Effect.Effect<boolean, never, any> = true) =>
|
|
143
143
|
(eff: Effect.Effect<any, any, any>) =>
|
|
144
144
|
eff.pipe(
|
|
145
145
|
typeof enabled === "boolean"
|
package/src/Binding.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
|
|
|
3
3
|
import * as Layer from "effect/Layer";
|
|
4
4
|
import * as Option from "effect/Option";
|
|
5
5
|
import { SingleShotGen } from "effect/Utils";
|
|
6
|
+
import type { Input } from "./Input.ts";
|
|
6
7
|
import * as Namespace from "./Namespace.ts";
|
|
7
8
|
import { ALCHEMY_PHASE } from "./Phase.ts";
|
|
8
9
|
import { tryFindProviderByType } from "./Provider.ts";
|
|
@@ -42,7 +43,10 @@ type BindParameters<
|
|
|
42
43
|
Parameters extends any[],
|
|
43
44
|
Req = never,
|
|
44
45
|
> = Parameters extends [infer First, ...infer Rest]
|
|
45
|
-
? [
|
|
46
|
+
? [
|
|
47
|
+
Input<First> | Effect.Effect<First, never, Req>,
|
|
48
|
+
...BindParameters<Rest, Req>,
|
|
49
|
+
]
|
|
46
50
|
: [];
|
|
47
51
|
|
|
48
52
|
/**
|
|
@@ -27,10 +27,20 @@ export type ApiTokenPermissionGroupRef =
|
|
|
27
27
|
| PermissionGroupName
|
|
28
28
|
| { id: string; meta?: { key?: string; value?: string } };
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Value of a resource entry in an {@link ApiTokenPolicy}. Usually `"*"`, but
|
|
32
|
+
* account-owned tokens must nest zone resources under the account resource —
|
|
33
|
+
* e.g. `{ "com.cloudflare.api.account.<id>": { "com.cloudflare.api.account.zone.*": "*" } }`
|
|
34
|
+
* — so a nested object is also allowed.
|
|
35
|
+
*/
|
|
36
|
+
export type ApiTokenResourceScope =
|
|
37
|
+
| string
|
|
38
|
+
| { [K in ApiTokenResourceKey]?: string };
|
|
39
|
+
|
|
30
40
|
export interface ApiTokenPolicy {
|
|
31
41
|
effect: "allow" | "deny";
|
|
32
42
|
permissionGroups: ApiTokenPermissionGroupRef[];
|
|
33
|
-
resources: { [K in ApiTokenResourceKey]?:
|
|
43
|
+
resources: { [K in ApiTokenResourceKey]?: ApiTokenResourceScope };
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
export interface ApiTokenCondition {
|
|
@@ -116,8 +126,8 @@ export const resolvePermissionGroup = (ref: ApiTokenPermissionGroupRef) => {
|
|
|
116
126
|
|
|
117
127
|
const resolveResources = (
|
|
118
128
|
resources: ApiTokenPolicy["resources"],
|
|
119
|
-
): Record<string,
|
|
120
|
-
const out: Record<string,
|
|
129
|
+
): Record<string, ApiTokenResourceScope> => {
|
|
130
|
+
const out: Record<string, ApiTokenResourceScope> = {};
|
|
121
131
|
for (const [key, value] of Object.entries(resources)) {
|
|
122
132
|
if (value === undefined) continue;
|
|
123
133
|
out[key] = value;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Credentials,
|
|
3
|
+
fromApiToken,
|
|
4
|
+
} from "@distilled.cloud/cloudflare/Credentials";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Layer from "effect/Layer";
|
|
7
|
+
import * as Redacted from "effect/Redacted";
|
|
8
|
+
import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient";
|
|
9
|
+
import { type HttpClient } from "effect/unstable/http/HttpClient";
|
|
10
|
+
import type * as Binding from "../../Binding.ts";
|
|
11
|
+
import * as Output from "../../Output.ts";
|
|
12
|
+
import { RuntimeContext } from "../../RuntimeContext.ts";
|
|
13
|
+
import { AccountApiToken } from "../ApiToken/AccountApiToken.ts";
|
|
14
|
+
import type { ApiTokenPermissionGroupRef } from "../ApiToken/Common.ts";
|
|
15
|
+
import type { Zone } from "../Zone/Zone.ts";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Runtime accessor for a DNS binding's token, obtained by binding the
|
|
19
|
+
* {@link AccountApiToken}'s `value` output in the Worker's Init phase. Reads the
|
|
20
|
+
* value back from the Worker's environment at runtime. DNS record operations
|
|
21
|
+
* are zone-scoped (the `zoneId` is passed per call), so the account id is not
|
|
22
|
+
* needed at runtime.
|
|
23
|
+
*/
|
|
24
|
+
export interface DnsToken {
|
|
25
|
+
/** The token's plaintext value (injected as a `secret_text` binding). */
|
|
26
|
+
value: Effect.Effect<Redacted.Redacted<string>>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Bind an {@link AccountApiToken}'s `value` output into the Worker (as a
|
|
31
|
+
* `secret_text` binding) and return the {@link DnsToken} accessor.
|
|
32
|
+
*/
|
|
33
|
+
export const bindDnsToken = (token: AccountApiToken) =>
|
|
34
|
+
Effect.gen(function* () {
|
|
35
|
+
const value = yield* token.value;
|
|
36
|
+
return { value } satisfies DnsToken;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve credentials from a bound token and provide them (plus the
|
|
41
|
+
* fetch-based HTTP client) to a raw SDK operation.
|
|
42
|
+
*/
|
|
43
|
+
export const authorizeDns =
|
|
44
|
+
(token: DnsToken) =>
|
|
45
|
+
<A, E>(
|
|
46
|
+
eff: Effect.Effect<A, E, Credentials | HttpClient>,
|
|
47
|
+
): Effect.Effect<A, E, RuntimeContext> =>
|
|
48
|
+
token.value.pipe(
|
|
49
|
+
Effect.flatMap((value) =>
|
|
50
|
+
eff.pipe(
|
|
51
|
+
Effect.provide(
|
|
52
|
+
fromApiToken({ apiToken: Redacted.value(value) }).pipe(
|
|
53
|
+
Layer.provideMerge(FetchHttpClient.layer),
|
|
54
|
+
),
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Shared runtime body for a DNS binding: create a token scoped to the requested
|
|
62
|
+
* zone, attach the (narrow) policy, bind the token's value and the zone's id
|
|
63
|
+
* into the Worker, then build the client. Pass the result to
|
|
64
|
+
* `Layer.effect(<Binding>, ...)`.
|
|
65
|
+
*
|
|
66
|
+
* The zone's `zoneId` is bound into the Worker at init time, so the resulting
|
|
67
|
+
* client closes over it and callers never pass it per request — the
|
|
68
|
+
* provisioned token only grants access to that one zone anyway.
|
|
69
|
+
*/
|
|
70
|
+
export const makeDnsClient = <C>(
|
|
71
|
+
Policy: Binding.Policy<
|
|
72
|
+
any,
|
|
73
|
+
any,
|
|
74
|
+
(token: AccountApiToken, zone: Zone) => Effect.Effect<void>
|
|
75
|
+
>,
|
|
76
|
+
tokenId: string,
|
|
77
|
+
makeClient: (token: DnsToken, zoneId: Effect.Effect<string>) => C,
|
|
78
|
+
) =>
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const Token = yield* AccountApiToken;
|
|
81
|
+
const attach = yield* Policy;
|
|
82
|
+
|
|
83
|
+
return Effect.fn(function* (zone: Zone) {
|
|
84
|
+
const token = yield* Token(tokenId);
|
|
85
|
+
yield* attach(token, zone);
|
|
86
|
+
const zoneId = yield* zone.zoneId;
|
|
87
|
+
return makeClient(yield* bindDnsToken(token), zoneId);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Build the deploy-time policy layer for a DNS binding: attach an allow policy
|
|
93
|
+
* with the given permission groups, scoped to the requested zone
|
|
94
|
+
* (`com.cloudflare.api.account.zone.<zoneId>`).
|
|
95
|
+
*/
|
|
96
|
+
export const makeDnsPolicyLive = <Self, Id extends string>(
|
|
97
|
+
Policy: Binding.Policy<
|
|
98
|
+
Self,
|
|
99
|
+
Id,
|
|
100
|
+
(token: AccountApiToken, zone: Zone) => Effect.Effect<void>
|
|
101
|
+
>,
|
|
102
|
+
sid: string,
|
|
103
|
+
permissionGroups: ApiTokenPermissionGroupRef[],
|
|
104
|
+
) =>
|
|
105
|
+
Policy.layer.succeed((_host, token, zone) =>
|
|
106
|
+
token.bind`${sid}(${zone})`({
|
|
107
|
+
policies: [
|
|
108
|
+
{
|
|
109
|
+
effect: "allow",
|
|
110
|
+
permissionGroups,
|
|
111
|
+
resources: zone.zoneId.pipe(
|
|
112
|
+
Output.flatMap(
|
|
113
|
+
(zoneId) =>
|
|
114
|
+
Output.interpolate`com.cloudflare.api.account.zone.${zoneId}`,
|
|
115
|
+
),
|
|
116
|
+
Output.map((zoneId) => ({
|
|
117
|
+
[zoneId]: "*",
|
|
118
|
+
})),
|
|
119
|
+
),
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GetRecordError,
|
|
3
|
+
GetRecordResponse,
|
|
4
|
+
ListRecordsError,
|
|
5
|
+
ListRecordsRequest,
|
|
6
|
+
ListRecordsResponse,
|
|
7
|
+
} from "@distilled.cloud/cloudflare/dns";
|
|
8
|
+
import * as dns from "@distilled.cloud/cloudflare/dns";
|
|
9
|
+
import * as Effect from "effect/Effect";
|
|
10
|
+
import * as Layer from "effect/Layer";
|
|
11
|
+
import * as Binding from "../../Binding.ts";
|
|
12
|
+
import type { RuntimeContext } from "../../RuntimeContext.ts";
|
|
13
|
+
import type { AccountApiToken } from "../ApiToken/AccountApiToken.ts";
|
|
14
|
+
import type { Zone } from "../Zone/Zone.ts";
|
|
15
|
+
import {
|
|
16
|
+
authorizeDns,
|
|
17
|
+
type DnsToken,
|
|
18
|
+
makeDnsClient,
|
|
19
|
+
makeDnsPolicyLive,
|
|
20
|
+
} from "./DnsBinding.ts";
|
|
21
|
+
|
|
22
|
+
/** List-records request, minus the zone id (bound at `.bind(zone)` time). */
|
|
23
|
+
export type ListRecordsRequestInput = Omit<ListRecordsRequest, "zoneId">;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read-only DNS record operations. Backed by the `DNS Read` permission group.
|
|
27
|
+
* The zone is fixed when the client is bound, so no `zoneId` is passed per call.
|
|
28
|
+
*/
|
|
29
|
+
export interface DnsReadClient {
|
|
30
|
+
/** Fetch a single DNS record by id. */
|
|
31
|
+
getDnsRecord(
|
|
32
|
+
dnsRecordId: string,
|
|
33
|
+
): Effect.Effect<GetRecordResponse, GetRecordError, RuntimeContext>;
|
|
34
|
+
/** List the DNS records in the bound zone. */
|
|
35
|
+
listDnsRecords(
|
|
36
|
+
request?: ListRecordsRequestInput,
|
|
37
|
+
): Effect.Effect<ListRecordsResponse, ListRecordsError, RuntimeContext>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Build the read-only client over a bound token and zone id. */
|
|
41
|
+
export const dnsReadClient = (
|
|
42
|
+
token: DnsToken,
|
|
43
|
+
zoneId: Effect.Effect<string>,
|
|
44
|
+
): DnsReadClient => {
|
|
45
|
+
const authorize = authorizeDns(token);
|
|
46
|
+
return {
|
|
47
|
+
getDnsRecord: Effect.fn("Cloudflare.Dns.getDnsRecord")(
|
|
48
|
+
function* (dnsRecordId) {
|
|
49
|
+
return yield* authorize(
|
|
50
|
+
dns.getRecord({ zoneId: yield* zoneId, dnsRecordId }),
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
),
|
|
54
|
+
listDnsRecords: Effect.fn("Cloudflare.Dns.listDnsRecords")(
|
|
55
|
+
function* (request) {
|
|
56
|
+
return yield* authorize(
|
|
57
|
+
dns.listRecords({ zoneId: yield* zoneId, ...request }),
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Binding that lets a Worker read Cloudflare DNS records at runtime.
|
|
66
|
+
*
|
|
67
|
+
* Creates a least-privilege {@link AccountApiToken} with only the `DNS Read`
|
|
68
|
+
* permission, scoped to the single zone passed to `bind`, and binds its value
|
|
69
|
+
* into the Worker so runtime code can authenticate.
|
|
70
|
+
*
|
|
71
|
+
* @binding
|
|
72
|
+
*
|
|
73
|
+
* @section Reading DNS records at runtime
|
|
74
|
+
* @example Read records from inside a Worker
|
|
75
|
+
* Bind the client in the Worker's Init phase and provide {@link DnsReadLive}.
|
|
76
|
+
* The zone is fixed by `.bind(zone)` — the provisioned token only grants
|
|
77
|
+
* access to that zone, so calls take no `zoneId`. Pass the {@link Zone}
|
|
78
|
+
* resource directly (it's an `Effect`), or `yield* Zone` for a resolved value.
|
|
79
|
+
* ```typescript
|
|
80
|
+
* import * as Cloudflare from "alchemy/Cloudflare";
|
|
81
|
+
* import * as Effect from "effect/Effect";
|
|
82
|
+
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
|
83
|
+
*
|
|
84
|
+
* const Zone = Cloudflare.Zone("MyZone", { name: "example.com" });
|
|
85
|
+
*
|
|
86
|
+
* export class DnsReaderWorker extends Cloudflare.Worker<DnsReaderWorker>()(
|
|
87
|
+
* "DnsReaderWorker",
|
|
88
|
+
* { main: import.meta.filename },
|
|
89
|
+
* Effect.gen(function* () {
|
|
90
|
+
* // Init phase — bind the read client scoped to the zone.
|
|
91
|
+
* const dns = yield* Cloudflare.DnsRead.bind(Zone);
|
|
92
|
+
*
|
|
93
|
+
* return {
|
|
94
|
+
* fetch: Effect.gen(function* () {
|
|
95
|
+
* const { result } = yield* dns.listDnsRecords({ type: "A" });
|
|
96
|
+
* const record = yield* dns.getDnsRecord(result[0].id);
|
|
97
|
+
* return yield* HttpServerResponse.json({ id: record.id });
|
|
98
|
+
* }),
|
|
99
|
+
* };
|
|
100
|
+
* }).pipe(Effect.provide(Cloudflare.DnsReadLive)),
|
|
101
|
+
* ) {}
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export class DnsRead extends Binding.Service<
|
|
105
|
+
DnsRead,
|
|
106
|
+
(zone: Zone) => Effect.Effect<DnsReadClient>
|
|
107
|
+
>()("Cloudflare.DnsRead") {}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Deploy-time policy for {@link DnsRead}. Attaches the `DNS Read` permission to
|
|
111
|
+
* the token via its binding contract.
|
|
112
|
+
*/
|
|
113
|
+
export class DnsReadPolicy extends Binding.Policy<
|
|
114
|
+
DnsReadPolicy,
|
|
115
|
+
(token: AccountApiToken, zone: Zone) => Effect.Effect<void>
|
|
116
|
+
>()("Cloudflare.DnsRead") {}
|
|
117
|
+
|
|
118
|
+
/** Runtime layer for {@link DnsRead}. */
|
|
119
|
+
export const DnsReadLive = Layer.effect(
|
|
120
|
+
DnsRead,
|
|
121
|
+
makeDnsClient(DnsReadPolicy, "DnsReadToken", dnsReadClient),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
/** Live deploy-time policy layer for {@link DnsReadPolicy}. */
|
|
125
|
+
export const DnsReadPolicyLive = makeDnsPolicyLive(
|
|
126
|
+
DnsReadPolicy,
|
|
127
|
+
"Cloudflare.DnsRead",
|
|
128
|
+
["DNS Read"],
|
|
129
|
+
);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as Binding from "../../Binding.ts";
|
|
4
|
+
import type { AccountApiToken } from "../ApiToken/AccountApiToken.ts";
|
|
5
|
+
import type { Zone } from "../Zone/Zone.ts";
|
|
6
|
+
import {
|
|
7
|
+
type DnsToken,
|
|
8
|
+
makeDnsClient,
|
|
9
|
+
makeDnsPolicyLive,
|
|
10
|
+
} from "./DnsBinding.ts";
|
|
11
|
+
import { dnsReadClient, type DnsReadClient } from "./DnsRead.ts";
|
|
12
|
+
import { dnsWriteClient, type DnsWriteClient } from "./DnsWrite.ts";
|
|
13
|
+
|
|
14
|
+
/** Combined read + write DNS record operations. */
|
|
15
|
+
export interface DnsReadWriteClient extends DnsReadClient, DnsWriteClient {}
|
|
16
|
+
|
|
17
|
+
/** Build the combined read + write client over a bound token and zone id. */
|
|
18
|
+
export const dnsReadWriteClient = (
|
|
19
|
+
token: DnsToken,
|
|
20
|
+
zoneId: Effect.Effect<string>,
|
|
21
|
+
): DnsReadWriteClient => ({
|
|
22
|
+
...dnsReadClient(token, zoneId),
|
|
23
|
+
...dnsWriteClient(token, zoneId),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Binding that lets a Worker perform the full Cloudflare DNS record CRUD
|
|
28
|
+
* surface at runtime.
|
|
29
|
+
*
|
|
30
|
+
* Creates a least-privilege {@link AccountApiToken} with both the `DNS Read`
|
|
31
|
+
* and `DNS Write` permissions, scoped to the single zone passed to `bind`, and
|
|
32
|
+
* binds its value into the Worker so runtime code can authenticate.
|
|
33
|
+
*
|
|
34
|
+
* @binding
|
|
35
|
+
*
|
|
36
|
+
* @section Managing DNS records at runtime
|
|
37
|
+
* @example Full CRUD from inside a Worker
|
|
38
|
+
* Bind the client in the Worker's Init phase and provide
|
|
39
|
+
* {@link DnsReadWriteLive}. The zone is fixed by `.bind(zone)` — the
|
|
40
|
+
* provisioned token only grants access to that zone, so calls take no
|
|
41
|
+
* `zoneId`. Pass the {@link Zone} resource directly (it's an `Effect`), or
|
|
42
|
+
* `yield* Zone` for a resolved value.
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import * as Cloudflare from "alchemy/Cloudflare";
|
|
45
|
+
* import * as Effect from "effect/Effect";
|
|
46
|
+
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
|
47
|
+
*
|
|
48
|
+
* const Zone = Cloudflare.Zone("MyZone", { name: "example.com" });
|
|
49
|
+
*
|
|
50
|
+
* export class DnsWorker extends Cloudflare.Worker<DnsWorker>()(
|
|
51
|
+
* "DnsWorker",
|
|
52
|
+
* { main: import.meta.filename },
|
|
53
|
+
* Effect.gen(function* () {
|
|
54
|
+
* // Init phase — bind the full CRUD client scoped to the zone.
|
|
55
|
+
* const dns = yield* Cloudflare.DnsReadWrite.bind(Zone);
|
|
56
|
+
*
|
|
57
|
+
* return {
|
|
58
|
+
* fetch: Effect.gen(function* () {
|
|
59
|
+
* const { result } = yield* dns.createDnsRecord({
|
|
60
|
+
* type: "A",
|
|
61
|
+
* name: "app.example.com",
|
|
62
|
+
* content: "192.0.2.1",
|
|
63
|
+
* ttl: 1,
|
|
64
|
+
* });
|
|
65
|
+
* const record = yield* dns.getDnsRecord(result.id);
|
|
66
|
+
* yield* dns.deleteDnsRecord(result.id);
|
|
67
|
+
* return yield* HttpServerResponse.json({ id: record.id });
|
|
68
|
+
* }),
|
|
69
|
+
* };
|
|
70
|
+
* }).pipe(Effect.provide(Cloudflare.DnsReadWriteLive)),
|
|
71
|
+
* ) {}
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export class DnsReadWrite extends Binding.Service<
|
|
75
|
+
DnsReadWrite,
|
|
76
|
+
(zone: Zone) => Effect.Effect<DnsReadWriteClient>
|
|
77
|
+
>()("Cloudflare.DnsReadWrite") {}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Deploy-time policy for {@link DnsReadWrite}. Attaches both the `DNS Read` and
|
|
81
|
+
* `DNS Write` permissions to the token via its binding contract.
|
|
82
|
+
*/
|
|
83
|
+
export class DnsReadWritePolicy extends Binding.Policy<
|
|
84
|
+
DnsReadWritePolicy,
|
|
85
|
+
(token: AccountApiToken, zone: Zone) => Effect.Effect<void>
|
|
86
|
+
>()("Cloudflare.DnsReadWrite") {}
|
|
87
|
+
|
|
88
|
+
/** Runtime layer for {@link DnsReadWrite}. */
|
|
89
|
+
export const DnsReadWriteLive = Layer.effect(
|
|
90
|
+
DnsReadWrite,
|
|
91
|
+
makeDnsClient(DnsReadWritePolicy, "DnsReadWriteToken", dnsReadWriteClient),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
/** Live deploy-time policy layer for {@link DnsReadWritePolicy}. */
|
|
95
|
+
export const DnsReadWritePolicyLive = makeDnsPolicyLive(
|
|
96
|
+
DnsReadWritePolicy,
|
|
97
|
+
"Cloudflare.DnsReadWrite",
|
|
98
|
+
["DNS Read", "DNS Write"],
|
|
99
|
+
);
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BatchRecordError,
|
|
3
|
+
BatchRecordRequest,
|
|
4
|
+
BatchRecordResponse,
|
|
5
|
+
CreateRecordError,
|
|
6
|
+
CreateRecordRequest,
|
|
7
|
+
CreateRecordResponse,
|
|
8
|
+
DeleteRecordError,
|
|
9
|
+
DeleteRecordResponse,
|
|
10
|
+
PatchRecordError,
|
|
11
|
+
PatchRecordRequest,
|
|
12
|
+
PatchRecordResponse,
|
|
13
|
+
UpdateRecordError,
|
|
14
|
+
UpdateRecordRequest,
|
|
15
|
+
UpdateRecordResponse,
|
|
16
|
+
} from "@distilled.cloud/cloudflare/dns";
|
|
17
|
+
import * as dns from "@distilled.cloud/cloudflare/dns";
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import * as Layer from "effect/Layer";
|
|
20
|
+
import * as Binding from "../../Binding.ts";
|
|
21
|
+
import type { RuntimeContext } from "../../RuntimeContext.ts";
|
|
22
|
+
import type { AccountApiToken } from "../ApiToken/AccountApiToken.ts";
|
|
23
|
+
import type { Zone } from "../Zone/Zone.ts";
|
|
24
|
+
import {
|
|
25
|
+
authorizeDns,
|
|
26
|
+
type DnsToken,
|
|
27
|
+
makeDnsClient,
|
|
28
|
+
makeDnsPolicyLive,
|
|
29
|
+
} from "./DnsBinding.ts";
|
|
30
|
+
|
|
31
|
+
/** Create-record request, minus the zone id (bound at `.bind(zone)` time). */
|
|
32
|
+
export type CreateRecordRequestInput = Omit<CreateRecordRequest, "zoneId">;
|
|
33
|
+
|
|
34
|
+
/** Update-record request, minus the zone id and record id. */
|
|
35
|
+
export type UpdateRecordRequestInput = Omit<
|
|
36
|
+
UpdateRecordRequest,
|
|
37
|
+
"zoneId" | "dnsRecordId"
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
/** Patch-record request, minus the zone id and record id. */
|
|
41
|
+
export type PatchRecordRequestInput = Omit<
|
|
42
|
+
PatchRecordRequest,
|
|
43
|
+
"zoneId" | "dnsRecordId"
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
/** Batch-records request, minus the zone id (bound at `.bind(zone)` time). */
|
|
47
|
+
export type BatchRecordRequestInput = Omit<BatchRecordRequest, "zoneId">;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mutating DNS record operations. Backed by the `DNS Write` permission group.
|
|
51
|
+
* The zone is fixed when the client is bound, so no `zoneId` is passed per call.
|
|
52
|
+
*/
|
|
53
|
+
export interface DnsWriteClient {
|
|
54
|
+
/** Create a DNS record. */
|
|
55
|
+
createDnsRecord(
|
|
56
|
+
request: CreateRecordRequestInput,
|
|
57
|
+
): Effect.Effect<CreateRecordResponse, CreateRecordError, RuntimeContext>;
|
|
58
|
+
/** Overwrite (PUT) a DNS record. */
|
|
59
|
+
updateDnsRecord(
|
|
60
|
+
dnsRecordId: string,
|
|
61
|
+
request: UpdateRecordRequestInput,
|
|
62
|
+
): Effect.Effect<UpdateRecordResponse, UpdateRecordError, RuntimeContext>;
|
|
63
|
+
/** Partially update (PATCH) a DNS record. */
|
|
64
|
+
patchDnsRecord(
|
|
65
|
+
dnsRecordId: string,
|
|
66
|
+
request: PatchRecordRequestInput,
|
|
67
|
+
): Effect.Effect<PatchRecordResponse, PatchRecordError, RuntimeContext>;
|
|
68
|
+
/** Delete a DNS record by id. */
|
|
69
|
+
deleteDnsRecord(
|
|
70
|
+
dnsRecordId: string,
|
|
71
|
+
): Effect.Effect<DeleteRecordResponse, DeleteRecordError, RuntimeContext>;
|
|
72
|
+
/** Apply a batch of create / update / patch / delete operations atomically. */
|
|
73
|
+
batchDnsRecords(
|
|
74
|
+
request: BatchRecordRequestInput,
|
|
75
|
+
): Effect.Effect<BatchRecordResponse, BatchRecordError, RuntimeContext>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Build the write client over a bound token and zone id. */
|
|
79
|
+
export const dnsWriteClient = (
|
|
80
|
+
token: DnsToken,
|
|
81
|
+
zoneId: Effect.Effect<string>,
|
|
82
|
+
): DnsWriteClient => {
|
|
83
|
+
const authorize = authorizeDns(token);
|
|
84
|
+
return {
|
|
85
|
+
createDnsRecord: Effect.fn("Cloudflare.Dns.createDnsRecord")(
|
|
86
|
+
function* (request) {
|
|
87
|
+
return yield* authorize(
|
|
88
|
+
dns.createRecord({ zoneId: yield* zoneId, ...request }),
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
),
|
|
92
|
+
updateDnsRecord: Effect.fn("Cloudflare.Dns.updateDnsRecord")(
|
|
93
|
+
function* (dnsRecordId, request) {
|
|
94
|
+
return yield* authorize(
|
|
95
|
+
dns.updateRecord({ zoneId: yield* zoneId, dnsRecordId, ...request }),
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
),
|
|
99
|
+
patchDnsRecord: Effect.fn("Cloudflare.Dns.patchDnsRecord")(
|
|
100
|
+
function* (dnsRecordId, request) {
|
|
101
|
+
return yield* authorize(
|
|
102
|
+
dns.patchRecord({ zoneId: yield* zoneId, dnsRecordId, ...request }),
|
|
103
|
+
);
|
|
104
|
+
},
|
|
105
|
+
),
|
|
106
|
+
deleteDnsRecord: Effect.fn("Cloudflare.Dns.deleteDnsRecord")(
|
|
107
|
+
function* (dnsRecordId) {
|
|
108
|
+
return yield* authorize(
|
|
109
|
+
dns.deleteRecord({ zoneId: yield* zoneId, dnsRecordId }),
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
),
|
|
113
|
+
batchDnsRecords: Effect.fn("Cloudflare.Dns.batchDnsRecords")(
|
|
114
|
+
function* (request) {
|
|
115
|
+
return yield* authorize(
|
|
116
|
+
dns.batchRecord({ zoneId: yield* zoneId, ...request }),
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Binding that lets a Worker create, update, and delete Cloudflare DNS records
|
|
125
|
+
* at runtime.
|
|
126
|
+
*
|
|
127
|
+
* Creates a least-privilege {@link AccountApiToken} with only the `DNS Write`
|
|
128
|
+
* permission, scoped to the single zone passed to `bind`, and binds its value
|
|
129
|
+
* into the Worker so runtime code can authenticate.
|
|
130
|
+
*
|
|
131
|
+
* @binding
|
|
132
|
+
*
|
|
133
|
+
* @section Mutating DNS records at runtime
|
|
134
|
+
* @example Create, update, and delete records from inside a Worker
|
|
135
|
+
* Bind the client in the Worker's Init phase and provide {@link DnsWriteLive}.
|
|
136
|
+
* The zone is fixed by `.bind(zone)` — the provisioned token only grants
|
|
137
|
+
* access to that zone, so calls take no `zoneId`. Pass the {@link Zone}
|
|
138
|
+
* resource directly (it's an `Effect`), or `yield* Zone` for a resolved value.
|
|
139
|
+
* ```typescript
|
|
140
|
+
* import * as Cloudflare from "alchemy/Cloudflare";
|
|
141
|
+
* import * as Effect from "effect/Effect";
|
|
142
|
+
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
|
143
|
+
*
|
|
144
|
+
* const Zone = Cloudflare.Zone("MyZone", { name: "example.com" });
|
|
145
|
+
*
|
|
146
|
+
* export class DnsWriterWorker extends Cloudflare.Worker<DnsWriterWorker>()(
|
|
147
|
+
* "DnsWriterWorker",
|
|
148
|
+
* { main: import.meta.filename },
|
|
149
|
+
* Effect.gen(function* () {
|
|
150
|
+
* // Init phase — bind the write client scoped to the zone.
|
|
151
|
+
* const dns = yield* Cloudflare.DnsWrite.bind(Zone);
|
|
152
|
+
*
|
|
153
|
+
* return {
|
|
154
|
+
* fetch: Effect.gen(function* () {
|
|
155
|
+
* const { result } = yield* dns.createDnsRecord({
|
|
156
|
+
* type: "A",
|
|
157
|
+
* name: "app.example.com",
|
|
158
|
+
* content: "192.0.2.1",
|
|
159
|
+
* ttl: 1,
|
|
160
|
+
* proxied: true,
|
|
161
|
+
* });
|
|
162
|
+
* yield* dns.updateDnsRecord(result.id, {
|
|
163
|
+
* type: "A",
|
|
164
|
+
* name: "app.example.com",
|
|
165
|
+
* content: "192.0.2.2",
|
|
166
|
+
* ttl: 1,
|
|
167
|
+
* });
|
|
168
|
+
* yield* dns.deleteDnsRecord(result.id);
|
|
169
|
+
* return yield* HttpServerResponse.json({ id: result.id });
|
|
170
|
+
* }),
|
|
171
|
+
* };
|
|
172
|
+
* }).pipe(Effect.provide(Cloudflare.DnsWriteLive)),
|
|
173
|
+
* ) {}
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @example Apply a batch of changes atomically
|
|
177
|
+
* ```typescript
|
|
178
|
+
* yield* dns.batchDnsRecords({
|
|
179
|
+
* posts: [{ type: "A", name: "a.example.com", content: "192.0.2.1", ttl: 1 }],
|
|
180
|
+
* deletes: [{ id: oldRecordId }],
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export class DnsWrite extends Binding.Service<
|
|
185
|
+
DnsWrite,
|
|
186
|
+
(zone: Zone) => Effect.Effect<DnsWriteClient>
|
|
187
|
+
>()("Cloudflare.DnsWrite") {}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Deploy-time policy for {@link DnsWrite}. Attaches the `DNS Write` permission
|
|
191
|
+
* to the token via its binding contract.
|
|
192
|
+
*/
|
|
193
|
+
export class DnsWritePolicy extends Binding.Policy<
|
|
194
|
+
DnsWritePolicy,
|
|
195
|
+
(token: AccountApiToken, zone: Zone) => Effect.Effect<void>
|
|
196
|
+
>()("Cloudflare.DnsWrite") {}
|
|
197
|
+
|
|
198
|
+
/** Runtime layer for {@link DnsWrite}. */
|
|
199
|
+
export const DnsWriteLive = Layer.effect(
|
|
200
|
+
DnsWrite,
|
|
201
|
+
makeDnsClient(DnsWritePolicy, "DnsWriteToken", dnsWriteClient),
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
/** Live deploy-time policy layer for {@link DnsWritePolicy}. */
|
|
205
|
+
export const DnsWritePolicyLive = makeDnsPolicyLive(
|
|
206
|
+
DnsWritePolicy,
|
|
207
|
+
"Cloudflare.DnsWrite",
|
|
208
|
+
["DNS Write"],
|
|
209
|
+
);
|
|
@@ -23,6 +23,7 @@ import * as CloudflareEnvironment from "./CloudflareEnvironment.ts";
|
|
|
23
23
|
import * as Containers from "./Container/index.ts";
|
|
24
24
|
import * as Credentials from "./Credentials.ts";
|
|
25
25
|
import * as D1 from "./D1/index.ts";
|
|
26
|
+
import * as Dns from "./Dns/index.ts";
|
|
26
27
|
import * as Email from "./Email/index.ts";
|
|
27
28
|
import * as Hyperdrive from "./Hyperdrive/index.ts";
|
|
28
29
|
import * as Images from "./Images/index.ts";
|
|
@@ -65,6 +66,9 @@ export const providers = () =>
|
|
|
65
66
|
Containers.Container,
|
|
66
67
|
D1.D1ConnectionPolicy,
|
|
67
68
|
D1.D1Database,
|
|
69
|
+
Dns.DnsReadPolicy,
|
|
70
|
+
Dns.DnsReadWritePolicy,
|
|
71
|
+
Dns.DnsWritePolicy,
|
|
68
72
|
Email.EmailAddress,
|
|
69
73
|
Email.EmailRouting,
|
|
70
74
|
Email.EmailRule,
|
|
@@ -115,6 +119,9 @@ export const providers = () =>
|
|
|
115
119
|
Containers.ContainerProvider(),
|
|
116
120
|
D1.D1ConnectionPolicyLive,
|
|
117
121
|
D1.DatabaseProvider(),
|
|
122
|
+
Dns.DnsReadPolicyLive,
|
|
123
|
+
Dns.DnsReadWritePolicyLive,
|
|
124
|
+
Dns.DnsWritePolicyLive,
|
|
118
125
|
Email.EmailAddressProvider(),
|
|
119
126
|
Email.EmailRoutingProvider(),
|
|
120
127
|
Email.EmailRuleProvider(),
|