Skip to content

Commit

Permalink
Allow more options for pdf, context and goto to be passed. Also worki…
Browse files Browse the repository at this point in the history
…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
rmehner committed Jan 26, 2023
1 parent 368c7f9 commit b4ebed6
Show file tree
Hide file tree
Showing 14 changed files with 2,737 additions and 197 deletions.
22 changes: 22 additions & 0 deletions .changeset/big-schools-argue.md
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.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ jobs:

- name: Run typecheck
run: npm run ts-check

- name: Run tests
run: npm test
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY ["package.json", "package-lock.json*", "./"]

RUN npm install --omit=dev --ignore-scripts

COPY ["index.js", "pdf_request_body.json", "./"]
COPY ["server.js", "app.js", "pdf_request_body.json", "./"]

ENTRYPOINT ["tini", "-v", "--"]
CMD ["node", "index.js"]
CMD ["node", "server.js"]
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ The server exposes the `/pdf` endpoint that listens to a POST request and expect
```json
{
"url": "https://your-target-url.com/site/you/want/a/pdf/of",
"options": {}
"pdfOptions": {},
"gotoOptions": {},
"browserContextOptions": {}
}
```

`options` are the options Playwright knows about PDF: https://playwright.dev/docs/api/class-page#page-pdf. We pass them to Playwright directly, so please refer to their docs.
- `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`

The response is the PDF file.

Expand Down
123 changes: 123 additions & 0 deletions app.js
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;
95 changes: 0 additions & 95 deletions index.js

This file was deleted.

7 changes: 6 additions & 1 deletion nodemon.json
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"
}
Loading

0 comments on commit b4ebed6

Please sign in to comment.