Skip to content

Commit 8e005e5

Browse files
Copilothotlong
andcommitted
Update MSW plugin and add getKernel to PluginContext
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 92f0485 commit 8e005e5

4 files changed

Lines changed: 107 additions & 8 deletions

File tree

packages/plugin-msw/src/msw-plugin.ts

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { http, HttpResponse } from 'msw';
22
import { setupWorker } from 'msw/browser';
3-
import { RuntimePlugin, RuntimeContext, ObjectStackRuntimeProtocol } from '@objectstack/runtime';
3+
import {
4+
RuntimePlugin,
5+
RuntimeContext,
6+
Plugin,
7+
PluginContext,
8+
ObjectStackRuntimeProtocol,
9+
ObjectKernel
10+
} from '@objectstack/runtime';
411

512
export interface MSWPluginOptions {
613
/**
@@ -149,10 +156,20 @@ export class ObjectStackServer {
149156
* This plugin enables Mock Service Worker integration for testing and development.
150157
* It automatically mocks API endpoints using the ObjectStack runtime protocol.
151158
*
159+
* Supports both legacy RuntimePlugin and new Plugin interfaces.
160+
*
152161
* @example
153162
* ```typescript
154163
* import { MSWPlugin } from '@objectstack/plugin-msw';
155164
*
165+
* // With new ObjectKernel
166+
* const kernel = new ObjectKernel();
167+
* kernel.use(new MSWPlugin({
168+
* enableBrowser: true,
169+
* baseUrl: '/api/v1'
170+
* }));
171+
*
172+
* // With legacy ObjectStackKernel
156173
* const runtime = new ObjectStackRuntime({
157174
* plugins: [
158175
* new MSWPlugin({
@@ -163,11 +180,14 @@ export class ObjectStackServer {
163180
* });
164181
* ```
165182
*/
166-
export class MSWPlugin implements RuntimePlugin {
167-
name = 'msw';
183+
export class MSWPlugin implements Plugin, RuntimePlugin {
184+
name = 'com.objectstack.plugin.msw';
185+
version = '1.0.0';
186+
168187
private options: MSWPluginOptions;
169188
private worker: any;
170189
private handlers: Array<any> = [];
190+
private protocol?: ObjectStackRuntimeProtocol;
171191

172192
constructor(options: MSWPluginOptions = {}) {
173193
this.options = {
@@ -178,9 +198,69 @@ export class MSWPlugin implements RuntimePlugin {
178198
};
179199
}
180200

201+
/**
202+
* New Plugin interface - init phase
203+
*/
204+
async init(ctx: PluginContext) {
205+
// Protocol will be created in start phase
206+
ctx.logger.log('[MSWPlugin] Initialized');
207+
}
208+
209+
/**
210+
* New Plugin interface - start phase
211+
*/
212+
async start(ctx: PluginContext) {
213+
// Get the kernel and create protocol
214+
if (ctx.getKernel) {
215+
const kernel = ctx.getKernel();
216+
this.protocol = new ObjectStackRuntimeProtocol(kernel);
217+
} else {
218+
ctx.logger.warn('[MSWPlugin] Cannot access kernel from context');
219+
}
220+
221+
this.setupHandlers();
222+
await this.startWorker();
223+
}
224+
225+
/**
226+
* New Plugin interface - destroy phase
227+
*/
228+
async destroy() {
229+
await this.stopWorker();
230+
}
231+
232+
/**
233+
* Legacy RuntimePlugin interface - install
234+
*/
181235
install(ctx: RuntimeContext) {
182236
const { engine } = ctx;
183-
const protocol = new ObjectStackRuntimeProtocol(engine);
237+
this.protocol = new ObjectStackRuntimeProtocol(engine);
238+
this.setupHandlers();
239+
}
240+
241+
/**
242+
* Legacy RuntimePlugin interface - onStart
243+
*/
244+
async onStart(ctx: RuntimeContext) {
245+
await this.startWorker();
246+
}
247+
248+
/**
249+
* Legacy RuntimePlugin interface - onStop
250+
*/
251+
async onStop() {
252+
await this.stopWorker();
253+
}
254+
255+
/**
256+
* Setup MSW handlers
257+
*/
258+
private setupHandlers() {
259+
if (!this.protocol) {
260+
throw new Error('[MSWPlugin] Protocol not initialized');
261+
}
262+
263+
const protocol = this.protocol;
184264

185265
// Initialize ObjectStackServer
186266
ObjectStackServer.init(
@@ -312,7 +392,10 @@ export class MSWPlugin implements RuntimePlugin {
312392
console.log(`[MSWPlugin] Installed ${this.handlers.length} request handlers.`);
313393
}
314394

315-
async onStart(ctx: RuntimeContext) {
395+
/**
396+
* Start the MSW worker
397+
*/
398+
private async startWorker() {
316399
if (this.options.enableBrowser && typeof window !== 'undefined') {
317400
// Browser environment
318401
this.worker = setupWorker(...this.handlers);
@@ -325,7 +408,10 @@ export class MSWPlugin implements RuntimePlugin {
325408
}
326409
}
327410

328-
async onStop() {
411+
/**
412+
* Stop the MSW worker
413+
*/
414+
private async stopWorker() {
329415
if (this.worker) {
330416
this.worker.stop();
331417
console.log(`[MSWPlugin] Stopped MSW worker.`);

packages/runtime/src/mini-kernel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class ObjectKernel {
5252
}
5353
},
5454
logger: console,
55+
getKernel: () => this,
5556
};
5657

5758
/**

packages/runtime/src/objectql-plugin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ export class ObjectQLPlugin implements Plugin, RuntimePlugin {
7272
*/
7373
async install(ctx: RuntimeContext) {
7474
// Attach the ObjectQL engine to the kernel for backward compatibility
75-
ctx.engine.ql = this.ql;
76-
console.log('[ObjectQLPlugin] ObjectQL engine registered (legacy mode)');
75+
// Only works with ObjectStackKernel (legacy kernel)
76+
if ('ql' in ctx.engine) {
77+
(ctx.engine as any).ql = this.ql;
78+
console.log('[ObjectQLPlugin] ObjectQL engine registered (legacy mode)');
79+
} else {
80+
console.log('[ObjectQLPlugin] Legacy install called on new kernel - skipping ql property assignment');
81+
}
7782
}
7883

7984
/**

packages/runtime/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface RuntimePlugin {
2626
* - Service registry (registerService/getService)
2727
* - Event/Hook system (hook/trigger)
2828
* - Logger
29+
* - Kernel instance (for advanced use cases)
2930
*/
3031
export interface PluginContext {
3132
/**
@@ -61,6 +62,12 @@ export interface PluginContext {
6162
* Logger instance
6263
*/
6364
logger: Console;
65+
66+
/**
67+
* Get the kernel instance (for advanced use cases)
68+
* @returns Kernel instance
69+
*/
70+
getKernel?(): any;
6471
}
6572

6673
/**

0 commit comments

Comments
 (0)