dce-reactkit 3.2.0-beta.3 → 3.2.0-beta.5
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/.vscode/settings.json +5 -0
- package/dist/cjs/index.js +160 -76
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.d.ts +2 -1
- package/dist/cjs/types/types/LogBuiltInCategory.d.ts +10 -0
- package/dist/esm/index.js +160 -77
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.d.ts +2 -1
- package/dist/esm/types/types/LogBuiltInCategory.d.ts +10 -0
- package/dist/index.d.ts +11 -1
- package/package.json +1 -1
- package/src/helpers/genRouteHandler.ts +187 -99
- package/src/helpers/logClientEvent.tsx +9 -2
- package/src/index.ts +2 -0
- package/src/types/LogBuiltInCategory.ts +11 -0
package/dist/cjs/index.js
CHANGED
|
@@ -2581,6 +2581,18 @@ var LogSource;
|
|
|
2581
2581
|
})(LogSource || (LogSource = {}));
|
|
2582
2582
|
var LogSource$1 = LogSource;
|
|
2583
2583
|
|
|
2584
|
+
/**
|
|
2585
|
+
* Built-in categories for logs
|
|
2586
|
+
* @author Gabe Abrams
|
|
2587
|
+
*/
|
|
2588
|
+
var LogBuiltInCategory;
|
|
2589
|
+
(function (LogBuiltInCategory) {
|
|
2590
|
+
LogBuiltInCategory["Uncategorized"] = "n/a";
|
|
2591
|
+
LogBuiltInCategory["ServerRenderedErrorPage"] = "_server-rendered-error-page";
|
|
2592
|
+
LogBuiltInCategory["ServerEndpointError"] = "_server-endpoint-error";
|
|
2593
|
+
})(LogBuiltInCategory || (LogBuiltInCategory = {}));
|
|
2594
|
+
var LogBuiltInCategory$1 = LogBuiltInCategory;
|
|
2595
|
+
|
|
2584
2596
|
/**
|
|
2585
2597
|
* Generate an express API route handler
|
|
2586
2598
|
* @author Gabe Abrams
|
|
@@ -2905,86 +2917,136 @@ const genRouteHandler = (opts) => {
|
|
|
2905
2917
|
* @author Gabe Abrams
|
|
2906
2918
|
*/
|
|
2907
2919
|
const logServerEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
2920
|
+
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
2908
2921
|
// NOTE: internally, we slip through an opts.overrideAsClientEvent boolean
|
|
2909
2922
|
// that indicates that this is actually a client event, but we don't
|
|
2910
2923
|
// include that in the LogFunction type because this is internal and
|
|
2911
2924
|
// hidden from users
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2925
|
+
try {
|
|
2926
|
+
// Parse user agent
|
|
2927
|
+
const { browser, device, } = parseUserAgent(req.headers['user-agent']);
|
|
2928
|
+
// Get time info in ET
|
|
2929
|
+
const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
|
|
2930
|
+
// Main log info
|
|
2931
|
+
const mainLogInfo = {
|
|
2932
|
+
id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
|
|
2933
|
+
userFirstName: launchInfo.userFirstName,
|
|
2934
|
+
userLastName: launchInfo.userLastName,
|
|
2935
|
+
userEmail: launchInfo.userEmail,
|
|
2936
|
+
userId: launchInfo.userId,
|
|
2937
|
+
isLearner: !!launchInfo.isLearner,
|
|
2938
|
+
isAdmin: !!launchInfo.isAdmin,
|
|
2939
|
+
isTTM: !!launchInfo.isTTM,
|
|
2940
|
+
courseId: launchInfo.courseId,
|
|
2941
|
+
courseName: launchInfo.courseName,
|
|
2942
|
+
browser,
|
|
2943
|
+
device,
|
|
2944
|
+
year,
|
|
2945
|
+
month,
|
|
2946
|
+
day,
|
|
2947
|
+
hour,
|
|
2948
|
+
minute,
|
|
2949
|
+
timestamp,
|
|
2950
|
+
category: (typeof opts.category === 'string'
|
|
2951
|
+
? opts.category
|
|
2952
|
+
: ((_d = ((_c = opts.category) !== null && _c !== void 0 ? _c : {})._) !== null && _d !== void 0 ? _d : LogBuiltInCategory$1.Uncategorized)),
|
|
2953
|
+
subcategory: (typeof opts.category === 'string'
|
|
2954
|
+
? opts.subcategory
|
|
2955
|
+
: ((_f = ((_e = opts.subcategory) !== null && _e !== void 0 ? _e : {})._) !== null && _f !== void 0 ? _f : LogBuiltInCategory$1.Uncategorized)),
|
|
2956
|
+
tags: (_g = opts.tags) !== null && _g !== void 0 ? _g : [],
|
|
2957
|
+
metadata: (_h = opts.metadata) !== null && _h !== void 0 ? _h : {},
|
|
2958
|
+
};
|
|
2959
|
+
// Type-specific info
|
|
2960
|
+
const typeSpecificInfo = (('error' in opts && opts.error)
|
|
2961
|
+
? {
|
|
2962
|
+
type: LogType$1.Error,
|
|
2963
|
+
errorMessage: (_j = opts.error.message) !== null && _j !== void 0 ? _j : 'Unknown message',
|
|
2964
|
+
errorCode: (_k = opts.error.code) !== null && _k !== void 0 ? _k : ReactKitErrorCode$1.NoCode,
|
|
2965
|
+
errorStack: (_l = opts.error.stack) !== null && _l !== void 0 ? _l : 'No stack',
|
|
2966
|
+
}
|
|
2967
|
+
: {
|
|
2968
|
+
type: LogType$1.Action,
|
|
2969
|
+
target: (_m = opts.target) !== null && _m !== void 0 ? _m : 'Unknown',
|
|
2970
|
+
action: (_o = opts.action) !== null && _o !== void 0 ? _o : 'Unknown'
|
|
2971
|
+
});
|
|
2972
|
+
// Source-specific info
|
|
2973
|
+
const sourceSpecificInfo = (opts.overrideAsClientEvent
|
|
2974
|
+
? {
|
|
2975
|
+
source: LogSource$1.Client,
|
|
2976
|
+
}
|
|
2977
|
+
: {
|
|
2978
|
+
source: LogSource$1.Server,
|
|
2979
|
+
routePath: req.path,
|
|
2980
|
+
routeTemplate: req.route.path,
|
|
2981
|
+
});
|
|
2982
|
+
// Build log event
|
|
2983
|
+
const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
|
|
2984
|
+
// Either print to console or save to db
|
|
2985
|
+
const logCollection = internalGetLogCollection();
|
|
2986
|
+
if (logCollection) {
|
|
2987
|
+
// Store to the log collection
|
|
2988
|
+
yield logCollection.insert(log);
|
|
2953
2989
|
}
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
source: LogSource$1.Client,
|
|
2990
|
+
else {
|
|
2991
|
+
// Print to console
|
|
2992
|
+
if (log.type === LogType$1.Error) {
|
|
2993
|
+
console.error('dce-reactkit error log:', log);
|
|
2994
|
+
}
|
|
2995
|
+
else {
|
|
2996
|
+
console.log('dce-reactkit action log:', log);
|
|
2997
|
+
}
|
|
2963
2998
|
}
|
|
2964
|
-
|
|
2999
|
+
// Return log entry
|
|
3000
|
+
return log;
|
|
3001
|
+
}
|
|
3002
|
+
catch (err) {
|
|
3003
|
+
// Print because we cannot store the error
|
|
3004
|
+
console.error('Could not log the following:', opts);
|
|
3005
|
+
// Create a dummy log to return
|
|
3006
|
+
const dummyMainInfo = {
|
|
3007
|
+
id: '-1',
|
|
3008
|
+
userFirstName: 'Unknown',
|
|
3009
|
+
userLastName: 'Unknown',
|
|
3010
|
+
userEmail: 'unknown@harvard.edu',
|
|
3011
|
+
userId: 1,
|
|
3012
|
+
isLearner: false,
|
|
3013
|
+
isAdmin: false,
|
|
3014
|
+
isTTM: false,
|
|
3015
|
+
courseId: 1,
|
|
3016
|
+
courseName: 'Unknown',
|
|
3017
|
+
browser: {
|
|
3018
|
+
name: 'Unknown',
|
|
3019
|
+
version: 'Unknown',
|
|
3020
|
+
},
|
|
3021
|
+
device: {
|
|
3022
|
+
isMobile: false,
|
|
3023
|
+
os: 'Unknown',
|
|
3024
|
+
},
|
|
3025
|
+
year: 1,
|
|
3026
|
+
month: 1,
|
|
3027
|
+
day: 1,
|
|
3028
|
+
hour: 1,
|
|
3029
|
+
minute: 1,
|
|
3030
|
+
timestamp: Date.now(),
|
|
3031
|
+
tags: [],
|
|
3032
|
+
metadata: {},
|
|
3033
|
+
category: LogBuiltInCategory$1.Uncategorized,
|
|
3034
|
+
subcategory: LogBuiltInCategory$1.Uncategorized,
|
|
3035
|
+
};
|
|
3036
|
+
const dummyTypeSpecificInfo = {
|
|
3037
|
+
type: LogType$1.Error,
|
|
3038
|
+
errorMessage: 'Unknown',
|
|
3039
|
+
errorCode: 'Unknown',
|
|
3040
|
+
errorStack: 'No Stack',
|
|
3041
|
+
};
|
|
3042
|
+
const dummySourceSpecificInfo = {
|
|
2965
3043
|
source: LogSource$1.Server,
|
|
2966
3044
|
routePath: req.path,
|
|
2967
3045
|
routeTemplate: req.route.path,
|
|
2968
|
-
}
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
// Either print to console or save to db
|
|
2972
|
-
const logCollection = internalGetLogCollection();
|
|
2973
|
-
if (logCollection) {
|
|
2974
|
-
// Store to the log collection
|
|
2975
|
-
yield logCollection.insert(log);
|
|
2976
|
-
}
|
|
2977
|
-
else {
|
|
2978
|
-
// Print to console
|
|
2979
|
-
if (log.type === LogType$1.Error) {
|
|
2980
|
-
console.error('dce-reactkit error log:', log);
|
|
2981
|
-
}
|
|
2982
|
-
else {
|
|
2983
|
-
console.log('dce-reactkit action log:', log);
|
|
2984
|
-
}
|
|
3046
|
+
};
|
|
3047
|
+
const log = Object.assign(Object.assign(Object.assign({}, dummyMainInfo), dummyTypeSpecificInfo), dummySourceSpecificInfo);
|
|
3048
|
+
return log;
|
|
2985
3049
|
}
|
|
2986
|
-
// Return log entry
|
|
2987
|
-
return log;
|
|
2988
3050
|
});
|
|
2989
3051
|
/*------------------------------------------------------------------------*/
|
|
2990
3052
|
/* Call handler */
|
|
@@ -3023,9 +3085,24 @@ const genRouteHandler = (opts) => {
|
|
|
3023
3085
|
* @param [opts.status=500] http status code
|
|
3024
3086
|
*/
|
|
3025
3087
|
const renderErrorPage = (opts = {}) => {
|
|
3026
|
-
var _a;
|
|
3088
|
+
var _a, _b;
|
|
3027
3089
|
const html = genErrorPage(opts);
|
|
3028
3090
|
send(html, (_a = opts.status) !== null && _a !== void 0 ? _a : 500);
|
|
3091
|
+
// Log
|
|
3092
|
+
logServerEvent({
|
|
3093
|
+
category: LogBuiltInCategory$1.ServerRenderedErrorPage,
|
|
3094
|
+
error: {
|
|
3095
|
+
message: `${opts.title}: ${opts.description}`,
|
|
3096
|
+
code: opts.code,
|
|
3097
|
+
},
|
|
3098
|
+
metadata: {
|
|
3099
|
+
title: opts.title,
|
|
3100
|
+
description: opts.description,
|
|
3101
|
+
code: opts.code,
|
|
3102
|
+
pageTitle: opts.pageTitle,
|
|
3103
|
+
status: (_b = opts.status) !== null && _b !== void 0 ? _b : 500,
|
|
3104
|
+
},
|
|
3105
|
+
});
|
|
3029
3106
|
};
|
|
3030
3107
|
// Call the handler
|
|
3031
3108
|
try {
|
|
@@ -3049,7 +3126,13 @@ const genRouteHandler = (opts) => {
|
|
|
3049
3126
|
catch (err) {
|
|
3050
3127
|
// Send error to client (only if next wasn't called)
|
|
3051
3128
|
if (!responseSent) {
|
|
3052
|
-
|
|
3129
|
+
handleError(res, err);
|
|
3130
|
+
// Log server-side error
|
|
3131
|
+
logServerEvent({
|
|
3132
|
+
category: LogBuiltInCategory$1.ServerEndpointError,
|
|
3133
|
+
error: err,
|
|
3134
|
+
});
|
|
3135
|
+
return;
|
|
3053
3136
|
}
|
|
3054
3137
|
// Log error that was not responded with
|
|
3055
3138
|
console.log('Error occurred but could not be sent to client because a response was already sent:', err);
|
|
@@ -3248,19 +3331,19 @@ const parallelLimit = (taskFunctions, limit) => __awaiter(void 0, void 0, void 0
|
|
|
3248
3331
|
* @author Gabe Abrams
|
|
3249
3332
|
*/
|
|
3250
3333
|
const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3251
|
-
var _a, _b;
|
|
3334
|
+
var _a, _b, _c, _d, _e;
|
|
3252
3335
|
return visitServerEndpoint({
|
|
3253
3336
|
path: LOG_ROUTE_PATH,
|
|
3254
3337
|
method: 'POST',
|
|
3255
3338
|
params: {
|
|
3256
3339
|
category: (typeof opts.category === 'string'
|
|
3257
3340
|
? opts.category
|
|
3258
|
-
: opts.category.
|
|
3341
|
+
: ((_b = ((_a = opts.category) !== null && _a !== void 0 ? _a : {})._) !== null && _b !== void 0 ? _b : LogBuiltInCategory$1.Uncategorized)),
|
|
3259
3342
|
subcategory: (typeof opts.category === 'string'
|
|
3260
3343
|
? opts.subcategory
|
|
3261
|
-
: ((
|
|
3344
|
+
: ((_d = ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : {})._) !== null && _d !== void 0 ? _d : LogBuiltInCategory$1.Uncategorized)),
|
|
3262
3345
|
tags: JSON.stringify(opts.tags),
|
|
3263
|
-
metadata: JSON.stringify((
|
|
3346
|
+
metadata: JSON.stringify((_e = opts.metadata) !== null && _e !== void 0 ? _e : {}),
|
|
3264
3347
|
errorMessage: (opts.error
|
|
3265
3348
|
? opts.error.message
|
|
3266
3349
|
: undefined),
|
|
@@ -3366,6 +3449,7 @@ exports.HOUR_IN_MS = HOUR_IN_MS;
|
|
|
3366
3449
|
exports.ItemPicker = ItemPicker;
|
|
3367
3450
|
exports.LoadingSpinner = LoadingSpinner;
|
|
3368
3451
|
exports.LogAction = LogAction$1;
|
|
3452
|
+
exports.LogBuiltInCategory = LogBuiltInCategory$1;
|
|
3369
3453
|
exports.LogSource = LogSource$1;
|
|
3370
3454
|
exports.LogType = LogType$1;
|
|
3371
3455
|
exports.MINUTE_IN_MS = MINUTE_IN_MS;
|