Skip to content

Commit 3a62a32

Browse files
committed
feat: Automatically encode page.html, pdf.headerTemplate and pdf.footerTemplate
1 parent ae813d1 commit 3a62a32

6 files changed

Lines changed: 97 additions & 4 deletions

File tree

src/Doczilla.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import axios, { Axios } from 'axios'
22

3+
import { version } from '../package.json'
34
import { PdfService } from './services/PdfService'
45
import { ScreenshotService } from './services/ScreenshotService'
56
import { WebhookService } from './services/WebhookService'
6-
import { version } from '../package.json'
77

88
interface DoczillaOptions {
99
baseURL?: string

src/__tests__/page.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, test } from '@jest/globals'
2+
import MockAdapter from 'axios-mock-adapter'
3+
4+
import Doczilla from '../Doczilla'
5+
6+
describe('Page', () => {
7+
8+
const client = new Doczilla('fake-api-token')
9+
// @ts-expect-error private property
10+
const axiosMock = new MockAdapter(client.client)
11+
12+
axiosMock.onAny().reply(200, Buffer.from(''))
13+
14+
test('it encode the page.html option', async () => {
15+
await client.pdf.direct({
16+
page: {
17+
html: '<div>Your first Doczilla PDF</div>'
18+
}
19+
})
20+
21+
expect(axiosMock.history.post.length).toBe(1)
22+
expect(axiosMock.history.post[0].data).toEqual(JSON.stringify({
23+
page: {
24+
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg=='
25+
}
26+
}))
27+
})
28+
29+
})

src/__tests__/pdf.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, test } from '@jest/globals'
2+
import MockAdapter from 'axios-mock-adapter'
3+
4+
import Doczilla from '../Doczilla'
5+
6+
describe('PDF', () => {
7+
8+
const client = new Doczilla('fake-api-token')
9+
// @ts-expect-error private property
10+
const axiosMock = new MockAdapter(client.client)
11+
12+
axiosMock.onAny().reply(200, Buffer.from(''))
13+
14+
test('it should encode the pdf.headerTemplate and pdf.footerTemplate options', async () => {
15+
await client.pdf.direct({
16+
page: {
17+
html: '<div>Your first Doczilla PDF</div>'
18+
},
19+
pdf: {
20+
headerTemplate: '<div>Header template</div>',
21+
footerTemplate: '<div>Footer template</div>'
22+
}
23+
})
24+
25+
expect(axiosMock.history.post.length).toBe(1)
26+
expect(axiosMock.history.post[0].data).toEqual(JSON.stringify({
27+
page: {
28+
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg=='
29+
},
30+
pdf: {
31+
headerTemplate: 'PGRpdj5IZWFkZXIgdGVtcGxhdGU8L2Rpdj4=',
32+
footerTemplate: 'PGRpdj5Gb290ZXIgdGVtcGxhdGU8L2Rpdj4='
33+
}
34+
}))
35+
})
36+
37+
})

src/services/BaseService.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Axios, AxiosHeaders, AxiosRequestConfig, isAxiosError } from 'axios'
22

3+
import type { AsyncPdf, AsyncScreenshot, CreatePdf, CreateScreenshot, SyncPdf, SyncScreenshot } from '../generated'
4+
5+
type PdfRequests = CreatePdf | SyncPdf | AsyncPdf
6+
type ScreenshotRequests = CreateScreenshot | SyncScreenshot | AsyncScreenshot
7+
type RequestBody = PdfRequests | ScreenshotRequests
8+
39
export class BaseService {
410

511
private rateLimit = {
@@ -16,11 +22,11 @@ export class BaseService {
1622

1723
constructor(private readonly client: Axios) {}
1824

19-
protected async post<T>(url: string, requestBody: object, config: AxiosRequestConfig = {}, retries = 1): Promise<T> {
25+
protected async post<T>(url: string, requestBody: RequestBody, config: AxiosRequestConfig = {}, retries = 1): Promise<T> {
2026
try {
2127
await this.waitForRateLimit()
2228

23-
const axiosResponse = await this.client.post<T>(url, requestBody, config)
29+
const axiosResponse = await this.client.post<T>(url, this.encodeRequestBody(requestBody), config)
2430
this.processRateLimit(new AxiosHeaders(axiosResponse.headers))
2531

2632
if (config.responseType === 'arraybuffer') {
@@ -37,6 +43,22 @@ export class BaseService {
3743
}
3844
}
3945

46+
private encodeRequestBody(requestBody: PdfRequests): object {
47+
if (requestBody.page.html) {
48+
requestBody.page.html = this.baseEncodeContent(requestBody.page.html)
49+
}
50+
51+
if (requestBody.pdf?.headerTemplate) {
52+
requestBody.pdf.headerTemplate = this.baseEncodeContent(requestBody.pdf.headerTemplate)
53+
}
54+
55+
if (requestBody.pdf?.footerTemplate) {
56+
requestBody.pdf.footerTemplate = this.baseEncodeContent(requestBody.pdf.footerTemplate)
57+
}
58+
59+
return requestBody
60+
}
61+
4062
private async waitForRateLimit(): Promise<void> {
4163
// Minus 1 to be safe
4264
if ((this.rateLimit.remaining - 1) <= 0) {
@@ -56,4 +78,7 @@ export class BaseService {
5678
}
5779
}
5880

81+
private baseEncodeContent(content: string): string {
82+
return Buffer.from(content).toString('base64')
83+
}
5984
}

src/services/PdfService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AsyncJob, AsyncPdf, CreatePdf, SyncJob, SyncPdf } from '../generated'
2+
23
import { BaseService } from './BaseService'
34

45
export class PdfService extends BaseService {

src/services/ScreenshotService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { BaseService } from './BaseService'
21
import type { AsyncJob, AsyncScreenshot, CreateScreenshot, SyncJob, SyncScreenshot } from '../generated'
32

3+
import { BaseService } from './BaseService'
4+
45
export class ScreenshotService extends BaseService {
56

67
/**

0 commit comments

Comments
 (0)