-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
209 lines (188 loc) · 7.09 KB
/
index.ts
File metadata and controls
209 lines (188 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { type ObjectKernel, HttpDispatcher, HttpDispatcherResult } from '@objectstack/runtime';
export interface SvelteKitAdapterOptions {
kernel: ObjectKernel;
prefix?: string;
}
/**
* Auth service interface with handleRequest method
*/
interface AuthService {
handleRequest(request: Request): Promise<Response>;
}
/**
* SvelteKit request event type (minimal interface to avoid hard dependency on @sveltejs/kit types at runtime)
*/
interface RequestEvent {
request: Request;
url: URL;
params: Record<string, string>;
}
/**
* Creates a SvelteKit request handler for ObjectStack API routes.
* Use in a catch-all `+server.ts` route like `src/routes/api/[...path]/+server.ts`.
*
* Only auth, GraphQL, storage, and discovery need explicit handling.
* All other routes delegate to `HttpDispatcher.dispatch()` automatically.
*
* @example
* ```ts
* // src/routes/api/[...path]/+server.ts
* import { createRequestHandler } from '@objectstack/sveltekit';
* import { kernel } from '$lib/kernel';
*
* const handler = createRequestHandler({ kernel });
*
* export const GET = handler;
* export const POST = handler;
* export const PUT = handler;
* export const PATCH = handler;
* export const DELETE = handler;
* ```
*/
export function createRequestHandler(options: SvelteKitAdapterOptions) {
const dispatcher = new HttpDispatcher(options.kernel);
const prefix = options.prefix || '/api';
const errorJson = (message: string, code: number = 500) => {
return new Response(JSON.stringify({ success: false, error: { message, code } }), {
status: code,
headers: { 'Content-Type': 'application/json' },
});
};
const toResponse = (result: HttpDispatcherResult): Response => {
if (result.handled) {
if (result.response) {
const headers = new Headers({ 'Content-Type': 'application/json' });
if (result.response.headers) {
Object.entries(result.response.headers).forEach(([k, v]) => headers.set(k, v as string));
}
return new Response(JSON.stringify(result.response.body), {
status: result.response.status,
headers,
});
}
if (result.result) {
const res = result.result;
if (res.type === 'redirect' && res.url) {
return new Response(null, {
status: 302,
headers: { Location: res.url },
});
}
if (res.type === 'stream' && res.stream) {
const headers = new Headers();
if (res.headers) {
Object.entries(res.headers).forEach(([k, v]) => headers.set(k, v as string));
}
return new Response(res.stream, { status: 200, headers });
}
return new Response(JSON.stringify(res), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
}
return errorJson('Not Found', 404);
};
return async function handler(event: RequestEvent): Promise<Response> {
const { request, url } = event;
const method = request.method;
const path = url.pathname.substring(prefix.length);
const segments = path.split('/').filter(Boolean);
// --- Discovery ---
if (segments.length === 0 && method === 'GET') {
return new Response(JSON.stringify({ data: await dispatcher.getDiscoveryInfo(prefix) }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
if (segments.length === 1 && segments[0] === 'discovery' && method === 'GET') {
return new Response(JSON.stringify({ data: await dispatcher.getDiscoveryInfo(prefix) }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
try {
// --- Auth (needs auth service integration) ---
if (segments[0] === 'auth') {
const subPath = segments.slice(1).join('/');
// Try AuthPlugin service first (prefer async to support factory-based services)
let authService: AuthService | null = null;
try {
if (typeof options.kernel.getServiceAsync === 'function') {
authService = await options.kernel.getServiceAsync<AuthService>('auth');
} else if (typeof options.kernel.getService === 'function') {
authService = options.kernel.getService<AuthService>('auth');
}
} catch {
// Service not registered — fall through to dispatcher
authService = null;
}
if (authService && typeof authService.handleRequest === 'function') {
return await authService.handleRequest(request);
}
// Fallback to dispatcher
const body = method === 'GET' || method === 'HEAD'
? {}
: await request.json().catch(() => ({}));
const result = await dispatcher.handleAuth(subPath, method, body, { request });
return toResponse(result);
}
// --- GraphQL (returns raw result, not HttpDispatcherResult) ---
if (segments[0] === 'graphql' && method === 'POST') {
const body = await request.json() as { query: string; variables?: any };
const result = await dispatcher.handleGraphQL(body, { request });
return new Response(JSON.stringify(result), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
// --- Storage (needs formData parsing) ---
if (segments[0] === 'storage') {
const subPath = segments.slice(1).join('/');
let file: any = undefined;
if (method === 'POST' && subPath === 'upload') {
const formData = await request.formData();
file = formData.get('file');
}
const result = await dispatcher.handleStorage(
subPath ? `/${subPath}` : '',
method,
file,
{ request },
);
return toResponse(result);
}
// --- Catch-all: delegate to dispatcher.dispatch() ---
// Handles meta, data, packages, analytics, automation, i18n, ui,
// openapi, custom API endpoints, and any future routes.
const subPath = path || '';
let body: any = undefined;
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
body = await request.json().catch(() => ({}));
}
const queryParams: Record<string, any> = {};
url.searchParams.forEach((val, key) => { queryParams[key] = val; });
const result = await dispatcher.dispatch(method, subPath, body, queryParams, { request }, prefix);
return toResponse(result);
} catch (err: any) {
return errorJson(err.message || 'Internal Server Error', err.statusCode || 500);
}
};
}
/**
* Creates a SvelteKit handle hook that attaches the kernel to event.locals.
*
* @example
* ```ts
* // src/hooks.server.ts
* import { createHandle } from '@objectstack/sveltekit';
* export const handle = createHandle({ kernel });
* ```
*/
export function createHandle(options: SvelteKitAdapterOptions) {
return async function handle({ event, resolve }: { event: any; resolve: (event: any) => Promise<Response> }) {
event.locals.objectStack = options.kernel;
return resolve(event);
};
}