Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(routes)!: rename camelCase query string params to snake_case #1964

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ updates:
- dependency-name: esbuild-plugin-glob
# Below are dependencies that have migrated to ESM
# in their next major version so we can't use them
- dependency-name: camelcase
update-types: ["version-update:semver-major"]
- dependency-name: file-type
update-types: ["version-update:semver-major"]
- dependency-name: is-html
Expand Down
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@fastify/static": "^8.0.3",
"@fastify/swagger": "^9.4.0",
"@fastify/under-pressure": "^9.0.1",
"camelcase": "^6.3.0",
"cfb": "^1.2.2",
"clean-css": "^5.3.3",
"cssesc": "^3.0.0",
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/pdf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const { randomUUID } = require("node:crypto");
const { mkdir, readFile, unlink } = require("node:fs/promises");
const camelCase = require("camelcase");
const { fixLatin1ToUtf8: fixUtf8 } = require("fix-latin1-to-utf8");
const fp = require("fastify-plugin");
const { glob } = require("glob");
Expand Down Expand Up @@ -93,17 +94,18 @@ async function plugin(server, options) {
/**
* Create copy of query string params and prune that,
* as some of the params may be used in other plugins.
* @type {Record<string, string | number | boolean>}
*/
const query = { ...req.query };
Object.keys(query).forEach((value) => {
if (!pdfToHtmlAcceptedParams.has(value)) {
delete query[value];
} else {
const query = {};
Object.keys(req.query).forEach((key) => {
const camelCaseKey = camelCase(key);

if (pdfToHtmlAcceptedParams.has(camelCaseKey)) {
/**
* Convert query string params to literal values to
* Convert query string params to literal keys to
* allow Poppler module to use them.
*/
query[value] = parseString(query[value]);
query[camelCaseKey] = parseString(req.query[key]);
}
});
Object.assign(config.pdfToHtmlOptions, query);
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/pdf-to-html/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ describe("PDF-to-HTML conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: "2",
ignoreImages: "false",
last_page_to_convert: "2",
ignore_images: "false",
...query,
},
headers: {
Expand Down Expand Up @@ -161,8 +161,8 @@ describe("PDF-to-HTML conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: "1",
ignoreImages: "false",
last_page_to_convert: "1",
ignore_images: "false",
},
headers: {
"content-type": "application/pdf",
Expand Down
21 changes: 12 additions & 9 deletions src/plugins/pdf-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const { randomUUID } = require("node:crypto");
const { mkdir, unlink } = require("node:fs/promises");
const camelCase = require("camelcase");
const { fixLatin1ToUtf8: fixUtf8 } = require("fix-latin1-to-utf8");
const fp = require("fastify-plugin");
const { glob } = require("glob");
Expand Down Expand Up @@ -111,10 +112,12 @@ async function plugin(server, options) {
* Create copy of query string params and convert query string params to literal
* values to allow Poppler module to use them, as some of the params may be used
* in other plugins.
* @type {Record<string, string | number | boolean>}
*/
const query = { ...req.query };
Object.keys(query).forEach((value) => {
query[value] = parseString(query[value]);
const query = {};
Object.keys(req.query).forEach((key) => {
const camelCaseKey = camelCase(key);
query[camelCaseKey] = parseString(req.query[key]);
});

const id = `${config.tempFilePrefix}_${randomUUID()}`;
Expand All @@ -126,9 +129,9 @@ async function plugin(server, options) {
*/
if (query.ocr === true && server.tesseract) {
// Prune params that pdfToCairo cannot accept
Object.keys(query).forEach((value) => {
if (!pdfToCairoAcceptedParams.has(value)) {
delete query[value];
Object.keys(query).forEach((key) => {
if (!pdfToCairoAcceptedParams.has(key)) {
delete query[key];
}
});

Expand Down Expand Up @@ -184,9 +187,9 @@ async function plugin(server, options) {
res.type("text/plain; charset=utf-8");
} else {
// Prune params that pdfToTxt cannot accept
Object.keys(query).forEach((value) => {
if (!pdfToTxtAcceptedParams.has(value)) {
delete query[value];
Object.keys(query).forEach((key) => {
if (!pdfToTxtAcceptedParams.has(key)) {
delete query[key];
}
});
Object.assign(config.pdfToTxtOptions, query);
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/pdf-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: "2",
lastPageToConvert: "2",
first_page_to_convert: "2",
last_page_to_convert: "2",
...query,
},
headers: {
Expand Down Expand Up @@ -105,8 +105,8 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: "1",
lastPageToConvert: "2",
first_page_to_convert: "1",
last_page_to_convert: "2",
...query,
},
headers: {
Expand Down Expand Up @@ -140,9 +140,9 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: "2",
generateHtmlMetaFile: "true",
lastPageToConvert: "2",
first_page_to_convert: "2",
generate_html_meta_file: "true",
last_page_to_convert: "2",
},
headers: {
"content-type": "application/pdf",
Expand Down Expand Up @@ -188,7 +188,7 @@ describe("PDF-to-TXT conversion plugin", () => {
testName: "is not a valid PDF file for OCR",
body: Buffer.from("test"),
query: {
lastPageToConvert: "1",
last_page_to_convert: "1",
ocr: "true",
},
},
Expand Down Expand Up @@ -241,7 +241,7 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: "1",
last_page_to_convert: "1",
...query,
},
headers: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/docx/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ async function route(server, options) {
const html = await server.docxToHtml(req.body);
const tidiedHtml = await server.tidyHtml(html, {
language: req.query.language,
removeAlt: req.query.removeAlt,
removeAlt: req.query.remove_alt,
});

res.type("text/html; charset=utf-8");
return server.tidyCss(tidiedHtml, {
fonts: req.query.fonts,
backgroundColor: req.query.backgroundColor,
backgroundColor: req.query.background_color,
});
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/docx/html/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("DOCX-to-HTML route", () => {
url: "/",
body: await readFile(filePath),
query: {
removeAlt: "true",
remove_alt: "true",
},
headers: {
accept: "application/json, text/html",
Expand Down
4 changes: 2 additions & 2 deletions src/routes/docx/html/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const docxToHtmlPostSchema = {
query: S.object()
.additionalProperties(false)
.prop(
"backgroundColor",
"background_color",
S.string()
.description(
"HTML document background color; set or replace the `background-color` property value of `div` CSS selectors"
Expand Down Expand Up @@ -57,7 +57,7 @@ const docxToHtmlPostSchema = {
.default("en")
)
.prop(
"removeAlt",
"remove_alt",
S.boolean().description(
"Set the `alt` attribute in `<img>` tags to an empty string"
)
Expand Down
2 changes: 1 addition & 1 deletion src/routes/html/txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function route(server, options) {
},
handler: async (req, res) => {
const tidiedHtml = await server.tidyHtml(req.body, {
removeHidden: req.query.extractHidden !== true,
removeHidden: req.query.extract_hidden !== true,
});

res.type("text/plain; charset=utf-8");
Expand Down
7 changes: 2 additions & 5 deletions src/routes/html/txt/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("HTML-to-TXT route", () => {
"content-type": "text/html",
},
query: {
extractHidden: "true",
extract_hidden: "true",
},
},
{
Expand All @@ -69,7 +69,7 @@ describe("HTML-to-TXT route", () => {
"content-type": "application/xhtml+xml",
},
query: {
extractHidden: "true",
extract_hidden: "true",
},
},
])("Returns $testName", async ({ filePath, headers, query }) => {
Expand Down Expand Up @@ -132,9 +132,6 @@ describe("HTML-to-TXT route", () => {
method: "POST",
url: "/",
body: Buffer.from("test"),
query: {
lastPageToConvert: "1",
},
headers: {
accept: "application/json, text/plain",
"content-type": "text/html",
Expand Down
2 changes: 1 addition & 1 deletion src/routes/html/txt/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const htmlToTxtPostSchema = {
query: S.object()
.additionalProperties(false)
.prop(
"extractHidden",
"extract_hidden",
S.boolean().description("Force hidden text extraction")
),
response: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/pdf/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ async function route(server, options) {
);
const tidiedHtml = await server.tidyHtml(embeddedHtml, {
language: req.query.language,
removeAlt: req.query.removeAlt,
removeAlt: req.query.remove_alt,
});

return server.tidyCss(tidiedHtml, {
fonts: req.query.fonts,
backgroundColor: req.query.backgroundColor,
backgroundColor: req.query.background_color,
});
},
});
Expand Down
Loading