Skip to content

Commit

Permalink
fix: updated compatible with latest deno and remove any in ts typings (
Browse files Browse the repository at this point in the history
  • Loading branch information
oknoorap authored Dec 18, 2022
1 parent 22216e2 commit 33b961f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions @types/http.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type HttpRequest = {
params?: any;
} & Request;
type HttpRequest = Request & {
params?: Record<string, unknown>;
};

type HttpResponse = {
headers: Headers;
Expand Down
30 changes: 16 additions & 14 deletions lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type BootstrapLog = {
type Server = {
port: number;
middleware: Promise<void>[];
env?: any;
env?: Record<string, unknown>;

// Hooks.
onBeforeStart: () => Promise<void>;
Expand Down Expand Up @@ -64,13 +64,14 @@ const initServer = async () => {
...(await import(customServerFile)),
};

const serverEnv: [string, any][] = [];
const serverEnv: Array<[string, unknown]> = [];
if (env instanceof Object && env.constructor === Object) {
for (const key in env) {
const value = env[key];
const envName = `SERVER_ENV_${key.toUpperCase()}`;
serverEnv.push([envName, value]);
Deno.env.set(envName, value);
const newEnv = <[string, unknown]>[envName, value];
serverEnv.push(newEnv);
Deno.env.set(envName, value as string);
}
}

Expand All @@ -91,7 +92,6 @@ export const serve = async () => {
const routers = await getRouters();

await onBeforeStart();
const server = httpServer({ port });

const logs = [
{
Expand All @@ -107,29 +107,31 @@ export const serve = async () => {
},
];

if (Object.keys(env).length > 0) {
if (env && Object.keys(env).length > 0) {
logs.push({
label: "environment",
info: Object.keys(env)
.map((item) => `- ${item}: ${env[item]}`)
.map((item) => `- ${item}: ${env?.[item]}`)
.join("\n"),
});
}

logger(logs);

await onStart();
for await (const req of server) {
const [router, params] = getRouterPatterns(routers, req.url);
httpServer(async (request: HttpRequest) => {
const [router, params] = getRouterPatterns(routers, new URL(request.url));
request.params = params;

const { headers, body, status } = await getRouterHandler(
router,
req,
request,
params
);
await req.respond({
headers,
body,

return new Response(body, {
status,
headers
});
}
}, { port });
};
12 changes: 7 additions & 5 deletions lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ enum LogColor {
Green = 32,
}

type LogMessage = string | number | Record<string, unknown> | JSON;

export const empty = () => {
console.log("");
};

export const chalk = (color: LogColor = LogColor.Cyan) =>
`\x1b[${color}m%s\x1b[0m`;

export const log = (messages: any[], options: LoggerOptions) => {
export const log = (messages: LogMessage[], options: LoggerOptions) => {
let { type = "info", label, padding = true } = options;
let color;
let logger = console.log;
Expand Down Expand Up @@ -48,19 +50,19 @@ export const log = (messages: any[], options: LoggerOptions) => {
logger(...messages);
};

export const info = (...messages: any[]) => {
export const info = (...messages: Array<LogMessage>) => {
log(messages, { type: "info" });
};

export const warn = (...messages: any[]) => {
export const warn = (...messages: Array<LogMessage>) => {
log(messages, { type: "warn" });
};

export const error = (...messages: any[]) => {
export const error = (...messages: Array<LogMessage>) => {
log(messages, { type: "error" });
};

export const success = (...messages: any[]) => {
export const success = (...messages: Array<LogMessage>) => {
log(messages, { type: "success" });
};

Expand Down
20 changes: 9 additions & 11 deletions lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ export const getRouters = async (): RouterGroups => {
* @param routerGroups Router groups
* @param url
*/
export const getRouterPatterns = (routerGroups: RouterGroups, url: string) => {
export const getRouterPatterns = (routerGroups: RouterGroups, url: URL) => {
let currentRouter: RouterMeta | undefined;
let currentGlob: string = "";
let routerParams: any = {};
let currentGlob = "";
let routerParams: Record<string, unknown> = {};

if (routerGroups[url]) {
[currentRouter] = routerGroups[url];
if (routerGroups[url.pathname]) {
[currentRouter] = routerGroups[url.pathname];
return [currentRouter, routerParams];
}

// TODO: Find best practice to find pattern efficiently
for (const routerList in routerGroups) {
const expr = new RegExp(routerList.replace(globExpr, "(.[^/]*)"), "g");

if (!expr.test(url)) {
if (!expr.test(url.pathname)) {
break;
}

Expand All @@ -180,7 +180,7 @@ export const getRouterPatterns = (routerGroups: RouterGroups, url: string) => {

// Set params
if (currentRouter) {
const params = getURLParams(url, currentGlob);
const params = getURLParams(url.pathname, currentGlob);
routerParams = Object.fromEntries(
currentRouter.params.map((item: RouterMeta, index: number) => [
item,
Expand All @@ -205,7 +205,7 @@ enum CommonContentType {
export const getRouterHandler = async (
router: RouterMeta,
request: HttpRequest,
params: any
params: Record<string, unknown>
): Promise<HttpResponse> => {
let body;
const headers = new Headers();
Expand All @@ -219,9 +219,7 @@ export const getRouterHandler = async (
},
};

if (!router) {
return response;
}
if (!router) return response;

const defaultRouter: Router = {
headers: {},
Expand Down

0 comments on commit 33b961f

Please sign in to comment.