dce-reactkit 3.2.0-beta.1 → 3.2.0-beta.11

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/esm/index.js CHANGED
@@ -2195,10 +2195,10 @@ const initServer = (opts) => {
2195
2195
  /**
2196
2196
  * Log an event
2197
2197
  * @author Gabe Abrams
2198
- * @param {string} category Category of the event (each app determines how to
2199
- * categorize its events)
2200
- * @param {string} subcategory Subcategory of the event (each app determines
2201
- * how to categorize its events)
2198
+ * @param {string} context Context of the event (each app determines how to
2199
+ * organize its contexts)
2200
+ * @param {string} subcontext Subcontext of the event (each app determines
2201
+ * how to organize its subcontexts)
2202
2202
  * @param {string} tags stringified list of tags that apply to this action
2203
2203
  * (each app determines tag usage)
2204
2204
  * @param {string} metadata stringified object containing optional custom metadata
@@ -2212,8 +2212,8 @@ const initServer = (opts) => {
2212
2212
  */
2213
2213
  opts.app.post(LOG_ROUTE_PATH, genRouteHandler({
2214
2214
  paramTypes: {
2215
- category: ParamType$1.String,
2216
- subcategory: ParamType$1.String,
2215
+ context: ParamType$1.String,
2216
+ subcontext: ParamType$1.String,
2217
2217
  tags: ParamType$1.JSON,
2218
2218
  metadata: ParamType$1.JSON,
2219
2219
  errorMessage: ParamType$1.StringOptional,
@@ -2226,8 +2226,8 @@ const initServer = (opts) => {
2226
2226
  const log = logServerEvent((params.errorMessage || params.errorCode || params.errorStack)
2227
2227
  // Error
2228
2228
  ? {
2229
- category: params.category,
2230
- subcategory: params.subcategory,
2229
+ context: params.context,
2230
+ subcontext: params.subcontext,
2231
2231
  tags: params.tags,
2232
2232
  metadata: params.metadata,
2233
2233
  error: {
@@ -2238,8 +2238,8 @@ const initServer = (opts) => {
2238
2238
  }
2239
2239
  // Action
2240
2240
  : {
2241
- category: params.category,
2242
- subcategory: params.subcategory,
2241
+ context: params.context,
2242
+ subcontext: params.subcontext,
2243
2243
  tags: params.tags,
2244
2244
  metadata: params.metadata,
2245
2245
  target: params.target,
@@ -2573,6 +2573,64 @@ var LogSource;
2573
2573
  })(LogSource || (LogSource = {}));
2574
2574
  var LogSource$1 = LogSource;
2575
2575
 
2576
+ /**
2577
+ * Built-in metadata for logs
2578
+ * @author Gabe Abrams
2579
+ */
2580
+ const LogBuiltInMetadata = {
2581
+ // Contexts
2582
+ Context: {
2583
+ Uncategorized: 'n/a',
2584
+ ServerRenderedErrorPage: '_server-rendered-error-page',
2585
+ ServerEndpointError: '_server-endpoint-error',
2586
+ },
2587
+ // Targets
2588
+ Target: {
2589
+ NoSpecificTarget: 'n/a',
2590
+ },
2591
+ };
2592
+
2593
+ /**
2594
+ * Types of actions
2595
+ * @author Gabe Abrams
2596
+ */
2597
+ var LogAction;
2598
+ (function (LogAction) {
2599
+ // Target was opened by the user (it was not on screen, but now it is)
2600
+ LogAction["Open"] = "open";
2601
+ // Target was closed by the user (it was on screen, but now it is not)
2602
+ LogAction["Close"] = "close";
2603
+ // Target was cancelled by the user (it was on closed without saving)
2604
+ LogAction["Cancel"] = "cancel";
2605
+ // Target was expanded by the user (it always remains on screen, but size was changed)
2606
+ LogAction["Expand"] = "expand";
2607
+ // Target was collapsed by the user (it always remains on screen, but size was changed)
2608
+ LogAction["Collapse"] = "collapse";
2609
+ // Target was viewed by the user (only for items that are not opened or closed, those must use Open/Close actions)
2610
+ LogAction["View"] = "view";
2611
+ // Target interrupted the user (popup, dialog, validation message, etc. appeared without user prompting)
2612
+ LogAction["Interrupt"] = "interrupt";
2613
+ // Target was created by the user (it did not exist before)
2614
+ LogAction["Create"] = "create";
2615
+ // Target was edited by the user (it existed and was changed)
2616
+ LogAction["Edit"] = "edit";
2617
+ // Target was deleted by the user (it existed and now it doesn't)
2618
+ LogAction["Delete"] = "delete";
2619
+ // Target was added by the user (it already existed and was added to another place)
2620
+ LogAction["Add"] = "add";
2621
+ // Target was removed by the user (it was removed from something but still exists)
2622
+ LogAction["Remove"] = "remove";
2623
+ // Target was activated by the user (click, check, tap, keypress, etc.)
2624
+ LogAction["Activate"] = "activate";
2625
+ // Target was deactivated by the user (click away, uncheck, tap outside of, tab away, etc.)
2626
+ LogAction["Deactivate"] = "deactivate";
2627
+ // User showed interest in a target (hover, peek, etc.)
2628
+ LogAction["Peek"] = "peek";
2629
+ // Unknown action
2630
+ LogAction["Unknown"] = "unknown";
2631
+ })(LogAction || (LogAction = {}));
2632
+ var LogAction$1 = LogAction;
2633
+
2576
2634
  /**
2577
2635
  * Generate an express API route handler
2578
2636
  * @author Gabe Abrams
@@ -2897,82 +2955,134 @@ const genRouteHandler = (opts) => {
2897
2955
  * @author Gabe Abrams
2898
2956
  */
2899
2957
  const logServerEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
2958
+ var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
2900
2959
  // NOTE: internally, we slip through an opts.overrideAsClientEvent boolean
2901
2960
  // that indicates that this is actually a client event, but we don't
2902
2961
  // include that in the LogFunction type because this is internal and
2903
2962
  // hidden from users
2904
- var _c, _d, _e, _f, _g, _h, _j, _k;
2905
- // Parse user agent
2906
- const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2907
- // Get time info in ET
2908
- const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2909
- // Main log info
2910
- const mainLogInfo = {
2911
- id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2912
- userFirstName: launchInfo.userFirstName,
2913
- userLastName: launchInfo.userLastName,
2914
- userEmail: launchInfo.userEmail,
2915
- userId: launchInfo.userId,
2916
- isLearner: !!launchInfo.isLearner,
2917
- isAdmin: !!launchInfo.isAdmin,
2918
- isTTM: !!launchInfo.isTTM,
2919
- courseId: launchInfo.courseId,
2920
- courseName: launchInfo.courseName,
2921
- browser,
2922
- device,
2923
- year,
2924
- month,
2925
- day,
2926
- hour,
2927
- minute,
2928
- timestamp,
2929
- category: opts.category,
2930
- subcategory: (_c = opts.subcategory) !== null && _c !== void 0 ? _c : 'none',
2931
- tags: (_d = opts.tags) !== null && _d !== void 0 ? _d : [],
2932
- metadata: (_e = opts.metadata) !== null && _e !== void 0 ? _e : {},
2933
- };
2934
- // Type-specific info
2935
- const typeSpecificInfo = (('error' in opts && opts.error)
2936
- ? {
2937
- type: LogType$1.Error,
2938
- errorMessage: (_f = opts.error.message) !== null && _f !== void 0 ? _f : 'Unknown message',
2939
- errorCode: (_g = opts.error.code) !== null && _g !== void 0 ? _g : ReactKitErrorCode$1.NoCode,
2940
- errorStack: (_h = opts.error.stack) !== null && _h !== void 0 ? _h : 'No stack',
2963
+ try {
2964
+ // Parse user agent
2965
+ const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2966
+ // Get time info in ET
2967
+ const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2968
+ // Main log info
2969
+ const mainLogInfo = {
2970
+ id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2971
+ userFirstName: launchInfo.userFirstName,
2972
+ userLastName: launchInfo.userLastName,
2973
+ userEmail: launchInfo.userEmail,
2974
+ userId: launchInfo.userId,
2975
+ isLearner: !!launchInfo.isLearner,
2976
+ isAdmin: !!launchInfo.isAdmin,
2977
+ isTTM: !!launchInfo.isTTM,
2978
+ courseId: launchInfo.courseId,
2979
+ courseName: launchInfo.courseName,
2980
+ browser,
2981
+ device,
2982
+ year,
2983
+ month,
2984
+ day,
2985
+ hour,
2986
+ minute,
2987
+ timestamp,
2988
+ context: (typeof opts.context === 'string'
2989
+ ? opts.context
2990
+ : ((_d = ((_c = opts.context) !== null && _c !== void 0 ? _c : {})._) !== null && _d !== void 0 ? _d : LogBuiltInMetadata.Context.Uncategorized)),
2991
+ subcontext: ((_e = opts.subcontext) !== null && _e !== void 0 ? _e : LogBuiltInMetadata.Context.Uncategorized),
2992
+ tags: (_f = opts.tags) !== null && _f !== void 0 ? _f : [],
2993
+ metadata: (_g = opts.metadata) !== null && _g !== void 0 ? _g : {},
2994
+ };
2995
+ // Type-specific info
2996
+ const typeSpecificInfo = (('error' in opts && opts.error)
2997
+ ? {
2998
+ type: LogType$1.Error,
2999
+ errorMessage: (_h = opts.error.message) !== null && _h !== void 0 ? _h : 'Unknown message',
3000
+ errorCode: (_j = opts.error.code) !== null && _j !== void 0 ? _j : ReactKitErrorCode$1.NoCode,
3001
+ errorStack: (_k = opts.error.stack) !== null && _k !== void 0 ? _k : 'No stack',
3002
+ }
3003
+ : {
3004
+ type: LogType$1.Action,
3005
+ target: ((_l = opts.target) !== null && _l !== void 0 ? _l : LogBuiltInMetadata.Target.NoSpecificTarget),
3006
+ action: ((_m = opts.action) !== null && _m !== void 0 ? _m : LogAction$1.Unknown),
3007
+ });
3008
+ // Source-specific info
3009
+ const sourceSpecificInfo = (opts.overrideAsClientEvent
3010
+ ? {
3011
+ source: LogSource$1.Client,
3012
+ }
3013
+ : {
3014
+ source: LogSource$1.Server,
3015
+ routePath: req.path,
3016
+ routeTemplate: req.route.path,
3017
+ });
3018
+ // Build log event
3019
+ const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
3020
+ // Either print to console or save to db
3021
+ const logCollection = internalGetLogCollection();
3022
+ if (logCollection) {
3023
+ // Store to the log collection
3024
+ yield logCollection.insert(log);
2941
3025
  }
2942
- : {
2943
- type: LogType$1.Action,
2944
- target: (_j = opts.target) !== null && _j !== void 0 ? _j : 'Unknown',
2945
- action: (_k = opts.action) !== null && _k !== void 0 ? _k : 'Unknown'
2946
- });
2947
- // Source-specific info
2948
- const sourceSpecificInfo = (opts.overrideAsClientEvent
2949
- ? {
2950
- source: LogSource$1.Client,
3026
+ else {
3027
+ // Print to console
3028
+ if (log.type === LogType$1.Error) {
3029
+ console.error('dce-reactkit error log:', log);
3030
+ }
3031
+ else {
3032
+ console.log('dce-reactkit action log:', log);
3033
+ }
2951
3034
  }
2952
- : {
3035
+ // Return log entry
3036
+ return log;
3037
+ }
3038
+ catch (err) {
3039
+ // Print because we cannot store the error
3040
+ console.error('Could not log the following:', opts);
3041
+ // Create a dummy log to return
3042
+ const dummyMainInfo = {
3043
+ id: '-1',
3044
+ userFirstName: 'Unknown',
3045
+ userLastName: 'Unknown',
3046
+ userEmail: 'unknown@harvard.edu',
3047
+ userId: 1,
3048
+ isLearner: false,
3049
+ isAdmin: false,
3050
+ isTTM: false,
3051
+ courseId: 1,
3052
+ courseName: 'Unknown',
3053
+ browser: {
3054
+ name: 'Unknown',
3055
+ version: 'Unknown',
3056
+ },
3057
+ device: {
3058
+ isMobile: false,
3059
+ os: 'Unknown',
3060
+ },
3061
+ year: 1,
3062
+ month: 1,
3063
+ day: 1,
3064
+ hour: 1,
3065
+ minute: 1,
3066
+ timestamp: Date.now(),
3067
+ tags: [],
3068
+ metadata: {},
3069
+ context: LogBuiltInMetadata.Context.Uncategorized,
3070
+ subcontext: LogBuiltInMetadata.Context.Uncategorized,
3071
+ };
3072
+ const dummyTypeSpecificInfo = {
3073
+ type: LogType$1.Error,
3074
+ errorMessage: 'Unknown',
3075
+ errorCode: 'Unknown',
3076
+ errorStack: 'No Stack',
3077
+ };
3078
+ const dummySourceSpecificInfo = {
2953
3079
  source: LogSource$1.Server,
2954
3080
  routePath: req.path,
2955
3081
  routeTemplate: req.route.path,
2956
- });
2957
- // Build log event
2958
- const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
2959
- // Either print to console or save to db
2960
- const logCollection = internalGetLogCollection();
2961
- if (logCollection) {
2962
- // Store to the log collection
2963
- yield logCollection.insert(log);
2964
- }
2965
- else {
2966
- // Print to console
2967
- if (log.type === LogType$1.Error) {
2968
- console.error('dce-reactkit error log:', log);
2969
- }
2970
- else {
2971
- console.log('dce-reactkit action log:', log);
2972
- }
3082
+ };
3083
+ const log = Object.assign(Object.assign(Object.assign({}, dummyMainInfo), dummyTypeSpecificInfo), dummySourceSpecificInfo);
3084
+ return log;
2973
3085
  }
2974
- // Return log entry
2975
- return log;
2976
3086
  });
2977
3087
  /*------------------------------------------------------------------------*/
2978
3088
  /* Call handler */
@@ -3011,9 +3121,24 @@ const genRouteHandler = (opts) => {
3011
3121
  * @param [opts.status=500] http status code
3012
3122
  */
3013
3123
  const renderErrorPage = (opts = {}) => {
3014
- var _a;
3124
+ var _a, _b;
3015
3125
  const html = genErrorPage(opts);
3016
3126
  send(html, (_a = opts.status) !== null && _a !== void 0 ? _a : 500);
3127
+ // Log
3128
+ logServerEvent({
3129
+ context: LogBuiltInMetadata.Context.ServerRenderedErrorPage,
3130
+ error: {
3131
+ message: `${opts.title}: ${opts.description}`,
3132
+ code: opts.code,
3133
+ },
3134
+ metadata: {
3135
+ title: opts.title,
3136
+ description: opts.description,
3137
+ code: opts.code,
3138
+ pageTitle: opts.pageTitle,
3139
+ status: (_b = opts.status) !== null && _b !== void 0 ? _b : 500,
3140
+ },
3141
+ });
3017
3142
  };
3018
3143
  // Call the handler
3019
3144
  try {
@@ -3037,7 +3162,13 @@ const genRouteHandler = (opts) => {
3037
3162
  catch (err) {
3038
3163
  // Send error to client (only if next wasn't called)
3039
3164
  if (!responseSent) {
3040
- return handleError(res, err);
3165
+ handleError(res, err);
3166
+ // Log server-side error
3167
+ logServerEvent({
3168
+ context: LogBuiltInMetadata.Context.ServerEndpointError,
3169
+ error: err,
3170
+ });
3171
+ return;
3041
3172
  }
3042
3173
  // Log error that was not responded with
3043
3174
  console.log('Error occurred but could not be sent to client because a response was already sent:', err);
@@ -3231,6 +3362,59 @@ const parallelLimit = (taskFunctions, limit) => __awaiter(void 0, void 0, void 0
3231
3362
  return results;
3232
3363
  });
3233
3364
 
3365
+ /**
3366
+ * Log a user action on the client (cannot be used on the server)
3367
+ * @author Gabe Abrams
3368
+ */
3369
+ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
3370
+ var _a, _b, _c, _d, _e, _f;
3371
+ return visitServerEndpoint({
3372
+ path: LOG_ROUTE_PATH,
3373
+ method: 'POST',
3374
+ params: {
3375
+ context: (typeof opts.context === 'string'
3376
+ ? opts.context
3377
+ : ((_b = ((_a = opts.context) !== null && _a !== void 0 ? _a : {})._) !== null && _b !== void 0 ? _b : LogBuiltInMetadata.Context.Uncategorized)),
3378
+ subcontext: ((_c = opts.subcontext) !== null && _c !== void 0 ? _c : LogBuiltInMetadata.Context.Uncategorized),
3379
+ tags: JSON.stringify((_d = opts.tags) !== null && _d !== void 0 ? _d : []),
3380
+ metadata: JSON.stringify((_e = opts.metadata) !== null && _e !== void 0 ? _e : {}),
3381
+ errorMessage: (opts.error
3382
+ ? opts.error.message
3383
+ : undefined),
3384
+ errorCode: (opts.error
3385
+ ? opts.error.code
3386
+ : undefined),
3387
+ errorStack: (opts.error
3388
+ ? opts.error.stack
3389
+ : undefined),
3390
+ target: (opts.action
3391
+ ? ((_f = opts.target) !== null && _f !== void 0 ? _f : LogBuiltInMetadata.Target.NoSpecificTarget)
3392
+ : undefined),
3393
+ action: (opts.action
3394
+ ? opts.action
3395
+ : undefined),
3396
+ },
3397
+ });
3398
+ });
3399
+
3400
+ /**
3401
+ * Initialize a log collection given the dce-mango Collection class
3402
+ * @author Gabe Abrams
3403
+ * @param Collection the Collection class from dce-mango
3404
+ * @returns initialized logCollection
3405
+ */
3406
+ const initLogCollection = (Collection) => {
3407
+ return new Collection('Log', {
3408
+ uniqueIndexKey: 'id',
3409
+ indexKeys: [
3410
+ 'courseId',
3411
+ 'context',
3412
+ 'subcontext',
3413
+ 'tags',
3414
+ ],
3415
+ });
3416
+ };
3417
+
3234
3418
  /**
3235
3419
  * Days of the week
3236
3420
  * @author Gabe Abrams
@@ -3247,44 +3431,5 @@ var DayOfWeek;
3247
3431
  })(DayOfWeek || (DayOfWeek = {}));
3248
3432
  var DayOfWeek$1 = DayOfWeek;
3249
3433
 
3250
- /**
3251
- * Types of actions
3252
- * @author Gabe Abrams
3253
- */
3254
- var LogAction;
3255
- (function (LogAction) {
3256
- // Target was opened by the user (it was not on screen, but now it is)
3257
- LogAction["Open"] = "open";
3258
- // Target was closed by the user (it was on screen, but now it is not)
3259
- LogAction["Close"] = "close";
3260
- // Target was expanded by the user (it always remains on screen, but size was changed)
3261
- LogAction["Expand"] = "expand";
3262
- // Target was collapsed by the user (it always remains on screen, but size was changed)
3263
- LogAction["Collapse"] = "collapse";
3264
- // Target was viewed by the user (only for items that are not opened or closed, those must use Open/Close actions)
3265
- LogAction["View"] = "view";
3266
- // Target interrupted the user (popup, dialog, validation message, etc. appeared without user prompting)
3267
- LogAction["Interrupt"] = "interrupt";
3268
- // Target was created by the user (it did not exist before)
3269
- LogAction["Create"] = "create";
3270
- // Target was edited by the user (it existed and was changed)
3271
- LogAction["Edit"] = "edit";
3272
- // Target was deleted by the user (it existed and now it doesn't)
3273
- LogAction["Delete"] = "delete";
3274
- // Target was added by the user (it already existed and was added to another place)
3275
- LogAction["Add"] = "add";
3276
- // Target was removed by the user (it was removed from something but still exists)
3277
- LogAction["Remove"] = "remove";
3278
- // Target was activated by the user (click, check, tap, keypress, etc.)
3279
- LogAction["Activate"] = "activate";
3280
- // Target was deactivated by the user (click away, uncheck, tap outside of, tab away, etc.)
3281
- LogAction["Deactivate"] = "deactivate";
3282
- // User showed interest in a target (hover, peek, etc.)
3283
- LogAction["Peek"] = "peek";
3284
- // Unknown action
3285
- LogAction["Unknown"] = "unknown";
3286
- })(LogAction || (LogAction = {}));
3287
- var LogAction$1 = LogAction;
3288
-
3289
- 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, initServer, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
3434
+ export { AppWrapper, ButtonInputGroup, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, ErrorBox, ErrorWithCode, HOUR_IN_MS, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInMetadata, 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 };
3290
3435
  //# sourceMappingURL=index.js.map