Source: services/dynamic-common.js

  1. /**
  2. * Common functions and utilities for tasks related to dynamic badges.
  3. *
  4. * @module
  5. */
  6. import Joi from 'joi'
  7. import toArray from '../core/base-service/to-array.js'
  8. import validate from '../core/base-service/validate.js'
  9. import { InvalidResponse } from './index.js'
  10. /**
  11. * Map of error codes and their corresponding error messages.
  12. *
  13. * @type {object}
  14. */
  15. const httpErrors = {
  16. 404: 'resource not found',
  17. }
  18. /**
  19. * Joi schema for validating individual value.
  20. * Checks if the individual value is of type string or number.
  21. *
  22. * @type {Joi}
  23. */
  24. const individualValueSchema = Joi.alternatives()
  25. .try(Joi.string(), Joi.number())
  26. .required()
  27. /**
  28. * Joi schema for validating compound value.
  29. * Checks if the compound value is of type individualValueSchema, array of individualValueSchema or empty array.
  30. *
  31. * @type {Joi}
  32. */
  33. const compoundValueSchema = Joi.alternatives().try(
  34. individualValueSchema,
  35. Joi.array().items(individualValueSchema).required(),
  36. Joi.array().length(0),
  37. )
  38. /**
  39. * Look up the value in the data object by key and validate the value against compoundValueSchema.
  40. *
  41. * @param {object} attrs Refer to individual attributes
  42. * @param {object} attrs.data Object containing the data for validation
  43. * @param {string} attrs.key Key to retrieve the data from object for validation
  44. * @throws {InvalidResponse|Error} Error if Joi validation fails due to invalid or no schema
  45. * @returns {object} Value if Joi validation is success
  46. */
  47. function transformAndValidate({ data, key }) {
  48. return validate(
  49. {
  50. ErrorClass: InvalidResponse,
  51. prettyErrorMessage: 'invalid key value',
  52. traceErrorMessage: 'Key value not valid for dynamic badge',
  53. traceSuccessMessage: 'Key value after validation',
  54. },
  55. data[key],
  56. compoundValueSchema,
  57. )
  58. }
  59. /**
  60. * Handles rendering concerns of dynamic badges.
  61. * Determines the label of the badge according to the tag and defaultLabel.
  62. * Determines the message of the badge according to the prefix, suffix and value.
  63. * Sets the color of the badge to blue.
  64. *
  65. * @param {object} attrs Refer to individual attributes
  66. * @param {string} attrs.defaultLabel default badge label
  67. * @param {string} [attrs.tag] If provided then this value will be appended to the badge label, e.g. `foobar@v1.23`
  68. * @param {any} attrs.value Value or array of value to be used for the badge message
  69. * @param {string} [attrs.prefix] If provided then the badge message will use this value as a prefix
  70. * @param {string} [attrs.suffix] If provided then the badge message will use this value as a suffix
  71. * @returns {object} Badge with label, message and color properties
  72. */
  73. function renderDynamicBadge({
  74. defaultLabel,
  75. tag,
  76. value,
  77. prefix = '',
  78. suffix = '',
  79. }) {
  80. const renderedValue =
  81. value === undefined ? 'not specified' : toArray(value).join(', ')
  82. return {
  83. label: tag ? `${defaultLabel}@${tag}` : defaultLabel,
  84. message: `${prefix}${renderedValue}${suffix}`,
  85. color: 'blue',
  86. }
  87. }
  88. export {
  89. httpErrors,
  90. individualValueSchema,
  91. transformAndValidate,
  92. renderDynamicBadge,
  93. }