@objectstack/plugin-msw 1.0.0 → 1.0.2
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/CHANGELOG.md +32 -0
- package/README.md +20 -6
- package/dist/msw-plugin.d.ts +51 -201
- package/dist/msw-plugin.js +97 -457
- package/package.json +7 -6
- package/src/msw-plugin.ts +120 -508
package/src/msw-plugin.ts
CHANGED
|
@@ -4,11 +4,12 @@ import {
|
|
|
4
4
|
Plugin,
|
|
5
5
|
PluginContext,
|
|
6
6
|
ObjectKernel,
|
|
7
|
-
|
|
7
|
+
HttpDispatcher,
|
|
8
|
+
HttpDispatcherResult
|
|
8
9
|
} from '@objectstack/runtime';
|
|
9
10
|
// import { ObjectStackProtocolImplementation } from '@objectstack/objectql';
|
|
10
11
|
import { ObjectStackProtocol } from '@objectstack/spec/api';
|
|
11
|
-
|
|
12
|
+
import { IDataEngine } from '@objectstack/core';
|
|
12
13
|
|
|
13
14
|
// Helper for parsing query parameters
|
|
14
15
|
function parseQueryParams(url: URL): Record<string, any> {
|
|
@@ -54,33 +55,6 @@ function parseQueryParams(url: URL): Record<string, any> {
|
|
|
54
55
|
return params;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
// Helper to normalize flat parameters into 'where' clause
|
|
58
|
-
function normalizeQuery(params: Record<string, any>): Record<string, any> {
|
|
59
|
-
// If 'where' is already present, trust it
|
|
60
|
-
if (params.where) return params;
|
|
61
|
-
|
|
62
|
-
const reserved = ['select', 'order', 'orderBy', 'sort', 'limit', 'skip', 'offset', 'top', 'page', 'pageSize', 'count'];
|
|
63
|
-
const where: Record<string, any> = {};
|
|
64
|
-
let hasWhere = false;
|
|
65
|
-
|
|
66
|
-
for (const key in params) {
|
|
67
|
-
if (!reserved.includes(key)) {
|
|
68
|
-
where[key] = params[key];
|
|
69
|
-
hasWhere = true;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (hasWhere) {
|
|
74
|
-
// Keep original params but add where.
|
|
75
|
-
// This allows protocols that look at root properties to still work,
|
|
76
|
-
// while providing 'where' for strict drivers.
|
|
77
|
-
return { ...params, where };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return params;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
58
|
export interface MSWPluginOptions {
|
|
85
59
|
/**
|
|
86
60
|
* Enable MSW in the browser environment
|
|
@@ -103,210 +77,9 @@ export interface MSWPluginOptions {
|
|
|
103
77
|
logRequests?: boolean;
|
|
104
78
|
}
|
|
105
79
|
|
|
106
|
-
/**
|
|
107
|
-
* ObjectStack Server Mock - Provides mock database functionality
|
|
108
|
-
*/
|
|
109
|
-
export class ObjectStackServer {
|
|
110
|
-
private static protocol: ObjectStackProtocol | null = null;
|
|
111
|
-
private static logger: any | null = null;
|
|
112
|
-
|
|
113
|
-
static init(protocol: ObjectStackProtocol, logger?: any) {
|
|
114
|
-
this.protocol = protocol;
|
|
115
|
-
this.logger = logger || {
|
|
116
|
-
info: console.log,
|
|
117
|
-
debug: console.debug,
|
|
118
|
-
warn: console.warn,
|
|
119
|
-
error: console.error
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
static async findData(object: string, params?: any) {
|
|
124
|
-
if (!this.protocol) {
|
|
125
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this.logger?.debug?.('MSW: Finding records', { object, params });
|
|
129
|
-
const result = await this.protocol.findData({ object, query: params || {} });
|
|
130
|
-
this.logger?.debug?.('MSW: Find completed', { object, count: result?.records?.length ?? 0 });
|
|
131
|
-
return {
|
|
132
|
-
status: 200,
|
|
133
|
-
data: result
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
static async getData(object: string, id: string) {
|
|
138
|
-
if (!this.protocol) {
|
|
139
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
this.logger?.debug?.('MSW: Getting record', { object, id });
|
|
143
|
-
try {
|
|
144
|
-
const result = await this.protocol.getData({ object, id });
|
|
145
|
-
this.logger?.debug?.('MSW: Get completed', { object, id });
|
|
146
|
-
return {
|
|
147
|
-
status: 200,
|
|
148
|
-
data: result
|
|
149
|
-
};
|
|
150
|
-
} catch (error) {
|
|
151
|
-
this.logger?.warn?.('MSW: Get failed - not found', { object, id });
|
|
152
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
153
|
-
return {
|
|
154
|
-
status: 404,
|
|
155
|
-
data: { error: message }
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
static async createData(object: string, data: any) {
|
|
161
|
-
if (!this.protocol) {
|
|
162
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
this.logger?.debug?.('MSW: Creating record', { object });
|
|
166
|
-
try {
|
|
167
|
-
const result = await this.protocol.createData({ object, data });
|
|
168
|
-
this.logger?.info?.('MSW: Record created', { object, id: result?.id });
|
|
169
|
-
return {
|
|
170
|
-
status: 201,
|
|
171
|
-
data: result
|
|
172
|
-
};
|
|
173
|
-
} catch (error) {
|
|
174
|
-
this.logger?.error?.('MSW: Create failed', error, { object });
|
|
175
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
176
|
-
return {
|
|
177
|
-
status: 400,
|
|
178
|
-
data: { error: message }
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
static async updateData(object: string, id: string, data: any) {
|
|
184
|
-
if (!this.protocol) {
|
|
185
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
this.logger?.debug?.('MSW: Updating record', { object, id });
|
|
189
|
-
try {
|
|
190
|
-
const result = await this.protocol.updateData({ object, id, data });
|
|
191
|
-
this.logger?.info?.('MSW: Record updated', { object, id });
|
|
192
|
-
return {
|
|
193
|
-
status: 200,
|
|
194
|
-
data: result
|
|
195
|
-
};
|
|
196
|
-
} catch (error) {
|
|
197
|
-
this.logger?.error?.('MSW: Update failed', error, { object, id });
|
|
198
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
199
|
-
return {
|
|
200
|
-
status: 400,
|
|
201
|
-
data: { error: message }
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
static async deleteData(object: string, id: string) {
|
|
207
|
-
if (!this.protocol) {
|
|
208
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
this.logger?.debug?.('MSW: Deleting record', { object, id });
|
|
212
|
-
try {
|
|
213
|
-
const result = await this.protocol.deleteData({ object, id });
|
|
214
|
-
this.logger?.info?.('MSW: Record deleted', { object, id, success: result?.success });
|
|
215
|
-
return {
|
|
216
|
-
status: 200,
|
|
217
|
-
data: result
|
|
218
|
-
};
|
|
219
|
-
} catch (error) {
|
|
220
|
-
this.logger?.error?.('MSW: Delete failed', error, { object, id });
|
|
221
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
222
|
-
return {
|
|
223
|
-
status: 400,
|
|
224
|
-
data: { error: message }
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
static async analyticsQuery(request: any) {
|
|
230
|
-
if (!this.protocol) {
|
|
231
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
this.logger?.debug?.('MSW: Executing analytics query', { request });
|
|
235
|
-
try {
|
|
236
|
-
const result = await this.protocol.analyticsQuery(request);
|
|
237
|
-
this.logger?.debug?.('MSW: Analytics query completed', { result });
|
|
238
|
-
return {
|
|
239
|
-
status: 200,
|
|
240
|
-
data: result
|
|
241
|
-
};
|
|
242
|
-
} catch (error) {
|
|
243
|
-
this.logger?.error?.('MSW: Analytics query failed', error);
|
|
244
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
245
|
-
return {
|
|
246
|
-
status: 400,
|
|
247
|
-
data: { error: message }
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
static async getAnalyticsMeta(request: any) {
|
|
253
|
-
if (!this.protocol) {
|
|
254
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
this.logger?.debug?.('MSW: Getting analytics metadata', { request });
|
|
258
|
-
try {
|
|
259
|
-
const result = await this.protocol.getAnalyticsMeta(request);
|
|
260
|
-
this.logger?.debug?.('MSW: Analytics metadata retrieved', { result });
|
|
261
|
-
return {
|
|
262
|
-
status: 200,
|
|
263
|
-
data: result
|
|
264
|
-
};
|
|
265
|
-
} catch (error) {
|
|
266
|
-
this.logger?.error?.('MSW: Analytics metadata failed', error);
|
|
267
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
268
|
-
return {
|
|
269
|
-
status: 400,
|
|
270
|
-
data: { error: message }
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
static async triggerAutomation(request: any) {
|
|
276
|
-
if (!this.protocol) {
|
|
277
|
-
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init() first.');
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
this.logger?.debug?.('MSW: Triggering automation', { request });
|
|
281
|
-
try {
|
|
282
|
-
const result = await this.protocol.triggerAutomation(request);
|
|
283
|
-
this.logger?.info?.('MSW: Automation triggered', { result });
|
|
284
|
-
return {
|
|
285
|
-
status: 200,
|
|
286
|
-
data: result
|
|
287
|
-
};
|
|
288
|
-
} catch (error) {
|
|
289
|
-
this.logger?.error?.('MSW: Automation trigger failed', error);
|
|
290
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
291
|
-
return {
|
|
292
|
-
status: 400,
|
|
293
|
-
data: { error: message }
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Legacy method names for compatibility
|
|
299
|
-
static async getUser(id: string) {
|
|
300
|
-
return this.getData('user', id);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
static async createUser(data: any) {
|
|
304
|
-
return this.createData('user', data);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
80
|
/**
|
|
309
81
|
* MSW Plugin for ObjectStack
|
|
82
|
+
|
|
310
83
|
*
|
|
311
84
|
* This plugin enables Mock Service Worker integration for testing and development.
|
|
312
85
|
* It automatically mocks API endpoints using the ObjectStack runtime protocol.
|
|
@@ -331,6 +104,7 @@ export class MSWPlugin implements Plugin {
|
|
|
331
104
|
private worker: any;
|
|
332
105
|
private handlers: Array<any> = [];
|
|
333
106
|
private protocol?: ObjectStackProtocol;
|
|
107
|
+
private dispatcher?: HttpDispatcher;
|
|
334
108
|
|
|
335
109
|
constructor(options: MSWPluginOptions = {}) {
|
|
336
110
|
this.options = {
|
|
@@ -411,299 +185,94 @@ export class MSWPlugin implements Plugin {
|
|
|
411
185
|
* Setup MSW handlers
|
|
412
186
|
*/
|
|
413
187
|
private setupHandlers(ctx: PluginContext) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
this.
|
|
417
|
-
|
|
418
|
-
];
|
|
419
|
-
return;
|
|
188
|
+
// Initialize HttpDispatcher
|
|
189
|
+
try {
|
|
190
|
+
this.dispatcher = new HttpDispatcher(ctx.getKernel());
|
|
191
|
+
} catch (e) {
|
|
192
|
+
ctx.logger.warn('[MSWPlugin] Could not initialize HttpDispatcher via Kernel. Falling back to simple handlers.');
|
|
420
193
|
}
|
|
421
194
|
|
|
422
|
-
const protocol = this.protocol;
|
|
423
|
-
|
|
424
|
-
// Initialize ObjectStackServer with structured logger
|
|
425
|
-
ObjectStackServer.init(
|
|
426
|
-
protocol,
|
|
427
|
-
this.options.logRequests ? ctx.logger : undefined
|
|
428
|
-
);
|
|
429
|
-
|
|
430
|
-
ctx.logger.debug('Initialized ObjectStackServer', { logRequests: this.options.logRequests });
|
|
431
|
-
|
|
432
195
|
const baseUrl = this.options.baseUrl || '/api/v1';
|
|
433
196
|
|
|
434
|
-
//
|
|
197
|
+
// Custom handlers have priority
|
|
435
198
|
this.handlers = [
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const discovery = await protocol.getDiscovery({});
|
|
439
|
-
return HttpResponse.json({
|
|
440
|
-
...discovery,
|
|
441
|
-
routes: {
|
|
442
|
-
data: `${baseUrl}/data`,
|
|
443
|
-
metadata: `${baseUrl}/meta`,
|
|
444
|
-
ui: `${baseUrl}/ui`,
|
|
445
|
-
auth: `${baseUrl}/auth`
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
}),
|
|
449
|
-
|
|
450
|
-
// Meta endpoints
|
|
451
|
-
http.get(`${baseUrl}/meta`, async ({ request }) => {
|
|
452
|
-
const url = new URL(request.url);
|
|
453
|
-
const query = parseQueryParams(url);
|
|
454
|
-
return HttpResponse.json(await protocol.getMetaTypes({ query }));
|
|
455
|
-
}),
|
|
199
|
+
...(this.options.customHandlers || [])
|
|
200
|
+
];
|
|
456
201
|
|
|
457
|
-
|
|
202
|
+
if (this.dispatcher) {
|
|
203
|
+
const dispatcher = this.dispatcher;
|
|
204
|
+
|
|
205
|
+
// Catch-all handler for ObjectStack Runtime
|
|
206
|
+
// We use a wildcard to capture all methods and paths under baseUrl
|
|
207
|
+
const catchAll = async ({ request, params }: any) => {
|
|
458
208
|
const url = new URL(request.url);
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
try {
|
|
465
|
-
return HttpResponse.json(
|
|
466
|
-
await protocol.getMetaItem({ type: params.type as string, name: params.name as string } as any)
|
|
467
|
-
);
|
|
468
|
-
} catch (error) {
|
|
469
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
470
|
-
return HttpResponse.json({ error: message }, { status: 404 });
|
|
471
|
-
}
|
|
472
|
-
}),
|
|
473
|
-
|
|
474
|
-
// Data endpoints
|
|
475
|
-
http.get(`${baseUrl}/data/:object`, async ({ params, request }) => {
|
|
476
|
-
try {
|
|
477
|
-
const url = new URL(request.url);
|
|
478
|
-
|
|
479
|
-
// Use helper to parse properly (handle multiple values, JSON strings, numbers)
|
|
480
|
-
const rawParams = parseQueryParams(url);
|
|
481
|
-
|
|
482
|
-
// Normalize to standard query object (move flats to 'where')
|
|
483
|
-
const queryParams = normalizeQuery(rawParams);
|
|
484
|
-
|
|
485
|
-
const result = await ObjectStackServer.findData(
|
|
486
|
-
params.object as string,
|
|
487
|
-
queryParams
|
|
488
|
-
);
|
|
489
|
-
return HttpResponse.json(result.data, {
|
|
490
|
-
status: result.status,
|
|
491
|
-
headers: { 'Cache-Control': 'no-store' }
|
|
492
|
-
});
|
|
493
|
-
} catch (error) {
|
|
494
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
495
|
-
return HttpResponse.json({ error: message }, { status: 404 });
|
|
496
|
-
}
|
|
497
|
-
}),
|
|
498
|
-
|
|
499
|
-
http.get(`${baseUrl}/data/:object/:id`, async ({ params }) => {
|
|
500
|
-
try {
|
|
501
|
-
const result = await ObjectStackServer.getData(
|
|
502
|
-
params.object as string,
|
|
503
|
-
params.id as string
|
|
504
|
-
);
|
|
505
|
-
return HttpResponse.json(result.data, {
|
|
506
|
-
status: result.status,
|
|
507
|
-
headers: { 'Cache-Control': 'no-store' }
|
|
508
|
-
});
|
|
509
|
-
} catch (error) {
|
|
510
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
511
|
-
return HttpResponse.json({ error: message }, { status: 404 });
|
|
512
|
-
}
|
|
513
|
-
}),
|
|
514
|
-
|
|
515
|
-
http.post(`${baseUrl}/data/:object`, async ({ params, request }) => {
|
|
516
|
-
try {
|
|
517
|
-
const body = await request.json();
|
|
518
|
-
const result = await ObjectStackServer.createData(
|
|
519
|
-
params.object as string,
|
|
520
|
-
body
|
|
521
|
-
);
|
|
522
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
523
|
-
} catch (error) {
|
|
524
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
525
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
526
|
-
}
|
|
527
|
-
}),
|
|
528
|
-
|
|
529
|
-
http.patch(`${baseUrl}/data/:object/:id`, async ({ params, request }) => {
|
|
530
|
-
try {
|
|
531
|
-
const body = await request.json();
|
|
532
|
-
const result = await ObjectStackServer.updateData(
|
|
533
|
-
params.object as string,
|
|
534
|
-
params.id as string,
|
|
535
|
-
body
|
|
536
|
-
);
|
|
537
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
538
|
-
} catch (error) {
|
|
539
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
540
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
541
|
-
}
|
|
542
|
-
}),
|
|
543
|
-
|
|
544
|
-
http.delete(`${baseUrl}/data/:object/:id`, async ({ params }) => {
|
|
545
|
-
try {
|
|
546
|
-
const result = await ObjectStackServer.deleteData(
|
|
547
|
-
params.object as string,
|
|
548
|
-
params.id as string
|
|
549
|
-
);
|
|
550
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
551
|
-
} catch (error) {
|
|
552
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
553
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
209
|
+
// Calculate path relative to API prefix
|
|
210
|
+
// e.g. /api/v1/data/contacts -> /data/contacts
|
|
211
|
+
let path = url.pathname;
|
|
212
|
+
if (path.startsWith(baseUrl)) {
|
|
213
|
+
path = path.slice(baseUrl.length);
|
|
554
214
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
http.post(`${baseUrl}/data/:object/createMany`, async ({ params, request }) => {
|
|
570
|
-
try {
|
|
571
|
-
const body = await request.json();
|
|
572
|
-
const records = Array.isArray(body) ? body : [];
|
|
573
|
-
const result = await protocol.createManyData({ object: params.object as string, records });
|
|
574
|
-
return HttpResponse.json(result, { status: 201 });
|
|
575
|
-
} catch (error) {
|
|
576
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
577
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
578
|
-
}
|
|
579
|
-
}),
|
|
580
|
-
|
|
581
|
-
http.post(`${baseUrl}/data/:object/updateMany`, async ({ params, request }) => {
|
|
582
|
-
try {
|
|
583
|
-
const body = await request.json() as any;
|
|
584
|
-
const result = await protocol.updateManyData({
|
|
585
|
-
object: params.object as string,
|
|
586
|
-
records: body?.records || [],
|
|
587
|
-
options: body?.options
|
|
588
|
-
});
|
|
589
|
-
return HttpResponse.json(result);
|
|
590
|
-
} catch (error) {
|
|
591
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
592
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
593
|
-
}
|
|
594
|
-
}),
|
|
595
|
-
|
|
596
|
-
http.post(`${baseUrl}/data/:object/deleteMany`, async ({ params, request }) => {
|
|
597
|
-
try {
|
|
598
|
-
const body = await request.json() as any;
|
|
599
|
-
const result = await protocol.deleteManyData({
|
|
600
|
-
object: params.object as string,
|
|
601
|
-
ids: body?.ids || [],
|
|
602
|
-
options: body?.options
|
|
603
|
-
});
|
|
604
|
-
return HttpResponse.json(result);
|
|
605
|
-
} catch (error) {
|
|
606
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
607
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
215
|
+
|
|
216
|
+
// Parse Body if present
|
|
217
|
+
let body: any = undefined;
|
|
218
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
219
|
+
try {
|
|
220
|
+
body = await request.clone().json();
|
|
221
|
+
} catch (e) {
|
|
222
|
+
try {
|
|
223
|
+
// Try form data if json fails?
|
|
224
|
+
// Dispatcher expects objects usually.
|
|
225
|
+
// For file upload, body might be FormData logic needed?
|
|
226
|
+
// For now assume JSON or text
|
|
227
|
+
} catch (e2) {}
|
|
228
|
+
}
|
|
608
229
|
}
|
|
609
|
-
}),
|
|
610
230
|
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
// Build response headers
|
|
630
|
-
const headers: Record<string, string> = {};
|
|
631
|
-
if (result.etag) {
|
|
632
|
-
const etagValue = result.etag.weak ? `W/"${result.etag.value}"` : `"${result.etag.value}"`;
|
|
633
|
-
headers['ETag'] = etagValue;
|
|
634
|
-
}
|
|
635
|
-
if (result.lastModified) {
|
|
636
|
-
headers['Last-Modified'] = new Date(result.lastModified).toUTCString();
|
|
231
|
+
// Parse Query
|
|
232
|
+
const query = parseQueryParams(url);
|
|
233
|
+
|
|
234
|
+
// Dispatch
|
|
235
|
+
const result = await dispatcher.dispatch(
|
|
236
|
+
request.method,
|
|
237
|
+
path,
|
|
238
|
+
body,
|
|
239
|
+
query,
|
|
240
|
+
{ request }
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
if (result.handled) {
|
|
244
|
+
if (result.response) {
|
|
245
|
+
return HttpResponse.json(result.response.body, {
|
|
246
|
+
status: result.response.status,
|
|
247
|
+
headers: result.response.headers as any
|
|
248
|
+
});
|
|
637
249
|
}
|
|
638
|
-
if (result.
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
250
|
+
if (result.result) {
|
|
251
|
+
// Handle special results (streams/redirects - unlikely in MSW but possible)
|
|
252
|
+
if (result.result.type === 'redirect') {
|
|
253
|
+
return HttpResponse.redirect(result.result.url);
|
|
254
|
+
}
|
|
255
|
+
// Fallback for others
|
|
256
|
+
return HttpResponse.json(result.result);
|
|
642
257
|
}
|
|
643
|
-
|
|
644
|
-
return HttpResponse.json(result.data, { headers });
|
|
645
|
-
} catch (error) {
|
|
646
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
647
|
-
return HttpResponse.json({ error: message }, { status: 404 });
|
|
648
|
-
}
|
|
649
|
-
}),
|
|
650
|
-
|
|
651
|
-
// UI Protocol endpoint
|
|
652
|
-
http.get(`${baseUrl}/ui/view/:object`, async ({ params, request }) => {
|
|
653
|
-
try {
|
|
654
|
-
const url = new URL(request.url);
|
|
655
|
-
const viewType = url.searchParams.get('type') || 'list';
|
|
656
|
-
const view = await protocol.getUiView({ object: params.object as string, type: viewType as 'list' | 'form' });
|
|
657
|
-
return HttpResponse.json(view);
|
|
658
|
-
} catch (error) {
|
|
659
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
660
|
-
return HttpResponse.json({ error: message }, { status: 404 });
|
|
661
|
-
}
|
|
662
|
-
}),
|
|
663
|
-
|
|
664
|
-
// Analytics Operations
|
|
665
|
-
http.post(`${baseUrl}/analytics/query`, async ({ request }) => {
|
|
666
|
-
try {
|
|
667
|
-
const body = await request.json();
|
|
668
|
-
const result = await ObjectStackServer.analyticsQuery(body);
|
|
669
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
670
|
-
} catch (error) {
|
|
671
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
672
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
673
|
-
}
|
|
674
|
-
}),
|
|
675
|
-
|
|
676
|
-
http.get(`${baseUrl}/analytics/meta`, async ({ request }) => {
|
|
677
|
-
try {
|
|
678
|
-
const url = new URL(request.url);
|
|
679
|
-
const query = parseQueryParams(url);
|
|
680
|
-
const result = await ObjectStackServer.getAnalyticsMeta(query);
|
|
681
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
682
|
-
} catch (error) {
|
|
683
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
684
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
685
258
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
try {
|
|
691
|
-
const body = await request.json();
|
|
692
|
-
const result = await ObjectStackServer.triggerAutomation(body);
|
|
693
|
-
return HttpResponse.json(result.data, { status: result.status });
|
|
694
|
-
} catch (error) {
|
|
695
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
696
|
-
return HttpResponse.json({ error: message }, { status: 400 });
|
|
697
|
-
}
|
|
698
|
-
}),
|
|
699
|
-
|
|
700
|
-
// Add custom handlers
|
|
701
|
-
...(this.options.customHandlers || [])
|
|
702
|
-
];
|
|
259
|
+
|
|
260
|
+
// Not handled by dispatcher (404 for this route subset)
|
|
261
|
+
return undefined; // Let MSW pass through or handle next
|
|
262
|
+
};
|
|
703
263
|
|
|
704
|
-
|
|
264
|
+
this.handlers.push(
|
|
265
|
+
http.all(`${baseUrl}/*`, catchAll),
|
|
266
|
+
http.all(`${baseUrl}`, catchAll) // Handle root if needed
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
ctx.logger.info('MSW handlers set up using HttpDispatcher', { baseUrl });
|
|
270
|
+
} else {
|
|
271
|
+
ctx.logger.warn('[MSWPlugin] No dispatcher available. No API routes registered.');
|
|
272
|
+
}
|
|
705
273
|
}
|
|
706
274
|
|
|
275
|
+
|
|
707
276
|
/**
|
|
708
277
|
* Start the MSW worker
|
|
709
278
|
*/
|
|
@@ -745,3 +314,46 @@ export class MSWPlugin implements Plugin {
|
|
|
745
314
|
return this.handlers;
|
|
746
315
|
}
|
|
747
316
|
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Static helper for interacting with ObjectStack protocol in MSW handlers
|
|
320
|
+
*/
|
|
321
|
+
export class ObjectStackServer {
|
|
322
|
+
private static protocol: ObjectStackProtocol;
|
|
323
|
+
|
|
324
|
+
static init(protocol: ObjectStackProtocol) {
|
|
325
|
+
this.protocol = protocol;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private static getProtocol(): ObjectStackProtocol {
|
|
329
|
+
if (!this.protocol) {
|
|
330
|
+
throw new Error('ObjectStackServer not initialized. Call ObjectStackServer.init(protocol) first.');
|
|
331
|
+
}
|
|
332
|
+
return this.protocol;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
static async findData(objectName: string, query?: any) {
|
|
336
|
+
const body = await this.getProtocol().findData({ object: objectName, query });
|
|
337
|
+
return { data: body, status: 200 };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
static async getData(objectName: string, id: string) {
|
|
341
|
+
const body = await this.getProtocol().getData({ object: objectName, id });
|
|
342
|
+
return { data: body, status: 200 };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
static async createData(objectName: string, data: any) {
|
|
346
|
+
const body = await this.getProtocol().createData({ object: objectName, data });
|
|
347
|
+
return { data: body, status: 201 };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
static async updateData(objectName: string, id: string, data: any) {
|
|
351
|
+
const body = await this.getProtocol().updateData({ object: objectName, id, data });
|
|
352
|
+
return { data: body, status: 200 };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
static async deleteData(objectName: string, id: string) {
|
|
356
|
+
const body = await this.getProtocol().deleteData({ object: objectName, id });
|
|
357
|
+
return { data: body, status: 200 };
|
|
358
|
+
}
|
|
359
|
+
}
|