Consolidate service definition schema, removing support for previewUrl (#2895)

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.
This commit is contained in:
Paul Melnikow
2019-01-30 18:35:11 -06:00
committed by GitHub
parent 7955f460f3
commit 3a7bddbc26
15 changed files with 117 additions and 462 deletions

View File

@@ -10,29 +10,26 @@ const optionalObjectOfKeyValues = Joi.object().pattern(
Joi.string().allow(null)
)
const optionalServiceData = Joi.object({
label: Joi.string(),
message: Joi.alternatives()
.try(
Joi.string()
.allow('')
.required(),
Joi.number()
)
.required(),
color: Joi.string(),
style: Joi.string(),
})
const schema = Joi.object({
// This should be:
// title: Joi.string().required(),
title: Joi.string(),
namedParams: optionalObjectOfKeyValues,
namedParams: optionalObjectOfKeyValues.required(),
queryParams: optionalObjectOfKeyValues.default({}),
pattern: Joi.string(),
staticPreview: optionalServiceData,
previewUrl: 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([]),
@@ -46,66 +43,45 @@ function validateExample(example, index, ServiceClass) {
`Example for ${ServiceClass.name} at index ${index}`
)
const { namedParams, pattern, staticPreview, previewUrl } = result
const { pattern, namedParams } = result
if (staticPreview) {
if (!pattern && !ServiceClass.route.pattern) {
throw new Error(
`Static preview for ${
ServiceClass.name
} at index ${index} does not declare a pattern`
)
} else if (!namedParams) {
throw new Error(
`Static preview for ${
ServiceClass.name
} at index ${index} does not declare namedParams`
)
}
if (previewUrl) {
throw new Error(
`Static preview for ${
ServiceClass.name
} at index ${index} also declares a dynamic previewUrl, which is not allowed`
)
}
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(
', '
)}`
)
}
} else if (!previewUrl) {
throw Error(
if (!pattern && !ServiceClass.route.pattern) {
throw new Error(
`Example for ${
ServiceClass.name
} at index ${index} is missing required previewUrl or staticPreview`
} 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(
', '
)}`
)
}
@@ -121,59 +97,39 @@ function transformExample(inExample, index, ServiceClass) {
queryParams,
pattern,
staticPreview,
previewUrl,
keywords,
documentation,
} = validateExample(inExample, index, ServiceClass)
let example
if (namedParams) {
example = {
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,
}
} else {
example = {
path: makeFullUrl(ServiceClass.route.base, previewUrl),
queryParams,
}
}
let preview
if (staticPreview) {
const {
text: [label, message],
color,
template: style,
namedLogo,
} = coalesceBadge(
{},
staticPreview,
ServiceClass.defaultBadgeData,
ServiceClass
)
preview = {
},
preview: {
label,
message: `${message}`,
color,
style: style === 'flat' ? undefined : style,
namedLogo,
}
} else {
preview = {
path: makeFullUrl(ServiceClass.route.base, previewUrl),
queryParams,
}
}
return {
title,
example,
preview,
},
keywords,
documentation: documentation ? { __html: documentation } : undefined,
}