Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { CreateUrlImageRequest } from '@html-css-to-image/client';

const request = new CreateUrlImageRequest({
url: 'https://example.com',
css: '.cookie-banner { display: none; }',
full_screen: true
});

Expand Down Expand Up @@ -83,7 +84,7 @@ These methods are handy when you have a lot of content that may never be rendere
### Signed Template URLs

```typescript
const signedUrl = client.createTemplatedImageUrl('your_template_id', {
const signedUrl = client.generateTemplatedImageUrl('your_template_id', {
title: 'Dynamic Title'
});

Expand All @@ -102,11 +103,11 @@ const this_item = {
id: 123,
updated_at: new Date(2025, 7, 15)
};
const url_request = new CreateUrlImageRequest({
url: `https://website.com/_social/${this_tem_id}?updated_at=${updated_at.valueOf()}`,
viewport_width: 600,
const url_request = new CreateUrlImageRequest({
url: `https://website.com/_social/${this_item.id}?updated_at=${this_item.updated_at.valueOf()}`,
viewport_width: 600,
viewport_height: 200});
const signedUrl = client.generateCreateAndRenderUr(url_request);
const signedUrl = client.generateCreateAndRenderUrl(url_request);

// now it's safe to use signedUrl on the frontend in a meta or img tag, without exposing your API key, and it will be kept updated as the updated_at changes.
```
Expand All @@ -116,13 +117,15 @@ const signedUrl = client.generateCreateAndRenderUr(url_request);
If you have created a template for your account, you can generate an image by passing your template ID and the data for your variables.

```typescript
const result = await client.createImage({
import { CreateTemplatedImageRequest } from '@html-css-to-image/client';

const result = await client.createImage(new CreateTemplatedImageRequest({
template_id: 'your_template_id',
template_values: {
title: 'Hello from Template',
subtitle: 'This is a dynamic value'
}
});
}));
```

### PDF Generation
Expand Down Expand Up @@ -200,4 +203,4 @@ const client = new HtmlCssToImageClient(apiId, apiKey, fetch);
> Check out the [HTML/CSS To Image Docs](https://docs.htmlcsstoimage.com) for more details on the API's capabilities.

> [!TIP]
> Get started for free at [htmlcsstoimage.com](https://htmlcsstoimage.com).
> Get started for free at [htmlcsstoimage.com](https://htmlcsstoimage.com).
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"name": "@html-css-to-image/client",
"version": "0.2.0",
"version": "0.3.0",
"type": "module",
"description": "Lightweight TypeScript client for the HTML/CSS to Image API. Generate images from HTML/CSS, URLs, or templates in your TypeScript or JavaScript projects.",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/htmlcsstoimage/ts-client.git"
Expand All @@ -18,7 +24,8 @@
],
"scripts": {
"test": "tsx --test tests/*.test.ts",
"build": "tsc",
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
"build": "npm run clean && tsc",
"prepublishOnly": "npm run build"
},
"keywords": [
Expand Down
54 changes: 48 additions & 6 deletions src/HtmlCssToImageClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import {IHtmlCssToImageClient} from "./IHtmlCssToImageClient.js";
import {CreateHtmlCssImageRequest, CreateTemplatedImageRequest, CreateUrlImageRequest, PDFOptions, PdfValueInput} from "./types/request.js";
import {CreateImageBatchResponse, CreateImageBatchSuccessResponse, CreateImageResponse} from "./types/response.js";
import {InternalCreateHtmlCssImageRequest, InternalCreateHtmlCssImageRequestWithOptionalHtml, InternalCreateUrlImageRequest, InternalCreateUrlImageRequestWithOptionalUrl, InternalPDFOptions} from "./types/internals.js";
import type {IHtmlCssToImageClient} from "./IHtmlCssToImageClient.js";
import {CreateTemplatedImageRequest} from "./types/request.js";
import type {BaseCreateImageRequest, CreateHtmlCssImageRequest, CreateUrlImageRequest, PDFOptions, PdfValueInput} from "./types/request.js";
import type {CreateImageBatchResponse, CreateImageBatchSuccessResponse, CreateImageResponse} from "./types/response.js";
import * as crypto from 'node:crypto';

export type FetchFunction = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;

interface InternalPDFOptions {
print_background?: boolean;
scale?: number;
margins?: string[];
page_height?: string;
page_width?: string;
}

type InternalBaseCreateRequest = Omit<BaseCreateImageRequest, 'pdf_options'> & {
pdf_options?: InternalPDFOptions;
};

type InternalCreateHtmlCssImageRequest = InternalBaseCreateRequest & {
html: string;
css?: string;
google_fonts?: string;
};

type InternalCreateHtmlCssImageRequestWithOptionalHtml = InternalBaseCreateRequest & {
html?: string;
css?: string;
google_fonts?: string;
};

type InternalCreateUrlImageRequest = InternalBaseCreateRequest & {
url: string;
css?: string;
full_screen?: boolean;
block_consent_banners?: boolean;
};

type InternalCreateUrlImageRequestWithOptionalUrl = InternalBaseCreateRequest & {
url?: string;
css?: string;
full_screen?: boolean;
block_consent_banners?: boolean;
};

export class HtmlCssToImageClient implements IHtmlCssToImageClient {

private readonly apiId: string;
Expand Down Expand Up @@ -65,7 +103,10 @@ export class HtmlCssToImageClient implements IHtmlCssToImageClient {
case 'url':
return this.mapUrlToInternalRequest(request, in_batch);
case 'templated':
return request;
{
const {__type, ...templatedRequest} = request;
return templatedRequest;
}
default:
throw new Error("Unsupported request type");
}
Expand Down Expand Up @@ -232,6 +273,7 @@ export class HtmlCssToImageClient implements IHtmlCssToImageClient {
}
return new_pdf_options;
}
return undefined;
}


Expand All @@ -251,4 +293,4 @@ export class HtmlCssToImageClient implements IHtmlCssToImageClient {
}


}
}
6 changes: 3 additions & 3 deletions src/IHtmlCssToImageClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {CreateHtmlCssImageRequest, CreateTemplatedImageRequest, CreateUrlImageRequest} from "./types/request.js";
import {CreateImageBatchResponse, CreateImageErrorResponse, CreateImageResponse, CreateImageSuccessResponse} from "./types/response.js";
import type {CreateHtmlCssImageRequest, CreateTemplatedImageRequest, CreateUrlImageRequest} from "./types/request.js";
import type {CreateImageBatchResponse, CreateImageResponse} from "./types/response.js";

export interface IHtmlCssToImageClient {
createImage(request: CreateHtmlCssImageRequest|CreateUrlImageRequest|CreateTemplatedImageRequest) : Promise<CreateImageResponse>;
createImageBatch<T extends CreateHtmlCssImageRequest|CreateUrlImageRequest>(variations: T[],default_options?:T) : Promise<CreateImageBatchResponse>;
generateCreateAndRenderUrl(request: CreateUrlImageRequest): string;
generateTemplatedImageUrl<T extends Record<string,any>>(template_id: string, template_values: T, template_version?: number):string;
generateTemplatedImageUrl(request: CreateTemplatedImageRequest):string;
}
}
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
export {
BaseCreateImageRequest,
CreateHtmlCssImageRequest,
CreateTemplatedImageRequest,
CreateUrlImageRequest,
PDFOptions,
PDFOptions
} from './types/request.js';

export type {
ColorSchemeType,
MediaType,
PdfMargins,
PdfValueInput,
PdfValueWithUnits,
PdfUnit
} from './types/request.js';

export {
export type {
CreateImageBatchResponse,
CreateImageBatchSuccessResponse,
CreateImageErrorResponse,
CreateImageResponse,
CreateImageBatchResponse
CreateImageSuccessResponse,
ValidationError
} from './types/response.js';
export { HtmlCssToImageClient } from './HtmlCssToImageClient.js';
export { IHtmlCssToImageClient } from './IHtmlCssToImageClient.js';
export type { FetchFunction } from './HtmlCssToImageClient.js';
export type { IHtmlCssToImageClient } from './IHtmlCssToImageClient.js';
156 changes: 0 additions & 156 deletions src/types/internals.ts

This file was deleted.

Loading
Loading