11import { http , HttpResponse } from 'msw' ;
22import { 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
512export 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+ throw new Error ( '[MSWPlugin] Cannot access kernel from context - getKernel() not available' ) ;
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.` ) ;
0 commit comments