|
9 | 9 | } from '@objectstack/runtime'; |
10 | 10 | // import { ObjectStackProtocolImplementation } from '@objectstack/objectql'; |
11 | 11 | import { ObjectStackProtocol } from '@objectstack/spec/api'; |
12 | | -// import { IDataEngine } from '@objectstack/core'; |
| 12 | +import { IDataEngine } from '@objectstack/core'; |
13 | 13 |
|
14 | 14 | // Helper for parsing query parameters |
15 | 15 | function parseQueryParams(url: URL): Record<string, any> { |
@@ -314,3 +314,46 @@ export class MSWPlugin implements Plugin { |
314 | 314 | return this.handlers; |
315 | 315 | } |
316 | 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 | +} |
0 commit comments