@placeos/ts-client 4.2.4 → 4.2.6

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.2.4",
2
+ "version": "4.2.6",
3
3
  "license": "MIT",
4
4
  "name": "@placeos/ts-client",
5
5
  "author": "Alex Sorafumo <alex@place.tech>",
package/src/api.ts CHANGED
@@ -11,7 +11,11 @@ export type {
11
11
  MockHttpRequestHandler,
12
12
  MockHttpRequestHandlerOptions,
13
13
  } from './http/interfaces';
14
- export { deregisterMockEndpoint, registerMockEndpoint } from './http/mock';
14
+ export {
15
+ deregisterMockEndpoint,
16
+ registerMockEndpoint,
17
+ setMockNotFoundHandler,
18
+ } from './http/mock';
15
19
 
16
20
  export { PlaceApplication } from './applications/application';
17
21
  export {
@@ -272,9 +272,7 @@ export function request(
272
272
  ): Observable<HttpResponse> {
273
273
  if (is_mock()) {
274
274
  const request_obs = mock_handler(method, url, options?.body);
275
- if (request_obs) {
276
- return request_obs;
277
- }
275
+ if (request_obs) return request_obs;
278
276
  }
279
277
  options.headers = options.headers || {};
280
278
  if (!options.headers['Content-Type'] && !options.headers['content-type']) {
package/src/http/mock.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { from, Observable } from 'rxjs';
1
+ import { from, Observable, throwError } from 'rxjs';
2
2
  import { delay } from 'rxjs/operators';
3
3
 
4
4
  import { convertPairStringToMap, log } from '../utilities/general';
@@ -15,6 +15,27 @@ import {
15
15
  */
16
16
  const _handlers: HashMap<MockHttpRequestHandler> = {};
17
17
 
18
+ let _error_handler: (
19
+ method: HttpVerb,
20
+ url: string,
21
+ ) => Observable<never> | null = (method: HttpVerb, url: string) => {
22
+ const error = new Error(`Mock endpoint not found: ${method} ${url}`);
23
+ (error as any).status = 404;
24
+ log('HTTP(M)', `404 ${method}:`, url);
25
+ return throwError(error);
26
+ };
27
+
28
+ /**
29
+ * Change the handler for not found endpoints
30
+ * Return `null` if you want to make the real request
31
+ * @param handler_fn Function to handle not found mocked endpoints
32
+ */
33
+ export function setMockNotFoundHandler(
34
+ handler_fn: (method: HttpVerb, url: string) => Observable<never> | null,
35
+ ) {
36
+ _error_handler = handler_fn;
37
+ }
38
+
18
39
  /**
19
40
  * Register handler for http endpoint
20
41
  * @param path URL to be handled
@@ -80,7 +101,7 @@ export function clearMockEndpoints(
80
101
  /**
81
102
  * @private
82
103
  * Perform mock request for the given method and URL.
83
- * Returns `null` if no handler for URL and method
104
+ * Returns a 404 error if no handler for URL and method
84
105
  * @param method Http Verb for request
85
106
  * @param url URL to perform request on
86
107
  * @param handler_map Handler map to query for the request handler.
@@ -97,7 +118,8 @@ export function mockRequest(
97
118
  const request = processRequest(url, handler, body);
98
119
  return onMockRequest(handler, request);
99
120
  }
100
- return null;
121
+ // Return 404 error when no handler is found
122
+ return _error_handler(method, url);
101
123
  }
102
124
 
103
125
  /**