|
1 | | -import * as cf from "cloudflare"; |
| 1 | +import { APIConnectionError, Cloudflare, type APIError } from "cloudflare"; |
| 2 | +import { |
| 3 | + isRequestOptions, |
| 4 | + type APIPromise, |
| 5 | + type RequestOptions, |
| 6 | +} from "cloudflare/core"; |
| 7 | +import type { ErrorData } from "cloudflare/resources"; |
| 8 | +import { Layer } from "effect"; |
2 | 9 | import * as Context from "effect/Context"; |
| 10 | +import * as Data from "effect/Data"; |
3 | 11 | import * as Effect from "effect/Effect"; |
4 | | -import * as Layer from "effect/Layer"; |
5 | | -import * as Option from "effect/Option"; |
6 | 12 |
|
7 | | -export class CloudflareApi extends Context.Tag("CloudflareApi")< |
8 | | - CloudflareApi, |
9 | | - cf.Cloudflare |
10 | | ->() {} |
11 | | - |
12 | | -export class CloudflareAccountId extends Context.Tag("CloudflareAccountId")< |
| 13 | +export class CloudflareAccountId extends Context.Tag("cloudflare/account-id")< |
13 | 14 | CloudflareAccountId, |
14 | 15 | string |
15 | | ->() {} |
| 16 | +>() { |
| 17 | + static readonly fromEnv = Layer.effect( |
| 18 | + CloudflareAccountId, |
| 19 | + Effect.gen(function* () { |
| 20 | + const accountId = process.env.CLOUDFLARE_ACCOUNT_ID; |
| 21 | + if (!accountId) { |
| 22 | + return yield* Effect.die("CLOUDFLARE_ACCOUNT_ID is not set"); |
| 23 | + } |
| 24 | + return accountId; |
| 25 | + }), |
| 26 | + ); |
| 27 | +} |
16 | 28 |
|
17 | | -export class CloudflareEmail extends Context.Tag("CloudflareEmail")< |
18 | | - CloudflareEmail, |
19 | | - string |
20 | | ->() {} |
| 29 | +export class CloudflareApi extends Effect.Service<CloudflareApi>()( |
| 30 | + "cloudflare/api", |
| 31 | + { |
| 32 | + effect: (options?: { |
| 33 | + baseUrl?: string; |
| 34 | + apiToken?: string; |
| 35 | + apiKey?: string; |
| 36 | + apiEmail?: string; |
| 37 | + }) => |
| 38 | + Effect.succeed( |
| 39 | + createRecursiveProxy( |
| 40 | + new Cloudflare({ |
| 41 | + baseURL: options?.baseUrl ?? import.meta.env.CLOUDFLARE_BASE_URL, |
| 42 | + apiToken: options?.apiToken ?? import.meta.env.CLOUDFLARE_API_TOKEN, |
| 43 | + apiKey: options?.apiKey ?? import.meta.env.CLOUDFLARE_API_KEY, |
| 44 | + apiEmail: options?.apiEmail ?? import.meta.env.CLOUDFLARE_API_EMAIL, |
| 45 | + }), |
| 46 | + ), |
| 47 | + ), |
| 48 | + }, |
| 49 | +) {} |
21 | 50 |
|
22 | | -export class CloudflareApiKey extends Context.Tag("CloudflareApiKey")< |
23 | | - CloudflareApiKey, |
24 | | - string |
25 | | ->() {} |
| 51 | +export class CloudflareApiError extends Data.Error<{ |
| 52 | + _tag: |
| 53 | + | "Connection" |
| 54 | + | "BadRequest" |
| 55 | + | "Authentication" |
| 56 | + | "PermissionDenied" |
| 57 | + | "NotFound" |
| 58 | + | "Conflict" |
| 59 | + | "UnprocessableEntity" |
| 60 | + | "RateLimit" |
| 61 | + | "InternalServerError" |
| 62 | + | "Unknown"; |
| 63 | + message: string; |
| 64 | + errors: ErrorData[]; |
| 65 | + cause: APIError; |
| 66 | +}> { |
| 67 | + static from(cause: APIError): CloudflareApiError { |
| 68 | + const error = new CloudflareApiError({ |
| 69 | + _tag: CloudflareApiError.getTag(cause), |
| 70 | + message: cause.message, |
| 71 | + errors: cause.errors, |
| 72 | + cause: cause, |
| 73 | + }); |
| 74 | + return error; |
| 75 | + } |
26 | 76 |
|
27 | | -export class CloudflareApiToken extends Context.Tag("CloudflareApiToken")< |
28 | | - CloudflareApiToken, |
29 | | - string |
30 | | ->() {} |
| 77 | + private static getTag(error: APIError): CloudflareApiError["_tag"] { |
| 78 | + if (error instanceof APIConnectionError) { |
| 79 | + return "Connection"; |
| 80 | + } |
| 81 | + switch (error.status) { |
| 82 | + case 400: |
| 83 | + return "BadRequest"; |
| 84 | + case 401: |
| 85 | + return "Authentication"; |
| 86 | + case 403: |
| 87 | + return "PermissionDenied"; |
| 88 | + case 404: |
| 89 | + return "NotFound"; |
| 90 | + case 409: |
| 91 | + return "Conflict"; |
| 92 | + case 422: |
| 93 | + return "UnprocessableEntity"; |
| 94 | + case 429: |
| 95 | + return "RateLimit"; |
| 96 | + case 500: |
| 97 | + return "InternalServerError"; |
| 98 | + default: |
| 99 | + return "Unknown"; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
31 | 103 |
|
32 | | -export class CloudflareBaseUrl extends Context.Tag("CloudflareBaseUrl")< |
33 | | - CloudflareBaseUrl, |
34 | | - string |
35 | | ->() {} |
| 104 | +type ToEffect<T> = { |
| 105 | + [K in keyof T]: T[K] extends (...args: any[]) => any |
| 106 | + ? ( |
| 107 | + ...args: Parameters<T[K]> |
| 108 | + ) => Effect.Effect<UnwrapAPIPromise<ReturnType<T[K]>>, CloudflareApiError> |
| 109 | + : T[K] extends Record<string, any> |
| 110 | + ? ToEffect<T[K]> |
| 111 | + : T[K]; |
| 112 | +}; |
36 | 113 |
|
37 | | -const tryGet = <Tag extends Context.Tag<any, any>>( |
38 | | - tag: Tag, |
39 | | - defaultValue: Tag["Service"] | undefined, |
40 | | -) => |
41 | | - Effect.gen(function* () { |
42 | | - const value = yield* Effect.serviceOption(tag); |
43 | | - return Option.getOrElse(value, () => defaultValue); |
44 | | - }); |
| 114 | +type UnwrapAPIPromise<T> = T extends APIPromise<infer U> ? U : never; |
45 | 115 |
|
46 | | -export const cloudflareApi = Layer.effect( |
47 | | - CloudflareApi, |
48 | | - Effect.gen(function* () { |
49 | | - const email = yield* tryGet( |
50 | | - CloudflareEmail, |
51 | | - import.meta.env.CLOUDFLARE_EMAIL, |
52 | | - ); |
53 | | - const apiKey = yield* tryGet( |
54 | | - CloudflareApiKey, |
55 | | - import.meta.env.CLOUDFLARE_API_KEY, |
56 | | - ); |
57 | | - const apiToken = yield* tryGet( |
58 | | - CloudflareApiToken, |
59 | | - import.meta.env.CLOUDFLARE_API_TOKEN, |
60 | | - ); |
61 | | - const baseURL = yield* tryGet( |
62 | | - CloudflareBaseUrl, |
63 | | - import.meta.env.CLOUDFLARE_BASE_URL, |
64 | | - ); |
65 | | - return new cf.Cloudflare({ |
66 | | - apiEmail: email, |
67 | | - apiKey: apiKey, |
68 | | - apiToken: apiToken, |
69 | | - baseURL: baseURL, |
70 | | - }); |
71 | | - }), |
72 | | -); |
| 116 | +const createRecursiveProxy = <T extends object>(target: T): ToEffect<T> => { |
| 117 | + return new Proxy(target as any, { |
| 118 | + get(target, prop, receiver) { |
| 119 | + const value = Reflect.get(target, prop, receiver); |
| 120 | + if (typeof value === "function") { |
| 121 | + return Effect.fnUntraced(function* (...args: any[]) { |
| 122 | + return yield* Effect.tryPromise({ |
| 123 | + try: async (signal) => { |
| 124 | + let modifiedArgs: any[]; |
| 125 | + if (isRequestOptions(args[args.length - 1])) { |
| 126 | + const options = args[args.length - 1] as RequestOptions; |
| 127 | + modifiedArgs = [ |
| 128 | + ...args.slice(0, -1), |
| 129 | + { |
| 130 | + ...options, |
| 131 | + signal: options?.signal |
| 132 | + ? AbortSignal.any([signal, options.signal]) |
| 133 | + : signal, |
| 134 | + }, |
| 135 | + ]; |
| 136 | + } else { |
| 137 | + modifiedArgs = [...args, { signal }]; |
| 138 | + } |
| 139 | + const result = await value.apply(target, modifiedArgs); |
| 140 | + return result; |
| 141 | + }, |
| 142 | + catch: (cause) => { |
| 143 | + const error = CloudflareApiError.from(cause as APIError); |
| 144 | + return error; |
| 145 | + }, |
| 146 | + }); |
| 147 | + }); |
| 148 | + } |
| 149 | + return createRecursiveProxy(value); |
| 150 | + }, |
| 151 | + }); |
| 152 | +}; |
0 commit comments