Skip to content

Commit

Permalink
refactor: simplify createRouter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Sep 2, 2023
1 parent 0519d57 commit 9c87773
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
53 changes: 23 additions & 30 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
RadixNodeData,
RadixRouterOptions,
LookupOptions,
RadixRouterOptionsPayload,
InsertOptions,
StaticRoutesMap,
} from "./types";
Expand All @@ -23,49 +22,43 @@ export function createRouter<T extends RadixNodeData = RadixNodeData>(
) as StaticRoutesMap,
};

const normalizeTrailingSlash = (p: string) =>
const normalizePath = (p: string) =>
options.strictTrailingSlash ? p : p.replace(/\/$/, "") || "/";

const isRadixRouterOptionsPayload = (
payload: RadixRouterOptions["routes"][string]
): payload is RadixRouterOptionsPayload =>
typeof payload === "object" && "method" in payload && "payload" in payload;
const getInsertOptions = (p: string) =>
"method" in options
? {
path: normalizePath(p),
payload: options.routes[p].payload,
method: options.routes[p].method,
}
: {
path: normalizePath(p),
payload: options.routes[p],
};

if (options.routes) {
for (const path in options.routes) {
const routeOptions = options.routes[path];
options.method && isRadixRouterOptionsPayload(routeOptions)
? insert(ctx, {
path: normalizeTrailingSlash(path),
method: routeOptions.method,
payload: routeOptions.payload,
})
: insert(ctx, {
path: normalizeTrailingSlash(path),
payload: routeOptions,
});
insert(ctx, getInsertOptions(path));
}
}

const insertRoute = (pathOrOptions: string | InsertOptions<T>, data?: T) =>
typeof pathOrOptions === "string"
? insert(ctx, {
path: normalizeTrailingSlash(pathOrOptions),
payload: data,
})
: insert(ctx, {
path: normalizeTrailingSlash(pathOrOptions.path),
payload: pathOrOptions.payload,
method: pathOrOptions.method,
});
const insertRoute = (pathOrObj: string | InsertOptions<T>, data?: T) => {
const isStr = typeof pathOrObj === "string";
return insert(ctx, {
path: normalizePath(isStr ? pathOrObj : pathOrObj.path),
payload: isStr ? data : pathOrObj.payload,
method: isStr ? undefined : pathOrObj.method,
});
};

return {
ctx,
// @ts-expect-error - types are not matching
lookup: (path: string, options?: LookupOptions) =>
lookup(ctx, normalizeTrailingSlash(path), options),
lookup(ctx, normalizePath(path), options),
insert: insertRoute,
remove: (path: string) => remove(ctx, normalizeTrailingSlash(path)),
remove: (path: string) => remove(ctx, normalizePath(path)),
};
}

Expand Down
14 changes: 11 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ export interface RadixNode<T extends RadixNodeData = RadixNodeData> {
}

export type RadixRouterOptionsPayload = Omit<InsertOptions, "path">;
export interface RadixRouterOptions {

interface RadixRouterOptionsV1 {
strictTrailingSlash?: boolean;
method?: boolean;
routes?: Record<string, unknown> | Record<string, RadixRouterOptionsPayload>;
routes?: Record<string, unknown>;
}

interface RadixRouterOptionsV2 {
strictTrailingSlash?: boolean;
method: boolean;
routes: Record<string, RadixRouterOptionsPayload>;
}

export type RadixRouterOptions = RadixRouterOptionsV1 | RadixRouterOptionsV2;

export type StaticRoutesMap = Record<HTTPMethod, Record<string, RadixNode>>;
export interface RadixRouterContext<T extends RadixNodeData = RadixNodeData> {
options: RadixRouterOptions;
Expand Down

0 comments on commit 9c87773

Please sign in to comment.