@objectstack/hono 4.0.3 → 4.0.5
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/README.md +83 -8
- package/dist/index.d.mts +44 -1
- package/dist/index.d.ts +44 -1
- package/dist/index.js +66 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +35 -6
- package/.turbo/turbo-build.log +0 -22
- package/CHANGELOG.md +0 -365
- package/src/__mocks__/runtime.ts +0 -16
- package/src/hono.test.ts +0 -950
- package/src/index.ts +0 -217
- package/tsconfig.json +0 -26
- package/vitest.config.ts +0 -16
package/src/index.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { Hono } from 'hono';
|
|
4
|
-
import { type ObjectKernel, HttpDispatcher, HttpDispatcherResult } from '@objectstack/runtime';
|
|
5
|
-
|
|
6
|
-
export interface ObjectStackHonoOptions {
|
|
7
|
-
kernel: ObjectKernel;
|
|
8
|
-
prefix?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Auth service interface with handleRequest method
|
|
13
|
-
*/
|
|
14
|
-
interface AuthService {
|
|
15
|
-
handleRequest(request: Request): Promise<Response>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Middleware mode for existing Hono apps
|
|
20
|
-
*/
|
|
21
|
-
export function objectStackMiddleware(kernel: ObjectKernel) {
|
|
22
|
-
return async (c: any, next: any) => {
|
|
23
|
-
c.set('objectStack', kernel);
|
|
24
|
-
await next();
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Creates a full-featured Hono app with all ObjectStack route dispatchers.
|
|
30
|
-
*
|
|
31
|
-
* Only routes that need framework-specific handling (auth service, storage
|
|
32
|
-
* formData, GraphQL raw result, discovery wrapper) are registered explicitly.
|
|
33
|
-
* All other routes (meta, data, packages, analytics, automation, i18n, ui,
|
|
34
|
-
* openapi, custom endpoints, and any future routes) are handled by a
|
|
35
|
-
* catch-all that delegates to `HttpDispatcher.dispatch()`.
|
|
36
|
-
*
|
|
37
|
-
* This means new routes added to `HttpDispatcher` automatically work in
|
|
38
|
-
* every adapter without any adapter-side code changes.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* import { createHonoApp } from '@objectstack/hono';
|
|
43
|
-
* const app = createHonoApp({ kernel });
|
|
44
|
-
* export default app;
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export function createHonoApp(options: ObjectStackHonoOptions): Hono {
|
|
48
|
-
const app = new Hono();
|
|
49
|
-
const prefix = options.prefix || '/api';
|
|
50
|
-
const dispatcher = new HttpDispatcher(options.kernel);
|
|
51
|
-
|
|
52
|
-
const errorJson = (c: any, message: string, code: number = 500) => {
|
|
53
|
-
return c.json({ success: false, error: { message, code } }, code);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const toResponse = (c: any, result: HttpDispatcherResult) => {
|
|
57
|
-
if (result.handled) {
|
|
58
|
-
if (result.response) {
|
|
59
|
-
if (result.response.headers) {
|
|
60
|
-
Object.entries(result.response.headers).forEach(([k, v]) => c.header(k, v as string));
|
|
61
|
-
}
|
|
62
|
-
return c.json(result.response.body, result.response.status);
|
|
63
|
-
}
|
|
64
|
-
if (result.result) {
|
|
65
|
-
const res = result.result;
|
|
66
|
-
if (res.type === 'redirect' && res.url) {
|
|
67
|
-
return c.redirect(res.url);
|
|
68
|
-
}
|
|
69
|
-
if (res.type === 'stream' && res.events) {
|
|
70
|
-
// SSE / Vercel Data Stream streaming response
|
|
71
|
-
const headers: Record<string, string> = {
|
|
72
|
-
'Content-Type': res.contentType || 'text/event-stream',
|
|
73
|
-
'Cache-Control': 'no-cache',
|
|
74
|
-
'Connection': 'keep-alive',
|
|
75
|
-
...(res.headers || {}),
|
|
76
|
-
};
|
|
77
|
-
const stream = new ReadableStream({
|
|
78
|
-
async start(controller) {
|
|
79
|
-
try {
|
|
80
|
-
const encoder = new TextEncoder();
|
|
81
|
-
for await (const event of res.events) {
|
|
82
|
-
const chunk = res.vercelDataStream
|
|
83
|
-
? (typeof event === 'string' ? event : JSON.stringify(event) + '\n')
|
|
84
|
-
: `data: ${JSON.stringify(event)}\n\n`;
|
|
85
|
-
controller.enqueue(encoder.encode(chunk));
|
|
86
|
-
}
|
|
87
|
-
} catch (err) {
|
|
88
|
-
// Stream error — close gracefully
|
|
89
|
-
} finally {
|
|
90
|
-
controller.close();
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
return new Response(stream, { status: 200, headers });
|
|
95
|
-
}
|
|
96
|
-
if (res.type === 'stream' && res.stream) {
|
|
97
|
-
if (res.headers) {
|
|
98
|
-
Object.entries(res.headers).forEach(([k, v]) => c.header(k, v as string));
|
|
99
|
-
}
|
|
100
|
-
return new Response(res.stream, { status: 200 });
|
|
101
|
-
}
|
|
102
|
-
return c.json(res, 200);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return errorJson(c, 'Not Found', 404);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// ─── Explicit routes (framework-specific handling required) ────────────────
|
|
109
|
-
|
|
110
|
-
// --- Discovery ---
|
|
111
|
-
app.get(prefix, async (c) => {
|
|
112
|
-
return c.json({ data: await dispatcher.getDiscoveryInfo(prefix) });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
app.get(`${prefix}/discovery`, async (c) => {
|
|
116
|
-
return c.json({ data: await dispatcher.getDiscoveryInfo(prefix) });
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// --- .well-known ---
|
|
120
|
-
app.get('/.well-known/objectstack', (c) => {
|
|
121
|
-
return c.redirect(prefix);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// --- Auth (needs auth service integration) ---
|
|
125
|
-
app.all(`${prefix}/auth/*`, async (c) => {
|
|
126
|
-
try {
|
|
127
|
-
const path = c.req.path.substring(`${prefix}/auth/`.length);
|
|
128
|
-
const method = c.req.method;
|
|
129
|
-
|
|
130
|
-
// Try AuthPlugin service first (prefer async to support factory-based services)
|
|
131
|
-
let authService: AuthService | null = null;
|
|
132
|
-
try {
|
|
133
|
-
if (typeof options.kernel.getServiceAsync === 'function') {
|
|
134
|
-
authService = await options.kernel.getServiceAsync<AuthService>('auth');
|
|
135
|
-
} else if (typeof options.kernel.getService === 'function') {
|
|
136
|
-
authService = options.kernel.getService<AuthService>('auth');
|
|
137
|
-
}
|
|
138
|
-
} catch {
|
|
139
|
-
// Service not registered — fall through to dispatcher
|
|
140
|
-
authService = null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (authService && typeof authService.handleRequest === 'function') {
|
|
144
|
-
const response = await authService.handleRequest(c.req.raw);
|
|
145
|
-
return new Response(response.body, {
|
|
146
|
-
status: response.status,
|
|
147
|
-
headers: response.headers,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Fallback to legacy dispatcher
|
|
152
|
-
const body = method === 'GET' || method === 'HEAD'
|
|
153
|
-
? {}
|
|
154
|
-
: await c.req.json().catch(() => ({}));
|
|
155
|
-
const result = await dispatcher.handleAuth(path, method, body, { request: c.req.raw });
|
|
156
|
-
return toResponse(c, result);
|
|
157
|
-
} catch (err: any) {
|
|
158
|
-
return errorJson(c, err.message || 'Internal Server Error', err.statusCode || 500);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// --- GraphQL (returns raw result, not HttpDispatcherResult) ---
|
|
163
|
-
app.post(`${prefix}/graphql`, async (c) => {
|
|
164
|
-
try {
|
|
165
|
-
const body = await c.req.json();
|
|
166
|
-
const result = await dispatcher.handleGraphQL(body, { request: c.req.raw });
|
|
167
|
-
return c.json(result);
|
|
168
|
-
} catch (err: any) {
|
|
169
|
-
return errorJson(c, err.message || 'Internal Server Error', err.statusCode || 500);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
// --- Storage (needs formData parsing) ---
|
|
174
|
-
app.all(`${prefix}/storage/*`, async (c) => {
|
|
175
|
-
try {
|
|
176
|
-
const subPath = c.req.path.substring(`${prefix}/storage`.length);
|
|
177
|
-
const method = c.req.method;
|
|
178
|
-
|
|
179
|
-
let file: any = undefined;
|
|
180
|
-
if (method === 'POST' && subPath === '/upload') {
|
|
181
|
-
const formData = await c.req.formData();
|
|
182
|
-
file = formData.get('file');
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const result = await dispatcher.handleStorage(subPath, method, file, { request: c.req.raw });
|
|
186
|
-
return toResponse(c, result);
|
|
187
|
-
} catch (err: any) {
|
|
188
|
-
return errorJson(c, err.message || 'Internal Server Error', err.statusCode || 500);
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// ─── Catch-all: delegate to dispatcher.dispatch() ─────────────────────────
|
|
193
|
-
// Handles meta, data, packages, analytics, automation, i18n, ui, openapi,
|
|
194
|
-
// custom API endpoints, and any future routes added to HttpDispatcher.
|
|
195
|
-
app.all(`${prefix}/*`, async (c) => {
|
|
196
|
-
try {
|
|
197
|
-
const subPath = c.req.path.substring(prefix.length);
|
|
198
|
-
const method = c.req.method;
|
|
199
|
-
|
|
200
|
-
let body: any = undefined;
|
|
201
|
-
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
|
|
202
|
-
body = await c.req.json().catch(() => ({}));
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const queryParams: Record<string, any> = {};
|
|
206
|
-
const url = new URL(c.req.url);
|
|
207
|
-
url.searchParams.forEach((val, key) => { queryParams[key] = val; });
|
|
208
|
-
|
|
209
|
-
const result = await dispatcher.dispatch(method, subPath, body, queryParams, { request: c.req.raw }, prefix);
|
|
210
|
-
return toResponse(c, result);
|
|
211
|
-
} catch (err: any) {
|
|
212
|
-
return errorJson(c, err.message || 'Internal Server Error', err.statusCode || 500);
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
return app;
|
|
217
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "./dist",
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"module": "NodeNext",
|
|
7
|
-
"moduleResolution": "NodeNext",
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"lib": [
|
|
11
|
-
"ES2020",
|
|
12
|
-
"DOM",
|
|
13
|
-
"DOM.Iterable"
|
|
14
|
-
],
|
|
15
|
-
"types": [
|
|
16
|
-
"node"
|
|
17
|
-
]
|
|
18
|
-
},
|
|
19
|
-
"include": [
|
|
20
|
-
"src/**/*"
|
|
21
|
-
],
|
|
22
|
-
"exclude": [
|
|
23
|
-
"node_modules",
|
|
24
|
-
"dist"
|
|
25
|
-
]
|
|
26
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { defineConfig } from 'vitest/config';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
|
|
6
|
-
export default defineConfig({
|
|
7
|
-
test: {
|
|
8
|
-
globals: true,
|
|
9
|
-
environment: 'node',
|
|
10
|
-
},
|
|
11
|
-
resolve: {
|
|
12
|
-
alias: {
|
|
13
|
-
'@objectstack/runtime': path.resolve(__dirname, 'src/__mocks__/runtime.ts'),
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
});
|