Skip to content

Commit

Permalink
chore: add metadata to publish on jsr registry
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeidx committed May 14, 2024
1 parent 1474db0 commit b21dd00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
8 changes: 8 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"name": "@atlasland/atlas",
"version": "0.5.2",
"exports": {
"./console": "./console/mod.ts",
"./error": "./error/mod.ts",
"./http": "./http/mod.ts",
"./log": "./log/mod.ts"
},
"compilerOptions": {
"allowJs": false,
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"],
Expand Down
43 changes: 15 additions & 28 deletions http/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,13 @@ export type HandlerMap = Map<Method, Handler>;
export type Handler<P extends Params = Record<string, string | undefined>> = (
request: Request,
context: Context<P>,
) =>
| Response
| Promise<Response>
| Record<string, unknown>
| Promise<Record<string, unknown>>;
) => Response | Promise<Response> | Record<string, unknown> | Promise<Record<string, unknown>>;

/** The Request context */
export type Context<P extends Params = Record<string, string | undefined>> =
& Partial<ConnInfo>
& {
/** The path parameters found in the URL pathname */
params: P;
};
export type Context<P extends Params = Record<string, string | undefined>> = Partial<ConnInfo> & {
/** The path parameters found in the URL pathname */
params: P;
};

/** The path parameters found in the URL pathname */
export type Params = Record<string, string | undefined>;
Expand All @@ -73,10 +67,7 @@ export class Router {
}

/** Handles an incoming request */
async handler(
request: Request,
connection?: ConnInfo,
) {
async handler(request: Request, connection?: ConnInfo) {
const { method } = request;
const { pathname, search } = new URL(request.url);

Expand Down Expand Up @@ -233,10 +224,7 @@ export async function fromFileSystem(path: string): Promise<Router> {
}

/** Find the best route match for a given request */
export function getRouteMatch(
request: Request,
routes: RouteMap,
): [Pattern, Handler] {
export function getRouteMatch(request: Request, routes: RouteMap): [Pattern, Handler] {
const method = toMethod(request.method);
const { pathname } = new URL(request.url);

Expand Down Expand Up @@ -270,10 +258,7 @@ export function notFound(): Response {
}

/** Redirects a request to a given destination. Defaults to a "307 Temporary Redirect" status code. */
export function redirect(
destination: URL | string,
status: RedirectStatus = 307,
): Response {
export function redirect(destination: URL | string, status: RedirectStatus = 307): Response {
const location = destination instanceof URL ? destination.toString() : destination;

return new Response(null, {
Expand Down Expand Up @@ -348,8 +333,7 @@ export function toPattern(pathname: string): Pattern {

/** Parses a pathname into a key-value params object for a given pattern. */
export function toParams(pathname: string, pattern: Pattern): Params {
return new URLPattern({ pathname: pattern })
.exec({ pathname })?.pathname.groups ?? {};
return new URLPattern({ pathname: pattern }).exec({ pathname })?.pathname.groups ?? {};
}

/** Transforms a given function into a Handler. Fallback to `notFound` */
Expand All @@ -370,12 +354,15 @@ export function isPattern(input: string): input is Pattern {
/** A type guard that determines if the input is a Router. */
// deno-lint-ignore no-explicit-any
export function isRouter(input: any): input is Router {
return input instanceof Router ||
input !== undefined &&
return (
input instanceof Router ||
(input !== undefined &&
input !== null &&
Array.isArray(input) === false &&
["string", "number", "boolean", "string"].includes(typeof input) === false &&
"handler" in input && isHandler(input.handler);
"handler" in input &&
isHandler(input.handler))
);
}

/** A type guard that determines if the input is a Handler. */
Expand Down

0 comments on commit b21dd00

Please sign in to comment.