Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 28 additions & 5 deletions src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,22 @@ export type EndpointContext<Path extends string, Options extends EndpointOptions
) => APIError;
};

export const createEndpoint = <Path extends string, Options extends EndpointOptions, R>(
export function createEndpoint<Path extends string, R>(
path: Path,
handler: (context: EndpointContext<Path, { method: "GET" }>) => Promise<R>,
): Endpoint<Path, { method: "GET" }, (inputCtx?: InputContext<Path, { method: "GET" }>) => Promise<R>>;
export function createEndpoint<Path extends string, Options extends EndpointOptions, R>(
path: Path,
options: Options,
handler: (context: EndpointContext<Path, Options>) => Promise<R>,
) => {
): Endpoint<Path, Options, (inputCtx?: InputContext<Path, Options>) => Promise<R>>;
export function createEndpoint<Path extends string, Options extends EndpointOptions, R>(
path: Path,
optionsOrHandler: Options | ((context: EndpointContext<Path, Options>) => Promise<R>),
handlerOrUndefined?: (context: EndpointContext<Path, Options>) => Promise<R>,
) {
const options = (handlerOrUndefined ? optionsOrHandler : { method: "GET" }) as Options;
const handler = (handlerOrUndefined || optionsOrHandler) as (context: EndpointContext<Path, Options>) => Promise<R>;
type Context = InputContext<Path, Options>;
const internalHandler = async <
AsResponse extends boolean = false,
Expand Down Expand Up @@ -371,11 +382,22 @@ export const createEndpoint = <Path extends string, Options extends EndpointOpti
};

createEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {
return <Path extends string, Opts extends EndpointOptions, R>(
function create<Path extends string, R>(
path: Path,
handler: (ctx: EndpointContext<Path, { method: "GET" }, InferUse<E["use"]>>) => Promise<R>,
): Endpoint<Path, { method: "GET" }, (inputCtx?: InputContext<Path, { method: "GET" }>) => Promise<R>>;
function create<Path extends string, Opts extends EndpointOptions, R>(
path: Path,
options: Opts,
handler: (ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => Promise<R>,
) => {
): Endpoint<Path, Opts, (inputCtx?: InputContext<Path, Opts>) => Promise<R>>;
function create<Path extends string, Opts extends EndpointOptions, R>(
path: Path,
optionsOrHandler: Opts | ((ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => Promise<R>),
handlerOrUndefined?: (ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => Promise<R>,
) {
const options = (handlerOrUndefined ? optionsOrHandler : { method: "GET" }) as Opts;
const handler = (handlerOrUndefined || optionsOrHandler) as (ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => Promise<R>;
return createEndpoint(
path,
{
Expand All @@ -384,7 +406,8 @@ createEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {
},
handler,
);
};
}
return create;
};

export type Endpoint<
Expand Down