@npora/request 1.0.1 → 1.1.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 +21 -0
- package/dist/index.cjs +110 -40
- package/dist/index.d.cts +49 -1
- package/dist/index.d.ts +49 -1
- package/dist/index.js +110 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -155,6 +155,27 @@ Built-in plugins:
|
|
|
155
155
|
Plugin-owned request options belong under `extensions`. Third-party plugins can
|
|
156
156
|
augment `RequestExtensions` through TypeScript module augmentation.
|
|
157
157
|
|
|
158
|
+
Inject a structured logger when request correlation and timing are needed:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
const request = createClient().use(
|
|
162
|
+
loggerPlugin({
|
|
163
|
+
logger: {
|
|
164
|
+
info(_message, entry) {
|
|
165
|
+
applicationLogger.info(entry)
|
|
166
|
+
},
|
|
167
|
+
error(_message, entry) {
|
|
168
|
+
applicationLogger.error(entry)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Lifecycle entries include a request identifier and timestamp. Completed
|
|
176
|
+
responses include total duration and attempt count; errors include the failed
|
|
177
|
+
attempt number. The default logger continues to use `console`.
|
|
178
|
+
|
|
158
179
|
## Interceptors
|
|
159
180
|
|
|
160
181
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -683,6 +683,7 @@ function setHeader(headers, key, value) {
|
|
|
683
683
|
// src/core/RequestContext.ts
|
|
684
684
|
var RequestContext = class {
|
|
685
685
|
constructor(config) {
|
|
686
|
+
this.attempt = 0;
|
|
686
687
|
this.config = config;
|
|
687
688
|
this.startTime = Date.now();
|
|
688
689
|
}
|
|
@@ -757,6 +758,7 @@ var Pipeline = class {
|
|
|
757
758
|
context.error = void 0;
|
|
758
759
|
context.response = void 0;
|
|
759
760
|
attempt += 1;
|
|
761
|
+
context.attempt = attempt;
|
|
760
762
|
try {
|
|
761
763
|
await waitForRetry(
|
|
762
764
|
decision.delay ?? 0,
|
|
@@ -2097,58 +2099,118 @@ function createProgress2(loaded, total) {
|
|
|
2097
2099
|
|
|
2098
2100
|
// src/plugins/loggerPlugin.ts
|
|
2099
2101
|
function loggerPlugin(defaultOptions = {}) {
|
|
2102
|
+
let nextRequestId = 0;
|
|
2103
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
2100
2104
|
return {
|
|
2101
2105
|
name: "logger",
|
|
2102
2106
|
install(context) {
|
|
2103
|
-
context.
|
|
2104
|
-
const
|
|
2105
|
-
config,
|
|
2107
|
+
context.hooks.onRequest((requestContext) => {
|
|
2108
|
+
const options = resolveExtensionConfig(
|
|
2109
|
+
requestContext.config,
|
|
2106
2110
|
"logger"
|
|
2107
2111
|
) ?? defaultOptions;
|
|
2108
|
-
if (
|
|
2109
|
-
return
|
|
2112
|
+
if (options.enabled === false) {
|
|
2113
|
+
return;
|
|
2110
2114
|
}
|
|
2111
|
-
|
|
2115
|
+
const state = {
|
|
2116
|
+
requestId: options.createRequestId?.() ?? `request-${++nextRequestId}`,
|
|
2117
|
+
options
|
|
2118
|
+
};
|
|
2119
|
+
states.set(requestContext, state);
|
|
2120
|
+
emitInfo(options, {
|
|
2112
2121
|
type: "request",
|
|
2113
|
-
|
|
2114
|
-
|
|
2122
|
+
requestId: state.requestId,
|
|
2123
|
+
timestamp: requestContext.startTime,
|
|
2124
|
+
method: requestContext.config.method ?? "GET",
|
|
2125
|
+
url: redactURL(requestContext.config.url)
|
|
2115
2126
|
});
|
|
2116
|
-
return config;
|
|
2117
2127
|
});
|
|
2118
|
-
context.
|
|
2119
|
-
const
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
) ?? defaultOptions;
|
|
2123
|
-
if (logger.enabled === false) {
|
|
2124
|
-
return response;
|
|
2128
|
+
context.hooks.onResponse((requestContext) => {
|
|
2129
|
+
const state = states.get(requestContext);
|
|
2130
|
+
if (!state || !requestContext.response) {
|
|
2131
|
+
return;
|
|
2125
2132
|
}
|
|
2126
|
-
|
|
2133
|
+
const timestamp = Date.now();
|
|
2134
|
+
emitInfo(state.options, {
|
|
2127
2135
|
type: "response",
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2136
|
+
requestId: state.requestId,
|
|
2137
|
+
timestamp,
|
|
2138
|
+
duration: Math.max(0, timestamp - requestContext.startTime),
|
|
2139
|
+
attempts: requestContext.attempt + 1,
|
|
2140
|
+
method: requestContext.config.method ?? "GET",
|
|
2141
|
+
url: redactURL(requestContext.config.url),
|
|
2142
|
+
status: requestContext.response.status
|
|
2131
2143
|
});
|
|
2132
|
-
return response;
|
|
2133
2144
|
});
|
|
2134
|
-
context.
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2145
|
+
context.hooks.onError((requestContext) => {
|
|
2146
|
+
let state = states.get(requestContext);
|
|
2147
|
+
if (!state) {
|
|
2148
|
+
const options = resolveExtensionConfig(
|
|
2149
|
+
requestContext.config,
|
|
2150
|
+
"logger"
|
|
2151
|
+
) ?? defaultOptions;
|
|
2152
|
+
if (options.enabled === false) {
|
|
2153
|
+
return;
|
|
2154
|
+
}
|
|
2155
|
+
state = {
|
|
2156
|
+
requestId: options.createRequestId?.() ?? `request-${++nextRequestId}`,
|
|
2157
|
+
options
|
|
2158
|
+
};
|
|
2159
|
+
states.set(requestContext, state);
|
|
2160
|
+
emitInfo(options, {
|
|
2161
|
+
type: "request",
|
|
2162
|
+
requestId: state.requestId,
|
|
2163
|
+
timestamp: requestContext.startTime,
|
|
2164
|
+
method: requestContext.config.method ?? "GET",
|
|
2165
|
+
url: redactURL(requestContext.config.url)
|
|
2166
|
+
});
|
|
2142
2167
|
}
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2168
|
+
const timestamp = Date.now();
|
|
2169
|
+
emitError(
|
|
2170
|
+
state.options,
|
|
2171
|
+
createErrorLog(
|
|
2172
|
+
requestContext.error,
|
|
2173
|
+
state.requestId,
|
|
2174
|
+
timestamp,
|
|
2175
|
+
Math.max(0, timestamp - requestContext.startTime),
|
|
2176
|
+
requestContext.attempt + 1,
|
|
2177
|
+
requestContext.config.method ?? "GET",
|
|
2178
|
+
redactURL(requestContext.config.url)
|
|
2179
|
+
)
|
|
2146
2180
|
);
|
|
2147
|
-
return error;
|
|
2148
2181
|
});
|
|
2149
2182
|
}
|
|
2150
2183
|
};
|
|
2151
2184
|
}
|
|
2185
|
+
var consoleLogger = {
|
|
2186
|
+
info(message, entry) {
|
|
2187
|
+
console.log(message, entry);
|
|
2188
|
+
},
|
|
2189
|
+
error(message, entry) {
|
|
2190
|
+
console.error(message, entry);
|
|
2191
|
+
}
|
|
2192
|
+
};
|
|
2193
|
+
function resolveLogger(options) {
|
|
2194
|
+
return options.logger ?? consoleLogger;
|
|
2195
|
+
}
|
|
2196
|
+
function emitInfo(options, entry) {
|
|
2197
|
+
try {
|
|
2198
|
+
void Promise.resolve(
|
|
2199
|
+
resolveLogger(options).info("[Npora Request]", entry)
|
|
2200
|
+
).catch(ignoreLoggerError);
|
|
2201
|
+
} catch {
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
function emitError(options, entry) {
|
|
2205
|
+
try {
|
|
2206
|
+
void Promise.resolve(
|
|
2207
|
+
resolveLogger(options).error("[Npora Request]", entry)
|
|
2208
|
+
).catch(ignoreLoggerError);
|
|
2209
|
+
} catch {
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
function ignoreLoggerError() {
|
|
2213
|
+
}
|
|
2152
2214
|
var SENSITIVE_QUERY_KEYS = /* @__PURE__ */ new Set([
|
|
2153
2215
|
"access_token",
|
|
2154
2216
|
"api-key",
|
|
@@ -2210,27 +2272,35 @@ function findAuthorityEnd(url, authorityStart) {
|
|
|
2210
2272
|
}
|
|
2211
2273
|
return url.length;
|
|
2212
2274
|
}
|
|
2213
|
-
function createErrorLog(error) {
|
|
2275
|
+
function createErrorLog(error, requestId, timestamp, duration, attempt, method, url) {
|
|
2276
|
+
const lifecycle = {
|
|
2277
|
+
type: "error",
|
|
2278
|
+
requestId,
|
|
2279
|
+
timestamp,
|
|
2280
|
+
duration,
|
|
2281
|
+
attempt,
|
|
2282
|
+
method,
|
|
2283
|
+
url
|
|
2284
|
+
};
|
|
2214
2285
|
if (error instanceof RequestError) {
|
|
2215
2286
|
return {
|
|
2216
|
-
|
|
2287
|
+
...lifecycle,
|
|
2217
2288
|
name: error.name,
|
|
2218
2289
|
message: error.message,
|
|
2219
2290
|
code: error.code,
|
|
2220
|
-
status: error.status
|
|
2221
|
-
method: error.config?.method ?? "GET",
|
|
2222
|
-
url: error.config ? redactURL(error.config.url) : void 0
|
|
2291
|
+
status: error.status
|
|
2223
2292
|
};
|
|
2224
2293
|
}
|
|
2225
2294
|
if (error instanceof Error) {
|
|
2226
2295
|
return {
|
|
2227
|
-
|
|
2296
|
+
...lifecycle,
|
|
2228
2297
|
name: error.name,
|
|
2229
2298
|
message: error.message
|
|
2230
2299
|
};
|
|
2231
2300
|
}
|
|
2232
2301
|
return {
|
|
2233
|
-
|
|
2302
|
+
...lifecycle,
|
|
2303
|
+
name: "UnknownError",
|
|
2234
2304
|
message: String(error)
|
|
2235
2305
|
};
|
|
2236
2306
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -40,6 +40,53 @@ interface AuthOptions {
|
|
|
40
40
|
}
|
|
41
41
|
interface LoggerOptions {
|
|
42
42
|
enabled?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Structured log destination.
|
|
45
|
+
*
|
|
46
|
+
* @default console
|
|
47
|
+
*/
|
|
48
|
+
logger?: RequestLogger;
|
|
49
|
+
/**
|
|
50
|
+
* Create the identifier shared by all lifecycle entries for a request.
|
|
51
|
+
*
|
|
52
|
+
* @default a plugin-local monotonic identifier
|
|
53
|
+
*/
|
|
54
|
+
createRequestId?: () => string;
|
|
55
|
+
}
|
|
56
|
+
interface RequestLogEntry {
|
|
57
|
+
type: 'request';
|
|
58
|
+
requestId: string;
|
|
59
|
+
timestamp: number;
|
|
60
|
+
method: HttpMethod;
|
|
61
|
+
url: string;
|
|
62
|
+
}
|
|
63
|
+
interface ResponseLogEntry {
|
|
64
|
+
type: 'response';
|
|
65
|
+
requestId: string;
|
|
66
|
+
timestamp: number;
|
|
67
|
+
duration: number;
|
|
68
|
+
attempts: number;
|
|
69
|
+
method: HttpMethod;
|
|
70
|
+
url: string;
|
|
71
|
+
status: number;
|
|
72
|
+
}
|
|
73
|
+
interface ErrorLogEntry {
|
|
74
|
+
type: 'error';
|
|
75
|
+
requestId: string;
|
|
76
|
+
timestamp: number;
|
|
77
|
+
duration: number;
|
|
78
|
+
attempt: number;
|
|
79
|
+
method: HttpMethod;
|
|
80
|
+
url: string;
|
|
81
|
+
name: string;
|
|
82
|
+
message: string;
|
|
83
|
+
code?: string;
|
|
84
|
+
status?: number;
|
|
85
|
+
}
|
|
86
|
+
type LoggerEntry = RequestLogEntry | ResponseLogEntry | ErrorLogEntry;
|
|
87
|
+
interface RequestLogger {
|
|
88
|
+
info(message: string, entry: RequestLogEntry | ResponseLogEntry): void | Promise<void>;
|
|
89
|
+
error(message: string, entry: ErrorLogEntry): void | Promise<void>;
|
|
43
90
|
}
|
|
44
91
|
type UploadData = FormData | Record<string, unknown>;
|
|
45
92
|
/**
|
|
@@ -236,6 +283,7 @@ declare class RequestContext<T = unknown> {
|
|
|
236
283
|
error?: unknown;
|
|
237
284
|
readonly startTime: number;
|
|
238
285
|
endTime?: number;
|
|
286
|
+
attempt: number;
|
|
239
287
|
constructor(config: RequestConfig);
|
|
240
288
|
}
|
|
241
289
|
|
|
@@ -487,4 +535,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
487
535
|
|
|
488
536
|
declare function uploadPlugin(): Plugin;
|
|
489
537
|
|
|
490
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
538
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,53 @@ interface AuthOptions {
|
|
|
40
40
|
}
|
|
41
41
|
interface LoggerOptions {
|
|
42
42
|
enabled?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Structured log destination.
|
|
45
|
+
*
|
|
46
|
+
* @default console
|
|
47
|
+
*/
|
|
48
|
+
logger?: RequestLogger;
|
|
49
|
+
/**
|
|
50
|
+
* Create the identifier shared by all lifecycle entries for a request.
|
|
51
|
+
*
|
|
52
|
+
* @default a plugin-local monotonic identifier
|
|
53
|
+
*/
|
|
54
|
+
createRequestId?: () => string;
|
|
55
|
+
}
|
|
56
|
+
interface RequestLogEntry {
|
|
57
|
+
type: 'request';
|
|
58
|
+
requestId: string;
|
|
59
|
+
timestamp: number;
|
|
60
|
+
method: HttpMethod;
|
|
61
|
+
url: string;
|
|
62
|
+
}
|
|
63
|
+
interface ResponseLogEntry {
|
|
64
|
+
type: 'response';
|
|
65
|
+
requestId: string;
|
|
66
|
+
timestamp: number;
|
|
67
|
+
duration: number;
|
|
68
|
+
attempts: number;
|
|
69
|
+
method: HttpMethod;
|
|
70
|
+
url: string;
|
|
71
|
+
status: number;
|
|
72
|
+
}
|
|
73
|
+
interface ErrorLogEntry {
|
|
74
|
+
type: 'error';
|
|
75
|
+
requestId: string;
|
|
76
|
+
timestamp: number;
|
|
77
|
+
duration: number;
|
|
78
|
+
attempt: number;
|
|
79
|
+
method: HttpMethod;
|
|
80
|
+
url: string;
|
|
81
|
+
name: string;
|
|
82
|
+
message: string;
|
|
83
|
+
code?: string;
|
|
84
|
+
status?: number;
|
|
85
|
+
}
|
|
86
|
+
type LoggerEntry = RequestLogEntry | ResponseLogEntry | ErrorLogEntry;
|
|
87
|
+
interface RequestLogger {
|
|
88
|
+
info(message: string, entry: RequestLogEntry | ResponseLogEntry): void | Promise<void>;
|
|
89
|
+
error(message: string, entry: ErrorLogEntry): void | Promise<void>;
|
|
43
90
|
}
|
|
44
91
|
type UploadData = FormData | Record<string, unknown>;
|
|
45
92
|
/**
|
|
@@ -236,6 +283,7 @@ declare class RequestContext<T = unknown> {
|
|
|
236
283
|
error?: unknown;
|
|
237
284
|
readonly startTime: number;
|
|
238
285
|
endTime?: number;
|
|
286
|
+
attempt: number;
|
|
239
287
|
constructor(config: RequestConfig);
|
|
240
288
|
}
|
|
241
289
|
|
|
@@ -487,4 +535,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
487
535
|
|
|
488
536
|
declare function uploadPlugin(): Plugin;
|
|
489
537
|
|
|
490
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
538
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.js
CHANGED
|
@@ -645,6 +645,7 @@ function setHeader(headers, key, value) {
|
|
|
645
645
|
// src/core/RequestContext.ts
|
|
646
646
|
var RequestContext = class {
|
|
647
647
|
constructor(config) {
|
|
648
|
+
this.attempt = 0;
|
|
648
649
|
this.config = config;
|
|
649
650
|
this.startTime = Date.now();
|
|
650
651
|
}
|
|
@@ -719,6 +720,7 @@ var Pipeline = class {
|
|
|
719
720
|
context.error = void 0;
|
|
720
721
|
context.response = void 0;
|
|
721
722
|
attempt += 1;
|
|
723
|
+
context.attempt = attempt;
|
|
722
724
|
try {
|
|
723
725
|
await waitForRetry(
|
|
724
726
|
decision.delay ?? 0,
|
|
@@ -2059,58 +2061,118 @@ function createProgress2(loaded, total) {
|
|
|
2059
2061
|
|
|
2060
2062
|
// src/plugins/loggerPlugin.ts
|
|
2061
2063
|
function loggerPlugin(defaultOptions = {}) {
|
|
2064
|
+
let nextRequestId = 0;
|
|
2065
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
2062
2066
|
return {
|
|
2063
2067
|
name: "logger",
|
|
2064
2068
|
install(context) {
|
|
2065
|
-
context.
|
|
2066
|
-
const
|
|
2067
|
-
config,
|
|
2069
|
+
context.hooks.onRequest((requestContext) => {
|
|
2070
|
+
const options = resolveExtensionConfig(
|
|
2071
|
+
requestContext.config,
|
|
2068
2072
|
"logger"
|
|
2069
2073
|
) ?? defaultOptions;
|
|
2070
|
-
if (
|
|
2071
|
-
return
|
|
2074
|
+
if (options.enabled === false) {
|
|
2075
|
+
return;
|
|
2072
2076
|
}
|
|
2073
|
-
|
|
2077
|
+
const state = {
|
|
2078
|
+
requestId: options.createRequestId?.() ?? `request-${++nextRequestId}`,
|
|
2079
|
+
options
|
|
2080
|
+
};
|
|
2081
|
+
states.set(requestContext, state);
|
|
2082
|
+
emitInfo(options, {
|
|
2074
2083
|
type: "request",
|
|
2075
|
-
|
|
2076
|
-
|
|
2084
|
+
requestId: state.requestId,
|
|
2085
|
+
timestamp: requestContext.startTime,
|
|
2086
|
+
method: requestContext.config.method ?? "GET",
|
|
2087
|
+
url: redactURL(requestContext.config.url)
|
|
2077
2088
|
});
|
|
2078
|
-
return config;
|
|
2079
2089
|
});
|
|
2080
|
-
context.
|
|
2081
|
-
const
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
) ?? defaultOptions;
|
|
2085
|
-
if (logger.enabled === false) {
|
|
2086
|
-
return response;
|
|
2090
|
+
context.hooks.onResponse((requestContext) => {
|
|
2091
|
+
const state = states.get(requestContext);
|
|
2092
|
+
if (!state || !requestContext.response) {
|
|
2093
|
+
return;
|
|
2087
2094
|
}
|
|
2088
|
-
|
|
2095
|
+
const timestamp = Date.now();
|
|
2096
|
+
emitInfo(state.options, {
|
|
2089
2097
|
type: "response",
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2098
|
+
requestId: state.requestId,
|
|
2099
|
+
timestamp,
|
|
2100
|
+
duration: Math.max(0, timestamp - requestContext.startTime),
|
|
2101
|
+
attempts: requestContext.attempt + 1,
|
|
2102
|
+
method: requestContext.config.method ?? "GET",
|
|
2103
|
+
url: redactURL(requestContext.config.url),
|
|
2104
|
+
status: requestContext.response.status
|
|
2093
2105
|
});
|
|
2094
|
-
return response;
|
|
2095
2106
|
});
|
|
2096
|
-
context.
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2107
|
+
context.hooks.onError((requestContext) => {
|
|
2108
|
+
let state = states.get(requestContext);
|
|
2109
|
+
if (!state) {
|
|
2110
|
+
const options = resolveExtensionConfig(
|
|
2111
|
+
requestContext.config,
|
|
2112
|
+
"logger"
|
|
2113
|
+
) ?? defaultOptions;
|
|
2114
|
+
if (options.enabled === false) {
|
|
2115
|
+
return;
|
|
2116
|
+
}
|
|
2117
|
+
state = {
|
|
2118
|
+
requestId: options.createRequestId?.() ?? `request-${++nextRequestId}`,
|
|
2119
|
+
options
|
|
2120
|
+
};
|
|
2121
|
+
states.set(requestContext, state);
|
|
2122
|
+
emitInfo(options, {
|
|
2123
|
+
type: "request",
|
|
2124
|
+
requestId: state.requestId,
|
|
2125
|
+
timestamp: requestContext.startTime,
|
|
2126
|
+
method: requestContext.config.method ?? "GET",
|
|
2127
|
+
url: redactURL(requestContext.config.url)
|
|
2128
|
+
});
|
|
2104
2129
|
}
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2130
|
+
const timestamp = Date.now();
|
|
2131
|
+
emitError(
|
|
2132
|
+
state.options,
|
|
2133
|
+
createErrorLog(
|
|
2134
|
+
requestContext.error,
|
|
2135
|
+
state.requestId,
|
|
2136
|
+
timestamp,
|
|
2137
|
+
Math.max(0, timestamp - requestContext.startTime),
|
|
2138
|
+
requestContext.attempt + 1,
|
|
2139
|
+
requestContext.config.method ?? "GET",
|
|
2140
|
+
redactURL(requestContext.config.url)
|
|
2141
|
+
)
|
|
2108
2142
|
);
|
|
2109
|
-
return error;
|
|
2110
2143
|
});
|
|
2111
2144
|
}
|
|
2112
2145
|
};
|
|
2113
2146
|
}
|
|
2147
|
+
var consoleLogger = {
|
|
2148
|
+
info(message, entry) {
|
|
2149
|
+
console.log(message, entry);
|
|
2150
|
+
},
|
|
2151
|
+
error(message, entry) {
|
|
2152
|
+
console.error(message, entry);
|
|
2153
|
+
}
|
|
2154
|
+
};
|
|
2155
|
+
function resolveLogger(options) {
|
|
2156
|
+
return options.logger ?? consoleLogger;
|
|
2157
|
+
}
|
|
2158
|
+
function emitInfo(options, entry) {
|
|
2159
|
+
try {
|
|
2160
|
+
void Promise.resolve(
|
|
2161
|
+
resolveLogger(options).info("[Npora Request]", entry)
|
|
2162
|
+
).catch(ignoreLoggerError);
|
|
2163
|
+
} catch {
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
function emitError(options, entry) {
|
|
2167
|
+
try {
|
|
2168
|
+
void Promise.resolve(
|
|
2169
|
+
resolveLogger(options).error("[Npora Request]", entry)
|
|
2170
|
+
).catch(ignoreLoggerError);
|
|
2171
|
+
} catch {
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
function ignoreLoggerError() {
|
|
2175
|
+
}
|
|
2114
2176
|
var SENSITIVE_QUERY_KEYS = /* @__PURE__ */ new Set([
|
|
2115
2177
|
"access_token",
|
|
2116
2178
|
"api-key",
|
|
@@ -2172,27 +2234,35 @@ function findAuthorityEnd(url, authorityStart) {
|
|
|
2172
2234
|
}
|
|
2173
2235
|
return url.length;
|
|
2174
2236
|
}
|
|
2175
|
-
function createErrorLog(error) {
|
|
2237
|
+
function createErrorLog(error, requestId, timestamp, duration, attempt, method, url) {
|
|
2238
|
+
const lifecycle = {
|
|
2239
|
+
type: "error",
|
|
2240
|
+
requestId,
|
|
2241
|
+
timestamp,
|
|
2242
|
+
duration,
|
|
2243
|
+
attempt,
|
|
2244
|
+
method,
|
|
2245
|
+
url
|
|
2246
|
+
};
|
|
2176
2247
|
if (error instanceof RequestError) {
|
|
2177
2248
|
return {
|
|
2178
|
-
|
|
2249
|
+
...lifecycle,
|
|
2179
2250
|
name: error.name,
|
|
2180
2251
|
message: error.message,
|
|
2181
2252
|
code: error.code,
|
|
2182
|
-
status: error.status
|
|
2183
|
-
method: error.config?.method ?? "GET",
|
|
2184
|
-
url: error.config ? redactURL(error.config.url) : void 0
|
|
2253
|
+
status: error.status
|
|
2185
2254
|
};
|
|
2186
2255
|
}
|
|
2187
2256
|
if (error instanceof Error) {
|
|
2188
2257
|
return {
|
|
2189
|
-
|
|
2258
|
+
...lifecycle,
|
|
2190
2259
|
name: error.name,
|
|
2191
2260
|
message: error.message
|
|
2192
2261
|
};
|
|
2193
2262
|
}
|
|
2194
2263
|
return {
|
|
2195
|
-
|
|
2264
|
+
...lifecycle,
|
|
2265
|
+
name: "UnknownError",
|
|
2196
2266
|
message: String(error)
|
|
2197
2267
|
};
|
|
2198
2268
|
}
|