@placeos/ts-client 4.2.5 → 4.2.7
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/api.d.ts +1 -1
- package/dist/http/mock.d.ts +7 -1
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +851 -844
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/api.ts +5 -1
- package/src/http/mock.ts +28 -9
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -11,7 +11,11 @@ export type {
|
|
|
11
11
|
MockHttpRequestHandler,
|
|
12
12
|
MockHttpRequestHandlerOptions,
|
|
13
13
|
} from './http/interfaces';
|
|
14
|
-
export {
|
|
14
|
+
export {
|
|
15
|
+
deregisterMockEndpoint,
|
|
16
|
+
registerMockEndpoint,
|
|
17
|
+
setMockNotFoundHandler,
|
|
18
|
+
} from './http/mock';
|
|
15
19
|
|
|
16
20
|
export { PlaceApplication } from './applications/application';
|
|
17
21
|
export {
|
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
|
|
@@ -30,7 +51,7 @@ export function registerMockEndpoint<T>(
|
|
|
30
51
|
deregisterMockEndpoint(handler_ops.method, handler_ops.path, handler_map);
|
|
31
52
|
const key = `${handler_ops.method}|${handler_ops.path}`;
|
|
32
53
|
const path_parts = handler_ops.path
|
|
33
|
-
.replace(/(http|https):\/\/[a-zA-Z0-9
|
|
54
|
+
.replace(/(http|https):\/\/[a-zA-Z0-9.-]*:?([0-9]*)?/g, '') // Remove URL origin
|
|
34
55
|
.replace(/^\//, '')
|
|
35
56
|
.split('/');
|
|
36
57
|
const handler: MockHttpRequestHandler<T> = {
|
|
@@ -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
|
|
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.
|
|
@@ -92,15 +113,13 @@ export function mockRequest(
|
|
|
92
113
|
body?: any,
|
|
93
114
|
handler_map: HashMap<MockHttpRequestHandler> = _handlers,
|
|
94
115
|
): Observable<HashMap | string | void> | null {
|
|
95
|
-
if (window.debug) {
|
|
96
|
-
console.log('Resolving request:', method, url, body);
|
|
97
|
-
}
|
|
98
116
|
const handler = findRequestHandler(method, url, handler_map);
|
|
99
117
|
if (handler) {
|
|
100
118
|
const request = processRequest(url, handler, body);
|
|
101
119
|
return onMockRequest(handler, request);
|
|
102
120
|
}
|
|
103
|
-
|
|
121
|
+
// Return 404 error when no handler is found
|
|
122
|
+
return _error_handler(method, url);
|
|
104
123
|
}
|
|
105
124
|
|
|
106
125
|
/**
|
|
@@ -116,7 +135,7 @@ export function findRequestHandler(
|
|
|
116
135
|
handler_map: HashMap<MockHttpRequestHandler> = _handlers,
|
|
117
136
|
): MockHttpRequestHandler | null {
|
|
118
137
|
const path = url
|
|
119
|
-
.replace(/(http|https):\/\/[a-zA-Z0-9
|
|
138
|
+
.replace(/(http|https):\/\/[a-zA-Z0-9.-]*:?([0-9]*)?/g, '')
|
|
120
139
|
.replace(/^\//, '')
|
|
121
140
|
.split('?')[0];
|
|
122
141
|
const route_parts = path.split('/');
|
|
@@ -162,7 +181,7 @@ export function processRequest<T = any>(
|
|
|
162
181
|
body?: any,
|
|
163
182
|
): MockHttpRequest {
|
|
164
183
|
const parts = url
|
|
165
|
-
.replace(/(http|https):\/\/[a-zA-Z0-9
|
|
184
|
+
.replace(/(http|https):\/\/[a-zA-Z0-9.-]*:?([0-9]*)?/g, '')
|
|
166
185
|
.split('?');
|
|
167
186
|
const path = parts[0].replace(/^\//, '');
|
|
168
187
|
const query = parts[1] || '';
|