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/esm/index.js
CHANGED
|
@@ -2573,6 +2573,18 @@ var LogSource;
|
|
|
2573
2573
|
})(LogSource || (LogSource = {}));
|
|
2574
2574
|
var LogSource$1 = LogSource;
|
|
2575
2575
|
|
|
2576
|
+
/**
|
|
2577
|
+
* Built-in categories for logs
|
|
2578
|
+
* @author Gabe Abrams
|
|
2579
|
+
*/
|
|
2580
|
+
var LogBuiltInCategory;
|
|
2581
|
+
(function (LogBuiltInCategory) {
|
|
2582
|
+
LogBuiltInCategory["Uncategorized"] = "n/a";
|
|
2583
|
+
LogBuiltInCategory["ServerRenderedErrorPage"] = "_server-rendered-error-page";
|
|
2584
|
+
LogBuiltInCategory["ServerEndpointError"] = "_server-endpoint-error";
|
|
2585
|
+
})(LogBuiltInCategory || (LogBuiltInCategory = {}));
|
|
2586
|
+
var LogBuiltInCategory$1 = LogBuiltInCategory;
|
|
2587
|
+
|
|
2576
2588
|
/**
|
|
2577
2589
|
* Generate an express API route handler
|
|
2578
2590
|
* @author Gabe Abrams
|
|
@@ -2897,86 +2909,136 @@ const genRouteHandler = (opts) => {
|
|
|
2897
2909
|
* @author Gabe Abrams
|
|
2898
2910
|
*/
|
|
2899
2911
|
const logServerEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
2912
|
+
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
2900
2913
|
// NOTE: internally, we slip through an opts.overrideAsClientEvent boolean
|
|
2901
2914
|
// that indicates that this is actually a client event, but we don't
|
|
2902
2915
|
// include that in the LogFunction type because this is internal and
|
|
2903
2916
|
// hidden from users
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
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
|
-
|
|
2917
|
+
try {
|
|
2918
|
+
// Parse user agent
|
|
2919
|
+
const { browser, device, } = parseUserAgent(req.headers['user-agent']);
|
|
2920
|
+
// Get time info in ET
|
|
2921
|
+
const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
|
|
2922
|
+
// Main log info
|
|
2923
|
+
const mainLogInfo = {
|
|
2924
|
+
id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
|
|
2925
|
+
userFirstName: launchInfo.userFirstName,
|
|
2926
|
+
userLastName: launchInfo.userLastName,
|
|
2927
|
+
userEmail: launchInfo.userEmail,
|
|
2928
|
+
userId: launchInfo.userId,
|
|
2929
|
+
isLearner: !!launchInfo.isLearner,
|
|
2930
|
+
isAdmin: !!launchInfo.isAdmin,
|
|
2931
|
+
isTTM: !!launchInfo.isTTM,
|
|
2932
|
+
courseId: launchInfo.courseId,
|
|
2933
|
+
courseName: launchInfo.courseName,
|
|
2934
|
+
browser,
|
|
2935
|
+
device,
|
|
2936
|
+
year,
|
|
2937
|
+
month,
|
|
2938
|
+
day,
|
|
2939
|
+
hour,
|
|
2940
|
+
minute,
|
|
2941
|
+
timestamp,
|
|
2942
|
+
category: (typeof opts.category === 'string'
|
|
2943
|
+
? opts.category
|
|
2944
|
+
: ((_d = ((_c = opts.category) !== null && _c !== void 0 ? _c : {})._) !== null && _d !== void 0 ? _d : LogBuiltInCategory$1.Uncategorized)),
|
|
2945
|
+
subcategory: (typeof opts.category === 'string'
|
|
2946
|
+
? opts.subcategory
|
|
2947
|
+
: ((_f = ((_e = opts.subcategory) !== null && _e !== void 0 ? _e : {})._) !== null && _f !== void 0 ? _f : LogBuiltInCategory$1.Uncategorized)),
|
|
2948
|
+
tags: (_g = opts.tags) !== null && _g !== void 0 ? _g : [],
|
|
2949
|
+
metadata: (_h = opts.metadata) !== null && _h !== void 0 ? _h : {},
|
|
2950
|
+
};
|
|
2951
|
+
// Type-specific info
|
|
2952
|
+
const typeSpecificInfo = (('error' in opts && opts.error)
|
|
2953
|
+
? {
|
|
2954
|
+
type: LogType$1.Error,
|
|
2955
|
+
errorMessage: (_j = opts.error.message) !== null && _j !== void 0 ? _j : 'Unknown message',
|
|
2956
|
+
errorCode: (_k = opts.error.code) !== null && _k !== void 0 ? _k : ReactKitErrorCode$1.NoCode,
|
|
2957
|
+
errorStack: (_l = opts.error.stack) !== null && _l !== void 0 ? _l : 'No stack',
|
|
2958
|
+
}
|
|
2959
|
+
: {
|
|
2960
|
+
type: LogType$1.Action,
|
|
2961
|
+
target: (_m = opts.target) !== null && _m !== void 0 ? _m : 'Unknown',
|
|
2962
|
+
action: (_o = opts.action) !== null && _o !== void 0 ? _o : 'Unknown'
|
|
2963
|
+
});
|
|
2964
|
+
// Source-specific info
|
|
2965
|
+
const sourceSpecificInfo = (opts.overrideAsClientEvent
|
|
2966
|
+
? {
|
|
2967
|
+
source: LogSource$1.Client,
|
|
2968
|
+
}
|
|
2969
|
+
: {
|
|
2970
|
+
source: LogSource$1.Server,
|
|
2971
|
+
routePath: req.path,
|
|
2972
|
+
routeTemplate: req.route.path,
|
|
2973
|
+
});
|
|
2974
|
+
// Build log event
|
|
2975
|
+
const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
|
|
2976
|
+
// Either print to console or save to db
|
|
2977
|
+
const logCollection = internalGetLogCollection();
|
|
2978
|
+
if (logCollection) {
|
|
2979
|
+
// Store to the log collection
|
|
2980
|
+
yield logCollection.insert(log);
|
|
2945
2981
|
}
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
source: LogSource$1.Client,
|
|
2982
|
+
else {
|
|
2983
|
+
// Print to console
|
|
2984
|
+
if (log.type === LogType$1.Error) {
|
|
2985
|
+
console.error('dce-reactkit error log:', log);
|
|
2986
|
+
}
|
|
2987
|
+
else {
|
|
2988
|
+
console.log('dce-reactkit action log:', log);
|
|
2989
|
+
}
|
|
2955
2990
|
}
|
|
2956
|
-
|
|
2991
|
+
// Return log entry
|
|
2992
|
+
return log;
|
|
2993
|
+
}
|
|
2994
|
+
catch (err) {
|
|
2995
|
+
// Print because we cannot store the error
|
|
2996
|
+
console.error('Could not log the following:', opts);
|
|
2997
|
+
// Create a dummy log to return
|
|
2998
|
+
const dummyMainInfo = {
|
|
2999
|
+
id: '-1',
|
|
3000
|
+
userFirstName: 'Unknown',
|
|
3001
|
+
userLastName: 'Unknown',
|
|
3002
|
+
userEmail: 'unknown@harvard.edu',
|
|
3003
|
+
userId: 1,
|
|
3004
|
+
isLearner: false,
|
|
3005
|
+
isAdmin: false,
|
|
3006
|
+
isTTM: false,
|
|
3007
|
+
courseId: 1,
|
|
3008
|
+
courseName: 'Unknown',
|
|
3009
|
+
browser: {
|
|
3010
|
+
name: 'Unknown',
|
|
3011
|
+
version: 'Unknown',
|
|
3012
|
+
},
|
|
3013
|
+
device: {
|
|
3014
|
+
isMobile: false,
|
|
3015
|
+
os: 'Unknown',
|
|
3016
|
+
},
|
|
3017
|
+
year: 1,
|
|
3018
|
+
month: 1,
|
|
3019
|
+
day: 1,
|
|
3020
|
+
hour: 1,
|
|
3021
|
+
minute: 1,
|
|
3022
|
+
timestamp: Date.now(),
|
|
3023
|
+
tags: [],
|
|
3024
|
+
metadata: {},
|
|
3025
|
+
category: LogBuiltInCategory$1.Uncategorized,
|
|
3026
|
+
subcategory: LogBuiltInCategory$1.Uncategorized,
|
|
3027
|
+
};
|
|
3028
|
+
const dummyTypeSpecificInfo = {
|
|
3029
|
+
type: LogType$1.Error,
|
|
3030
|
+
errorMessage: 'Unknown',
|
|
3031
|
+
errorCode: 'Unknown',
|
|
3032
|
+
errorStack: 'No Stack',
|
|
3033
|
+
};
|
|
3034
|
+
const dummySourceSpecificInfo = {
|
|
2957
3035
|
source: LogSource$1.Server,
|
|
2958
3036
|
routePath: req.path,
|
|
2959
3037
|
routeTemplate: req.route.path,
|
|
2960
|
-
}
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
// Either print to console or save to db
|
|
2964
|
-
const logCollection = internalGetLogCollection();
|
|
2965
|
-
if (logCollection) {
|
|
2966
|
-
// Store to the log collection
|
|
2967
|
-
yield logCollection.insert(log);
|
|
2968
|
-
}
|
|
2969
|
-
else {
|
|
2970
|
-
// Print to console
|
|
2971
|
-
if (log.type === LogType$1.Error) {
|
|
2972
|
-
console.error('dce-reactkit error log:', log);
|
|
2973
|
-
}
|
|
2974
|
-
else {
|
|
2975
|
-
console.log('dce-reactkit action log:', log);
|
|
2976
|
-
}
|
|
3038
|
+
};
|
|
3039
|
+
const log = Object.assign(Object.assign(Object.assign({}, dummyMainInfo), dummyTypeSpecificInfo), dummySourceSpecificInfo);
|
|
3040
|
+
return log;
|
|
2977
3041
|
}
|
|
2978
|
-
// Return log entry
|
|
2979
|
-
return log;
|
|
2980
3042
|
});
|
|
2981
3043
|
/*------------------------------------------------------------------------*/
|
|
2982
3044
|
/* Call handler */
|
|
@@ -3015,9 +3077,24 @@ const genRouteHandler = (opts) => {
|
|
|
3015
3077
|
* @param [opts.status=500] http status code
|
|
3016
3078
|
*/
|
|
3017
3079
|
const renderErrorPage = (opts = {}) => {
|
|
3018
|
-
var _a;
|
|
3080
|
+
var _a, _b;
|
|
3019
3081
|
const html = genErrorPage(opts);
|
|
3020
3082
|
send(html, (_a = opts.status) !== null && _a !== void 0 ? _a : 500);
|
|
3083
|
+
// Log
|
|
3084
|
+
logServerEvent({
|
|
3085
|
+
category: LogBuiltInCategory$1.ServerRenderedErrorPage,
|
|
3086
|
+
error: {
|
|
3087
|
+
message: `${opts.title}: ${opts.description}`,
|
|
3088
|
+
code: opts.code,
|
|
3089
|
+
},
|
|
3090
|
+
metadata: {
|
|
3091
|
+
title: opts.title,
|
|
3092
|
+
description: opts.description,
|
|
3093
|
+
code: opts.code,
|
|
3094
|
+
pageTitle: opts.pageTitle,
|
|
3095
|
+
status: (_b = opts.status) !== null && _b !== void 0 ? _b : 500,
|
|
3096
|
+
},
|
|
3097
|
+
});
|
|
3021
3098
|
};
|
|
3022
3099
|
// Call the handler
|
|
3023
3100
|
try {
|
|
@@ -3041,7 +3118,13 @@ const genRouteHandler = (opts) => {
|
|
|
3041
3118
|
catch (err) {
|
|
3042
3119
|
// Send error to client (only if next wasn't called)
|
|
3043
3120
|
if (!responseSent) {
|
|
3044
|
-
|
|
3121
|
+
handleError(res, err);
|
|
3122
|
+
// Log server-side error
|
|
3123
|
+
logServerEvent({
|
|
3124
|
+
category: LogBuiltInCategory$1.ServerEndpointError,
|
|
3125
|
+
error: err,
|
|
3126
|
+
});
|
|
3127
|
+
return;
|
|
3045
3128
|
}
|
|
3046
3129
|
// Log error that was not responded with
|
|
3047
3130
|
console.log('Error occurred but could not be sent to client because a response was already sent:', err);
|
|
@@ -3240,19 +3323,19 @@ const parallelLimit = (taskFunctions, limit) => __awaiter(void 0, void 0, void 0
|
|
|
3240
3323
|
* @author Gabe Abrams
|
|
3241
3324
|
*/
|
|
3242
3325
|
const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3243
|
-
var _a, _b;
|
|
3326
|
+
var _a, _b, _c, _d, _e;
|
|
3244
3327
|
return visitServerEndpoint({
|
|
3245
3328
|
path: LOG_ROUTE_PATH,
|
|
3246
3329
|
method: 'POST',
|
|
3247
3330
|
params: {
|
|
3248
3331
|
category: (typeof opts.category === 'string'
|
|
3249
3332
|
? opts.category
|
|
3250
|
-
: opts.category.
|
|
3333
|
+
: ((_b = ((_a = opts.category) !== null && _a !== void 0 ? _a : {})._) !== null && _b !== void 0 ? _b : LogBuiltInCategory$1.Uncategorized)),
|
|
3251
3334
|
subcategory: (typeof opts.category === 'string'
|
|
3252
3335
|
? opts.subcategory
|
|
3253
|
-
: ((
|
|
3336
|
+
: ((_d = ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : {})._) !== null && _d !== void 0 ? _d : LogBuiltInCategory$1.Uncategorized)),
|
|
3254
3337
|
tags: JSON.stringify(opts.tags),
|
|
3255
|
-
metadata: JSON.stringify((
|
|
3338
|
+
metadata: JSON.stringify((_e = opts.metadata) !== null && _e !== void 0 ? _e : {}),
|
|
3256
3339
|
errorMessage: (opts.error
|
|
3257
3340
|
? opts.error.message
|
|
3258
3341
|
: undefined),
|
|
@@ -3345,5 +3428,5 @@ var LogAction;
|
|
|
3345
3428
|
})(LogAction || (LogAction = {}));
|
|
3346
3429
|
var LogAction$1 = LogAction;
|
|
3347
3430
|
|
|
3348
|
-
export { AppWrapper, ButtonInputGroup, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, ErrorBox, ErrorWithCode, HOUR_IN_MS, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, ceilToNumDecimals, confirm, floorToNumDecimals, forceNumIntoBounds, genRouteHandler, getHumanReadableDate, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initLogCollection, initServer, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
|
|
3431
|
+
export { AppWrapper, ButtonInputGroup, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, ErrorBox, ErrorWithCode, HOUR_IN_MS, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInCategory$1 as LogBuiltInCategory, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, ceilToNumDecimals, confirm, floorToNumDecimals, forceNumIntoBounds, genRouteHandler, getHumanReadableDate, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initLogCollection, initServer, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
|
|
3349
3432
|
//# sourceMappingURL=index.js.map
|