| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Loopback from "../globals/Loopback.ts"; |
| 3 | import type * as LoopbackServer from "../globals/LoopbackServer.ts"; |
| 4 | import * as Plugin from "../Plugin.ts"; |
| 5 | import type { BindingHook } from "../PluginContext.ts"; |
| 6 | |
| 7 | export interface LoopbackProps { |
| 8 | /** |
| 9 | * Name of the binding to register. |
| 10 | */ |
| 11 | readonly binding: string; |
| 12 | /** |
| 13 | * Name of the route to register. This must be unique within the current loopback server. |
| 14 | */ |
| 15 | readonly name: string; |
| 16 | /** |
| 17 | * Handler to invoke when the route is matched. |
| 18 | */ |
| 19 | readonly handler: LoopbackServer.RouteHandler; |
| 20 | } |
| 21 | |
| 22 | export const local = ({ |
| 23 | binding, |
| 24 | name, |
| 25 | handler, |
| 26 | }: LoopbackProps): BindingHook<Loopback.Loopback> => |
| 27 | Effect.map( |
| 28 | Plugin.use(Loopback.Loopback, (loopback) => |
| 29 | loopback.api.route(name, handler), |
| 30 | ), |
| 31 | (service) => ({ |
| 32 | name: binding, |
| 33 | service, |
| 34 | }), |
| 35 | ); |