feat: add relay factory
This commit is contained in:
@@ -7,7 +7,7 @@ import { Route, RouteMethod } from "./route.ts";
|
|||||||
|
|
||||||
const SUPPORTED_MEHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
const SUPPORTED_MEHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
||||||
|
|
||||||
export class RelayApi<TRelays extends (Procedure | Route)[]> {
|
export class Api<TRelays extends (Procedure | Route)[]> {
|
||||||
readonly #index = {
|
readonly #index = {
|
||||||
rest: new Map<string, Route>(),
|
rest: new Map<string, Route>(),
|
||||||
rpc: new Map<string, Procedure>(),
|
rpc: new Map<string, Procedure>(),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { Api } from "./api.ts";
|
||||||
import { makeRelayClient, RelayClient, RelayClientConfig } from "./client.ts";
|
import { makeRelayClient, RelayClient, RelayClientConfig } from "./client.ts";
|
||||||
import { Procedure } from "./procedure.ts";
|
import { Procedure } from "./procedure.ts";
|
||||||
import { Route, RouteMethod } from "./route.ts";
|
import { Route, RouteMethod } from "./route.ts";
|
||||||
@@ -24,6 +25,15 @@ export class Relay<
|
|||||||
indexRelays(relays, this.#index);
|
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.
|
* Create a new relay client instance from the instance procedures.
|
||||||
*
|
*
|
||||||
|
|||||||
1
mod.ts
1
mod.ts
@@ -1,6 +1,5 @@
|
|||||||
export * from "./libraries/action.ts";
|
export * from "./libraries/action.ts";
|
||||||
export * from "./libraries/adapter.ts";
|
export * from "./libraries/adapter.ts";
|
||||||
export * from "./libraries/api.ts";
|
|
||||||
export * from "./libraries/errors.ts";
|
export * from "./libraries/errors.ts";
|
||||||
export * from "./libraries/procedure.ts";
|
export * from "./libraries/procedure.ts";
|
||||||
export * from "./libraries/relay.ts";
|
export * from "./libraries/relay.ts";
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { RelayApi } from "../../libraries/api.ts";
|
|
||||||
import { NotFoundError } from "../../mod.ts";
|
import { NotFoundError } from "../../mod.ts";
|
||||||
import { addNumbers } from "./actions.ts";
|
import { addNumbers } from "./actions.ts";
|
||||||
import { relay } from "./relay.ts";
|
import { relay } from "./relay.ts";
|
||||||
@@ -6,7 +5,7 @@ import { User } from "./user.ts";
|
|||||||
|
|
||||||
export let users: User[] = [];
|
export let users: User[] = [];
|
||||||
|
|
||||||
export const api = new RelayApi([
|
export const api = relay.api([
|
||||||
relay.method("user:create").handle(async ({ name, email }) => {
|
relay.method("user:create").handle(async ({ name, email }) => {
|
||||||
const id = crypto.randomUUID();
|
const id = crypto.randomUUID();
|
||||||
users.push({ id, name, email, createdAt: new Date() });
|
users.push({ id, name, email, createdAt: new Date() });
|
||||||
|
|||||||
Reference in New Issue
Block a user