From 7df498d743a59a0ddfc72cc5d50deac64fd0bb7f Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Mon, 30 Jun 2025 17:13:32 -0300 Subject: [PATCH] feat: createEndpoint accepts 2 arguments and uses GET as method. --- src/endpoint.test.ts | 10 ++++++++++ src/endpoint.ts | 33 ++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/endpoint.test.ts b/src/endpoint.test.ts index 2ed46d6..d7d5dff 100644 --- a/src/endpoint.test.ts +++ b/src/endpoint.test.ts @@ -278,6 +278,16 @@ describe("types", async () => { ); }); + it("default GET method", async () => { + createEndpoint( + "/api/*", + async (ctx) => { + expectTypeOf(ctx.params).toEqualTypeOf<{ _: string }>(); + }, + ); + + }); + it("method", async () => { createEndpoint( "/test", diff --git a/src/endpoint.ts b/src/endpoint.ts index bd5f2b7..bd05301 100644 --- a/src/endpoint.ts +++ b/src/endpoint.ts @@ -314,11 +314,22 @@ export type EndpointContext APIError; }; -export const createEndpoint = ( +export function createEndpoint( + path: Path, + handler: (context: EndpointContext) => Promise, +): Endpoint) => Promise>; +export function createEndpoint( path: Path, options: Options, handler: (context: EndpointContext) => Promise, -) => { +): Endpoint) => Promise>; +export function createEndpoint( + path: Path, + optionsOrHandler: Options | ((context: EndpointContext) => Promise), + handlerOrUndefined?: (context: EndpointContext) => Promise, +) { + const options = (handlerOrUndefined ? optionsOrHandler : { method: "GET" }) as Options; + const handler = (handlerOrUndefined || optionsOrHandler) as (context: EndpointContext) => Promise; type Context = InputContext; const internalHandler = async < AsResponse extends boolean = false, @@ -371,11 +382,22 @@ export const createEndpoint = (opts?: E) => { - return ( + function create( + path: Path, + handler: (ctx: EndpointContext>) => Promise, + ): Endpoint) => Promise>; + function create( path: Path, options: Opts, handler: (ctx: EndpointContext>) => Promise, - ) => { + ): Endpoint) => Promise>; + function create( + path: Path, + optionsOrHandler: Opts | ((ctx: EndpointContext>) => Promise), + handlerOrUndefined?: (ctx: EndpointContext>) => Promise, + ) { + const options = (handlerOrUndefined ? optionsOrHandler : { method: "GET" }) as Opts; + const handler = (handlerOrUndefined || optionsOrHandler) as (ctx: EndpointContext>) => Promise; return createEndpoint( path, { @@ -384,7 +406,8 @@ createEndpoint.create = (opts?: E) => { }, handler, ); - }; + } + return create; }; export type Endpoint<