You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
2.4 KiB

  1. // Converts an object of environment variables into a Swagger UI config object
  2. const configSchema = require("./variables")
  3. const defaultBaseConfig = {
  4. url: {
  5. value: "https://petstore.swagger.io/v2/swagger.json",
  6. schema: {
  7. type: "string",
  8. base: true
  9. }
  10. },
  11. dom_id: {
  12. value: "#swagger-ui",
  13. schema: {
  14. type: "string",
  15. base: true
  16. }
  17. },
  18. deepLinking: {
  19. value: "true",
  20. schema: {
  21. type: "boolean",
  22. base: true
  23. }
  24. },
  25. presets: {
  26. value: `[\n SwaggerUIBundle.presets.apis,\n SwaggerUIStandalonePreset\n]`,
  27. schema: {
  28. type: "array",
  29. base: true
  30. }
  31. },
  32. plugins: {
  33. value: `[\n SwaggerUIBundle.plugins.DownloadUrl\n]`,
  34. schema: {
  35. type: "array",
  36. base: true
  37. }
  38. },
  39. layout: {
  40. value: "StandaloneLayout",
  41. schema: {
  42. type: "string",
  43. base: true
  44. }
  45. }
  46. }
  47. function objectToKeyValueString(env, { injectBaseConfig = false, schema = configSchema, baseConfig = defaultBaseConfig } = {}) {
  48. let valueStorage = injectBaseConfig ? Object.assign({}, baseConfig) : {}
  49. const keys = Object.keys(env)
  50. // Compute an intermediate representation that holds candidate values and schemas.
  51. //
  52. // This is useful for deduping between multiple env keys that set the same
  53. // config variable.
  54. keys.forEach(key => {
  55. const varSchema = schema[key]
  56. const value = env[key]
  57. if(!varSchema) return
  58. if(varSchema.onFound) {
  59. varSchema.onFound()
  60. }
  61. const storageContents = valueStorage[varSchema.name]
  62. if(storageContents) {
  63. if (varSchema.legacy === true && !storageContents.schema.base) {
  64. // If we're looking at a legacy var, it should lose out to any already-set value
  65. // except for base values
  66. return
  67. }
  68. delete valueStorage[varSchema.name]
  69. }
  70. valueStorage[varSchema.name] = {
  71. value,
  72. schema: varSchema
  73. }
  74. })
  75. // Compute a key:value string based on valueStorage's contents.
  76. let result = ""
  77. Object.keys(valueStorage).forEach(key => {
  78. const value = valueStorage[key]
  79. const escapedName = /[^a-zA-Z0-9]/.test(key) ? `"${key}"` : key
  80. if (value.schema.type === "string") {
  81. result += `${escapedName}: "${value.value}",\n`
  82. } else {
  83. result += `${escapedName}: ${value.value === "" ? `undefined` : value.value},\n`
  84. }
  85. })
  86. return result.trim()
  87. }
  88. module.exports = objectToKeyValueString