Template
1
0

feat: add relay factory

This commit is contained in:
2025-04-25 19:02:22 +00:00
parent 9ff4d2c5fc
commit ee67183a4c
4 changed files with 12 additions and 4 deletions

View File

@@ -7,7 +7,7 @@ import { Route, RouteMethod } from "./route.ts";
const SUPPORTED_MEHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
export class RelayApi<TRelays extends (Procedure | Route)[]> {
export class Api<TRelays extends (Procedure | Route)[]> {
readonly #index = {
rest: new Map<string, Route>(),
rpc: new Map<string, Procedure>(),

View File

@@ -1,3 +1,4 @@
import { Api } from "./api.ts";
import { makeRelayClient, RelayClient, RelayClientConfig } from "./client.ts";
import { Procedure } from "./procedure.ts";
import { Route, RouteMethod } from "./route.ts";
@@ -24,6 +25,15 @@ export class Relay<
indexRelays(relays, this.#index);
}
/**
* Create a new relay api instance with the given relays.
*
* @param relays - List of relays to handle.
*/
api<TRelays extends (Procedure | Route)[]>(relays: TRelays): Api<TRelays> {
return new Api<TRelays>(relays);
}
/**
* Create a new relay client instance from the instance procedures.
*

1
mod.ts
View File

@@ -1,6 +1,5 @@
export * from "./libraries/action.ts";
export * from "./libraries/adapter.ts";
export * from "./libraries/api.ts";
export * from "./libraries/errors.ts";
export * from "./libraries/procedure.ts";
export * from "./libraries/relay.ts";

View File

@@ -1,4 +1,3 @@
import { RelayApi } from "../../libraries/api.ts";
import { NotFoundError } from "../../mod.ts";
import { addNumbers } from "./actions.ts";
import { relay } from "./relay.ts";
@@ -6,7 +5,7 @@ import { User } from "./user.ts";
export let users: User[] = [];
export const api = new RelayApi([
export const api = relay.api([
relay.method("user:create").handle(async ({ name, email }) => {
const id = crypto.randomUUID();
users.push({ id, name, email, createdAt: new Date() });