@scenarist/msw-adapter 0.4.15 → 0.5.1

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.
@@ -12,6 +12,9 @@ export type DynamicHandlerOptions = {
12
12
  export type DynamicRequestResolution = {
13
13
  readonly type: "handled";
14
14
  readonly response: Response;
15
+ } | {
16
+ readonly type: "rejected";
17
+ readonly response: Response;
15
18
  } | {
16
19
  readonly type: "unmatched";
17
20
  readonly strictMode: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"dynamic-handler.d.ts","sourceRoot":"","sources":["../../src/handlers/dynamic-handler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,iBAAiB,EAIjB,gBAAgB,EAChB,cAAc,EACd,MAAM,EACP,MAAM,iBAAiB,CAAC;AAKzB,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC;IACjD,QAAQ,CAAC,iBAAiB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,cAAc,GAAG,SAAS,CAAC;IAC3E,QAAQ,CAAC,qBAAqB,EAAE,CAC9B,UAAU,EAAE,MAAM,KACf,iBAAiB,GAAG,SAAS,CAAC;IACnC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAC5C,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,CAAC;IACzC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAChC;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;CAC7B,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC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,EAAE,QAAQ,CAAC;CAC7B,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEN,MAAM,MAAM,sBAAsB,GAAG,CACnC,OAAO,EAAE,OAAO,KACb,OAAO,CAAC,wBAAwB,CAAC,CAAC;AA+IvC,eAAO,MAAM,4BAA4B,GACvC,SAAS,qBAAqB,KAC7B,sBAyJF,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAC/B,SAAS,qBAAqB,KAC7B,WAgBF,CAAC"}
@@ -44,6 +44,18 @@ const extractHttpRequestContext = async (request) => {
44
44
  query,
45
45
  };
46
46
  };
47
+ /**
48
+ * Marks an error raised by a configured errorBehaviors 'throw', so the shared
49
+ * server can let another registration handle the request first. Unexpected
50
+ * errors (including ScenaristErrors from collaborators) stay terminal.
51
+ */
52
+ class ErrorBehaviorRejection extends Error {
53
+ error;
54
+ constructor(error) {
55
+ super(error.message);
56
+ this.error = error;
57
+ }
58
+ }
47
59
  /**
48
60
  * Check if a mock matches the request's method and URL.
49
61
  * Returns match result with extracted URL params if matching.
@@ -116,7 +128,7 @@ export const createDynamicRequestResolver = (options) => {
116
128
  // Check for missing test ID
117
129
  if (!testId) {
118
130
  if (options.errorBehaviors?.onMissingTestId === "throw") {
119
- throw new ScenaristError("Missing test ID header. Ensure your test setup sends the x-scenarist-test-id header with each request.", {
131
+ throw new ErrorBehaviorRejection(new ScenaristError("Missing test ID header. Ensure your test setup sends the x-scenarist-test-id header with each request.", {
120
132
  code: ErrorCodes.MISSING_TEST_ID,
121
133
  context: {
122
134
  requestInfo: {
@@ -125,7 +137,7 @@ export const createDynamicRequestResolver = (options) => {
125
137
  },
126
138
  hint: "This typically means: 1) Test didn't call switchScenario() before making requests, 2) Request originated outside the test context, or 3) Header forwarding is misconfigured.",
127
139
  },
128
- });
140
+ }));
129
141
  }
130
142
  if (options.errorBehaviors?.onMissingTestId === "warn" &&
131
143
  options.logger) {
@@ -157,7 +169,7 @@ export const createDynamicRequestResolver = (options) => {
157
169
  : options.errorBehaviors?.onNoMockFound;
158
170
  // Handle error based on configured behavior
159
171
  if (errorBehavior === "throw") {
160
- throw result.error;
172
+ throw new ErrorBehaviorRejection(result.error);
161
173
  }
162
174
  if (errorBehavior === "warn" && options.logger) {
163
175
  options.logger.warn("matching", result.error.message, {
@@ -172,7 +184,9 @@ export const createDynamicRequestResolver = (options) => {
172
184
  strictMode: options.strictMode,
173
185
  };
174
186
  }
175
- catch (error) {
187
+ catch (thrown) {
188
+ const rejected = thrown instanceof ErrorBehaviorRejection;
189
+ const error = rejected ? thrown.error : thrown;
176
190
  // Log the error via Logger if available
177
191
  if (options.logger) {
178
192
  const errorMessage = error instanceof Error ? error.message : String(error);
@@ -207,7 +221,7 @@ export const createDynamicRequestResolver = (options) => {
207
221
  code: errorCode,
208
222
  };
209
223
  return {
210
- type: "handled",
224
+ type: rejected ? "rejected" : "handled",
211
225
  response: new Response(JSON.stringify(responseBody), {
212
226
  status: 500,
213
227
  headers: { "Content-Type": "application/json" },
@@ -220,7 +234,7 @@ export const createDynamicHandler = (options) => {
220
234
  const resolveRequest = createDynamicRequestResolver(options);
221
235
  return http.all("*", async ({ request }) => {
222
236
  const resolution = await resolveRequest(request);
223
- if (resolution.type === "handled") {
237
+ if (resolution.type !== "unmatched") {
224
238
  return resolution.response;
225
239
  }
226
240
  if (resolution.strictMode) {
@@ -1 +1 @@
1
- {"version":3,"file":"create-shared-server.d.ts","sourceRoot":"","sources":["../../src/server/create-shared-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AACzD,OAAO,EAEL,KAAK,qBAAqB,EAE1B,KAAK,sBAAsB,EAC5B,MAAM,gCAAgC,CAAC;AAExC,KAAK,YAAY,GAAG;IAClB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,cAAc,EAAE,sBAAsB,CAA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BAAoB,CAAA;KAAE,CAAC;CACpD,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B,CAAC;AAsEF,OAAO,CAAC,MAAM,CAAC;IACb,IAAI,6BAA6B,EAAE,iBAAiB,GAAG,SAAS,CAAC;CAClE;AAMD,eAAO,MAAM,qBAAqB,GAChC,SAAS,qBAAqB,KAC7B,eAoDF,CAAC"}
@@ -14,6 +14,12 @@ const resolveRequest = async (registrations, request) => {
14
14
  if (remainingResolution.type === "handled") {
15
15
  return remainingResolution;
16
16
  }
17
+ if (resolution.type === "rejected") {
18
+ return resolution;
19
+ }
20
+ if (remainingResolution.type === "rejected") {
21
+ return remainingResolution;
22
+ }
17
23
  return {
18
24
  type: "unmatched",
19
25
  strictMode: resolution.strictMode || remainingResolution.strictMode,
@@ -28,7 +34,7 @@ const createSharedServerState = () => {
28
34
  };
29
35
  const server = setupServer(http.all("*", async ({ request }) => {
30
36
  const resolution = await resolveRequest(snapshot.value.registrations, request);
31
- if (resolution.type === "handled") {
37
+ if (resolution.type !== "unmatched") {
32
38
  return resolution.response;
33
39
  }
34
40
  if (resolution.strictMode) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenarist/msw-adapter",
3
- "version": "0.4.15",
3
+ "version": "0.5.1",
4
4
  "description": "Internal: MSW integration layer for Scenarist framework adapters",
5
5
  "author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
6
6
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "LICENSE"
42
42
  ],
43
43
  "dependencies": {
44
- "@scenarist/core": "0.4.15",
44
+ "@scenarist/core": "0.5.1",
45
45
  "path-to-regexp": "^6.3.0"
46
46
  },
47
47
  "peerDependencies": {