Files
shields/core/base-service/route.js
dependabot[bot] b9d96755ec chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 (#9357)
* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* reformat all the things (prettier 3)

* update tests to await calls to prettier.format()

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
2023-07-10 09:27:51 +00:00

83 lines
2.2 KiB
JavaScript

import escapeStringRegexp from 'escape-string-regexp'
import Joi from 'joi'
import { pathToRegexp } from 'path-to-regexp'
function makeFullUrl(base, partialUrl) {
return `/${[base, partialUrl].filter(Boolean).join('/')}`
}
const isValidRoute = Joi.object({
base: Joi.string().allow('').required(),
pattern: Joi.string().allow(''),
format: Joi.string(),
capture: Joi.alternatives().conditional('format', {
is: Joi.string().required(),
then: Joi.array().items(Joi.string().required()),
}),
queryParamSchema: Joi.object().schema(),
})
.xor('pattern', 'format')
.required()
function assertValidRoute(route, message = undefined) {
Joi.assert(route, isValidRoute, message)
}
function prepareRoute({ base, pattern, format, capture, withPng }) {
const extensionRegex = ['', '.svg', '.json']
.concat(withPng ? ['.png'] : [])
.map(escapeStringRegexp)
.join('|')
let regex, captureNames
if (pattern === undefined) {
regex = new RegExp(`^${makeFullUrl(base, format)}(${extensionRegex})$`)
captureNames = capture || []
} else {
const fullPattern = `${makeFullUrl(base, pattern)}:ext(${extensionRegex})`
const keys = []
regex = pathToRegexp(fullPattern, keys, {
strict: true,
sensitive: true,
})
captureNames = keys.map(item => item.name).slice(0, -1)
}
return { regex, captureNames }
}
function namedParamsForMatch(captureNames = [], match, ServiceClass) {
// Assume the last match is the format, and drop match[0], which is the
// entire match.
const captures = match.slice(1, -1)
if (captureNames.length !== captures.length) {
throw new Error(
`Service ${ServiceClass.name} declares incorrect number of named params ` +
`(expected ${captures.length}, got ${captureNames.length})`,
)
}
const result = {}
captureNames.forEach((name, index) => {
result[name] = captures[index]
})
return result
}
function getQueryParamNames({ queryParamSchema }) {
if (queryParamSchema) {
const { keys, renames = [] } = queryParamSchema.describe()
return Object.keys(keys).concat(renames.map(({ from }) => from))
} else {
return []
}
}
export {
makeFullUrl,
isValidRoute,
assertValidRoute,
prepareRoute,
namedParamsForMatch,
getQueryParamNames,
}