@beyonk/http 12.5.0 → 12.6.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/README.MD +2 -0
- package/dist/index.cjs +16 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +16 -1
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -198,6 +198,8 @@ ctx can be whatever you want really - it is whatever you pass in as `context(...
|
|
|
198
198
|
|
|
199
199
|
However, if the `context` object you pass in has a `fetch` function, this is used as the `fetch` for XHR requests.
|
|
200
200
|
|
|
201
|
+
If it has a `logger` with an `error(details, message)` method, such as a pino logger, every failure that reaches a `default` handler (request-level or global) is logged through it before the handler runs, with `err`, `status`, `method`, `url` and `body`. Without one the console is used. Failures caught by a status-specific handler such as `.notFound()` are not logged.
|
|
202
|
+
|
|
201
203
|
```js
|
|
202
204
|
export async function preload () {
|
|
203
205
|
await Api
|
package/dist/index.cjs
CHANGED
|
@@ -144,11 +144,26 @@ var Api = class {
|
|
|
144
144
|
handle(e, ctx) {
|
|
145
145
|
const constructorName = Object.getPrototypeOf(e).constructor.name;
|
|
146
146
|
const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
|
|
147
|
-
const
|
|
147
|
+
const specificHandler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName];
|
|
148
|
+
const handler = specificHandler || this.defaultHandler || this.options.handlers && this.options.handlers.default || ((e2) => {
|
|
148
149
|
console.error(constructorName, e2.message, e2);
|
|
149
150
|
});
|
|
151
|
+
if (!specificHandler) {
|
|
152
|
+
this.log(e, ctx);
|
|
153
|
+
}
|
|
150
154
|
return handler(e, ctx);
|
|
151
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Log a failed request through the context logger, or the console
|
|
158
|
+
* @param {HttpError} e - Error instance
|
|
159
|
+
* @param {ApiContext} [ctx] - API context
|
|
160
|
+
*/
|
|
161
|
+
log(e, ctx) {
|
|
162
|
+
const logger = ctx && ctx.logger || console;
|
|
163
|
+
const { method, url } = e.request || {};
|
|
164
|
+
const reason = e.status ? `with ${e.status}` : `(${e.message})`;
|
|
165
|
+
logger.error({ err: e, status: e.status, method, url, body: e.body }, `${method} ${url} failed ${reason}`);
|
|
166
|
+
}
|
|
152
167
|
/**
|
|
153
168
|
* Send the HTTP request
|
|
154
169
|
* @template T
|
package/dist/index.d.cts
CHANGED
|
@@ -30,6 +30,10 @@ type RetryOptions = {
|
|
|
30
30
|
type ApiContext = {
|
|
31
31
|
/** Fetch client */
|
|
32
32
|
fetch?: FetchClient;
|
|
33
|
+
/** Logger for failed requests, defaults to console */
|
|
34
|
+
logger?: {
|
|
35
|
+
error: (details: Record<string, any>, message: string) => void;
|
|
36
|
+
};
|
|
33
37
|
/** Additional context properties */
|
|
34
38
|
[key: string]: any;
|
|
35
39
|
};
|
|
@@ -152,6 +156,12 @@ declare class Api {
|
|
|
152
156
|
* @returns {any} Result of error handler
|
|
153
157
|
*/
|
|
154
158
|
handle(e: HttpError, ctx?: ApiContext): any;
|
|
159
|
+
/**
|
|
160
|
+
* Log a failed request through the context logger, or the console
|
|
161
|
+
* @param {HttpError} e - Error instance
|
|
162
|
+
* @param {ApiContext} [ctx] - API context
|
|
163
|
+
*/
|
|
164
|
+
log(e: HttpError, ctx?: ApiContext): void;
|
|
155
165
|
/**
|
|
156
166
|
* Send the HTTP request
|
|
157
167
|
* @template T
|
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,10 @@ type RetryOptions = {
|
|
|
30
30
|
type ApiContext = {
|
|
31
31
|
/** Fetch client */
|
|
32
32
|
fetch?: FetchClient;
|
|
33
|
+
/** Logger for failed requests, defaults to console */
|
|
34
|
+
logger?: {
|
|
35
|
+
error: (details: Record<string, any>, message: string) => void;
|
|
36
|
+
};
|
|
33
37
|
/** Additional context properties */
|
|
34
38
|
[key: string]: any;
|
|
35
39
|
};
|
|
@@ -152,6 +156,12 @@ declare class Api {
|
|
|
152
156
|
* @returns {any} Result of error handler
|
|
153
157
|
*/
|
|
154
158
|
handle(e: HttpError, ctx?: ApiContext): any;
|
|
159
|
+
/**
|
|
160
|
+
* Log a failed request through the context logger, or the console
|
|
161
|
+
* @param {HttpError} e - Error instance
|
|
162
|
+
* @param {ApiContext} [ctx] - API context
|
|
163
|
+
*/
|
|
164
|
+
log(e: HttpError, ctx?: ApiContext): void;
|
|
155
165
|
/**
|
|
156
166
|
* Send the HTTP request
|
|
157
167
|
* @template T
|
package/dist/index.js
CHANGED
|
@@ -119,11 +119,26 @@ var Api = class {
|
|
|
119
119
|
handle(e, ctx) {
|
|
120
120
|
const constructorName = Object.getPrototypeOf(e).constructor.name;
|
|
121
121
|
const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
|
|
122
|
-
const
|
|
122
|
+
const specificHandler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName];
|
|
123
|
+
const handler = specificHandler || this.defaultHandler || this.options.handlers && this.options.handlers.default || ((e2) => {
|
|
123
124
|
console.error(constructorName, e2.message, e2);
|
|
124
125
|
});
|
|
126
|
+
if (!specificHandler) {
|
|
127
|
+
this.log(e, ctx);
|
|
128
|
+
}
|
|
125
129
|
return handler(e, ctx);
|
|
126
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Log a failed request through the context logger, or the console
|
|
133
|
+
* @param {HttpError} e - Error instance
|
|
134
|
+
* @param {ApiContext} [ctx] - API context
|
|
135
|
+
*/
|
|
136
|
+
log(e, ctx) {
|
|
137
|
+
const logger = ctx && ctx.logger || console;
|
|
138
|
+
const { method, url } = e.request || {};
|
|
139
|
+
const reason = e.status ? `with ${e.status}` : `(${e.message})`;
|
|
140
|
+
logger.error({ err: e, status: e.status, method, url, body: e.body }, `${method} ${url} failed ${reason}`);
|
|
141
|
+
}
|
|
127
142
|
/**
|
|
128
143
|
* Send the HTTP request
|
|
129
144
|
* @template T
|