routefly 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/HonoServerAdapter-M5Q3KGEI.js +6 -0
- package/dist/chunk-GAM7LLVT.js +117 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +132 -0
- package/package.json +24 -4
- package/README.md +0 -3
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/adapters/HonoServerAdapter.ts
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { serve } from "@hono/node-server";
|
|
4
|
+
var HonoServerAdapter = class {
|
|
5
|
+
app;
|
|
6
|
+
globalMiddlewares = [];
|
|
7
|
+
constructor() {
|
|
8
|
+
this.app = new Hono();
|
|
9
|
+
}
|
|
10
|
+
use(middleware) {
|
|
11
|
+
this.globalMiddlewares.push(middleware);
|
|
12
|
+
this.app.use("*", async (c, next) => {
|
|
13
|
+
await middleware(c, next);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
addRoute(method, path, handler, middlewares = []) {
|
|
17
|
+
const honoMiddlewares = middlewares.map((mw) => async (c, next) => {
|
|
18
|
+
const req = await this.buildRequest(c);
|
|
19
|
+
const res = this.createResponse();
|
|
20
|
+
await mw(req, res, next);
|
|
21
|
+
});
|
|
22
|
+
this.app.on(method.toUpperCase(), path, ...honoMiddlewares, async (c) => {
|
|
23
|
+
try {
|
|
24
|
+
const req = await this.buildRequest(c);
|
|
25
|
+
const res = this.createResponse();
|
|
26
|
+
const result = await handler(req, res);
|
|
27
|
+
if (result instanceof Response) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
if (result !== void 0) {
|
|
31
|
+
return c.json(result);
|
|
32
|
+
}
|
|
33
|
+
return c.body(null, 204);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(`[Framework Error] ${method} ${path}:`, error);
|
|
36
|
+
return c.json(
|
|
37
|
+
{
|
|
38
|
+
error: "Internal Server Error",
|
|
39
|
+
message: process.env.NODE_ENV === "development" ? error.message : "Something went wrong"
|
|
40
|
+
},
|
|
41
|
+
500
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async buildRequest(c) {
|
|
47
|
+
const rawBody = await c.req.text();
|
|
48
|
+
let body = rawBody;
|
|
49
|
+
const contentType = c.req.header("content-type") || "";
|
|
50
|
+
if (contentType.includes("application/json") && rawBody) {
|
|
51
|
+
try {
|
|
52
|
+
body = JSON.parse(rawBody);
|
|
53
|
+
} catch {
|
|
54
|
+
body = {};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
body,
|
|
59
|
+
params: c.req.param(),
|
|
60
|
+
query: c.req.query(),
|
|
61
|
+
headers: Object.fromEntries(c.req.raw.headers.entries()),
|
|
62
|
+
cookies: this.parseCookies(c.req.header("cookie") || "")
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
parseCookies(cookieString) {
|
|
66
|
+
const cookies = {};
|
|
67
|
+
cookieString.split(";").forEach((cookie) => {
|
|
68
|
+
const [key, value] = cookie.split("=").map((c) => c.trim());
|
|
69
|
+
if (key) cookies[key] = decodeURIComponent(value);
|
|
70
|
+
});
|
|
71
|
+
return cookies;
|
|
72
|
+
}
|
|
73
|
+
createResponse() {
|
|
74
|
+
let statusCode = 200;
|
|
75
|
+
const headers = new Headers({
|
|
76
|
+
"X-Content-Type-Options": "nosniff",
|
|
77
|
+
"X-Frame-Options": "DENY"
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
status: (code) => {
|
|
81
|
+
statusCode = code;
|
|
82
|
+
return this;
|
|
83
|
+
},
|
|
84
|
+
setHeader: (key, value) => {
|
|
85
|
+
headers.set(key, value);
|
|
86
|
+
return this;
|
|
87
|
+
},
|
|
88
|
+
json: (data, status) => {
|
|
89
|
+
headers.set("Content-Type", "application/json");
|
|
90
|
+
return new Response(JSON.stringify(data), { status: status || statusCode, headers });
|
|
91
|
+
},
|
|
92
|
+
html: (html, status) => {
|
|
93
|
+
headers.set("Content-Type", "text/html");
|
|
94
|
+
return new Response(html, { status: status || statusCode, headers });
|
|
95
|
+
},
|
|
96
|
+
redirect: (url, status = 302) => {
|
|
97
|
+
headers.set("Location", url);
|
|
98
|
+
return new Response(null, { status, headers });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
start(port) {
|
|
103
|
+
console.log(`\u{1F680} Server listening on http://localhost:${port}`);
|
|
104
|
+
serve({
|
|
105
|
+
fetch: this.app.fetch,
|
|
106
|
+
port
|
|
107
|
+
});
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
}
|
|
110
|
+
getApp() {
|
|
111
|
+
return this.app;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export {
|
|
116
|
+
HonoServerAdapter
|
|
117
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
interface AppRequest {
|
|
2
|
+
body: any;
|
|
3
|
+
params: Record<string, string>;
|
|
4
|
+
query: Record<string, string>;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
cookies: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
interface AppResponse {
|
|
9
|
+
status(code: number): this;
|
|
10
|
+
setHeader(key: string, value: string): this;
|
|
11
|
+
json(data: any, status?: number): Response;
|
|
12
|
+
html(html: string, status?: number): Response;
|
|
13
|
+
redirect(url: string, status?: number): Response;
|
|
14
|
+
}
|
|
15
|
+
interface ServerPort {
|
|
16
|
+
addRoute(method: string, path: string, handler: Function, middlewares?: Function[]): void;
|
|
17
|
+
use(middleware: Function): void;
|
|
18
|
+
start(port: number): Promise<void>;
|
|
19
|
+
getApp(): any;
|
|
20
|
+
}
|
|
21
|
+
interface RouteDefinition {
|
|
22
|
+
method: string;
|
|
23
|
+
path: string;
|
|
24
|
+
handler: Function;
|
|
25
|
+
middlewares: Function[];
|
|
26
|
+
}
|
|
27
|
+
type ConfigServer = {
|
|
28
|
+
server: ServerPort | "default";
|
|
29
|
+
root: string;
|
|
30
|
+
filePattern: string;
|
|
31
|
+
exclude: string[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
declare class RouteRegistry {
|
|
35
|
+
private static routes;
|
|
36
|
+
static add(route: RouteDefinition): void;
|
|
37
|
+
static getAll(): RouteDefinition[];
|
|
38
|
+
static clear(): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare const Get: (path: string, ...middlewares: Function[]) => (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
42
|
+
declare const Post: (path: string, ...middlewares: Function[]) => (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
43
|
+
declare const Put: (path: string, ...middlewares: Function[]) => (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
44
|
+
declare const Patch: (path: string, ...middlewares: Function[]) => (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
45
|
+
declare const Delete: (path: string, ...middlewares: Function[]) => (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
46
|
+
|
|
47
|
+
declare class HonoServerAdapter implements ServerPort {
|
|
48
|
+
private app;
|
|
49
|
+
private globalMiddlewares;
|
|
50
|
+
constructor();
|
|
51
|
+
use(middleware: Function): void;
|
|
52
|
+
addRoute(method: string, path: string, handler: Function, middlewares?: Function[]): void;
|
|
53
|
+
private buildRequest;
|
|
54
|
+
private parseCookies;
|
|
55
|
+
private createResponse;
|
|
56
|
+
start(port: number): Promise<void>;
|
|
57
|
+
getApp(): any;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare function bootstrap(config?: ConfigServer): Promise<ServerPort>;
|
|
61
|
+
|
|
62
|
+
declare function loadConfig(): Promise<ConfigServer>;
|
|
63
|
+
|
|
64
|
+
export { type AppRequest, type AppResponse, type ConfigServer, Delete, Get, HonoServerAdapter, Patch, Post, Put, type RouteDefinition, RouteRegistry, type ServerPort, bootstrap, loadConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HonoServerAdapter
|
|
3
|
+
} from "./chunk-GAM7LLVT.js";
|
|
4
|
+
|
|
5
|
+
// src/core/registry.ts
|
|
6
|
+
var RouteRegistry = class {
|
|
7
|
+
static routes = [];
|
|
8
|
+
static add(route) {
|
|
9
|
+
this.routes.push(route);
|
|
10
|
+
}
|
|
11
|
+
static getAll() {
|
|
12
|
+
return this.routes;
|
|
13
|
+
}
|
|
14
|
+
static clear() {
|
|
15
|
+
this.routes = [];
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/core/decorators.ts
|
|
20
|
+
function createRouteDecorator(method) {
|
|
21
|
+
return function(path, ...middlewares) {
|
|
22
|
+
return function(target, propertyKey, descriptor) {
|
|
23
|
+
const originalMethod = descriptor.value;
|
|
24
|
+
const handler = async function(req, res) {
|
|
25
|
+
const instance = new target.constructor();
|
|
26
|
+
return originalMethod.call(instance, req, res);
|
|
27
|
+
};
|
|
28
|
+
RouteRegistry.add({
|
|
29
|
+
method: method.toUpperCase(),
|
|
30
|
+
path,
|
|
31
|
+
handler,
|
|
32
|
+
middlewares
|
|
33
|
+
});
|
|
34
|
+
return descriptor;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
var Get = createRouteDecorator("GET");
|
|
39
|
+
var Post = createRouteDecorator("POST");
|
|
40
|
+
var Put = createRouteDecorator("PUT");
|
|
41
|
+
var Patch = createRouteDecorator("PATCH");
|
|
42
|
+
var Delete = createRouteDecorator("DELETE");
|
|
43
|
+
|
|
44
|
+
// src/bootstrap.ts
|
|
45
|
+
import { readdir } from "fs/promises";
|
|
46
|
+
import { join as join2, resolve } from "path";
|
|
47
|
+
import { pathToFileURL as pathToFileURL2 } from "url";
|
|
48
|
+
|
|
49
|
+
// src/config-loader.ts
|
|
50
|
+
import { existsSync, writeFileSync } from "fs";
|
|
51
|
+
import { join } from "path";
|
|
52
|
+
import { pathToFileURL } from "url";
|
|
53
|
+
var DEFAULT_CONFIG = {
|
|
54
|
+
server: "default",
|
|
55
|
+
root: "./src",
|
|
56
|
+
filePattern: "*.routes.ts",
|
|
57
|
+
exclude: ["node_modules", "dist", "utils", "types", "domain"]
|
|
58
|
+
};
|
|
59
|
+
var CONFIG_TEMPLATE = `import type { ConfigServer } from "routefly";
|
|
60
|
+
|
|
61
|
+
export const config: ConfigServer = {
|
|
62
|
+
server: "default",
|
|
63
|
+
root: "./src",
|
|
64
|
+
filePattern: "*.routes.ts",
|
|
65
|
+
exclude: ["node_modules", "dist", "utils", "types", "domain"]
|
|
66
|
+
};
|
|
67
|
+
`;
|
|
68
|
+
async function loadConfig() {
|
|
69
|
+
const cwd = process.cwd();
|
|
70
|
+
const tsPath = join(cwd, "config.server.ts");
|
|
71
|
+
const jsPath = join(cwd, "config.server.js");
|
|
72
|
+
let config;
|
|
73
|
+
if (existsSync(tsPath) || existsSync(jsPath)) {
|
|
74
|
+
const finalPath = existsSync(tsPath) ? tsPath : jsPath;
|
|
75
|
+
const module = await import(pathToFileURL(finalPath).href);
|
|
76
|
+
if (!module.config) {
|
|
77
|
+
throw new Error('config.server.ts precisa exportar um objeto "config"');
|
|
78
|
+
}
|
|
79
|
+
config = module.config;
|
|
80
|
+
} else {
|
|
81
|
+
writeFileSync(tsPath, CONFIG_TEMPLATE, { flag: "wx" });
|
|
82
|
+
config = { ...DEFAULT_CONFIG };
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
...DEFAULT_CONFIG,
|
|
86
|
+
...config,
|
|
87
|
+
server: config.server === "default" ? new HonoServerAdapter() : config.server
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/bootstrap.ts
|
|
92
|
+
async function bootstrap(config) {
|
|
93
|
+
const finalConfig = config ?? await loadConfig();
|
|
94
|
+
const server = finalConfig.server === "default" ? new (await import("./HonoServerAdapter-M5Q3KGEI.js")).HonoServerAdapter() : finalConfig.server;
|
|
95
|
+
const absoluteRoot = resolve(process.cwd(), finalConfig.root);
|
|
96
|
+
const suffix = finalConfig.filePattern.replace("*", "");
|
|
97
|
+
const files = await scanDirectory(absoluteRoot, suffix, finalConfig.exclude);
|
|
98
|
+
files.sort();
|
|
99
|
+
for (const file of files) {
|
|
100
|
+
await import(pathToFileURL2(file).href);
|
|
101
|
+
}
|
|
102
|
+
for (const route of RouteRegistry.getAll()) {
|
|
103
|
+
server.addRoute(route.method, route.path, route.handler, route.middlewares);
|
|
104
|
+
}
|
|
105
|
+
return server;
|
|
106
|
+
}
|
|
107
|
+
async function scanDirectory(dir, suffix, exclude) {
|
|
108
|
+
const files = [];
|
|
109
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
110
|
+
for (const entry of entries) {
|
|
111
|
+
const fullPath = join2(dir, entry.name);
|
|
112
|
+
if (entry.isDirectory()) {
|
|
113
|
+
if (!exclude.includes(entry.name)) {
|
|
114
|
+
files.push(...await scanDirectory(fullPath, suffix, exclude));
|
|
115
|
+
}
|
|
116
|
+
} else if (entry.isFile() && entry.name.endsWith(suffix)) {
|
|
117
|
+
files.push(fullPath);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return files;
|
|
121
|
+
}
|
|
122
|
+
export {
|
|
123
|
+
Delete,
|
|
124
|
+
Get,
|
|
125
|
+
HonoServerAdapter,
|
|
126
|
+
Patch,
|
|
127
|
+
Post,
|
|
128
|
+
Put,
|
|
129
|
+
RouteRegistry,
|
|
130
|
+
bootstrap,
|
|
131
|
+
loadConfig
|
|
132
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "routefly",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Framework web com decorator",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup",
|
|
10
|
+
"dev": "tsup --watch",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@hono/node-server": "^2.1.11",
|
|
15
|
+
"hono": "^4.0.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/bun": "^1.4.5",
|
|
19
|
+
"@types/node": "^20.19.43",
|
|
20
|
+
"tsup": "^8.0.0",
|
|
21
|
+
"typescript": "^5.3.0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
]
|
|
26
|
+
}
|
package/README.md
DELETED