@scenarist/express-adapter 0.0.1 → 0.1.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/LICENSE +21 -0
- package/README.md +1004 -28
- package/dist/context/express-request-context.d.ts +11 -0
- package/dist/context/express-request-context.d.ts.map +1 -0
- package/dist/context/express-request-context.js +25 -0
- package/dist/endpoints/scenario-endpoints.d.ts +4 -0
- package/dist/endpoints/scenario-endpoints.d.ts.map +1 -0
- package/dist/endpoints/scenario-endpoints.js +63 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/middleware/test-id-middleware.d.ts +6 -0
- package/dist/middleware/test-id-middleware.d.ts.map +1 -0
- package/dist/middleware/test-id-middleware.js +12 -0
- package/dist/setup/impl.d.ts +50 -0
- package/dist/setup/impl.d.ts.map +1 -0
- package/dist/setup/impl.js +72 -0
- package/dist/setup/production.d.ts +8 -0
- package/dist/setup/production.d.ts.map +1 -0
- package/dist/setup/production.js +7 -0
- package/dist/setup/setup-scenarist.d.ts +29 -0
- package/dist/setup/setup-scenarist.d.ts.map +1 -0
- package/dist/setup/setup-scenarist.js +35 -0
- package/package.json +78 -7
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Request } from 'express';
|
|
2
|
+
import { type RequestContext, type ScenaristConfig } from '@scenarist/core';
|
|
3
|
+
export declare class ExpressRequestContext implements RequestContext {
|
|
4
|
+
private readonly req;
|
|
5
|
+
private readonly config;
|
|
6
|
+
constructor(req: Request, config: ScenaristConfig);
|
|
7
|
+
getTestId(): string;
|
|
8
|
+
getHeaders(): Record<string, string | string[] | undefined>;
|
|
9
|
+
getHostname(): string;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=express-request-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express-request-context.d.ts","sourceRoot":"","sources":["../../src/context/express-request-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAA4B,KAAK,cAAc,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEtG,qBAAa,qBAAsB,YAAW,cAAc;IAExD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,eAAe;IAG1C,SAAS,IAAI,MAAM;IAcnB,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAI3D,WAAW,IAAI,MAAM;CAGtB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
2
|
+
export class ExpressRequestContext {
|
|
3
|
+
req;
|
|
4
|
+
config;
|
|
5
|
+
constructor(req, config) {
|
|
6
|
+
this.req = req;
|
|
7
|
+
this.config = config;
|
|
8
|
+
}
|
|
9
|
+
getTestId() {
|
|
10
|
+
const header = this.req.headers[SCENARIST_TEST_ID_HEADER];
|
|
11
|
+
if (typeof header === 'string') {
|
|
12
|
+
return header;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(header) && header.length > 0 && header[0]) {
|
|
15
|
+
return header[0];
|
|
16
|
+
}
|
|
17
|
+
return this.config.defaultTestId;
|
|
18
|
+
}
|
|
19
|
+
getHeaders() {
|
|
20
|
+
return this.req.headers;
|
|
21
|
+
}
|
|
22
|
+
getHostname() {
|
|
23
|
+
return this.req.hostname;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ScenarioManager, type ScenaristConfig } from "@scenarist/core";
|
|
2
|
+
import { Router } from "express";
|
|
3
|
+
export declare const createScenarioEndpoints: (manager: ScenarioManager, config: ScenaristConfig) => Router;
|
|
4
|
+
//# sourceMappingURL=scenario-endpoints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scenario-endpoints.d.ts","sourceRoot":"","sources":["../../src/endpoints/scenario-endpoints.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,MAAM,EAA+B,MAAM,SAAS,CAAC;AA0E9D,eAAO,MAAM,uBAAuB,GAClC,SAAS,eAAe,EACxB,QAAQ,eAAe,KACtB,MAOF,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ScenarioRequestSchema, } from "@scenarist/core";
|
|
2
|
+
import { Router } from "express";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ExpressRequestContext } from "../context/express-request-context.js";
|
|
5
|
+
const handleSetScenario = (manager, config) => {
|
|
6
|
+
return (req, res) => {
|
|
7
|
+
try {
|
|
8
|
+
const { scenario } = ScenarioRequestSchema.parse(req.body);
|
|
9
|
+
const context = new ExpressRequestContext(req, config);
|
|
10
|
+
const testId = context.getTestId();
|
|
11
|
+
const result = manager.switchScenario(testId, scenario);
|
|
12
|
+
if (!result.success) {
|
|
13
|
+
res.status(400).json({
|
|
14
|
+
error: result.error.message,
|
|
15
|
+
});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
res.status(200).json({
|
|
19
|
+
success: true,
|
|
20
|
+
testId,
|
|
21
|
+
scenarioId: scenario,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof z.ZodError) {
|
|
26
|
+
res.status(400).json({
|
|
27
|
+
error: "Invalid request body",
|
|
28
|
+
details: error.issues,
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
res.status(500).json({
|
|
33
|
+
error: "Internal server error",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const handleGetScenario = (manager, config) => {
|
|
39
|
+
return (req, res) => {
|
|
40
|
+
const context = new ExpressRequestContext(req, config);
|
|
41
|
+
const testId = context.getTestId();
|
|
42
|
+
const activeScenario = manager.getActiveScenario(testId);
|
|
43
|
+
if (!activeScenario) {
|
|
44
|
+
res.status(404).json({
|
|
45
|
+
error: "No active scenario for this test ID",
|
|
46
|
+
testId,
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const scenarioDefinition = manager.getScenarioById(activeScenario.scenarioId);
|
|
51
|
+
res.status(200).json({
|
|
52
|
+
testId,
|
|
53
|
+
scenarioId: activeScenario.scenarioId,
|
|
54
|
+
...(scenarioDefinition && { scenarioName: scenarioDefinition.name }),
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export const createScenarioEndpoints = (manager, config) => {
|
|
59
|
+
const router = Router();
|
|
60
|
+
router.post(config.endpoints.setScenario, handleSetScenario(manager, config));
|
|
61
|
+
router.get(config.endpoints.getScenario, handleGetScenario(manager, config));
|
|
62
|
+
return router;
|
|
63
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createScenarist } from './setup/setup-scenarist.js';
|
|
2
|
+
export type { ExpressAdapterOptions, ExpressScenarist, } from './setup/setup-scenarist.js';
|
|
3
|
+
export { ExpressRequestContext } from './context/express-request-context.js';
|
|
4
|
+
export { createTestIdMiddleware, testIdStorage } from './middleware/test-id-middleware.js';
|
|
5
|
+
export { createScenarioEndpoints } from './endpoints/scenario-endpoints.js';
|
|
6
|
+
export { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
7
|
+
export type { ScenaristScenario, ScenaristMock, ScenaristResponse, ScenaristSequence, ScenaristMatch, ScenaristCaptureConfig, ScenaristScenarios, ScenaristConfig, ScenaristResult, } from '@scenarist/core';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,YAAY,EACV,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAC3F,OAAO,EAAE,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AAG5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,EACf,eAAe,GAChB,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Primary API - batteries included setup
|
|
2
|
+
export { createScenarist } from './setup/setup-scenarist.js';
|
|
3
|
+
// Low-level API - for advanced users who need custom wiring
|
|
4
|
+
export { ExpressRequestContext } from './context/express-request-context.js';
|
|
5
|
+
export { createTestIdMiddleware, testIdStorage } from './middleware/test-id-middleware.js';
|
|
6
|
+
export { createScenarioEndpoints } from './endpoints/scenario-endpoints.js';
|
|
7
|
+
// Re-export core types and constants for user convenience (users should only install this adapter)
|
|
8
|
+
export { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
import type { RequestHandler } from 'express';
|
|
3
|
+
import type { ScenaristConfig } from '@scenarist/core';
|
|
4
|
+
export declare const testIdStorage: AsyncLocalStorage<string>;
|
|
5
|
+
export declare const createTestIdMiddleware: (config: ScenaristConfig) => RequestHandler;
|
|
6
|
+
//# sourceMappingURL=test-id-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-id-middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/test-id-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGvD,eAAO,MAAM,aAAa,2BAAkC,CAAC;AAE7D,eAAO,MAAM,sBAAsB,GAAI,QAAQ,eAAe,KAAG,cAShE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
import { ExpressRequestContext } from '../context/express-request-context.js';
|
|
3
|
+
export const testIdStorage = new AsyncLocalStorage();
|
|
4
|
+
export const createTestIdMiddleware = (config) => {
|
|
5
|
+
return (req, _res, next) => {
|
|
6
|
+
const context = new ExpressRequestContext(req, config);
|
|
7
|
+
const testId = context.getTestId();
|
|
8
|
+
testIdStorage.run(testId, () => {
|
|
9
|
+
next();
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { type BaseAdapterOptions, type ScenaristAdapter, type ScenaristScenarios } from '@scenarist/core';
|
|
3
|
+
/**
|
|
4
|
+
* Express-specific adapter options.
|
|
5
|
+
*
|
|
6
|
+
* Extends BaseAdapterOptions to ensure consistency across all adapters.
|
|
7
|
+
*
|
|
8
|
+
* @template T - Scenarios object for type-safe scenario IDs
|
|
9
|
+
*/
|
|
10
|
+
export type ExpressAdapterOptions<T extends ScenaristScenarios = ScenaristScenarios> = BaseAdapterOptions<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Express adapter instance.
|
|
13
|
+
*
|
|
14
|
+
* Implements ScenaristAdapter<Router> to ensure API consistency.
|
|
15
|
+
*
|
|
16
|
+
* @template T - Scenarios object for type-safe scenario IDs
|
|
17
|
+
*/
|
|
18
|
+
export type ExpressScenarist<T extends ScenaristScenarios = ScenaristScenarios> = ScenaristAdapter<Router, T>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a Scenarist instance for Express.
|
|
21
|
+
*
|
|
22
|
+
* This is the primary API for Express users. It wires everything automatically:
|
|
23
|
+
* - MSW server with dynamic handler
|
|
24
|
+
* - Test ID middleware
|
|
25
|
+
* - Scenario endpoints
|
|
26
|
+
* - Scenario manager with all scenarios registered upfront
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const scenarios = {
|
|
31
|
+
* default: { id: 'default', ... }, // Required!
|
|
32
|
+
* cartWithState: { id: 'cartWithState', ... },
|
|
33
|
+
* premiumUser: { id: 'premiumUser', ... },
|
|
34
|
+
* } as const satisfies ScenaristScenarios;
|
|
35
|
+
*
|
|
36
|
+
* const scenarist = createScenarist({
|
|
37
|
+
* enabled: true,
|
|
38
|
+
* scenarios,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* app.use(scenarist.middleware);
|
|
42
|
+
* beforeAll(() => scenarist.start());
|
|
43
|
+
* afterAll(() => scenarist.stop());
|
|
44
|
+
*
|
|
45
|
+
* // TypeScript provides autocomplete for scenario IDs:
|
|
46
|
+
* scenarist.switchScenario('test-123', 'premiumUser');
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const createScenaristImpl: <T extends ScenaristScenarios>(options: ExpressAdapterOptions<T>) => ExpressScenarist<T>;
|
|
50
|
+
//# sourceMappingURL=impl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"impl.d.ts","sourceRoot":"","sources":["../../src/setup/impl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EAQL,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACxB,MAAM,iBAAiB,CAAC;AAMzB;;;;;;GAMG;AACH,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS,kBAAkB,GAAG,kBAAkB,IACjF,kBAAkB,CAAC,CAAC,CAAC,CAAC;AAExB;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,kBAAkB,GAAG,kBAAkB,IAC5E,gBAAgB,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,kBAAkB,EAC9D,SAAS,qBAAqB,CAAC,CAAC,CAAC,KAChC,gBAAgB,CAAC,CAAC,CA2CpB,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { setupServer } from 'msw/node';
|
|
3
|
+
import { buildConfig, createScenarioManager, createResponseSelector, InMemoryScenarioRegistry, InMemoryScenarioStore, createInMemorySequenceTracker, createInMemoryStateManager, } from '@scenarist/core';
|
|
4
|
+
import { createDynamicHandler } from '@scenarist/msw-adapter';
|
|
5
|
+
import { testIdStorage } from '../middleware/test-id-middleware.js';
|
|
6
|
+
import { createTestIdMiddleware } from '../middleware/test-id-middleware.js';
|
|
7
|
+
import { createScenarioEndpoints } from '../endpoints/scenario-endpoints.js';
|
|
8
|
+
/**
|
|
9
|
+
* Create a Scenarist instance for Express.
|
|
10
|
+
*
|
|
11
|
+
* This is the primary API for Express users. It wires everything automatically:
|
|
12
|
+
* - MSW server with dynamic handler
|
|
13
|
+
* - Test ID middleware
|
|
14
|
+
* - Scenario endpoints
|
|
15
|
+
* - Scenario manager with all scenarios registered upfront
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const scenarios = {
|
|
20
|
+
* default: { id: 'default', ... }, // Required!
|
|
21
|
+
* cartWithState: { id: 'cartWithState', ... },
|
|
22
|
+
* premiumUser: { id: 'premiumUser', ... },
|
|
23
|
+
* } as const satisfies ScenaristScenarios;
|
|
24
|
+
*
|
|
25
|
+
* const scenarist = createScenarist({
|
|
26
|
+
* enabled: true,
|
|
27
|
+
* scenarios,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* app.use(scenarist.middleware);
|
|
31
|
+
* beforeAll(() => scenarist.start());
|
|
32
|
+
* afterAll(() => scenarist.stop());
|
|
33
|
+
*
|
|
34
|
+
* // TypeScript provides autocomplete for scenario IDs:
|
|
35
|
+
* scenarist.switchScenario('test-123', 'premiumUser');
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const createScenaristImpl = (options) => {
|
|
39
|
+
const config = buildConfig(options);
|
|
40
|
+
const registry = options.registry ?? new InMemoryScenarioRegistry();
|
|
41
|
+
const store = options.store ?? new InMemoryScenarioStore();
|
|
42
|
+
const stateManager = createInMemoryStateManager();
|
|
43
|
+
const sequenceTracker = createInMemorySequenceTracker();
|
|
44
|
+
const manager = createScenarioManager({ registry, store, stateManager, sequenceTracker });
|
|
45
|
+
// Register all scenarios upfront from the scenarios object
|
|
46
|
+
Object.values(options.scenarios).forEach((scenario) => {
|
|
47
|
+
manager.registerScenario(scenario);
|
|
48
|
+
});
|
|
49
|
+
const responseSelector = createResponseSelector({ sequenceTracker, stateManager });
|
|
50
|
+
const handler = createDynamicHandler({
|
|
51
|
+
getTestId: (_request) => testIdStorage.getStore() ?? config.defaultTestId,
|
|
52
|
+
getActiveScenario: (testId) => manager.getActiveScenario(testId),
|
|
53
|
+
getScenarioDefinition: (scenarioId) => manager.getScenarioById(scenarioId),
|
|
54
|
+
strictMode: config.strictMode,
|
|
55
|
+
responseSelector,
|
|
56
|
+
});
|
|
57
|
+
const server = setupServer(handler);
|
|
58
|
+
const middleware = Router();
|
|
59
|
+
middleware.use(createTestIdMiddleware(config));
|
|
60
|
+
middleware.use(createScenarioEndpoints(manager, config));
|
|
61
|
+
return {
|
|
62
|
+
config,
|
|
63
|
+
middleware,
|
|
64
|
+
switchScenario: (testId, scenarioId) => manager.switchScenario(testId, scenarioId),
|
|
65
|
+
getActiveScenario: (testId) => manager.getActiveScenario(testId),
|
|
66
|
+
getScenarioById: (scenarioId) => manager.getScenarioById(scenarioId),
|
|
67
|
+
listScenarios: () => manager.listScenarios(),
|
|
68
|
+
clearScenario: (testId) => manager.clearScenario(testId),
|
|
69
|
+
start: () => server.listen(),
|
|
70
|
+
stop: async () => server.close(),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ScenaristScenarios } from '@scenarist/core';
|
|
2
|
+
export type { ExpressAdapterOptions, ExpressScenarist } from './impl.js';
|
|
3
|
+
/**
|
|
4
|
+
* Production-only entry point that returns undefined without loading any dependencies.
|
|
5
|
+
* This file has ZERO imports to guarantee tree-shaking.
|
|
6
|
+
*/
|
|
7
|
+
export declare const createScenarist: <T extends ScenaristScenarios>(_options: import("./impl.js").ExpressAdapterOptions<T>) => Promise<import("./impl.js").ExpressScenarist<T> | undefined>;
|
|
8
|
+
//# sourceMappingURL=production.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/setup/production.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzE;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,kBAAkB,EAChE,UAAU,OAAO,WAAW,EAAE,qBAAqB,CAAC,CAAC,CAAC,KACrD,OAAO,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,CAE7D,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ScenaristScenarios } from '@scenarist/core';
|
|
2
|
+
export type { ExpressAdapterOptions, ExpressScenarist } from './impl.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a Scenarist instance for Express.
|
|
5
|
+
*
|
|
6
|
+
* In production (NODE_ENV=production), this returns undefined to enable
|
|
7
|
+
* tree-shaking of all Scenarist code. In development/test environments,
|
|
8
|
+
* it returns a fully-functional ExpressScenarist instance.
|
|
9
|
+
*
|
|
10
|
+
* This pattern uses dynamic import to enable tree-shaking: the impl.js module
|
|
11
|
+
* is only loaded when needed (non-production). Combined with sideEffects: false,
|
|
12
|
+
* bundlers can eliminate the entire impl.js module from production builds.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const scenarist = await createScenarist({
|
|
17
|
+
* enabled: true,
|
|
18
|
+
* scenarios,
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* if (scenarist) {
|
|
22
|
+
* app.use(scenarist.middleware);
|
|
23
|
+
* beforeAll(() => scenarist.start());
|
|
24
|
+
* afterAll(() => scenarist.stop());
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const createScenarist: <T extends ScenaristScenarios>(options: import("./impl.js").ExpressAdapterOptions<T>) => Promise<import("./impl.js").ExpressScenarist<T> | undefined>;
|
|
29
|
+
//# sourceMappingURL=setup-scenarist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-scenarist.d.ts","sourceRoot":"","sources":["../../src/setup/setup-scenarist.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,kBAAkB,EAChE,SAAS,OAAO,WAAW,EAAE,qBAAqB,CAAC,CAAC,CAAC,KACpD,OAAO,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,CAU7D,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a Scenarist instance for Express.
|
|
3
|
+
*
|
|
4
|
+
* In production (NODE_ENV=production), this returns undefined to enable
|
|
5
|
+
* tree-shaking of all Scenarist code. In development/test environments,
|
|
6
|
+
* it returns a fully-functional ExpressScenarist instance.
|
|
7
|
+
*
|
|
8
|
+
* This pattern uses dynamic import to enable tree-shaking: the impl.js module
|
|
9
|
+
* is only loaded when needed (non-production). Combined with sideEffects: false,
|
|
10
|
+
* bundlers can eliminate the entire impl.js module from production builds.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const scenarist = await createScenarist({
|
|
15
|
+
* enabled: true,
|
|
16
|
+
* scenarios,
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* if (scenarist) {
|
|
20
|
+
* app.use(scenarist.middleware);
|
|
21
|
+
* beforeAll(() => scenarist.start());
|
|
22
|
+
* afterAll(() => scenarist.stop());
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const createScenarist = async (options) => {
|
|
27
|
+
// In production, return undefined without loading impl.js
|
|
28
|
+
// Dynamic import below is never executed, enabling tree-shaking
|
|
29
|
+
if (process.env.NODE_ENV === 'production') {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
// In non-production, dynamically import and create instance
|
|
33
|
+
const { createScenaristImpl } = await import('./impl.js');
|
|
34
|
+
return createScenaristImpl(options);
|
|
35
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,81 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scenarist/express-adapter",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Express middleware adapter for Scenarist",
|
|
5
|
+
"author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/citypaul/scenarist#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/citypaul/scenarist.git",
|
|
11
|
+
"directory": "packages/express-adapter"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/citypaul/scenarist/issues"
|
|
15
|
+
},
|
|
5
16
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
17
|
+
"express",
|
|
18
|
+
"middleware",
|
|
19
|
+
"msw",
|
|
20
|
+
"testing",
|
|
21
|
+
"scenario-testing",
|
|
22
|
+
"mock-service-worker",
|
|
23
|
+
"integration-testing",
|
|
24
|
+
"e2e-testing",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18.0.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"type": "module",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"main": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
40
|
+
"production": "./dist/setup/production.js",
|
|
41
|
+
"default": "./dist/index.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"README.md",
|
|
47
|
+
"LICENSE"
|
|
48
|
+
],
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"zod": "^4.1.12",
|
|
51
|
+
"@scenarist/core": "0.0.0",
|
|
52
|
+
"@scenarist/msw-adapter": "0.0.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"express": "^4.18.0 || ^5.0.0",
|
|
56
|
+
"msw": "^2.0.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/express": "^5.0.5",
|
|
60
|
+
"@types/node": "^24.10.1",
|
|
61
|
+
"@types/supertest": "^6.0.2",
|
|
62
|
+
"@vitest/coverage-v8": "^4.0.8",
|
|
63
|
+
"eslint": "^9.39.1",
|
|
64
|
+
"express": "^5.1.0",
|
|
65
|
+
"msw": "^2.12.1",
|
|
66
|
+
"supertest": "^7.0.0",
|
|
67
|
+
"typescript": "^5.9.3",
|
|
68
|
+
"vitest": "^4.0.8",
|
|
69
|
+
"@scenarist/eslint-config": "^0.0.0",
|
|
70
|
+
"@scenarist/typescript-config": "0.0.0"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"build": "tsc",
|
|
74
|
+
"dev": "tsc --watch",
|
|
75
|
+
"test": "vitest run",
|
|
76
|
+
"test:watch": "vitest",
|
|
77
|
+
"typecheck": "tsc --project tsconfig.typecheck.json --noEmit",
|
|
78
|
+
"lint": "eslint .",
|
|
79
|
+
"clean": "rm -rf dist"
|
|
80
|
+
}
|
|
81
|
+
}
|