The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GeoMaps from "@alchemy.run/aws/GeoMaps"; |
| 2 | import * as GeoPlaces from "@alchemy.run/aws/GeoPlaces"; |
| 3 | import * as GeoRoutes from "@alchemy.run/aws/GeoRoutes"; |
| 4 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 13 | |
| 14 | export class GeoTestFunction extends Lambda.Function<Lambda.Function>()("GeoTestFunction") {} |
| 15 | |
| 16 | export default GeoTestFunction.make( |
| 17 | { |
| 18 | main, |
| 19 | functionUrl: true, |
| 20 | // Geo calls fan out to upstream providers and can exceed Lambda's 3s default. |
| 21 | timeout: Duration.seconds(30), |
| 22 | }, |
| 23 | Effect.gen(function* () { |
| 24 | const searchText = yield* GeoPlaces.SearchText(); |
| 25 | const geocode = yield* GeoPlaces.Geocode(); |
| 26 | const reverseGeocode = yield* GeoPlaces.ReverseGeocode(); |
| 27 | const autocomplete = yield* GeoPlaces.Autocomplete(); |
| 28 | const calculateRoutes = yield* GeoRoutes.CalculateRoutes(); |
| 29 | const calculateIsolines = yield* GeoRoutes.CalculateIsolines(); |
| 30 | const getTile = yield* GeoMaps.GetTile(); |
| 31 | const getStaticMap = yield* GeoMaps.GetStaticMap(); |
| 32 | |
| 33 | return { |
| 34 | fetch: Effect.gen(function* () { |
| 35 | const request = yield* HttpServerRequest; |
| 36 | const pathname = new URL(request.originalUrl).pathname; |
| 37 | |
| 38 | // Cheap readiness route — no AWS call. |
| 39 | if (request.method === "GET" && pathname === "/ping") { |
| 40 | return yield* HttpServerResponse.json({ ok: true }); |
| 41 | } |
| 42 | |
| 43 | if (request.method === "GET" && pathname === "/search-text") { |
| 44 | const result = yield* searchText({ |
| 45 | QueryText: "Space Needle, Seattle, WA", |
| 46 | MaxResults: 5, |
| 47 | // SearchText requires exactly one of BiasPosition, |
| 48 | // Filter.BoundingBox, or Filter.Circle. [longitude, latitude] |
| 49 | BiasPosition: [-122.3493, 47.6205], |
| 50 | }); |
| 51 | const items = result.ResultItems ?? []; |
| 52 | return yield* HttpServerResponse.json({ |
| 53 | count: items.length, |
| 54 | firstPosition: items[0]?.Position, |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | if (request.method === "GET" && pathname === "/geocode") { |
| 59 | const result = yield* geocode({ |
| 60 | QueryText: "1600 Pennsylvania Ave NW, Washington, DC", |
| 61 | MaxResults: 1, |
| 62 | }); |
| 63 | const items = result.ResultItems ?? []; |
| 64 | return yield* HttpServerResponse.json({ |
| 65 | count: items.length, |
| 66 | position: items[0]?.Position, |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | if (request.method === "GET" && pathname === "/reverse-geocode") { |
| 71 | const result = yield* reverseGeocode({ |
| 72 | // [longitude, latitude] — near the Space Needle, Seattle. |
| 73 | QueryPosition: [-122.3493, 47.6205], |
| 74 | MaxResults: 1, |
| 75 | }); |
| 76 | const items = result.ResultItems ?? []; |
| 77 | return yield* HttpServerResponse.json({ |
| 78 | count: items.length, |
| 79 | label: items[0]?.Address?.Label, |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | if (request.method === "GET" && pathname === "/calculate-routes") { |
| 84 | const result = yield* calculateRoutes({ |
| 85 | // [longitude, latitude] — two points in Seattle. |
| 86 | Origin: [-122.339, 47.61], |
| 87 | Destination: [-122.201, 47.61], |
| 88 | TravelMode: "Car", |
| 89 | }); |
| 90 | const routes = result.Routes ?? []; |
| 91 | return yield* HttpServerResponse.json({ |
| 92 | count: routes.length, |
| 93 | distance: routes[0]?.Summary?.Distance, |
| 94 | duration: routes[0]?.Summary?.Duration, |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | if (request.method === "GET" && pathname === "/autocomplete") { |
| 99 | const result = yield* autocomplete({ |
| 100 | QueryText: "1600 Pennsylvania", |
| 101 | MaxResults: 5, |
| 102 | }); |
| 103 | const items = result.ResultItems ?? []; |
| 104 | return yield* HttpServerResponse.json({ |
| 105 | count: items.length, |
| 106 | firstTitle: items[0]?.Title, |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | if (request.method === "GET" && pathname === "/calculate-isolines") { |
| 111 | const result = yield* calculateIsolines({ |
| 112 | // [longitude, latitude] — downtown Seattle. |
| 113 | Origin: [-122.339, 47.61], |
| 114 | // 10-minute drive-time isochrone. |
| 115 | Thresholds: { Time: [600] }, |
| 116 | TravelMode: "Car", |
| 117 | }); |
| 118 | const isolines = result.Isolines ?? []; |
| 119 | return yield* HttpServerResponse.json({ |
| 120 | count: isolines.length, |
| 121 | geometryCount: isolines[0]?.Geometries?.length ?? 0, |
| 122 | pricingBucket: result.PricingBucket, |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | if (request.method === "GET" && pathname === "/get-tile") { |
| 127 | const result = yield* getTile({ |
| 128 | Tileset: "vector.basemap", |
| 129 | Z: "0", |
| 130 | X: "0", |
| 131 | Y: "0", |
| 132 | }); |
| 133 | return yield* HttpServerResponse.json({ |
| 134 | byteLength: result.Blob?.byteLength ?? 0, |
| 135 | contentType: result.ContentType, |
| 136 | pricingBucket: result.PricingBucket, |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | if (request.method === "GET" && pathname === "/get-static-map") { |
| 141 | const result = yield* getStaticMap({ |
| 142 | // FileName must match ^map(@2x)?$ (it is the {FileName} path label). |
| 143 | FileName: "map", |
| 144 | // "longitude,latitude" — the Space Needle, Seattle. |
| 145 | Center: "-122.3493,47.6205", |
| 146 | Zoom: 12, |
| 147 | Width: 400, |
| 148 | Height: 300, |
| 149 | }); |
| 150 | return yield* HttpServerResponse.json({ |
| 151 | byteLength: result.Blob?.byteLength ?? 0, |
| 152 | contentType: result.ContentType, |
| 153 | pricingBucket: result.PricingBucket, |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | return yield* HttpServerResponse.json( |
| 158 | { error: "Not found", method: request.method, pathname }, |
| 159 | { status: 404 }, |
| 160 | ); |
| 161 | }).pipe(Effect.orDie), |
| 162 | }; |
| 163 | }).pipe( |
| 164 | Effect.provide( |
| 165 | Layer.mergeAll( |
| 166 | GeoPlaces.SearchTextHttp, |
| 167 | GeoPlaces.GeocodeHttp, |
| 168 | GeoPlaces.ReverseGeocodeHttp, |
| 169 | GeoPlaces.AutocompleteHttp, |
| 170 | GeoRoutes.CalculateRoutesHttp, |
| 171 | GeoRoutes.CalculateIsolinesHttp, |
| 172 | GeoMaps.GetTileHttp, |
| 173 | GeoMaps.GetStaticMapHttp, |
| 174 | ), |
| 175 | ), |
| 176 | ), |
| 177 | ); |