Skip to content

Commit

Permalink
feat: Automatically encode page.html, pdf.headerTemplate and `pdf…
Browse files Browse the repository at this point in the history
….footerTemplate`
  • Loading branch information
TriPSs committed Dec 23, 2023
1 parent ae813d1 commit 3a62a32
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Doczilla.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios, { Axios } from 'axios'

import { version } from '../package.json'
import { PdfService } from './services/PdfService'
import { ScreenshotService } from './services/ScreenshotService'
import { WebhookService } from './services/WebhookService'
import { version } from '../package.json'

interface DoczillaOptions {
baseURL?: string
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/page.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, test } from '@jest/globals'
import MockAdapter from 'axios-mock-adapter'

import Doczilla from '../Doczilla'

describe('Page', () => {

const client = new Doczilla('fake-api-token')
// @ts-expect-error private property
const axiosMock = new MockAdapter(client.client)

axiosMock.onAny().reply(200, Buffer.from(''))

test('it encode the page.html option', async () => {
await client.pdf.direct({
page: {
html: '<div>Your first Doczilla PDF</div>'
}
})

expect(axiosMock.history.post.length).toBe(1)
expect(axiosMock.history.post[0].data).toEqual(JSON.stringify({
page: {
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg=='
}
}))
})

})
37 changes: 37 additions & 0 deletions src/__tests__/pdf.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from '@jest/globals'
import MockAdapter from 'axios-mock-adapter'

import Doczilla from '../Doczilla'

describe('PDF', () => {

const client = new Doczilla('fake-api-token')
// @ts-expect-error private property
const axiosMock = new MockAdapter(client.client)

axiosMock.onAny().reply(200, Buffer.from(''))

test('it should encode the pdf.headerTemplate and pdf.footerTemplate options', async () => {
await client.pdf.direct({
page: {
html: '<div>Your first Doczilla PDF</div>'
},
pdf: {
headerTemplate: '<div>Header template</div>',
footerTemplate: '<div>Footer template</div>'
}
})

expect(axiosMock.history.post.length).toBe(1)
expect(axiosMock.history.post[0].data).toEqual(JSON.stringify({
page: {
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg=='
},
pdf: {
headerTemplate: 'PGRpdj5IZWFkZXIgdGVtcGxhdGU8L2Rpdj4=',
footerTemplate: 'PGRpdj5Gb290ZXIgdGVtcGxhdGU8L2Rpdj4='
}
}))
})

})
29 changes: 27 additions & 2 deletions src/services/BaseService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Axios, AxiosHeaders, AxiosRequestConfig, isAxiosError } from 'axios'

import type { AsyncPdf, AsyncScreenshot, CreatePdf, CreateScreenshot, SyncPdf, SyncScreenshot } from '../generated'

type PdfRequests = CreatePdf | SyncPdf | AsyncPdf
type ScreenshotRequests = CreateScreenshot | SyncScreenshot | AsyncScreenshot
type RequestBody = PdfRequests | ScreenshotRequests

export class BaseService {

private rateLimit = {
Expand All @@ -16,11 +22,11 @@ export class BaseService {

constructor(private readonly client: Axios) {}

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

const axiosResponse = await this.client.post<T>(url, requestBody, config)
const axiosResponse = await this.client.post<T>(url, this.encodeRequestBody(requestBody), config)
this.processRateLimit(new AxiosHeaders(axiosResponse.headers))

if (config.responseType === 'arraybuffer') {
Expand All @@ -37,6 +43,22 @@ export class BaseService {
}
}

private encodeRequestBody(requestBody: PdfRequests): object {
if (requestBody.page.html) {
requestBody.page.html = this.baseEncodeContent(requestBody.page.html)
}

if (requestBody.pdf?.headerTemplate) {
requestBody.pdf.headerTemplate = this.baseEncodeContent(requestBody.pdf.headerTemplate)
}

if (requestBody.pdf?.footerTemplate) {
requestBody.pdf.footerTemplate = this.baseEncodeContent(requestBody.pdf.footerTemplate)
}

return requestBody
}

private async waitForRateLimit(): Promise<void> {
// Minus 1 to be safe
if ((this.rateLimit.remaining - 1) <= 0) {
Expand All @@ -56,4 +78,7 @@ export class BaseService {
}
}

private baseEncodeContent(content: string): string {
return Buffer.from(content).toString('base64')
}
}
1 change: 1 addition & 0 deletions src/services/PdfService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AsyncJob, AsyncPdf, CreatePdf, SyncJob, SyncPdf } from '../generated'

import { BaseService } from './BaseService'

export class PdfService extends BaseService {
Expand Down
3 changes: 2 additions & 1 deletion src/services/ScreenshotService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseService } from './BaseService'
import type { AsyncJob, AsyncScreenshot, CreateScreenshot, SyncJob, SyncScreenshot } from '../generated'

import { BaseService } from './BaseService'

export class ScreenshotService extends BaseService {

/**
Expand Down

0 comments on commit 3a62a32

Please sign in to comment.