widelogger 0.4.0 → 0.5.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/dist/index.d.ts +8 -2
- package/dist/index.js +82 -8
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -11,12 +11,18 @@ export interface WideloggerOptions {
|
|
|
11
11
|
export interface ErrorFieldsOptions {
|
|
12
12
|
prefix?: string;
|
|
13
13
|
includeStack?: boolean;
|
|
14
|
+
slug?: string;
|
|
15
|
+
retriable?: boolean;
|
|
16
|
+
requiresReauth?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface StickyHandle {
|
|
19
|
+
sticky: () => void;
|
|
14
20
|
}
|
|
15
21
|
declare function measure<K extends string, T>(key: DottedKey<K>, callback: () => Promise<T>): Promise<T>;
|
|
16
22
|
declare function measure<K extends string, T>(key: DottedKey<K>, callback: () => T): T;
|
|
17
23
|
export declare const widelog: {
|
|
18
|
-
set: <K extends string>(key: DottedKey<K>, value: FieldValue) =>
|
|
19
|
-
setFields: (fields: Record<string, unknown>) =>
|
|
24
|
+
set: <K extends string>(key: DottedKey<K>, value: FieldValue) => StickyHandle;
|
|
25
|
+
setFields: (fields: Record<string, unknown>) => StickyHandle;
|
|
20
26
|
count: <K extends string>(key: DottedKey<K>, amount?: number) => void;
|
|
21
27
|
append: <K extends string>(key: DottedKey<K>, value: FieldValue) => void;
|
|
22
28
|
max: <K extends string>(key: DottedKey<K>, value: number) => void;
|
package/dist/index.js
CHANGED
|
@@ -105,7 +105,15 @@ var flush = (context) => {
|
|
|
105
105
|
if (!context) {
|
|
106
106
|
return {};
|
|
107
107
|
}
|
|
108
|
+
const hasStickyOperations = context.stickyOperations.length > 0;
|
|
109
|
+
const hasOperations = context.operations.length > 0;
|
|
110
|
+
if (!(hasStickyOperations || hasOperations)) {
|
|
111
|
+
return {};
|
|
112
|
+
}
|
|
108
113
|
const agg = createAggregators();
|
|
114
|
+
for (const entry of context.stickyOperations) {
|
|
115
|
+
processOperation(agg, entry);
|
|
116
|
+
}
|
|
109
117
|
for (const entry of context.operations) {
|
|
110
118
|
processOperation(agg, entry);
|
|
111
119
|
}
|
|
@@ -117,6 +125,7 @@ var flush = (context) => {
|
|
|
117
125
|
// src/index.ts
|
|
118
126
|
var isFieldValue = (value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
119
127
|
var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
128
|
+
var isFieldValueArray = (value) => Array.isArray(value) && value.every(isFieldValue);
|
|
120
129
|
function getErrorFields(error, includeStack = true) {
|
|
121
130
|
if (error instanceof Error) {
|
|
122
131
|
return {
|
|
@@ -136,9 +145,32 @@ function getErrorFields(error, includeStack = true) {
|
|
|
136
145
|
error_message: "Unknown error"
|
|
137
146
|
};
|
|
138
147
|
}
|
|
148
|
+
var extractErrorProperty = (error, property) => {
|
|
149
|
+
if (isRecord2(error) && property in error) {
|
|
150
|
+
return error[property];
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
};
|
|
154
|
+
var noop = () => {
|
|
155
|
+
return;
|
|
156
|
+
};
|
|
157
|
+
var noopSticky = { sticky: noop };
|
|
139
158
|
var storage = new AsyncLocalStorage;
|
|
140
159
|
function pushOp(operation) {
|
|
141
|
-
storage.getStore()
|
|
160
|
+
const store = storage.getStore();
|
|
161
|
+
if (!store) {
|
|
162
|
+
return noopSticky;
|
|
163
|
+
}
|
|
164
|
+
store.operations.push(operation);
|
|
165
|
+
return {
|
|
166
|
+
sticky: () => {
|
|
167
|
+
const index = store.operations.indexOf(operation);
|
|
168
|
+
if (index !== -1) {
|
|
169
|
+
store.operations.splice(index, 1);
|
|
170
|
+
}
|
|
171
|
+
store.stickyOperations.push(operation);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
142
174
|
}
|
|
143
175
|
function applyFields(operations, fields, parentKey) {
|
|
144
176
|
for (const key of Object.keys(fields)) {
|
|
@@ -148,6 +180,12 @@ function applyFields(operations, fields, parentKey) {
|
|
|
148
180
|
operations.push({ operation: "set", key: fullKey, value });
|
|
149
181
|
continue;
|
|
150
182
|
}
|
|
183
|
+
if (isFieldValueArray(value)) {
|
|
184
|
+
for (const element of value) {
|
|
185
|
+
operations.push({ operation: "append", key: fullKey, value: element });
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
151
189
|
if (isRecord2(value)) {
|
|
152
190
|
applyFields(operations, value, fullKey);
|
|
153
191
|
}
|
|
@@ -177,13 +215,21 @@ function measure(key, callback) {
|
|
|
177
215
|
}
|
|
178
216
|
var widelog = {
|
|
179
217
|
set: (key, value) => {
|
|
180
|
-
pushOp({ operation: "set", key, value });
|
|
218
|
+
return pushOp({ operation: "set", key, value });
|
|
181
219
|
},
|
|
182
220
|
setFields: (fields) => {
|
|
183
|
-
const
|
|
184
|
-
if (
|
|
185
|
-
|
|
221
|
+
const store = storage.getStore();
|
|
222
|
+
if (!store) {
|
|
223
|
+
return noopSticky;
|
|
186
224
|
}
|
|
225
|
+
const startIndex = store.operations.length;
|
|
226
|
+
applyFields(store.operations, fields);
|
|
227
|
+
return {
|
|
228
|
+
sticky: () => {
|
|
229
|
+
const added = store.operations.splice(startIndex);
|
|
230
|
+
store.stickyOperations.push(...added);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
187
233
|
},
|
|
188
234
|
count: (key, amount = 1) => {
|
|
189
235
|
pushOp({ operation: "count", key, amount });
|
|
@@ -229,10 +275,37 @@ var widelog = {
|
|
|
229
275
|
value: fields.error_stack
|
|
230
276
|
});
|
|
231
277
|
}
|
|
278
|
+
const slug = options.slug ?? extractErrorProperty(error, "slug");
|
|
279
|
+
if (typeof slug === "string") {
|
|
280
|
+
context.operations.push({
|
|
281
|
+
operation: "set",
|
|
282
|
+
key: `${prefix}.slug`,
|
|
283
|
+
value: slug
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
const retriable = options.retriable ?? extractErrorProperty(error, "retriable");
|
|
287
|
+
if (typeof retriable === "boolean") {
|
|
288
|
+
context.operations.push({
|
|
289
|
+
operation: "set",
|
|
290
|
+
key: `${prefix}.retriable`,
|
|
291
|
+
value: retriable
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
const requiresReauth = options.requiresReauth ?? extractErrorProperty(error, "requiresReauth");
|
|
295
|
+
if (typeof requiresReauth === "boolean") {
|
|
296
|
+
context.operations.push({
|
|
297
|
+
operation: "set",
|
|
298
|
+
key: `${prefix}.requires_reauth`,
|
|
299
|
+
value: requiresReauth
|
|
300
|
+
});
|
|
301
|
+
}
|
|
232
302
|
},
|
|
233
303
|
flush: () => {
|
|
234
304
|
const store = storage.getStore();
|
|
235
|
-
if (!store
|
|
305
|
+
if (!store) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (store.operations.length === 0 && store.stickyOperations.length === 0) {
|
|
236
309
|
return;
|
|
237
310
|
}
|
|
238
311
|
const event = flush(store);
|
|
@@ -276,12 +349,13 @@ var widelogger = (options) => {
|
|
|
276
349
|
};
|
|
277
350
|
const clearContext = () => {
|
|
278
351
|
const context2 = storage.getStore();
|
|
279
|
-
if (context2
|
|
352
|
+
if (context2) {
|
|
280
353
|
context2.operations = [];
|
|
354
|
+
context2.stickyOperations = [];
|
|
281
355
|
}
|
|
282
356
|
};
|
|
283
357
|
function context(callback) {
|
|
284
|
-
return storage.run({ operations: [], transport }, () => {
|
|
358
|
+
return storage.run({ operations: [], stickyOperations: [], transport }, () => {
|
|
285
359
|
let result;
|
|
286
360
|
try {
|
|
287
361
|
result = callback();
|
package/dist/types.d.ts
CHANGED