terrier-engine 4.13.3 → 4.13.4

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
@@ -4,7 +4,7 @@
4
4
  "files": [
5
5
  "*"
6
6
  ],
7
- "version": "4.13.3",
7
+ "version": "4.13.4",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/Terrier-Tech/terrier-engine"
package/terrier/api.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Logger } from "tuff-core/logging"
2
- import { QueryParams } from "tuff-core/urls"
1
+ import {Logger} from "tuff-core/logging"
2
+ import {QueryParams} from "tuff-core/urls"
3
3
  import {LogEntry} from "./logging"
4
4
 
5
5
  const log = new Logger('Api')
@@ -27,9 +27,37 @@ export class ApiException extends Error {
27
27
  }
28
28
  }
29
29
 
30
- async function request<ResponseType>(url: string, config: RequestInit): Promise<ResponseType> {
30
+ export type RequestFunc = <ResponseType>(url: string, config: RequestInit) => Promise<ResponseType>
31
+
32
+ /**
33
+ * Decorates all API requests made by the `Api` module with the given decorator method.
34
+ *
35
+ * This can be dangerous and should be used sparingly! You can't remove a decorator once it has been added,
36
+ * and decorators apply to _all_ API requests. It is most appropriate for adding ubiquitous parameters and headers,
37
+ * such as authentication tokens.
38
+ *
39
+ * @param decorator a function that takes a request function and returns a decorated request function
40
+ *
41
+ * @example
42
+ * // adds a decorator that logs the request parameters and response for every request.
43
+ * Api.addRequestDecorator((inner) => async (url: string, config: RequestInit) => {
44
+ * console.log("before request:", url, config)
45
+ * const res = await inner(url, config)
46
+ * console.log("after request:", res)
47
+ * return res
48
+ * })
49
+ */
50
+ function addRequestDecorator(decorator: (inner: RequestFunc) => RequestFunc) {
51
+ _decoratedRequest = decorator(_decoratedRequest)
52
+ }
53
+
54
+ let _decoratedRequest: RequestFunc = async <RequestType>(url: string, config: RequestInit) => {
31
55
  const response = await fetch(url, config)
32
- return await response.json()
56
+ return await response.json() as RequestType
57
+ }
58
+
59
+ async function request<ResponseType>(url: string, config: RequestInit): Promise<ResponseType> {
60
+ return await _decoratedRequest(url, config)
33
61
  }
34
62
 
35
63
  async function apiRequest<ResponseType>(url: string, config: RequestInit): Promise<ApiResponse & ResponseType> {
@@ -216,6 +244,7 @@ const Api = {
216
244
  safeGet,
217
245
  safePost,
218
246
  post,
219
- stream
247
+ stream,
248
+ addRequestDecorator,
220
249
  }
221
250
  export default Api