-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow more options for pdf, context and goto to be passed. Also worki…
…ng validations. We allow way more options now to be given in the payload: - `pdfOptions` are the options for the `pdf` call in Playwright: https://playwright.dev/docs/api/class-page#page-pdf. - `gotoOptions` are the options for the `goto` method in Playwright: https://playwright.dev/docs/api/class-page#page-goto - if you don't pass anything, we default to a timeout of 60000 and wait for `networkidle` - `browserContextOptions` are the options for the browser context in Playwright: https://playwright.dev/docs/api/class-browser#browser-new-context - if you don't pass anything, we default to set `ignoreHTTPSErrors` to `true` We finally added tests too! ✌️ - We don't allow `options` in the payload anymore. Those are `pdfOptions` now. To adapt your code, rename `options` to `pdfOptions` and you should be good to go. This change was needed to allow more options for other methods we're using (like `timeout` for `goto`). - We have stricter validation now and don't allow unknown keys in the option objects now. Previously we've ignored them, but we've now opted to bail out with an validation error so the user gets notified on typos and such.
- Loading branch information
Showing
14 changed files
with
2,737 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
"bits-to-dead-trees": major | ||
--- | ||
|
||
Allow more options for pdf, context and goto to be passed. Also working validations. | ||
|
||
We allow way more options now to be given in the payload: | ||
|
||
- `pdfOptions` are the options for the `pdf` call in Playwright: https://playwright.dev/docs/api/class-page#page-pdf. | ||
- `gotoOptions` are the options for the `goto` method in Playwright: https://playwright.dev/docs/api/class-page#page-goto | ||
- if you don't pass anything, we default to a timeout of 60000 and wait for `networkidle` | ||
- `browserContextOptions` are the options for the browser context in Playwright: https://playwright.dev/docs/api/class-browser#browser-new-context | ||
- if you don't pass anything, we default to set `ignoreHTTPSErrors` to `true` | ||
|
||
We finally added tests too! ✌️ | ||
|
||
## Breaking Changes | ||
|
||
- We don't allow `options` in the payload anymore. Those are `pdfOptions` now. To adapt your code, rename `options` to `pdfOptions` and you should be good to go. | ||
This change was needed to allow more options for other methods we're using (like `timeout` for `goto`). | ||
- We have stricter validation now and don't allow unknown keys in the option objects now. Previously we've ignored them, but we've now opted to bail out with | ||
an validation error so the user gets notified on typos and such. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ jobs: | |
|
||
- name: Run typecheck | ||
run: npm run ts-check | ||
|
||
- name: Run tests | ||
run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// @ts-check | ||
|
||
import { fastify } from "fastify"; | ||
import { chromium } from "playwright-core"; | ||
import lodash from "lodash"; | ||
import PdfRequestBodySchema from "./pdf_request_body.json" assert { type: "json" }; | ||
|
||
const { isEmpty } = lodash; | ||
|
||
/** | ||
* @typedef {Parameters<import("playwright-core").Page["pdf"]>[0]} PdfOptions | ||
* @typedef {Parameters<import("playwright-core").Page["goto"]>[1]} GotoOptions | ||
* @typedef {import("playwright-core").BrowserContextOptions} BrowserContextOptions | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} PdfRequestBody | ||
* @property {string} url | ||
* @property {PdfOptions} pdfOptions | ||
* @property {BrowserContextOptions} browserContextOptions | ||
* @property {GotoOptions} gotoOptions | ||
*/ | ||
|
||
export const defaultBrowserContextOptions = /** @type {const} */ ({ | ||
ignoreHTTPSErrors: true, | ||
}); | ||
export const defaultGotoOptions = /** @type {const} */ ({ | ||
timeout: 60000, | ||
waitUntil: "networkidle", | ||
}); | ||
|
||
/** | ||
* | ||
* @param {string} url | ||
* @param {PdfOptions} pdfOptions | ||
* @param {BrowserContextOptions} browserContextOptions | ||
* @param {GotoOptions} gotoOptions | ||
* @returns Buffer | ||
*/ | ||
const createPDF = async ( | ||
url, | ||
pdfOptions = {}, | ||
browserContextOptions = {}, | ||
gotoOptions = {} | ||
) => { | ||
const browser = await chromium.launch(); | ||
|
||
try { | ||
const context = await browser.newContext(browserContextOptions); | ||
const page = await context.newPage(); | ||
|
||
await page.goto(url, gotoOptions); | ||
|
||
const pdf = await page.pdf(pdfOptions); | ||
return pdf; | ||
} finally { | ||
await browser.close(); | ||
} | ||
}; | ||
|
||
/** | ||
* | ||
* @param {import("fastify").FastifyServerOptions} opts | ||
*/ | ||
function build(opts = {}) { | ||
const fastifyOptions = { | ||
...opts, | ||
ajv: { | ||
customOptions: { | ||
// we want validation errors if someone passes an unknown option | ||
removeAdditional: false, | ||
// Don't adhere to ajvs strict types, as our schema is auto generated anyway | ||
allowUnionTypes: true, | ||
}, | ||
}, | ||
}; | ||
const app = fastify(fastifyOptions); | ||
|
||
app.post( | ||
"/pdf", | ||
{ schema: { body: PdfRequestBodySchema }, attachValidation: true }, | ||
/** | ||
* | ||
* @param {import('fastify').FastifyRequest<{Body: PdfRequestBody}>} request | ||
* @param {import('fastify').FastifyReply} response | ||
*/ | ||
async (request, response) => { | ||
if (request.validationError) { | ||
response.code(400); | ||
|
||
return { | ||
error: true, | ||
message: request.validationError.message, | ||
}; | ||
} | ||
|
||
const { url, pdfOptions, browserContextOptions, gotoOptions } = | ||
request.body; | ||
|
||
const pdfOpts = !isEmpty(pdfOptions) ? pdfOptions : {}; | ||
const contextOpts = isEmpty(browserContextOptions) | ||
? defaultBrowserContextOptions | ||
: browserContextOptions; | ||
const gotoOpts = isEmpty(gotoOptions) ? defaultGotoOptions : gotoOptions; | ||
|
||
try { | ||
return createPDF(url, pdfOpts, contextOpts, gotoOpts); | ||
} catch (error) { | ||
response.code(500); | ||
|
||
return { | ||
error, | ||
}; | ||
} | ||
} | ||
); | ||
|
||
app.get("/health", async (_, response) => response.code(200).send()); | ||
|
||
return app; | ||
} | ||
|
||
export default build; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
"watch": ["index.js", "schemas/pdf_request_body.json", "package-lock.json"], | ||
"watch": [ | ||
"app.js", | ||
"server.js", | ||
"schemas/pdf_request_body.json", | ||
"package-lock.json" | ||
], | ||
"ext": "js,json" | ||
} |
Oops, something went wrong.