Skip to content

Commit

Permalink
Merge pull request #28 from Doczilla-APP/templating
Browse files Browse the repository at this point in the history
Templating
  • Loading branch information
TriPSs authored Dec 8, 2024
2 parents 801edd1 + 9cdb6ee commit 46b5283
Show file tree
Hide file tree
Showing 21 changed files with 1,747 additions and 668 deletions.
25 changes: 12 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"sideEffects": false,
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/types/index.d.ts"
"import": "./dist/index.mjs"
},
"./package.json": "./package.json"
},
Expand All @@ -47,24 +47,23 @@
"test": "jest"
},
"dependencies": {
"axios": "^1.7.5"
"axios": "^1.7.7"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@swc/core": "^1.7.14",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"axios-mock-adapter": "^1.22.0",
"eslint": "9.8.0",
"eslint-plugin-import": "2.29.1",
"@types/jest": "^29.5.14",
"@types/node": "^22.9.1",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"axios-mock-adapter": "^2.1.0",
"eslint": "9.15.0",
"eslint-plugin-import": "2.31.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"jest": "^29.7.0",
"openapi-typescript-codegen": "^0.29.0",
"ts-jest": "^29.2.5",
"tsup": "^8.2.4",
"typescript": "^5.5"
"tsup": "^8.3.5",
"typescript": "^5.6"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions src/Doczilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios, { Axios } from 'axios'
import { version } from '../package.json'
import { PdfService } from './services/PdfService'
import { ScreenshotService } from './services/ScreenshotService'
import { TemplateService } from './services/TemplateService'
import { WebhookService } from './services/WebhookService'

interface DoczillaOptions {
Expand All @@ -16,6 +17,7 @@ export default class Doczilla {
public readonly pdf: PdfService
public readonly screenshot: ScreenshotService
public readonly webhook: WebhookService
public readonly template: TemplateService

constructor(token: string, options: DoczillaOptions = {}) {
if (!token) {
Expand All @@ -32,6 +34,7 @@ export default class Doczilla {

this.pdf = new PdfService(this.client)
this.screenshot = new ScreenshotService(this.client)
this.template = new TemplateService(this.client)
this.webhook = new WebhookService()
}

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

import Doczilla from '../Doczilla'

describe('Template', () => {

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 page.html, page.htmlTemplate, pdf.headerHtml and pdf.footerHtml options', async () => {
await client.template.direct('fake-template-id', {
page: {
html: '<div>Your first Doczilla PDF</div>',
htmlTemplate: '<div>Your first Doczilla {{ type }}</div>'
},
pdf: {
headerHtml: '<div>Header template</div>',
footerHtml: '<div>Footer template</div>'
}
})

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

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

import Doczilla from '../Doczilla'
import { CreateTemplate } from '../generated'

describe('Templates', () => {

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

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

describe('create', () => {
test('it should encode the page.html, page.htmlTemplate, pdf.headerHtml and pdf.footerHtml options', async () => {
await client.template.create({
name: 'fake template',
output: CreateTemplate.output.PDF,
variables: {
page: {
html: '<div>Your first Doczilla PDF</div>',
htmlTemplate: '<div>Your first Doczilla {{ type }}</div>'
},
pdf: {
headerHtml: '<div>Header template</div>',
footerHtml: '<div>Footer template</div>'
}
}
})

expect(axiosMock.history.post.length).toBe(1)
expect(axiosMock.history.post[0].data).toEqual(JSON.stringify({
name: 'fake template',
output: CreateTemplate.output.PDF,
variables: {
page: {
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg==',
htmlTemplate: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIHt7IHR5cGUgfX08L2Rpdj4='
},
pdf: {
headerHtml: 'PGRpdj5IZWFkZXIgdGVtcGxhdGU8L2Rpdj4=',
footerHtml: 'PGRpdj5Gb290ZXIgdGVtcGxhdGU8L2Rpdj4='
}
}
}))
})
})

describe('update', () => {
test('it should encode the page.html, page.htmlTemplate, pdf.headerHtml and pdf.footerHtml options', async () => {
await client.template.update('fake-template-id', {
variables: {
page: {
html: '<div>Your first Doczilla PDF</div>',
htmlTemplate: '<div>Your first Doczilla {{ type }}</div>'
},
pdf: {
headerHtml: '<div>Header template</div>',
footerHtml: '<div>Footer template</div>'
}
}
})

expect(axiosMock.history.post.length).toBe(2)
expect(axiosMock.history.post[1].data).toEqual(JSON.stringify({
variables: {
page: {
html: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIFBERjwvZGl2Pg==',
htmlTemplate: 'PGRpdj5Zb3VyIGZpcnN0IERvY3ppbGxhIHt7IHR5cGUgfX08L2Rpdj4='
},
pdf: {
headerHtml: 'PGRpdj5IZWFkZXIgdGVtcGxhdGU8L2Rpdj4=',
footerHtml: 'PGRpdj5Gb290ZXIgdGVtcGxhdGU8L2Rpdj4='
}
}
}))
})
})

})
9 changes: 9 additions & 0 deletions src/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
/* tslint:disable */
/* eslint-disable */

export type { AsyncFromTemplate } from './models/AsyncFromTemplate';
export { AsyncJob } from './models/AsyncJob';
export type { AsyncPdf } from './models/AsyncPdf';
export type { AsyncScreenshot } from './models/AsyncScreenshot';
export type { BadRequestResponse } from './models/BadRequestResponse';
export type { CreateFromTemplate } from './models/CreateFromTemplate';
export type { CreatePdf } from './models/CreatePdf';
export type { CreateScreenshot } from './models/CreateScreenshot';
export { CreateTemplate } from './models/CreateTemplate';
export type { ForbiddenResponse } from './models/ForbiddenResponse';
export type { InternalServerErrorResponse } from './models/InternalServerErrorResponse';
export { ListTemplate } from './models/ListTemplate';
export type { ListTemplateResponse } from './models/ListTemplateResponse';
export type { PageAuthentication } from './models/PageAuthentication';
export { PageCookie } from './models/PageCookie';
export { PageOptions } from './models/PageOptions';
Expand All @@ -27,10 +32,14 @@ export { ScreenshotOverlay } from './models/ScreenshotOverlay';
export type { ScreenshotViewport } from './models/ScreenshotViewport';
export type { StorageOptions } from './models/StorageOptions';
export type { SubscriptionLimitReachedResponse } from './models/SubscriptionLimitReachedResponse';
export type { SyncFromTemplate } from './models/SyncFromTemplate';
export { SyncJob } from './models/SyncJob';
export type { SyncPdf } from './models/SyncPdf';
export type { SyncScreenshot } from './models/SyncScreenshot';
export { Template } from './models/Template';
export type { TemplateVariables } from './models/TemplateVariables';
export type { TooManyRequestsResponse } from './models/TooManyRequestsResponse';
export type { UnauthorizedResponse } from './models/UnauthorizedResponse';
export { UpdateTemplate } from './models/UpdateTemplate';
export { WebhookEvent } from './models/WebhookEvent';
export { WebhookOptions } from './models/WebhookOptions';
29 changes: 29 additions & 0 deletions src/generated/models/AsyncFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* generated using openapi-typescript-codegen -- do not edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { PageOptions } from './PageOptions';
import type { PdfOptions } from './PdfOptions';
import type { ScreenshotOptions } from './ScreenshotOptions';
import type { StorageOptions } from './StorageOptions';
import type { WebhookOptions } from './WebhookOptions';
export type AsyncFromTemplate = {
/**
* Page options.
*/
page?: PageOptions | null;
/**
* Screenshot options, only used if Template's output is "SCREENSHOT".
*/
screenshot?: ScreenshotOptions | null;
/**
* Pdf options, only used if Template's output is "PDF".
*/
pdf?: PdfOptions | null;
storage?: StorageOptions | null;
/**
* Webhook to call when screenshot is generated.
*/
webhook: WebhookOptions;
};

22 changes: 22 additions & 0 deletions src/generated/models/CreateFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* generated using openapi-typescript-codegen -- do not edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { PageOptions } from './PageOptions';
import type { PdfOptions } from './PdfOptions';
import type { ScreenshotOptions } from './ScreenshotOptions';
export type CreateFromTemplate = {
/**
* Page options.
*/
page?: PageOptions | null;
/**
* Screenshot options, only used if Template's output is "SCREENSHOT".
*/
screenshot?: ScreenshotOptions | null;
/**
* Pdf options, only used if Template's output is "PDF".
*/
pdf?: PdfOptions | null;
};

29 changes: 29 additions & 0 deletions src/generated/models/CreateTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* generated using openapi-typescript-codegen -- do not edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { TemplateVariables } from './TemplateVariables';
export type CreateTemplate = {
/**
* The name of the template.
*/
name: string;
/**
* The output format of the template.
*/
output: CreateTemplate.output;
/**
* The variables of the template.
*/
variables: TemplateVariables;
};
export namespace CreateTemplate {
/**
* The output format of the template.
*/
export enum output {
PDF = 'PDF',
IMAGE = 'IMAGE',
}
}

36 changes: 36 additions & 0 deletions src/generated/models/ListTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* generated using openapi-typescript-codegen -- do not edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type ListTemplate = {
/**
* The unique identifier of the template.
*/
id: string;
/**
* The name of the template.
*/
name: string;
/**
* The output format of the template.
*/
output: ListTemplate.output;
/**
* The date and time the template was created.
*/
createdAt: string;
/**
* The date and time the template was last updated.
*/
updatedAt: string;
};
export namespace ListTemplate {
/**
* The output format of the template.
*/
export enum output {
PDF = 'PDF',
IMAGE = 'IMAGE',
}
}

20 changes: 20 additions & 0 deletions src/generated/models/ListTemplateResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* generated using openapi-typescript-codegen -- do not edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { ListTemplate } from './ListTemplate';
export type ListTemplateResponse = {
/**
* Is true if there is a next page.
*/
hasNextPage: boolean;
/**
* Is true if there is a previous page.
*/
hasPreviousPage: boolean;
/**
* The list of templates.
*/
data: Array<ListTemplate>;
};

2 changes: 1 addition & 1 deletion src/generated/models/PageOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type PageOptions = {
*/
html?: string | null;
/**
* HTML template to render. Needs to be base64 encoded!
* HTML template to render, uses [Handlebars](https://handlebarsjs.com/) to render your HTML together with the provided `templateData`. Needs to be base64 encoded!
*/
htmlTemplate?: string | null;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/generated/models/ScreenshotOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type ScreenshotOptions = {
*/
clip?: ScreenshotClip | null;
/**
* It captures the DOM element matching the given CSS [selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors). This will overwrite the `clip` property and set `fullPage` to `false`.
* It captures the DOM element matching the given CSS [selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors). This will overwrite the `clip` property and set `fullPage` to `false`. Will wait for the element to become visible for a maximum of 15 seconds.
*/
element?: string | null;
/**
Expand Down
Loading

0 comments on commit 46b5283

Please sign in to comment.