The best way to control Claude Code, Codex and more from any computer or phone

main
ts 149 lines 5.82 KB
Raw
1 import type { Input } from "alchemy/Input";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { AddressList } from "./AddressList.ts";
5 import type { Archive } from "./Archive.ts";
6
7 /**
8 * Shared scaffolding for SES Mail Manager HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
12 * below. Everything except the operation, the injected identifier, and the
13 * IAM action list is boilerplate.
14 */
15
16 const RegisterHostPolicy = Effect.gen(function* () {
17 const bind = yield* AWSBindings.Bind;
18 return Effect.fn(function* (
19 tag: string,
20 resource: AddressList | Archive,
21 arn: Input<string>,
22 actions: readonly string[],
23 ) {
24 if (!globalThis.__ALCHEMY_RUNTIME__) {
25 yield* bind`${tag}(${resource})`({
26 policyStatements: [
27 {
28 Effect: "Allow",
29 Action: [...actions],
30 Resource: [arn],
31 },
32 ],
33 });
34 }
35 });
36 });
37
38 /**
39 * Build the impl Effect for a Mail Manager operation scoped to an
40 * {@link AddressList} whose request carries `AddressListId`: the deploy-time
41 * half grants `actions` on the bound list's ARN, and the runtime half
42 * injects the list's id into every request.
43 */
44 export const makeAddressListHttpBinding = <I extends { AddressListId: string }, A, E, R>(options: {
45 /** Fully-qualified binding tag, e.g. `AWS.MailManager.GetMemberOfAddressList`. */
46 tag: string;
47 /** The distilled operation. */
48 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
49 /** IAM actions granted on the address list ARN. */
50 actions: readonly string[];
51 }) =>
52 Effect.gen(function* () {
53 const registerHostPolicy = yield* RegisterHostPolicy;
54 const op = yield* AWSBindings.withCredentials(options.operation);
55
56 return Effect.fn(function* (list: AddressList) {
57 const AddressListId = yield* list.addressListId;
58 yield* registerHostPolicy(options.tag, list, list.addressListArn, options.actions);
59 return Effect.fn(`${options.tag}(${list.LogicalId})`)(function* (
60 request: Omit<I, "AddressListId">,
61 ) {
62 const addressListId = yield* AddressListId;
63 return yield* op({ ...request, AddressListId: addressListId } as I);
64 });
65 });
66 });
67
68 /**
69 * Build the impl Effect for a Mail Manager import-job operation scoped to an
70 * {@link AddressList} but keyed by `JobId` (the id of a job created against
71 * that list): the deploy-time half grants `actions` on the bound list's ARN;
72 * the runtime half passes the caller's request through unchanged.
73 */
74 export const makeAddressListJobHttpBinding = <I, A, E, R>(options: {
75 /** Fully-qualified binding tag, e.g. `AWS.MailManager.StartAddressListImportJob`. */
76 tag: string;
77 /** The distilled operation, invoked with the caller's request as-is. */
78 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
79 /** IAM actions granted on the address list ARN. */
80 actions: readonly string[];
81 }) =>
82 Effect.gen(function* () {
83 const registerHostPolicy = yield* RegisterHostPolicy;
84 const op = yield* AWSBindings.withCredentials(options.operation);
85
86 return Effect.fn(function* (list: AddressList) {
87 yield* registerHostPolicy(options.tag, list, list.addressListArn, options.actions);
88 return Effect.fn(`${options.tag}(${list.LogicalId})`)(function* (request: I) {
89 return yield* op(request);
90 });
91 });
92 });
93
94 /**
95 * Build the impl Effect for a Mail Manager operation scoped to an
96 * {@link Archive} whose request carries `ArchiveId`: the deploy-time half
97 * grants `actions` on the bound archive's ARN, and the runtime half injects
98 * the archive's id into every request.
99 */
100 export const makeArchiveHttpBinding = <I extends { ArchiveId: string }, A, E, R>(options: {
101 /** Fully-qualified binding tag, e.g. `AWS.MailManager.StartArchiveSearch`. */
102 tag: string;
103 /** The distilled operation. */
104 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
105 /** IAM actions granted on the archive ARN. */
106 actions: readonly string[];
107 }) =>
108 Effect.gen(function* () {
109 const registerHostPolicy = yield* RegisterHostPolicy;
110 const op = yield* AWSBindings.withCredentials(options.operation);
111
112 return Effect.fn(function* (archive: Archive) {
113 const ArchiveId = yield* archive.archiveId;
114 yield* registerHostPolicy(options.tag, archive, archive.archiveArn, options.actions);
115 return Effect.fn(`${options.tag}(${archive.LogicalId})`)(function* (
116 request: Omit<I, "ArchiveId">,
117 ) {
118 const archiveId = yield* ArchiveId;
119 return yield* op({ ...request, ArchiveId: archiveId } as I);
120 });
121 });
122 });
123
124 /**
125 * Build the impl Effect for a Mail Manager operation scoped to an
126 * {@link Archive} but keyed by a task id (`SearchId`, `ExportId`,
127 * `ArchivedMessageId`): the deploy-time half grants `actions` on the bound
128 * archive's ARN; the runtime half passes the caller's request through
129 * unchanged.
130 */
131 export const makeArchiveTaskHttpBinding = <I, A, E, R>(options: {
132 /** Fully-qualified binding tag, e.g. `AWS.MailManager.GetArchiveSearchResults`. */
133 tag: string;
134 /** The distilled operation, invoked with the caller's request as-is. */
135 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
136 /** IAM actions granted on the archive ARN. */
137 actions: readonly string[];
138 }) =>
139 Effect.gen(function* () {
140 const registerHostPolicy = yield* RegisterHostPolicy;
141 const op = yield* AWSBindings.withCredentials(options.operation);
142
143 return Effect.fn(function* (archive: Archive) {
144 yield* registerHostPolicy(options.tag, archive, archive.archiveArn, options.actions);
145 return Effect.fn(`${options.tag}(${archive.LogicalId})`)(function* (request: I) {
146 return yield* op(request);
147 });
148 });
149 });