@koloseum/utils 0.2.13 → 0.2.15

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/utils.d.ts CHANGED
@@ -109,6 +109,12 @@ export declare const Utility: {
109
109
  * @returns {Promise<County[]>} A list of objects with the county `name`, `code`, and a list of `subCounties`
110
110
  */
111
111
  getKenyaCounties: (sortBy?: "name" | "code") => Promise<County[]>;
112
+ /**
113
+ * Formats a date to an ISO string in Kenyan time, i.e. `Africa/Nairobi` (UTC+3).
114
+ * @param {Date | string | number | null} date - The date to format
115
+ * @returns {string} The formatted date in ISO string format, or an empty string if invalid input
116
+ */
117
+ getKenyanISOString: (date: Date | string | number | null) => string;
112
118
  /**
113
119
  * Returns the URL for a menu item based on the slug.
114
120
  * @param {string} base - The base URL
@@ -217,9 +223,10 @@ export declare const Utility: {
217
223
  /**
218
224
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
219
225
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
226
+ * @param {boolean} clientSafe - Whether to clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; defaults to `true`
220
227
  * @returns An object with an `error` if any has occurred
221
228
  */
222
- parsePostgrestError: (postgrestError: PostgrestError | null) => {
229
+ parsePostgrestError: (postgrestError: PostgrestError | null, clientSafe?: boolean) => {
223
230
  error?: CustomError;
224
231
  };
225
232
  /**
package/dist/utils.js CHANGED
@@ -358,6 +358,23 @@ export const Utility = {
358
358
  // Return sorted counties
359
359
  return counties.sort((a, b) => (sortBy === "name" ? a.name.localeCompare(b.name) : a.code - b.code));
360
360
  },
361
+ /**
362
+ * Formats a date to an ISO string in Kenyan time, i.e. `Africa/Nairobi` (UTC+3).
363
+ * @param {Date | string | number | null} date - The date to format
364
+ * @returns {string} The formatted date in ISO string format, or an empty string if invalid input
365
+ */
366
+ getKenyanISOString: (date) => {
367
+ // Return empty string if no date is provided or input is invalid
368
+ if (!date || (typeof date !== "string" && typeof date !== "number" && !(date instanceof Date)))
369
+ return "";
370
+ // Get locale string to format
371
+ const dateObj = date instanceof Date ? date : new Date(date);
372
+ const localeString = dateObj.toLocaleString("sv-SE", {
373
+ timeZone: "Africa/Nairobi"
374
+ });
375
+ // Return formatted string
376
+ return localeString.replace(" ", "T") + "+03:00";
377
+ },
361
378
  /**
362
379
  * Returns the URL for a menu item based on the slug.
363
380
  * @param {string} base - The base URL
@@ -1251,17 +1268,21 @@ export const Utility = {
1251
1268
  /**
1252
1269
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
1253
1270
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
1271
+ * @param {boolean} clientSafe - Whether to clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; defaults to `true`
1254
1272
  * @returns An object with an `error` if any has occurred
1255
1273
  */
1256
- parsePostgrestError: (postgrestError) => {
1274
+ parsePostgrestError: (postgrestError, clientSafe = true) => {
1257
1275
  // Return undefined if no error occurred
1258
1276
  let error;
1259
1277
  if (!postgrestError)
1260
1278
  return { error };
1261
- // Return custom error if hint is a number between 400 and 599
1279
+ // Get custom error code from hint
1262
1280
  const customErrorCode = Number(postgrestError.hint);
1281
+ // Clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; see https://koloseum-technologies.sentry.io/issues/6766267685/
1282
+ let statusCode = clientSafe ? (customErrorCode >= 500 ? 422 : customErrorCode) : customErrorCode;
1283
+ // Return custom error if hint is a number between 400 and 599
1263
1284
  if (!isNaN(customErrorCode) && customErrorCode >= 400 && customErrorCode <= 599) {
1264
- error = Utility.customError(customErrorCode, postgrestError.message);
1285
+ error = Utility.customError(statusCode, postgrestError.message);
1265
1286
  return { error };
1266
1287
  }
1267
1288
  // Map Postgrest error codes to custom error codes
@@ -1299,11 +1320,12 @@ export const Utility = {
1299
1320
  // Return custom error if Postgrest error code is found
1300
1321
  for (const { code, status } of errorMap)
1301
1322
  if (postgrestError.code === code || postgrestError.code.startsWith(code)) {
1302
- error = Utility.customError(status, Status.ERROR);
1323
+ statusCode = clientSafe ? (status >= 500 ? 422 : status) : status;
1324
+ error = Utility.customError(statusCode, Status.ERROR);
1303
1325
  return { error };
1304
1326
  }
1305
1327
  // Return generic error
1306
- error = Utility.customError(500, Status.ERROR);
1328
+ error = Utility.customError(clientSafe ? 422 : 500, Status.ERROR);
1307
1329
  return { error };
1308
1330
  },
1309
1331
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",