@schmock/angular 1.3.1 → 1.4.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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { HttpInterceptor, HttpRequest } from "@angular/common/http";
2
- import type { CallableMockInstance, ResponseResult } from "@schmock/core";
2
+ import type { CallableMockInstance } from "@schmock/core";
3
3
  /**
4
4
  * Configuration options for Angular adapter
5
5
  */
@@ -39,7 +39,15 @@ export interface AngularAdapterOptions {
39
39
  * @param request - Original Angular request
40
40
  * @returns Modified response
41
41
  */
42
- transformResponse?: (response: ResponseResult, request: HttpRequest<any>) => ResponseResult;
42
+ transformResponse?: (response: {
43
+ status: number;
44
+ body: unknown;
45
+ headers: Record<string, string>;
46
+ }, request: HttpRequest<any>) => {
47
+ status: number;
48
+ body: unknown;
49
+ headers: Record<string, string>;
50
+ };
43
51
  }
44
52
  /**
45
53
  * Create an Angular HTTP interceptor from a Schmock instance
@@ -49,7 +57,7 @@ export declare function createSchmockInterceptor(mock: CallableMockInstance, opt
49
57
  * Provider configuration for Angular module
50
58
  */
51
59
  export declare function provideSchmockInterceptor(mock: CallableMockInstance, options?: AngularAdapterOptions): {
52
- provide: string;
60
+ provide: import("@angular/core").InjectionToken<readonly HttpInterceptor[]>;
53
61
  useClass: new () => HttpInterceptor;
54
62
  multi: boolean;
55
63
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,eAAe,EACf,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAO9B,OAAO,KAAK,EACV,oBAAoB,EAEpB,cAAc,EACf,MAAM,eAAe,CAAC;AAyCvB;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;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G;AACH,wBAAgB,YAAY,CAC1B,OAAO,GAAE,MAAM,GAAG,MAAuB,GACxC,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,OAAO,GAAE,MAAM,GAAG,MAAoB,GACrC,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,OAAO,GAAE,MAAM,GAAG,MAAgC,GACjD,CAAC,MAAM,EAAE,MAAM,CAAC,CAGlB;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAEtD;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAE1C;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,GAAE,eAAoB,GAC5B,iBAAiB,CAAC,CAAC,CAAC,CAgBtB"}
package/dist/index.js CHANGED
@@ -11,11 +11,13 @@ var __legacyDecorateClassTS = function(decorators, target, key, desc) {
11
11
 
12
12
  // src/index.ts
13
13
  import {
14
+ HTTP_INTERCEPTORS,
14
15
  HttpErrorResponse,
15
16
  HttpHeaders,
16
17
  HttpResponse
17
18
  } from "@angular/common/http";
18
19
  import { Injectable } from "@angular/core";
20
+ import { ROUTE_NOT_FOUND_CODE } from "@schmock/core";
19
21
  import { Observable } from "rxjs";
20
22
  var HTTP_METHODS = [
21
23
  "GET",
@@ -86,7 +88,7 @@ function headersToObject(request) {
86
88
  const headers = {};
87
89
  request.headers.keys().forEach((key) => {
88
90
  const value = request.headers.get(key);
89
- if (value) {
91
+ if (value !== null) {
90
92
  headers[key] = value;
91
93
  }
92
94
  });
@@ -124,15 +126,17 @@ function createSchmockInterceptor(mock, options = {}) {
124
126
  };
125
127
  }
126
128
  return new Observable((observer) => {
129
+ let innerSub;
127
130
  mock.handle(requestData.method, requestData.path, {
128
131
  headers: requestData.headers,
129
132
  body: requestData.body,
130
133
  query: requestData.query
131
134
  }).then((schmockResponse) => {
132
- const isRouteNotFound = schmockResponse?.status === 404 && schmockResponse?.body?.code === "ROUTE_NOT_FOUND";
135
+ const body = schmockResponse.body;
136
+ const isRouteNotFound = schmockResponse.status === 404 && body !== null && typeof body === "object" && "code" in body && body.code === ROUTE_NOT_FOUND_CODE;
133
137
  if (isRouteNotFound && passthrough) {
134
- next.handle(req).subscribe(observer);
135
- } else if (isRouteNotFound && !passthrough) {
138
+ innerSub = next.handle(req).subscribe(observer);
139
+ } else if (isRouteNotFound) {
136
140
  observer.error(new HttpErrorResponse({
137
141
  error: { message: "No matching mock route found" },
138
142
  status: 404,
@@ -147,8 +151,10 @@ function createSchmockInterceptor(mock, options = {}) {
147
151
  const status = response.status || 200;
148
152
  if (status >= 400) {
149
153
  let errorBody = response.body;
150
- if (status === 500 && errorFormatter && response.body?.error && response.body?.code) {
151
- const error = new Error(response.body.error);
154
+ const respBody = response.body;
155
+ if (status === 500 && errorFormatter && respBody !== null && typeof respBody === "object" && "error" in respBody && "code" in respBody) {
156
+ const errMsg = typeof respBody.error === "string" ? respBody.error : "Unknown error";
157
+ const error = new Error(errMsg);
152
158
  errorBody = errorFormatter(error, req);
153
159
  }
154
160
  observer.error(new HttpErrorResponse({
@@ -175,10 +181,10 @@ function createSchmockInterceptor(mock, options = {}) {
175
181
  if (errorFormatter) {
176
182
  errorBody = errorFormatter(error instanceof Error ? error : new Error(String(error)), req);
177
183
  } else {
178
- const errorWithCode = error;
184
+ const hasCode = error !== null && typeof error === "object" && "code" in error && typeof error.code === "string";
179
185
  errorBody = {
180
186
  error: error instanceof Error ? error.message : "Internal Server Error",
181
- code: errorWithCode.code ?? "INTERNAL_ERROR"
187
+ code: hasCode ? error.code : "INTERNAL_ERROR"
182
188
  };
183
189
  }
184
190
  observer.error(new HttpErrorResponse({
@@ -188,6 +194,9 @@ function createSchmockInterceptor(mock, options = {}) {
188
194
  url: req.url
189
195
  }));
190
196
  });
197
+ return () => {
198
+ innerSub?.unsubscribe();
199
+ };
191
200
  });
192
201
  }
193
202
  }
@@ -198,7 +207,7 @@ function createSchmockInterceptor(mock, options = {}) {
198
207
  }
199
208
  function provideSchmockInterceptor(mock, options) {
200
209
  return {
201
- provide: "HTTP_INTERCEPTORS",
210
+ provide: HTTP_INTERCEPTORS,
202
211
  useClass: createSchmockInterceptor(mock, options),
203
212
  multi: true
204
213
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schmock/angular",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "Angular HTTP interceptor adapter for Schmock",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -34,16 +34,13 @@
34
34
  "rxjs": "^7.0.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@amiceli/vitest-cucumber": "^6.1.0",
38
- "@angular/common": "^21.0.5",
37
+ "@angular/common": "^21.1.2",
39
38
  "@angular/compiler": "^21.0.5",
40
- "@angular/core": "^21.0.5",
39
+ "@angular/core": "^21.1.1",
41
40
  "@angular/platform-browser": "^21.0.5",
42
41
  "@angular/platform-browser-dynamic": "^21.0.5",
43
- "@schmock/core": "^1.0.0",
44
- "@types/node": "^25.0.0",
45
- "jsdom": "^27.3.0",
46
- "rxjs": "^7.8.1",
42
+ "@types/node": "^25.1.0",
43
+ "jsdom": "^27.4.0",
47
44
  "typescript": "^5.9.3",
48
45
  "vitest": "^4.0.5",
49
46
  "zone.js": "^0.16.0"