This removes `LONG_CACHE` and its descendants, which was a feature that added `?maxAge` to the live preview badges in the frontend. Since they are all static that is no longer needed, as the static badges all have longer cache timeouts regardless.
142 lines
3.3 KiB
JavaScript
142 lines
3.3 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('joi')
|
|
const pathToRegexp = require('path-to-regexp')
|
|
const coalesceBadge = require('./coalesce-badge')
|
|
const { makeFullUrl } = require('./route')
|
|
|
|
const optionalObjectOfKeyValues = Joi.object().pattern(
|
|
/./,
|
|
Joi.string().allow(null)
|
|
)
|
|
|
|
const schema = Joi.object({
|
|
// This should be:
|
|
// title: Joi.string().required(),
|
|
title: Joi.string(),
|
|
namedParams: optionalObjectOfKeyValues.required(),
|
|
queryParams: optionalObjectOfKeyValues.default({}),
|
|
pattern: Joi.string(),
|
|
staticPreview: Joi.object({
|
|
label: Joi.string(),
|
|
message: Joi.alternatives()
|
|
.try(
|
|
Joi.string()
|
|
.allow('')
|
|
.required(),
|
|
Joi.number()
|
|
)
|
|
.required(),
|
|
color: Joi.string(),
|
|
style: Joi.string(),
|
|
}).required(),
|
|
keywords: Joi.array()
|
|
.items(Joi.string())
|
|
.default([]),
|
|
documentation: Joi.string(), // Valid HTML.
|
|
}).required()
|
|
|
|
function validateExample(example, index, ServiceClass) {
|
|
const result = Joi.attempt(
|
|
example,
|
|
schema,
|
|
`Example for ${ServiceClass.name} at index ${index}`
|
|
)
|
|
|
|
const { pattern, namedParams } = result
|
|
|
|
if (!pattern && !ServiceClass.route.pattern) {
|
|
throw new Error(
|
|
`Example for ${
|
|
ServiceClass.name
|
|
} at index ${index} does not declare a pattern`
|
|
)
|
|
}
|
|
if (pattern === ServiceClass.route.pattern) {
|
|
throw new Error(
|
|
`Example for ${
|
|
ServiceClass.name
|
|
} at index ${index} declares a redundant pattern which should be removed`
|
|
)
|
|
}
|
|
|
|
// Make sure we can build the full URL using these patterns.
|
|
try {
|
|
pathToRegexp.compile(pattern || ServiceClass.route.pattern)(namedParams)
|
|
} catch (e) {
|
|
throw Error(
|
|
`In example for ${
|
|
ServiceClass.name
|
|
} at index ${index}, ${e.message.toLowerCase()}`
|
|
)
|
|
}
|
|
// Make sure there are no extra keys.
|
|
let keys = []
|
|
pathToRegexp(pattern || ServiceClass.route.pattern, keys)
|
|
keys = keys.map(({ name }) => name)
|
|
const extraKeys = Object.keys(namedParams).filter(k => !keys.includes(k))
|
|
if (extraKeys.length) {
|
|
throw Error(
|
|
`In example for ${
|
|
ServiceClass.name
|
|
} at index ${index}, namedParams contains unknown keys: ${extraKeys.join(
|
|
', '
|
|
)}`
|
|
)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
function transformExample(inExample, index, ServiceClass) {
|
|
const {
|
|
// We should get rid of this transform, since the class name is never what
|
|
// we want to see.
|
|
title = ServiceClass.name,
|
|
namedParams,
|
|
queryParams,
|
|
pattern,
|
|
staticPreview,
|
|
keywords,
|
|
documentation,
|
|
} = validateExample(inExample, index, ServiceClass)
|
|
|
|
const {
|
|
text: [label, message],
|
|
color,
|
|
template: style,
|
|
namedLogo,
|
|
} = coalesceBadge(
|
|
{},
|
|
staticPreview,
|
|
ServiceClass.defaultBadgeData,
|
|
ServiceClass
|
|
)
|
|
|
|
return {
|
|
title,
|
|
example: {
|
|
pattern: makeFullUrl(
|
|
ServiceClass.route.base,
|
|
pattern || ServiceClass.route.pattern
|
|
),
|
|
namedParams,
|
|
queryParams,
|
|
},
|
|
preview: {
|
|
label,
|
|
message: `${message}`,
|
|
color,
|
|
style: style === 'flat' ? undefined : style,
|
|
namedLogo,
|
|
},
|
|
keywords,
|
|
documentation: documentation ? { __html: documentation } : undefined,
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
validateExample,
|
|
transformExample,
|
|
}
|