The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as ServiceQuotas from "@alchemy.run/aws/ServiceQuotas"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | export class ServiceQuotasTestFunction extends Lambda.Function<Lambda.Function>()( |
| 13 | "ServiceQuotasTestFunction", |
| 14 | ) {} |
| 15 | |
| 16 | export default ServiceQuotasTestFunction.make( |
| 17 | { |
| 18 | main, |
| 19 | functionUrl: true, |
| 20 | timeout: Duration.seconds(30), |
| 21 | }, |
| 22 | Effect.gen(function* () { |
| 23 | const getServiceQuota = yield* ServiceQuotas.GetServiceQuota(); |
| 24 | const getAWSDefaultServiceQuota = yield* ServiceQuotas.GetAWSDefaultServiceQuota(); |
| 25 | const listServices = yield* ServiceQuotas.ListServices(); |
| 26 | const listServiceQuotas = yield* ServiceQuotas.ListServiceQuotas(); |
| 27 | const listRequestedServiceQuotaChangeHistoryByQuota = |
| 28 | yield* ServiceQuotas.ListRequestedServiceQuotaChangeHistoryByQuota(); |
| 29 | const requestServiceQuotaIncrease = yield* ServiceQuotas.RequestServiceQuotaIncrease(); |
| 30 | |
| 31 | return { |
| 32 | fetch: Effect.gen(function* () { |
| 33 | const request = yield* HttpServerRequest; |
| 34 | const url = new URL(request.originalUrl); |
| 35 | const pathname = url.pathname; |
| 36 | |
| 37 | // Cheap readiness route — no Service Quotas call. |
| 38 | if (request.method === "GET" && pathname === "/ping") { |
| 39 | return yield* HttpServerResponse.json({ ok: true }); |
| 40 | } |
| 41 | |
| 42 | if (request.method === "GET" && pathname === "/quota") { |
| 43 | const serviceCode = url.searchParams.get("service"); |
| 44 | const quotaCode = url.searchParams.get("quota"); |
| 45 | if (serviceCode === null || quotaCode === null) { |
| 46 | return yield* HttpServerResponse.json( |
| 47 | { error: "service and quota query params are required" }, |
| 48 | { status: 400 }, |
| 49 | ); |
| 50 | } |
| 51 | return yield* getServiceQuota({ |
| 52 | ServiceCode: serviceCode, |
| 53 | QuotaCode: quotaCode, |
| 54 | }).pipe( |
| 55 | Effect.flatMap((result) => |
| 56 | HttpServerResponse.json({ |
| 57 | quotaCode: result.Quota?.QuotaCode, |
| 58 | quotaName: result.Quota?.QuotaName, |
| 59 | value: result.Quota?.Value, |
| 60 | adjustable: result.Quota?.Adjustable, |
| 61 | }), |
| 62 | ), |
| 63 | // The typed not-found tag round-trips as a 404 so the test can |
| 64 | // assert the binding surfaces distilled's typed error union. |
| 65 | Effect.catchTag("NoSuchResourceException", () => |
| 66 | HttpServerResponse.json({ tag: "NoSuchResourceException" }, { status: 404 }), |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | if (request.method === "GET" && pathname === "/default-quota") { |
| 72 | const serviceCode = url.searchParams.get("service"); |
| 73 | const quotaCode = url.searchParams.get("quota"); |
| 74 | if (serviceCode === null || quotaCode === null) { |
| 75 | return yield* HttpServerResponse.json( |
| 76 | { error: "service and quota query params are required" }, |
| 77 | { status: 400 }, |
| 78 | ); |
| 79 | } |
| 80 | return yield* getAWSDefaultServiceQuota({ |
| 81 | ServiceCode: serviceCode, |
| 82 | QuotaCode: quotaCode, |
| 83 | }).pipe( |
| 84 | Effect.flatMap((result) => |
| 85 | HttpServerResponse.json({ |
| 86 | quotaCode: result.Quota?.QuotaCode, |
| 87 | value: result.Quota?.Value, |
| 88 | }), |
| 89 | ), |
| 90 | Effect.catchTag("NoSuchResourceException", () => |
| 91 | HttpServerResponse.json({ tag: "NoSuchResourceException" }, { status: 404 }), |
| 92 | ), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | if (request.method === "GET" && pathname === "/services") { |
| 97 | const result = yield* listServices({ MaxResults: 20 }); |
| 98 | return yield* HttpServerResponse.json({ |
| 99 | serviceCodes: (result.Services ?? []).map((s) => s.ServiceCode), |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | if (request.method === "GET" && pathname === "/quotas") { |
| 104 | const serviceCode = url.searchParams.get("service"); |
| 105 | if (serviceCode === null) { |
| 106 | return yield* HttpServerResponse.json( |
| 107 | { error: "service query param is required" }, |
| 108 | { status: 400 }, |
| 109 | ); |
| 110 | } |
| 111 | return yield* listServiceQuotas({ |
| 112 | ServiceCode: serviceCode, |
| 113 | MaxResults: 20, |
| 114 | }).pipe( |
| 115 | Effect.flatMap((result) => |
| 116 | HttpServerResponse.json({ |
| 117 | quotaCodes: (result.Quotas ?? []).map((q) => q.QuotaCode), |
| 118 | }), |
| 119 | ), |
| 120 | Effect.catchTag("NoSuchResourceException", () => |
| 121 | HttpServerResponse.json({ tag: "NoSuchResourceException" }, { status: 404 }), |
| 122 | ), |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | if (request.method === "GET" && pathname === "/history") { |
| 127 | const serviceCode = url.searchParams.get("service"); |
| 128 | const quotaCode = url.searchParams.get("quota"); |
| 129 | if (serviceCode === null || quotaCode === null) { |
| 130 | return yield* HttpServerResponse.json( |
| 131 | { error: "service and quota query params are required" }, |
| 132 | { status: 400 }, |
| 133 | ); |
| 134 | } |
| 135 | return yield* listRequestedServiceQuotaChangeHistoryByQuota({ |
| 136 | ServiceCode: serviceCode, |
| 137 | QuotaCode: quotaCode, |
| 138 | }).pipe( |
| 139 | Effect.flatMap((result) => |
| 140 | HttpServerResponse.json({ |
| 141 | count: (result.RequestedQuotas ?? []).length, |
| 142 | }), |
| 143 | ), |
| 144 | Effect.catchTag("NoSuchResourceException", () => |
| 145 | HttpServerResponse.json({ tag: "NoSuchResourceException" }, { status: 404 }), |
| 146 | ), |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | // Exercises the write-path IAM grant without mutating the account: |
| 151 | // the bogus quota code fails with the typed NoSuchResourceException |
| 152 | // BEFORE any request is submitted. An AccessDenied would surface as |
| 153 | // a 500 (die) instead. |
| 154 | if (request.method === "POST" && pathname === "/request-increase") { |
| 155 | const serviceCode = url.searchParams.get("service"); |
| 156 | const quotaCode = url.searchParams.get("quota"); |
| 157 | if (serviceCode === null || quotaCode === null) { |
| 158 | return yield* HttpServerResponse.json( |
| 159 | { error: "service and quota query params are required" }, |
| 160 | { status: 400 }, |
| 161 | ); |
| 162 | } |
| 163 | return yield* requestServiceQuotaIncrease({ |
| 164 | ServiceCode: serviceCode, |
| 165 | QuotaCode: quotaCode, |
| 166 | DesiredValue: 1, |
| 167 | }).pipe( |
| 168 | Effect.flatMap((result) => |
| 169 | HttpServerResponse.json({ |
| 170 | requestId: result.RequestedQuota?.Id, |
| 171 | status: result.RequestedQuota?.Status, |
| 172 | }), |
| 173 | ), |
| 174 | Effect.catchTag("NoSuchResourceException", () => |
| 175 | HttpServerResponse.json({ tag: "NoSuchResourceException" }, { status: 404 }), |
| 176 | ), |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | return yield* HttpServerResponse.json( |
| 181 | { error: "Not found", method: request.method, pathname }, |
| 182 | { status: 404 }, |
| 183 | ); |
| 184 | }).pipe(Effect.orDie), |
| 185 | }; |
| 186 | }).pipe( |
| 187 | Effect.provide( |
| 188 | Layer.mergeAll( |
| 189 | ServiceQuotas.GetServiceQuotaHttp, |
| 190 | ServiceQuotas.GetAWSDefaultServiceQuotaHttp, |
| 191 | ServiceQuotas.ListServicesHttp, |
| 192 | ServiceQuotas.ListServiceQuotasHttp, |
| 193 | ServiceQuotas.ListRequestedServiceQuotaChangeHistoryByQuotaHttp, |
| 194 | ServiceQuotas.RequestServiceQuotaIncreaseHttp, |
| 195 | ), |
| 196 | ), |
| 197 | ), |
| 198 | ); |