IzzyBeraja / Flags

0 stars 0 forks source link

Make it easier to build route types #48

Closed IzzyBeraja closed 10 months ago

IzzyBeraja commented 11 months ago

Currently I create the type for each route as follows:

import type { RequestHandlerAsync, Params } from "../../../types/types.js";

export interface PostRequest {}

type UserCreated = {
  createdUser: Account;
};

type UserNotCreated = {
  error: string;
};

export type PostResponse = UserCreated | UserNotCreated;

export const PostRequestSchema {}

type PostRouteHandler = RequestHandlerAsync<Params, PostResponse, PostRequest>;

export const Post: PostRouteHandler = () => {};

Instead I'd like to something closer to this as it's clearer:

import type { Error, RequestHandlerAsync } from "../../../types/types.js";

export interface PostRequest {}
export interface PostResponse {
  createdUser: Account;
}

export const PostRequestSchema {}

type PostHandler = {
  request: PostRequest;
  response: PostResponse | Error;
};

export const Post: RouteHandlerAsync<PostHandler> = () => {};

Namely, I'd like to move the handler into its own object, that doesn't require me to include stuff like Params Dictionary or QueryString if I don't need them. As well, I'd like to centralize my error handling and include it in the handler type so it's easier to see that it can error out. The first approach modifies the PostResponse which is messy since it needs more types.

IzzyBeraja commented 10 months ago

Added in commit 76c52cf51268221cd5b94051948a2766a1f2e944