@objectstack/plugin-msw 0.4.2 → 0.6.0
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 +17 -0
- package/dist/msw-plugin.d.ts +37 -18
- package/dist/msw-plugin.js +53 -15
- package/package.json +5 -5
- package/src/msw-plugin.ts +70 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @objectstack/plugin-msw
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- b2df5f7: Unified version bump to 0.5.0
|
|
8
|
+
|
|
9
|
+
- Standardized all package versions to 0.5.0 across the monorepo
|
|
10
|
+
- Fixed driver-memory package.json paths for proper module resolution
|
|
11
|
+
- Ensured all packages are in sync for the 0.5.0 release
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [b2df5f7]
|
|
16
|
+
- @objectstack/spec@0.6.0
|
|
17
|
+
- @objectstack/runtime@0.6.0
|
|
18
|
+
- @objectstack/types@0.6.0
|
|
19
|
+
|
|
3
20
|
## 0.4.2
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/msw-plugin.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Plugin, PluginContext } from '@objectstack/runtime';
|
|
2
|
+
import { IObjectStackProtocol } from '@objectstack/spec/api';
|
|
2
3
|
export interface MSWPluginOptions {
|
|
3
4
|
/**
|
|
4
5
|
* Enable MSW in the browser environment
|
|
@@ -23,13 +24,10 @@ export interface MSWPluginOptions {
|
|
|
23
24
|
export declare class ObjectStackServer {
|
|
24
25
|
private static protocol;
|
|
25
26
|
private static logger;
|
|
26
|
-
static init(protocol:
|
|
27
|
+
static init(protocol: IObjectStackProtocol, logger?: (message: string, ...meta: any[]) => void): void;
|
|
27
28
|
static findData(object: string, params?: any): Promise<{
|
|
28
29
|
status: number;
|
|
29
|
-
data:
|
|
30
|
-
value: any;
|
|
31
|
-
count: any;
|
|
32
|
-
};
|
|
30
|
+
data: any;
|
|
33
31
|
}>;
|
|
34
32
|
static getData(object: string, id: string): Promise<{
|
|
35
33
|
status: number;
|
|
@@ -66,25 +64,46 @@ export declare class ObjectStackServer {
|
|
|
66
64
|
* ```typescript
|
|
67
65
|
* import { MSWPlugin } from '@objectstack/plugin-msw';
|
|
68
66
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* ]
|
|
76
|
-
* });
|
|
67
|
+
* // With ObjectKernel
|
|
68
|
+
* const kernel = new ObjectKernel();
|
|
69
|
+
* kernel.use(new MSWPlugin({
|
|
70
|
+
* enableBrowser: true,
|
|
71
|
+
* baseUrl: '/api/v1'
|
|
72
|
+
* }));
|
|
77
73
|
* ```
|
|
78
74
|
*/
|
|
79
|
-
export declare class MSWPlugin implements
|
|
75
|
+
export declare class MSWPlugin implements Plugin {
|
|
80
76
|
name: string;
|
|
77
|
+
version: string;
|
|
81
78
|
private options;
|
|
82
79
|
private worker;
|
|
83
80
|
private handlers;
|
|
81
|
+
private protocol?;
|
|
84
82
|
constructor(options?: MSWPluginOptions);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Init phase
|
|
85
|
+
*/
|
|
86
|
+
init(ctx: PluginContext): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Start phase
|
|
89
|
+
*/
|
|
90
|
+
start(ctx: PluginContext): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Destroy phase
|
|
93
|
+
*/
|
|
94
|
+
destroy(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Setup MSW handlers
|
|
97
|
+
*/
|
|
98
|
+
private setupHandlers;
|
|
99
|
+
/**
|
|
100
|
+
* Start the MSW worker
|
|
101
|
+
*/
|
|
102
|
+
private startWorker;
|
|
103
|
+
/**
|
|
104
|
+
* Stop the MSW worker
|
|
105
|
+
*/
|
|
106
|
+
private stopWorker;
|
|
88
107
|
/**
|
|
89
108
|
* Get the MSW worker instance for advanced use cases
|
|
90
109
|
*/
|
package/dist/msw-plugin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { http, HttpResponse } from 'msw';
|
|
2
2
|
import { setupWorker } from 'msw/browser';
|
|
3
|
-
import {
|
|
3
|
+
import { ObjectStackProtocolImplementation } from '@objectstack/runtime';
|
|
4
4
|
/**
|
|
5
5
|
* ObjectStack Server Mock - Provides mock database functionality
|
|
6
6
|
*/
|
|
@@ -120,19 +120,18 @@ ObjectStackServer.logger = null;
|
|
|
120
120
|
* ```typescript
|
|
121
121
|
* import { MSWPlugin } from '@objectstack/plugin-msw';
|
|
122
122
|
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* ]
|
|
130
|
-
* });
|
|
123
|
+
* // With ObjectKernel
|
|
124
|
+
* const kernel = new ObjectKernel();
|
|
125
|
+
* kernel.use(new MSWPlugin({
|
|
126
|
+
* enableBrowser: true,
|
|
127
|
+
* baseUrl: '/api/v1'
|
|
128
|
+
* }));
|
|
131
129
|
* ```
|
|
132
130
|
*/
|
|
133
131
|
export class MSWPlugin {
|
|
134
132
|
constructor(options = {}) {
|
|
135
|
-
this.name = 'msw';
|
|
133
|
+
this.name = 'com.objectstack.plugin.msw';
|
|
134
|
+
this.version = '1.0.0';
|
|
136
135
|
this.handlers = [];
|
|
137
136
|
this.options = {
|
|
138
137
|
enableBrowser: true,
|
|
@@ -141,9 +140,42 @@ export class MSWPlugin {
|
|
|
141
140
|
...options
|
|
142
141
|
};
|
|
143
142
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Init phase
|
|
145
|
+
*/
|
|
146
|
+
async init(ctx) {
|
|
147
|
+
// Protocol will be created in start phase
|
|
148
|
+
ctx.logger.log('[MSWPlugin] Initialized');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Start phase
|
|
152
|
+
*/
|
|
153
|
+
async start(ctx) {
|
|
154
|
+
try {
|
|
155
|
+
const dataEngine = ctx.getService('objectql');
|
|
156
|
+
this.protocol = new ObjectStackProtocolImplementation(dataEngine);
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
console.error('[MSWPlugin] Failed to initialize protocol', e);
|
|
160
|
+
throw new Error('[MSWPlugin] Failed to initialize protocol (missing objectql service?)');
|
|
161
|
+
}
|
|
162
|
+
this.setupHandlers();
|
|
163
|
+
await this.startWorker();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Destroy phase
|
|
167
|
+
*/
|
|
168
|
+
async destroy() {
|
|
169
|
+
await this.stopWorker();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Setup MSW handlers
|
|
173
|
+
*/
|
|
174
|
+
setupHandlers() {
|
|
175
|
+
if (!this.protocol) {
|
|
176
|
+
throw new Error('[MSWPlugin] Protocol not initialized');
|
|
177
|
+
}
|
|
178
|
+
const protocol = this.protocol;
|
|
147
179
|
// Initialize ObjectStackServer
|
|
148
180
|
ObjectStackServer.init(protocol, this.options.logRequests ? console.log : undefined);
|
|
149
181
|
const baseUrl = this.options.baseUrl || '/api/v1';
|
|
@@ -245,7 +277,10 @@ export class MSWPlugin {
|
|
|
245
277
|
];
|
|
246
278
|
console.log(`[MSWPlugin] Installed ${this.handlers.length} request handlers.`);
|
|
247
279
|
}
|
|
248
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Start the MSW worker
|
|
282
|
+
*/
|
|
283
|
+
async startWorker() {
|
|
249
284
|
if (this.options.enableBrowser && typeof window !== 'undefined') {
|
|
250
285
|
// Browser environment
|
|
251
286
|
this.worker = setupWorker(...this.handlers);
|
|
@@ -258,7 +293,10 @@ export class MSWPlugin {
|
|
|
258
293
|
console.log(`[MSWPlugin] Browser mode disabled or not in browser environment.`);
|
|
259
294
|
}
|
|
260
295
|
}
|
|
261
|
-
|
|
296
|
+
/**
|
|
297
|
+
* Stop the MSW worker
|
|
298
|
+
*/
|
|
299
|
+
async stopWorker() {
|
|
262
300
|
if (this.worker) {
|
|
263
301
|
this.worker.stop();
|
|
264
302
|
console.log(`[MSWPlugin] Stopped MSW worker.`);
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@objectstack/plugin-msw",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "MSW (Mock Service Worker) Plugin for ObjectStack Runtime",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"msw": "^2.0.0",
|
|
9
|
-
"@objectstack/spec": "0.
|
|
10
|
-
"@objectstack/types": "0.
|
|
9
|
+
"@objectstack/spec": "0.6.0",
|
|
10
|
+
"@objectstack/types": "0.6.0"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@types/node": "^20.0.0",
|
|
14
14
|
"typescript": "^5.0.0",
|
|
15
|
-
"@objectstack/runtime": "0.
|
|
15
|
+
"@objectstack/runtime": "0.6.0"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@objectstack/runtime": "^0.
|
|
18
|
+
"@objectstack/runtime": "^0.6.0"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsc"
|
package/src/msw-plugin.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { http, HttpResponse } from 'msw';
|
|
2
2
|
import { setupWorker } from 'msw/browser';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
Plugin,
|
|
5
|
+
PluginContext,
|
|
6
|
+
ObjectKernel,
|
|
7
|
+
ObjectStackProtocolImplementation,
|
|
8
|
+
IDataEngine
|
|
9
|
+
} from '@objectstack/runtime';
|
|
10
|
+
import { IObjectStackProtocol } from '@objectstack/spec/api';
|
|
11
|
+
// import { IDataEngine } from '@objectstack/core';
|
|
4
12
|
|
|
5
13
|
export interface MSWPluginOptions {
|
|
6
14
|
/**
|
|
@@ -28,10 +36,10 @@ export interface MSWPluginOptions {
|
|
|
28
36
|
* ObjectStack Server Mock - Provides mock database functionality
|
|
29
37
|
*/
|
|
30
38
|
export class ObjectStackServer {
|
|
31
|
-
private static protocol:
|
|
39
|
+
private static protocol: IObjectStackProtocol | null = null;
|
|
32
40
|
private static logger: ((message: string, ...meta: any[]) => void) | null = null;
|
|
33
41
|
|
|
34
|
-
static init(protocol:
|
|
42
|
+
static init(protocol: IObjectStackProtocol, logger?: (message: string, ...meta: any[]) => void) {
|
|
35
43
|
this.protocol = protocol;
|
|
36
44
|
this.logger = logger || console.log;
|
|
37
45
|
}
|
|
@@ -153,21 +161,22 @@ export class ObjectStackServer {
|
|
|
153
161
|
* ```typescript
|
|
154
162
|
* import { MSWPlugin } from '@objectstack/plugin-msw';
|
|
155
163
|
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* ]
|
|
163
|
-
* });
|
|
164
|
+
* // With ObjectKernel
|
|
165
|
+
* const kernel = new ObjectKernel();
|
|
166
|
+
* kernel.use(new MSWPlugin({
|
|
167
|
+
* enableBrowser: true,
|
|
168
|
+
* baseUrl: '/api/v1'
|
|
169
|
+
* }));
|
|
164
170
|
* ```
|
|
165
171
|
*/
|
|
166
|
-
export class MSWPlugin implements
|
|
167
|
-
name = 'msw';
|
|
172
|
+
export class MSWPlugin implements Plugin {
|
|
173
|
+
name = 'com.objectstack.plugin.msw';
|
|
174
|
+
version = '1.0.0';
|
|
175
|
+
|
|
168
176
|
private options: MSWPluginOptions;
|
|
169
177
|
private worker: any;
|
|
170
178
|
private handlers: Array<any> = [];
|
|
179
|
+
private protocol?: IObjectStackProtocol;
|
|
171
180
|
|
|
172
181
|
constructor(options: MSWPluginOptions = {}) {
|
|
173
182
|
this.options = {
|
|
@@ -178,9 +187,46 @@ export class MSWPlugin implements RuntimePlugin {
|
|
|
178
187
|
};
|
|
179
188
|
}
|
|
180
189
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
190
|
+
/**
|
|
191
|
+
* Init phase
|
|
192
|
+
*/
|
|
193
|
+
async init(ctx: PluginContext) {
|
|
194
|
+
// Protocol will be created in start phase
|
|
195
|
+
ctx.logger.log('[MSWPlugin] Initialized');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Start phase
|
|
200
|
+
*/
|
|
201
|
+
async start(ctx: PluginContext) {
|
|
202
|
+
try {
|
|
203
|
+
const dataEngine = ctx.getService<IDataEngine>('objectql');
|
|
204
|
+
this.protocol = new ObjectStackProtocolImplementation(dataEngine);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
console.error('[MSWPlugin] Failed to initialize protocol', e);
|
|
207
|
+
throw new Error('[MSWPlugin] Failed to initialize protocol (missing objectql service?)');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this.setupHandlers();
|
|
211
|
+
await this.startWorker();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Destroy phase
|
|
216
|
+
*/
|
|
217
|
+
async destroy() {
|
|
218
|
+
await this.stopWorker();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Setup MSW handlers
|
|
223
|
+
*/
|
|
224
|
+
private setupHandlers() {
|
|
225
|
+
if (!this.protocol) {
|
|
226
|
+
throw new Error('[MSWPlugin] Protocol not initialized');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const protocol = this.protocol;
|
|
184
230
|
|
|
185
231
|
// Initialize ObjectStackServer
|
|
186
232
|
ObjectStackServer.init(
|
|
@@ -312,7 +358,10 @@ export class MSWPlugin implements RuntimePlugin {
|
|
|
312
358
|
console.log(`[MSWPlugin] Installed ${this.handlers.length} request handlers.`);
|
|
313
359
|
}
|
|
314
360
|
|
|
315
|
-
|
|
361
|
+
/**
|
|
362
|
+
* Start the MSW worker
|
|
363
|
+
*/
|
|
364
|
+
private async startWorker() {
|
|
316
365
|
if (this.options.enableBrowser && typeof window !== 'undefined') {
|
|
317
366
|
// Browser environment
|
|
318
367
|
this.worker = setupWorker(...this.handlers);
|
|
@@ -325,7 +374,10 @@ export class MSWPlugin implements RuntimePlugin {
|
|
|
325
374
|
}
|
|
326
375
|
}
|
|
327
376
|
|
|
328
|
-
|
|
377
|
+
/**
|
|
378
|
+
* Stop the MSW worker
|
|
379
|
+
*/
|
|
380
|
+
private async stopWorker() {
|
|
329
381
|
if (this.worker) {
|
|
330
382
|
this.worker.stop();
|
|
331
383
|
console.log(`[MSWPlugin] Stopped MSW worker.`);
|