The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Hetzner from "@distilled.cloud/hetzner"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | export type CatalogKind = "location" | "serverType" | "image" | "iso" | "loadBalancerType"; |
| 6 | |
| 7 | export class CatalogNotFound extends Data.TaggedError("Hetzner.CatalogNotFound")<{ |
| 8 | kind: CatalogKind; |
| 9 | ref: string; |
| 10 | }> {} |
| 11 | |
| 12 | const numericId = (ref: string | number): number | undefined => |
| 13 | typeof ref === "number" ? ref : /^\d+$/.test(ref) ? Number(ref) : undefined; |
| 14 | |
| 15 | const notFound = (kind: CatalogKind, ref: string | number) => |
| 16 | new CatalogNotFound({ kind, ref: String(ref) }); |
| 17 | |
| 18 | /** |
| 19 | * Resolve a Hetzner Location by name (`nbg1`, `fsn1`, `hel1`, …) or |
| 20 | * numeric id. |
| 21 | */ |
| 22 | export const findLocation = (ref: string | number) => |
| 23 | Effect.gen(function* () { |
| 24 | const id = numericId(ref); |
| 25 | if (id !== undefined) { |
| 26 | const { location } = yield* Hetzner.locations |
| 27 | .getLocation({ id }) |
| 28 | .pipe(Effect.catchTag("NotFound", () => Effect.fail(notFound("location", ref)))); |
| 29 | return location; |
| 30 | } |
| 31 | const { locations } = yield* Hetzner.locations.listLocations({ |
| 32 | name: String(ref), |
| 33 | per_page: 50, |
| 34 | }); |
| 35 | const found = locations.find((item) => item.name === ref); |
| 36 | if (found === undefined) { |
| 37 | return yield* notFound("location", ref); |
| 38 | } |
| 39 | return found; |
| 40 | }); |
| 41 | |
| 42 | /** |
| 43 | * Resolve a Hetzner Server Type by name (`cx22`, `cpx11`, …) or numeric id. |
| 44 | */ |
| 45 | export const findServerType = (ref: string | number) => |
| 46 | Effect.gen(function* () { |
| 47 | const id = numericId(ref); |
| 48 | if (id !== undefined) { |
| 49 | const { server_type } = yield* Hetzner.serverTypes |
| 50 | .getServerType({ id }) |
| 51 | .pipe(Effect.catchTag("NotFound", () => Effect.fail(notFound("serverType", ref)))); |
| 52 | return server_type; |
| 53 | } |
| 54 | const { server_types } = yield* Hetzner.serverTypes.listServerTypes({ |
| 55 | name: String(ref), |
| 56 | per_page: 50, |
| 57 | }); |
| 58 | const found = server_types.find((item) => item.name === ref); |
| 59 | if (found === undefined) { |
| 60 | return yield* notFound("serverType", ref); |
| 61 | } |
| 62 | return found; |
| 63 | }); |
| 64 | |
| 65 | /** |
| 66 | * Resolve a Hetzner Image by name (`ubuntu-24.04`, …) or numeric id. |
| 67 | */ |
| 68 | export const findImage = (ref: string | number) => |
| 69 | Effect.gen(function* () { |
| 70 | const id = numericId(ref); |
| 71 | if (id !== undefined) { |
| 72 | const { image } = yield* Hetzner.images |
| 73 | .getImage({ id }) |
| 74 | .pipe(Effect.catchTag("NotFound", () => Effect.fail(notFound("image", ref)))); |
| 75 | return image; |
| 76 | } |
| 77 | const { images } = yield* Hetzner.images.listImages({ |
| 78 | name: String(ref), |
| 79 | include_deprecated: true, |
| 80 | per_page: 50, |
| 81 | }); |
| 82 | const found = images.find((item) => item.name === ref); |
| 83 | if (found === undefined) { |
| 84 | return yield* notFound("image", ref); |
| 85 | } |
| 86 | return found; |
| 87 | }); |
| 88 | |
| 89 | /** |
| 90 | * Resolve a Hetzner ISO by name or numeric id. |
| 91 | */ |
| 92 | export const findIso = (ref: string | number) => |
| 93 | Effect.gen(function* () { |
| 94 | const id = numericId(ref); |
| 95 | if (id !== undefined) { |
| 96 | const { iso } = yield* Hetzner.isos |
| 97 | .getIso({ id }) |
| 98 | .pipe(Effect.catchTag("NotFound", () => Effect.fail(notFound("iso", ref)))); |
| 99 | return iso; |
| 100 | } |
| 101 | const { isos } = yield* Hetzner.isos.listIsos({ name: String(ref), per_page: 50 }); |
| 102 | const found = isos.find((item) => item.name === ref); |
| 103 | if (found === undefined) { |
| 104 | return yield* notFound("iso", ref); |
| 105 | } |
| 106 | return found; |
| 107 | }); |
| 108 | |
| 109 | /** |
| 110 | * Resolve a Hetzner Load Balancer Type by name (`lb11`, `lb21`, …) or |
| 111 | * numeric id. |
| 112 | */ |
| 113 | export const findLoadBalancerType = (ref: string | number) => |
| 114 | Effect.gen(function* () { |
| 115 | const id = numericId(ref); |
| 116 | if (id !== undefined) { |
| 117 | const { load_balancer_type } = yield* Hetzner.loadBalancerTypes |
| 118 | .getLoadBalancerType({ id }) |
| 119 | .pipe(Effect.catchTag("NotFound", () => Effect.fail(notFound("loadBalancerType", ref)))); |
| 120 | return load_balancer_type; |
| 121 | } |
| 122 | const { load_balancer_types } = yield* Hetzner.loadBalancerTypes.listLoadBalancerTypes({ |
| 123 | name: String(ref), |
| 124 | per_page: 50, |
| 125 | }); |
| 126 | const found = load_balancer_types.find((item) => item.name === ref); |
| 127 | if (found === undefined) { |
| 128 | return yield* notFound("loadBalancerType", ref); |
| 129 | } |
| 130 | return found; |
| 131 | }); |