diff --git a/package.json b/package.json index 3fee9ce..ef22789 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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": "yarn@4.2.2", "engines": { diff --git a/src/Doczilla.ts b/src/Doczilla.ts index b8ac534..69c79ba 100644 --- a/src/Doczilla.ts +++ b/src/Doczilla.ts @@ -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 { @@ -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) { @@ -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() } diff --git a/src/__tests__/template.spec.ts b/src/__tests__/template.spec.ts new file mode 100644 index 0000000..bda54ce --- /dev/null +++ b/src/__tests__/template.spec.ts @@ -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: '
Your first Doczilla PDF
', + htmlTemplate: '
Your first Doczilla {{ type }}
' + }, + pdf: { + headerHtml: '
Header template
', + footerHtml: '
Footer template
' + } + }) + + 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=' + } + })) + }) + +}) \ No newline at end of file diff --git a/src/__tests__/templates.spec.ts b/src/__tests__/templates.spec.ts new file mode 100644 index 0000000..d49af58 --- /dev/null +++ b/src/__tests__/templates.spec.ts @@ -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: '
Your first Doczilla PDF
', + htmlTemplate: '
Your first Doczilla {{ type }}
' + }, + pdf: { + headerHtml: '
Header template
', + footerHtml: '
Footer template
' + } + } + }) + + 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: '
Your first Doczilla PDF
', + htmlTemplate: '
Your first Doczilla {{ type }}
' + }, + pdf: { + headerHtml: '
Header template
', + footerHtml: '
Footer template
' + } + } + }) + + 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=' + } + } + })) + }) + }) + +}) \ No newline at end of file diff --git a/src/generated/index.ts b/src/generated/index.ts index e27c7ec..b6fe427 100644 --- a/src/generated/index.ts +++ b/src/generated/index.ts @@ -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'; @@ -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'; diff --git a/src/generated/models/AsyncFromTemplate.ts b/src/generated/models/AsyncFromTemplate.ts new file mode 100644 index 0000000..d2eb555 --- /dev/null +++ b/src/generated/models/AsyncFromTemplate.ts @@ -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; +}; + diff --git a/src/generated/models/CreateFromTemplate.ts b/src/generated/models/CreateFromTemplate.ts new file mode 100644 index 0000000..00b4032 --- /dev/null +++ b/src/generated/models/CreateFromTemplate.ts @@ -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; +}; + diff --git a/src/generated/models/CreateTemplate.ts b/src/generated/models/CreateTemplate.ts new file mode 100644 index 0000000..7fd6af5 --- /dev/null +++ b/src/generated/models/CreateTemplate.ts @@ -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', + } +} + diff --git a/src/generated/models/ListTemplate.ts b/src/generated/models/ListTemplate.ts new file mode 100644 index 0000000..2132d81 --- /dev/null +++ b/src/generated/models/ListTemplate.ts @@ -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', + } +} + diff --git a/src/generated/models/ListTemplateResponse.ts b/src/generated/models/ListTemplateResponse.ts new file mode 100644 index 0000000..aeaca1a --- /dev/null +++ b/src/generated/models/ListTemplateResponse.ts @@ -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; +}; + diff --git a/src/generated/models/PageOptions.ts b/src/generated/models/PageOptions.ts index 330a9bf..6e75c6d 100644 --- a/src/generated/models/PageOptions.ts +++ b/src/generated/models/PageOptions.ts @@ -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; /** diff --git a/src/generated/models/ScreenshotOptions.ts b/src/generated/models/ScreenshotOptions.ts index b17046d..1e6aa2f 100644 --- a/src/generated/models/ScreenshotOptions.ts +++ b/src/generated/models/ScreenshotOptions.ts @@ -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; /** diff --git a/src/generated/models/SyncFromTemplate.ts b/src/generated/models/SyncFromTemplate.ts new file mode 100644 index 0000000..cab7161 --- /dev/null +++ b/src/generated/models/SyncFromTemplate.ts @@ -0,0 +1,24 @@ +/* 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'; +export type SyncFromTemplate = { + /** + * 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; +}; + diff --git a/src/generated/models/Template.ts b/src/generated/models/Template.ts new file mode 100644 index 0000000..fca3448 --- /dev/null +++ b/src/generated/models/Template.ts @@ -0,0 +1,41 @@ +/* generated using openapi-typescript-codegen -- do not edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import type { TemplateVariables } from './TemplateVariables'; +export type Template = { + /** + * The unique identifier of the template. + */ + id: string; + /** + * The name of the template. + */ + name: string; + /** + * The output format of the template. + */ + output: Template.output; + /** + * The date and time the template was created. + */ + createdAt: string; + /** + * The date and time the template was last updated. + */ + updatedAt: string; + /** + * The variables of the template. + */ + variables: TemplateVariables; +}; +export namespace Template { + /** + * The output format of the template. + */ + export enum output { + PDF = 'PDF', + IMAGE = 'IMAGE', + } +} + diff --git a/src/generated/models/TemplateVariables.ts b/src/generated/models/TemplateVariables.ts new file mode 100644 index 0000000..5910f83 --- /dev/null +++ b/src/generated/models/TemplateVariables.ts @@ -0,0 +1,16 @@ +/* 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 TemplateVariables = { + /** + * Page options, either provide the `url`, `html` or `htmlTemplate` option. + */ + page: PageOptions; + screenshot?: ScreenshotOptions | null; + pdf?: PdfOptions | null; +}; + diff --git a/src/generated/models/UpdateTemplate.ts b/src/generated/models/UpdateTemplate.ts new file mode 100644 index 0000000..ed0e2f9 --- /dev/null +++ b/src/generated/models/UpdateTemplate.ts @@ -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 UpdateTemplate = { + /** + * The name of the template. + */ + name?: string; + /** + * The output format of the template. + */ + output?: UpdateTemplate.output; + /** + * The variables of the template. + */ + variables?: TemplateVariables; +}; +export namespace UpdateTemplate { + /** + * The output format of the template. + */ + export enum output { + PDF = 'PDF', + IMAGE = 'IMAGE', + } +} + diff --git a/src/services/BaseService.ts b/src/services/BaseService.ts index 16ef1d9..54424fc 100644 --- a/src/services/BaseService.ts +++ b/src/services/BaseService.ts @@ -1,10 +1,21 @@ -import { Axios, AxiosHeaders, AxiosRequestConfig, isAxiosError } from 'axios' - -import type { AsyncPdf, AsyncScreenshot, CreatePdf, CreateScreenshot, SyncPdf, SyncScreenshot } from '../generated' +import { Axios, AxiosHeaders, AxiosRequestConfig, isAxiosError, Method } from 'axios' + +import type { + AsyncFromTemplate, + AsyncPdf, + AsyncScreenshot, + CreateFromTemplate, + CreatePdf, + CreateScreenshot, + SyncFromTemplate, + SyncPdf, + SyncScreenshot +} from '../generated' type PdfRequests = CreatePdf | SyncPdf | AsyncPdf type ScreenshotRequests = CreateScreenshot | SyncScreenshot | AsyncScreenshot -type RequestBody = PdfRequests | ScreenshotRequests +type TemplateRequests = CreateFromTemplate | SyncFromTemplate | AsyncFromTemplate +type RequestBody = PdfRequests | ScreenshotRequests | TemplateRequests export class BaseService { @@ -22,11 +33,16 @@ export class BaseService { constructor(private readonly client: Axios) {} - protected async post(url: string, requestBody: RequestBody, config: AxiosRequestConfig = {}, retries = 1): Promise { + protected async request(method: Method, url: string, requestBody?: object, config: AxiosRequestConfig = {}, retries = 1): Promise { try { await this.waitForRateLimit() - const axiosResponse = await this.client.post(url, this.encodeRequestBody(requestBody), config) + const axiosResponse = await this.client.request({ + method, + url: url, + data: requestBody ? this.encodeRequestBody(requestBody) : undefined, + ...config + }) this.processRateLimit(new AxiosHeaders(axiosResponse.headers)) if (config.responseType === 'arraybuffer') { @@ -36,28 +52,32 @@ export class BaseService { return axiosResponse.data } catch (err) { if (isAxiosError(err) && err.status === 429 && retries > 0) { - return this.post(url, requestBody, config, retries - 1) + return this.request(method, url, requestBody, config, retries - 1) } throw err } } - private encodeRequestBody(requestBody: PdfRequests): object { - if (requestBody.page.html) { - requestBody.page.html = this.baseEncodeContent(requestBody.page.html) - } + protected encodeRequestBody(requestBody: object): object { + if (this.isGenerateRequest(requestBody)) { + if (requestBody.page.html) { + requestBody.page.html = this.baseEncodeContent(requestBody.page.html) + } - if (requestBody.page.htmlTemplate) { - requestBody.page.htmlTemplate = this.baseEncodeContent(requestBody.page.htmlTemplate) + if (requestBody.page.htmlTemplate) { + requestBody.page.htmlTemplate = this.baseEncodeContent(requestBody.page.htmlTemplate) + } } - if (requestBody.pdf?.headerHtml) { - requestBody.pdf.headerHtml = this.baseEncodeContent(requestBody.pdf.headerHtml) - } + if (this.isPdfRequest(requestBody)) { + if (requestBody.pdf?.headerHtml) { + requestBody.pdf.headerHtml = this.baseEncodeContent(requestBody.pdf.headerHtml) + } - if (requestBody.pdf?.footerHtml) { - requestBody.pdf.footerHtml = this.baseEncodeContent(requestBody.pdf.footerHtml) + if (requestBody.pdf?.footerHtml) { + requestBody.pdf.footerHtml = this.baseEncodeContent(requestBody.pdf.footerHtml) + } } return requestBody @@ -85,4 +105,13 @@ export class BaseService { private baseEncodeContent(content: string): string { return Buffer.from(content).toString('base64') } + + private isPdfRequest(requestBody: object): requestBody is PdfRequests { + return requestBody && 'pdf' in requestBody + } + + private isGenerateRequest(requestBody: object): requestBody is RequestBody { + return requestBody && 'page' in requestBody + } + } diff --git a/src/services/PdfService.ts b/src/services/PdfService.ts index a4bb6ae..e98f15c 100644 --- a/src/services/PdfService.ts +++ b/src/services/PdfService.ts @@ -11,7 +11,7 @@ export class PdfService extends BaseService { * @throws AxiosError */ public direct(requestBody: CreatePdf): Promise { - return this.post('/v1/pdf', requestBody, { + return this.request('POST', '/v1/pdf', requestBody, { responseType: 'arraybuffer' }) } @@ -23,7 +23,7 @@ export class PdfService extends BaseService { * @throws AxiosError */ public async sync(requestBody: SyncPdf): Promise { - return this.post('/v1/pdf/sync', requestBody) + return this.request('POST', '/v1/pdf/sync', requestBody) } /** @@ -33,7 +33,7 @@ export class PdfService extends BaseService { * @throws AxiosError */ public async(requestBody: AsyncPdf): Promise { - return this.post('/v1/pdf/async', requestBody) + return this.request('POST', '/v1/pdf/async', requestBody) } } diff --git a/src/services/ScreenshotService.ts b/src/services/ScreenshotService.ts index 338216a..dffd1c6 100644 --- a/src/services/ScreenshotService.ts +++ b/src/services/ScreenshotService.ts @@ -11,7 +11,7 @@ export class ScreenshotService extends BaseService { * @throws AxiosError */ public direct(requestBody: CreateScreenshot): Promise { - return this.post('/v1/screenshot', requestBody, { + return this.request('POST', '/v1/screenshot', requestBody, { responseType: 'arraybuffer' }) } @@ -23,7 +23,7 @@ export class ScreenshotService extends BaseService { * @throws AxiosError */ public sync(requestBody: SyncScreenshot): Promise { - return this.post('/v1/screenshot/sync', requestBody) + return this.request('POST', '/v1/screenshot/sync', requestBody) } /** @@ -33,7 +33,7 @@ export class ScreenshotService extends BaseService { * @throws AxiosError */ public async(requestBody: AsyncScreenshot): Promise { - return this.post('/v1/screenshot/async', requestBody) + return this.request('POST', '/v1/screenshot/async', requestBody) } } diff --git a/src/services/TemplateService.ts b/src/services/TemplateService.ts new file mode 100644 index 0000000..6184faf --- /dev/null +++ b/src/services/TemplateService.ts @@ -0,0 +1,111 @@ +import type { + AsyncFromTemplate, + AsyncJob, + CreateFromTemplate, + CreateTemplate, + ListTemplateResponse, + SyncFromTemplate, + SyncJob, + Template, + UpdateTemplate +} from '../generated' + +import { BaseService } from './BaseService' + +export class TemplateService extends BaseService { + + /** + * Create the template and directly return the raw result. + * @param id + * @param requestBody + * @returns Buffer + * @throws AxiosError + */ + public direct(id: string, requestBody: CreateFromTemplate): Promise { + return this.request('POST', `/v1/template/${id}`, requestBody, { + responseType: 'arraybuffer' + }) + } + + /** + * Create the template and write the result directly in your bucket. + * @param id + * @param requestBody + * @returns SyncJob + * @throws AxiosError + */ + public async sync(id: string, requestBody: SyncFromTemplate): Promise { + return this.request('POST', `/v1/template/${id}/sync`, requestBody) + } + + /** + * Queue the creation of the template and call the webhook with the result. + * @param id + * @param requestBody + * @returns AsyncJob + * @throws AxiosError + */ + public async(id: string, requestBody: AsyncFromTemplate): Promise { + return this.request('POST', `/v1/template/${id}/async`, requestBody) + } + + /** + * List all available templates. + * @param params + * @returns ListTemplateResponse + * @throws AxiosError + */ + public list(params: { page?: number } = {}): Promise { + return this.request('GET', '/v1/templates', undefined, { + params + }) + } + + /** + * Creates a new template with the provided request body. + * @param requestBody + * @returns Template + * @throws AxiosError + */ + public create(requestBody: CreateTemplate): Promise