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

main
ts 112 lines 4.06 KB
Raw
1 import * as Output from "alchemy/Output";
2 import type { Output as OutputType } from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import type { Repository } from "./Repository.ts";
6
7 /**
8 * Shared scaffolding for CodeArtifact HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, makeRepositoryHttpBinding({ … }))`. Everything except
12 * the operation, the IAM action list, and the granted ARNs is boilerplate:
13 * the runtime callable injects `domain`, `domainOwner`, and `repository`
14 * from the bound {@link Repository}.
15 */
16
17 /** The repository-scoping fields every CodeArtifact package operation takes. */
18 interface RepositoryScopedRequest {
19 domain: string;
20 domainOwner?: string;
21 repository: string;
22 }
23
24 /** The bound repository's own ARN (repository-level actions). */
25 export const repositoryArns = (repository: Repository): OutputType<string>[] => [
26 repository.repositoryArn,
27 ];
28
29 /**
30 * ARN matching every package in the bound repository
31 * (`arn:…:package/{domain}/{repository}/*`) — package-level actions like
32 * `codeartifact:DescribePackage` authorize against the package resource,
33 * not the repository.
34 */
35 export const packageArns = (repository: Repository): OutputType<string>[] => [
36 repository.repositoryArn.pipe(
37 Output.map((arn) => `${arn.replace(":repository/", ":package/")}/*`),
38 ),
39 ];
40
41 /**
42 * ARNs matching every repository and every package in the bound repository's
43 * domain — for operations like `codeartifact:CopyPackageVersions` that read
44 * from a sibling repository in the same domain.
45 */
46 export const domainWideArns = (repository: Repository): OutputType<string>[] =>
47 ["repository", "package"].map((kind) =>
48 repository.repositoryArn.pipe(
49 Output.map((arn) => {
50 const [prefix, rest] = arn.split(":repository/");
51 return `${prefix}:${kind}/${rest!.split("/")[0]}/*`;
52 }),
53 ),
54 );
55
56 /**
57 * Build the impl Effect for a repository-scoped CodeArtifact operation. The
58 * deploy-time half grants `actions` on `resources` (default: the repository
59 * ARN); the runtime callable injects the bound repository's `domain`,
60 * `domainOwner`, and `repository` into every request.
61 *
62 * Operations that scope by a differently-named field (e.g.
63 * `CopyPackageVersions`' `destinationRepository`) stay bespoke in their own
64 * `{Op}Http.ts` and reuse just the ARN helpers above.
65 */
66 export const makeRepositoryHttpBinding = <
67 I extends { domain: string; domainOwner?: string },
68 A,
69 E,
70 R,
71 >(options: {
72 /** Fully-qualified binding tag, e.g. `AWS.CodeArtifact.ListPackages`. */
73 tag: string;
74 /** The distilled operation. */
75 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
76 /** IAM actions granted on `resources`. */
77 actions: readonly string[];
78 /** ARNs the actions are granted on. @default the repository ARN */
79 resources?: (repository: Repository) => OutputType<string>[];
80 }) =>
81 Effect.gen(function* () {
82 const bind = yield* AWSBindings.Bind;
83 const op = yield* AWSBindings.withCredentials(options.operation);
84
85 return Effect.fn(function* (repository: Repository) {
86 const DomainName = yield* repository.domainName;
87 const DomainOwner = yield* repository.domainOwner;
88 const RepositoryName = yield* repository.repositoryName;
89 if (!globalThis.__ALCHEMY_RUNTIME__) {
90 yield* bind`${options.tag}(${repository})`({
91 policyStatements: [
92 {
93 Effect: "Allow",
94 Action: [...options.actions],
95 Resource: (options.resources ?? repositoryArns)(repository),
96 },
97 ],
98 });
99 }
100 return Effect.fn(`${options.tag}(${repository.LogicalId})`)(function* (
101 request: Omit<I, keyof RepositoryScopedRequest>,
102 ) {
103 const owner = yield* DomainOwner;
104 return yield* op({
105 ...request,
106 domain: yield* DomainName,
107 ...(owner === "" ? {} : { domainOwner: owner }),
108 repository: yield* RepositoryName,
109 } as unknown as I);
110 });
111 });
112 });