Fix comment newline handling

This commit is contained in:
Zoe Roux 2025-01-06 01:31:34 +01:00
parent 3d6912b60d
commit 0499be4194
No known key found for this signature in database
4 changed files with 14 additions and 6 deletions

View File

@ -136,7 +136,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
...KError,
description: comment`
The Accept-Language header can't be satisfied (all languages listed are
unavailable). Try with another languages or add * to the list of languages
unavailable.) Try with another languages or add * to the list of languages
to fallback to any language.
`,
examples: [
@ -158,6 +158,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
}) => {
const langs = processLanguages(languages);
const [transQ, transCol] = getTranslationQuery(langs);
// TODO: move this to typebox transform
const order = sort.map((x) => {
const desc = x[0] === "-";
const key = (desc ? x.substring(1) : x) as RemovePrefix<typeof x, "-">;
@ -203,7 +204,11 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
"-nextRefresh",
]),
// TODO: support explode: true (allow sort=slug,-createdAt). needs a pr to elysia
{ explode: false, default: ["slug"] },
{
explode: false,
default: ["slug"],
description: "How to sort the query",
},
),
filter: t.Optional(Filter({ def: movieFilters })),
limit: t.Integer({

View File

@ -24,9 +24,9 @@ export const Filter = ({
t.String({
description: comment`
${description}
This is based on [odata's filter specification](https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptionfilter).
Filters available: ${Object.keys(def).join(", ")}
This is based on [odata's filter specification](https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptionfilter).
Filters available: ${Object.keys(def).join(", ")}.
`,
example: "(rating gt 75 and genres has action) or status eq planned",
}),

View File

@ -2,7 +2,10 @@
export const comment = (str: TemplateStringsArray, ...values: any[]) =>
str
.reduce((acc, str, i) => `${acc}${values[i - 1]}${str}`)
.replace(/(^[^\S\n]+|\s+$|^\s+)/gm, "");
.replace(/(^\s)|(\s+$)/g, "") // first & last whitespaces
.replace(/^[ \t]+/gm, "") // leading spaces
.replace(/([^\n])\n([^\n])/g, "$1 $2") // two lines to space separated line
.replace(/\n{2}/g, "\n"); // keep newline if there's an empty line
export type RemovePrefix<
T extends string,

View File

@ -130,7 +130,7 @@ describe("Get all movies", () => {
expect(body).toMatchObject({
status: 422,
message:
"Invalid property: slug.\nExpected one of genres, rating, status, runtime, airDate, originalLanguage.",
"Invalid property: slug. Expected one of genres, rating, status, runtime, airDate, originalLanguage.",
details: {
in: "slug eq bubble",
},