@ogify/core 0.1.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -86
- package/dist/index.d.mts +77 -72
- package/dist/index.d.ts +77 -72
- package/dist/index.js +318 -51
- package/dist/index.mjs +295 -51
- package/package.json +15 -26
- package/LICENSE +0 -21
package/dist/index.d.mts
CHANGED
|
@@ -22,6 +22,30 @@ import { FontWeight, FontStyle } from 'satori';
|
|
|
22
22
|
* - `openmoji`: Open-source emoji with outlined style
|
|
23
23
|
*/
|
|
24
24
|
type OgEmojiProvider = 'twemoji' | 'fluent' | 'fluentFlat' | 'noto' | 'blobmoji' | 'openmoji';
|
|
25
|
+
/**
|
|
26
|
+
* Cache configuration for fonts and icons.
|
|
27
|
+
*
|
|
28
|
+
* Supports two caching strategies:
|
|
29
|
+
* - Memory: Fast in-memory cache with LRU eviction
|
|
30
|
+
* - Filesystem: Persistent cache stored on disk
|
|
31
|
+
*/
|
|
32
|
+
type OgCacheConfig = {
|
|
33
|
+
/** Memory-based caching strategy */
|
|
34
|
+
type: 'memory';
|
|
35
|
+
/** Time-to-live in milliseconds (default: 3600000 = 1 hour) */
|
|
36
|
+
ttl?: number;
|
|
37
|
+
/** Maximum number of items to cache (default: 100) */
|
|
38
|
+
max?: number;
|
|
39
|
+
} | {
|
|
40
|
+
/** Filesystem-based caching strategy */
|
|
41
|
+
type: 'filesystem';
|
|
42
|
+
/** Directory to store cache files (default: '.ogify-cache') */
|
|
43
|
+
dir?: string;
|
|
44
|
+
/** Time-to-live in milliseconds (default: 3600000 = 1 hour) */
|
|
45
|
+
ttl?: number;
|
|
46
|
+
/** Maximum number of items to cache (default: 100) */
|
|
47
|
+
max?: number;
|
|
48
|
+
};
|
|
25
49
|
/**
|
|
26
50
|
* Supported font file formats.
|
|
27
51
|
*
|
|
@@ -90,12 +114,15 @@ type OgFontConfig = {
|
|
|
90
114
|
* `;
|
|
91
115
|
*/
|
|
92
116
|
type OgTemplateOptions = {
|
|
93
|
-
/**
|
|
94
|
-
|
|
117
|
+
/** Custom fonts to use in this template */
|
|
118
|
+
fonts?: OgFontConfig[];
|
|
119
|
+
/** Optional emoji provider to use in this template */
|
|
120
|
+
emojiProvider?: OgEmojiProvider;
|
|
95
121
|
/** Optional custom width in pixels (default: 1200) */
|
|
96
122
|
width?: number;
|
|
97
123
|
/** Optional custom height in pixels (default: 630) */
|
|
98
124
|
height?: number;
|
|
125
|
+
isRTL?: boolean;
|
|
99
126
|
};
|
|
100
127
|
/**
|
|
101
128
|
* Key-value pairs representing dynamic template parameters.
|
|
@@ -103,6 +130,7 @@ type OgTemplateOptions = {
|
|
|
103
130
|
* Parameters are the data that gets injected into templates to create
|
|
104
131
|
* personalized OG images. Values can be:
|
|
105
132
|
* - Strings: Text content (titles, descriptions, names)
|
|
133
|
+
* - String Arrays: Lists of tags, categories
|
|
106
134
|
* - Numbers: Counts, dates, metrics
|
|
107
135
|
* - Booleans: Flags, toggles, states
|
|
108
136
|
*
|
|
@@ -114,7 +142,7 @@ type OgTemplateOptions = {
|
|
|
114
142
|
* published: true
|
|
115
143
|
* };
|
|
116
144
|
*/
|
|
117
|
-
type OgTemplateParams = Record<string, string | number | boolean>;
|
|
145
|
+
type OgTemplateParams = Record<string, string | string[] | number | boolean>;
|
|
118
146
|
/**
|
|
119
147
|
* Complete definition of an Open Graph image template.
|
|
120
148
|
*
|
|
@@ -124,20 +152,16 @@ type OgTemplateParams = Record<string, string | number | boolean>;
|
|
|
124
152
|
* - What fonts to use
|
|
125
153
|
* - Metadata for identification
|
|
126
154
|
*/
|
|
127
|
-
type OgTemplate = {
|
|
128
|
-
/** Unique identifier for this template (e.g., 'blog-post', 'product-card') */
|
|
129
|
-
id: string;
|
|
130
|
-
/** Human-readable name for display purposes */
|
|
131
|
-
name: string;
|
|
132
|
-
/** Brief description of what this template is used for */
|
|
133
|
-
description: string;
|
|
155
|
+
type OgTemplate<TParams = OgTemplateParams> = {
|
|
134
156
|
/**
|
|
135
157
|
* Function that generates HTML markup from template parameters.
|
|
136
158
|
*
|
|
137
159
|
* The HTML should use inline styles (Tailwind-like utilities are supported).
|
|
138
160
|
* Flexbox and basic CSS properties are supported by Satori.
|
|
139
161
|
*/
|
|
140
|
-
renderer: (props: OgTemplateOptions
|
|
162
|
+
renderer: (props: OgTemplateOptions & {
|
|
163
|
+
params: TParams;
|
|
164
|
+
}) => string;
|
|
141
165
|
/**
|
|
142
166
|
* Custom fonts to use in this template.
|
|
143
167
|
*
|
|
@@ -159,16 +183,29 @@ type OgTemplate = {
|
|
|
159
183
|
* The handler manages a registry of templates and provides a unified
|
|
160
184
|
* interface for rendering them to images.
|
|
161
185
|
*/
|
|
162
|
-
type OgTemplateRenderer = {
|
|
163
|
-
/**
|
|
164
|
-
templates:
|
|
186
|
+
type OgTemplateRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>> = {
|
|
187
|
+
/** Map of template definitions to register, keyed by template ID */
|
|
188
|
+
templates: {
|
|
189
|
+
[K in keyof TMap]: OgTemplate<TMap[K]>;
|
|
190
|
+
};
|
|
165
191
|
/**
|
|
166
|
-
*
|
|
192
|
+
* Shared parameter values applied to all templates.
|
|
167
193
|
*
|
|
168
194
|
* These values are merged with user-provided parameters,
|
|
169
195
|
* with user values taking precedence.
|
|
170
196
|
*/
|
|
171
|
-
|
|
197
|
+
sharedParams?: Partial<TMap[keyof TMap]> | (() => Promise<Partial<TMap[keyof TMap]>>);
|
|
198
|
+
/**
|
|
199
|
+
* Cache configuration for fonts and icons.
|
|
200
|
+
*
|
|
201
|
+
* When provided, enables LRU caching to improve performance by:
|
|
202
|
+
* - Reducing redundant network requests for fonts and emojis
|
|
203
|
+
* - Supporting memory or filesystem-based caching strategies
|
|
204
|
+
* - Configurable TTL and maximum cache size
|
|
205
|
+
*
|
|
206
|
+
* If not provided, falls back to simple in-memory caching.
|
|
207
|
+
*/
|
|
208
|
+
cache?: OgCacheConfig;
|
|
172
209
|
/**
|
|
173
210
|
* Hook called before rendering.
|
|
174
211
|
*
|
|
@@ -178,7 +215,7 @@ type OgTemplateRenderer = {
|
|
|
178
215
|
* - Authentication checks
|
|
179
216
|
* - Rate limiting
|
|
180
217
|
*/
|
|
181
|
-
beforeRender?: (templateId:
|
|
218
|
+
beforeRender?: (templateId: keyof TMap, params: TMap[keyof TMap]) => void | Promise<void>;
|
|
182
219
|
/**
|
|
183
220
|
* Hook called after rendering.
|
|
184
221
|
*
|
|
@@ -188,7 +225,7 @@ type OgTemplateRenderer = {
|
|
|
188
225
|
* - Sending notifications
|
|
189
226
|
* - Updating metrics
|
|
190
227
|
*/
|
|
191
|
-
afterRender?: (templateId:
|
|
228
|
+
afterRender?: (templateId: keyof TMap, params: TMap[keyof TMap], imageBuffer: Buffer) => void | Promise<void>;
|
|
192
229
|
};
|
|
193
230
|
|
|
194
231
|
/**
|
|
@@ -217,7 +254,7 @@ type OgTemplateRenderer = {
|
|
|
217
254
|
* @returns true if validation passes
|
|
218
255
|
* @throws Error if any required field is missing or invalid
|
|
219
256
|
*/
|
|
220
|
-
declare function validateTemplate(config: OgTemplate): boolean;
|
|
257
|
+
declare function validateTemplate<TemplateParams>(config: OgTemplate<TemplateParams>): boolean;
|
|
221
258
|
/**
|
|
222
259
|
* Defines and validates a new OG template.
|
|
223
260
|
*
|
|
@@ -229,17 +266,19 @@ declare function validateTemplate(config: OgTemplate): boolean;
|
|
|
229
266
|
* - Type safety ensures correct template structure
|
|
230
267
|
* - Clear intent in code (self-documenting)
|
|
231
268
|
*
|
|
232
|
-
* @param config - Complete template configuration including
|
|
269
|
+
* @param config - Complete template configuration including renderer function and fonts
|
|
233
270
|
* @returns The validated template configuration
|
|
234
271
|
* @throws Error if validation fails
|
|
235
272
|
*/
|
|
236
|
-
declare function defineTemplate(config: OgTemplate): OgTemplate
|
|
273
|
+
declare function defineTemplate<TemplateParams>(config: OgTemplate<TemplateParams>): OgTemplate<TemplateParams>;
|
|
237
274
|
/**
|
|
238
275
|
* Template Handler class for managing multiple templates and rendering them to images.
|
|
239
276
|
*/
|
|
240
|
-
declare class TemplateRenderer {
|
|
277
|
+
declare class TemplateRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>> {
|
|
241
278
|
/** Configuration including global settings and lifecycle hooks */
|
|
242
279
|
private config;
|
|
280
|
+
/** Cache manager for fonts and icons */
|
|
281
|
+
private cacheManager;
|
|
243
282
|
/**
|
|
244
283
|
* Internal registry mapping template IDs to template definitions.
|
|
245
284
|
*
|
|
@@ -254,14 +293,14 @@ declare class TemplateRenderer {
|
|
|
254
293
|
*
|
|
255
294
|
* @param config - Handler configuration with templates and global settings
|
|
256
295
|
*/
|
|
257
|
-
constructor(config: OgTemplateRenderer);
|
|
296
|
+
constructor(config: OgTemplateRenderer<TMap>);
|
|
258
297
|
/**
|
|
259
298
|
* Registers multiple templates into the internal registry.
|
|
260
299
|
*
|
|
261
300
|
* Templates are indexed by their ID for fast lookup.
|
|
262
301
|
* If a template with the same ID already exists, it will be overwritten.
|
|
263
302
|
*
|
|
264
|
-
* @param templates -
|
|
303
|
+
* @param templates - Map of template definitions to register
|
|
265
304
|
*/
|
|
266
305
|
private registerTemplates;
|
|
267
306
|
/**
|
|
@@ -275,18 +314,7 @@ declare class TemplateRenderer {
|
|
|
275
314
|
* @param id - The template ID to look up
|
|
276
315
|
* @returns The template definition, or undefined if not found
|
|
277
316
|
*/
|
|
278
|
-
getTemplate(id:
|
|
279
|
-
/**
|
|
280
|
-
* Gets all registered template IDs.
|
|
281
|
-
*
|
|
282
|
-
* Useful for:
|
|
283
|
-
* - Building template selection dropdowns
|
|
284
|
-
* - Listing available templates in documentation
|
|
285
|
-
* - Debugging template registration
|
|
286
|
-
*
|
|
287
|
-
* @returns Array of template IDs
|
|
288
|
-
*/
|
|
289
|
-
getTemplateIds(): string[];
|
|
317
|
+
getTemplate<K extends keyof TMap>(id: K): OgTemplate<TMap[K]> | undefined;
|
|
290
318
|
/**
|
|
291
319
|
* Renders a template to a PNG image buffer.
|
|
292
320
|
*
|
|
@@ -308,17 +336,14 @@ declare class TemplateRenderer {
|
|
|
308
336
|
* - Throws if rendering fails (font loading, HTML generation, etc.)
|
|
309
337
|
*
|
|
310
338
|
* @param templateId - ID of the template to render
|
|
311
|
-
* @param params - Parameters to pass to the template
|
|
339
|
+
* @param params - Parameters (or function returning params) to pass to the template
|
|
312
340
|
* @param options - Optional rendering options
|
|
313
341
|
* @param options.width - Custom image width in pixels (default: 1200)
|
|
314
342
|
* @param options.height - Custom image height in pixels (default: 630)
|
|
315
343
|
* @returns Promise resolving to a PNG image buffer
|
|
316
344
|
* @throws Error if template is not found or rendering fails
|
|
317
345
|
*/
|
|
318
|
-
renderToImage(templateId:
|
|
319
|
-
width: number;
|
|
320
|
-
height: number;
|
|
321
|
-
}): Promise<Buffer>;
|
|
346
|
+
renderToImage<K extends keyof TMap>(templateId: K, params: TMap[K] | (() => Promise<TMap[K]>), options?: OgTemplateOptions): Promise<Buffer>;
|
|
322
347
|
}
|
|
323
348
|
/**
|
|
324
349
|
* Factory function to create a new TemplateRenderer instance.
|
|
@@ -329,7 +354,7 @@ declare class TemplateRenderer {
|
|
|
329
354
|
* @param config - Handler configuration with templates and settings
|
|
330
355
|
* @returns A new TemplateRenderer instance
|
|
331
356
|
*/
|
|
332
|
-
declare function
|
|
357
|
+
declare function createRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>>(config: OgTemplateRenderer<TMap>): TemplateRenderer<TMap>;
|
|
333
358
|
|
|
334
359
|
/**
|
|
335
360
|
* OG image rendering engine.
|
|
@@ -366,35 +391,15 @@ declare function createTemplateRenderer(config: OgTemplateRenderer): TemplateRen
|
|
|
366
391
|
* - SVG rendering fails
|
|
367
392
|
* - PNG conversion fails
|
|
368
393
|
*/
|
|
369
|
-
declare function renderTemplate(template: OgTemplate
|
|
370
|
-
width: number;
|
|
371
|
-
height: number;
|
|
372
|
-
}): Promise<Buffer>;
|
|
394
|
+
declare function renderTemplate<TParams = OgTemplateParams>(template: OgTemplate<TParams>, params: TParams, options?: OgTemplateOptions): Promise<Buffer>;
|
|
373
395
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
* Fetches a font file from a URL and returns it as an ArrayBuffer, with caching.
|
|
383
|
-
*
|
|
384
|
-
* This function is used to download font files from CDNs (like Google Fonts)
|
|
385
|
-
* so they can be embedded in OG images during server-side rendering.
|
|
386
|
-
* @param url - The URL of the font file to download
|
|
387
|
-
* @returns Promise resolving to the font data as an ArrayBuffer
|
|
388
|
-
*
|
|
389
|
-
* @throws Will throw if the network request fails or the URL is invalid
|
|
390
|
-
*
|
|
391
|
-
* @example
|
|
392
|
-
* // First call - downloads from network
|
|
393
|
-
* const fontData1 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
394
|
-
*
|
|
395
|
-
* // Second call - returns cached data (no network request)
|
|
396
|
-
* const fontData2 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
397
|
-
*/
|
|
398
|
-
declare const loadFontFromUrl: (url: string) => Promise<ArrayBuffer>;
|
|
396
|
+
type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
|
|
397
|
+
type ClassDictionary = Record<string, any>;
|
|
398
|
+
type ClassArray = ClassValue[];
|
|
399
|
+
declare function clsx(...inputs: ClassValue[]): string;
|
|
400
|
+
|
|
401
|
+
declare const objectToStyle: (style: Record<string, string | number | undefined | null> | undefined | null, options?: {
|
|
402
|
+
isRTL?: boolean;
|
|
403
|
+
}) => string;
|
|
399
404
|
|
|
400
|
-
export { type OgEmojiProvider, type OgFontConfig, type OgFontFormat, type OgTemplate, type OgTemplateOptions, type OgTemplateParams, type OgTemplateRenderer, TemplateRenderer,
|
|
405
|
+
export { type OgCacheConfig, type OgEmojiProvider, type OgFontConfig, type OgFontFormat, type OgTemplate, type OgTemplateOptions, type OgTemplateParams, type OgTemplateRenderer, TemplateRenderer, clsx, createRenderer, defineTemplate, objectToStyle, renderTemplate, validateTemplate };
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,30 @@ import { FontWeight, FontStyle } from 'satori';
|
|
|
22
22
|
* - `openmoji`: Open-source emoji with outlined style
|
|
23
23
|
*/
|
|
24
24
|
type OgEmojiProvider = 'twemoji' | 'fluent' | 'fluentFlat' | 'noto' | 'blobmoji' | 'openmoji';
|
|
25
|
+
/**
|
|
26
|
+
* Cache configuration for fonts and icons.
|
|
27
|
+
*
|
|
28
|
+
* Supports two caching strategies:
|
|
29
|
+
* - Memory: Fast in-memory cache with LRU eviction
|
|
30
|
+
* - Filesystem: Persistent cache stored on disk
|
|
31
|
+
*/
|
|
32
|
+
type OgCacheConfig = {
|
|
33
|
+
/** Memory-based caching strategy */
|
|
34
|
+
type: 'memory';
|
|
35
|
+
/** Time-to-live in milliseconds (default: 3600000 = 1 hour) */
|
|
36
|
+
ttl?: number;
|
|
37
|
+
/** Maximum number of items to cache (default: 100) */
|
|
38
|
+
max?: number;
|
|
39
|
+
} | {
|
|
40
|
+
/** Filesystem-based caching strategy */
|
|
41
|
+
type: 'filesystem';
|
|
42
|
+
/** Directory to store cache files (default: '.ogify-cache') */
|
|
43
|
+
dir?: string;
|
|
44
|
+
/** Time-to-live in milliseconds (default: 3600000 = 1 hour) */
|
|
45
|
+
ttl?: number;
|
|
46
|
+
/** Maximum number of items to cache (default: 100) */
|
|
47
|
+
max?: number;
|
|
48
|
+
};
|
|
25
49
|
/**
|
|
26
50
|
* Supported font file formats.
|
|
27
51
|
*
|
|
@@ -90,12 +114,15 @@ type OgFontConfig = {
|
|
|
90
114
|
* `;
|
|
91
115
|
*/
|
|
92
116
|
type OgTemplateOptions = {
|
|
93
|
-
/**
|
|
94
|
-
|
|
117
|
+
/** Custom fonts to use in this template */
|
|
118
|
+
fonts?: OgFontConfig[];
|
|
119
|
+
/** Optional emoji provider to use in this template */
|
|
120
|
+
emojiProvider?: OgEmojiProvider;
|
|
95
121
|
/** Optional custom width in pixels (default: 1200) */
|
|
96
122
|
width?: number;
|
|
97
123
|
/** Optional custom height in pixels (default: 630) */
|
|
98
124
|
height?: number;
|
|
125
|
+
isRTL?: boolean;
|
|
99
126
|
};
|
|
100
127
|
/**
|
|
101
128
|
* Key-value pairs representing dynamic template parameters.
|
|
@@ -103,6 +130,7 @@ type OgTemplateOptions = {
|
|
|
103
130
|
* Parameters are the data that gets injected into templates to create
|
|
104
131
|
* personalized OG images. Values can be:
|
|
105
132
|
* - Strings: Text content (titles, descriptions, names)
|
|
133
|
+
* - String Arrays: Lists of tags, categories
|
|
106
134
|
* - Numbers: Counts, dates, metrics
|
|
107
135
|
* - Booleans: Flags, toggles, states
|
|
108
136
|
*
|
|
@@ -114,7 +142,7 @@ type OgTemplateOptions = {
|
|
|
114
142
|
* published: true
|
|
115
143
|
* };
|
|
116
144
|
*/
|
|
117
|
-
type OgTemplateParams = Record<string, string | number | boolean>;
|
|
145
|
+
type OgTemplateParams = Record<string, string | string[] | number | boolean>;
|
|
118
146
|
/**
|
|
119
147
|
* Complete definition of an Open Graph image template.
|
|
120
148
|
*
|
|
@@ -124,20 +152,16 @@ type OgTemplateParams = Record<string, string | number | boolean>;
|
|
|
124
152
|
* - What fonts to use
|
|
125
153
|
* - Metadata for identification
|
|
126
154
|
*/
|
|
127
|
-
type OgTemplate = {
|
|
128
|
-
/** Unique identifier for this template (e.g., 'blog-post', 'product-card') */
|
|
129
|
-
id: string;
|
|
130
|
-
/** Human-readable name for display purposes */
|
|
131
|
-
name: string;
|
|
132
|
-
/** Brief description of what this template is used for */
|
|
133
|
-
description: string;
|
|
155
|
+
type OgTemplate<TParams = OgTemplateParams> = {
|
|
134
156
|
/**
|
|
135
157
|
* Function that generates HTML markup from template parameters.
|
|
136
158
|
*
|
|
137
159
|
* The HTML should use inline styles (Tailwind-like utilities are supported).
|
|
138
160
|
* Flexbox and basic CSS properties are supported by Satori.
|
|
139
161
|
*/
|
|
140
|
-
renderer: (props: OgTemplateOptions
|
|
162
|
+
renderer: (props: OgTemplateOptions & {
|
|
163
|
+
params: TParams;
|
|
164
|
+
}) => string;
|
|
141
165
|
/**
|
|
142
166
|
* Custom fonts to use in this template.
|
|
143
167
|
*
|
|
@@ -159,16 +183,29 @@ type OgTemplate = {
|
|
|
159
183
|
* The handler manages a registry of templates and provides a unified
|
|
160
184
|
* interface for rendering them to images.
|
|
161
185
|
*/
|
|
162
|
-
type OgTemplateRenderer = {
|
|
163
|
-
/**
|
|
164
|
-
templates:
|
|
186
|
+
type OgTemplateRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>> = {
|
|
187
|
+
/** Map of template definitions to register, keyed by template ID */
|
|
188
|
+
templates: {
|
|
189
|
+
[K in keyof TMap]: OgTemplate<TMap[K]>;
|
|
190
|
+
};
|
|
165
191
|
/**
|
|
166
|
-
*
|
|
192
|
+
* Shared parameter values applied to all templates.
|
|
167
193
|
*
|
|
168
194
|
* These values are merged with user-provided parameters,
|
|
169
195
|
* with user values taking precedence.
|
|
170
196
|
*/
|
|
171
|
-
|
|
197
|
+
sharedParams?: Partial<TMap[keyof TMap]> | (() => Promise<Partial<TMap[keyof TMap]>>);
|
|
198
|
+
/**
|
|
199
|
+
* Cache configuration for fonts and icons.
|
|
200
|
+
*
|
|
201
|
+
* When provided, enables LRU caching to improve performance by:
|
|
202
|
+
* - Reducing redundant network requests for fonts and emojis
|
|
203
|
+
* - Supporting memory or filesystem-based caching strategies
|
|
204
|
+
* - Configurable TTL and maximum cache size
|
|
205
|
+
*
|
|
206
|
+
* If not provided, falls back to simple in-memory caching.
|
|
207
|
+
*/
|
|
208
|
+
cache?: OgCacheConfig;
|
|
172
209
|
/**
|
|
173
210
|
* Hook called before rendering.
|
|
174
211
|
*
|
|
@@ -178,7 +215,7 @@ type OgTemplateRenderer = {
|
|
|
178
215
|
* - Authentication checks
|
|
179
216
|
* - Rate limiting
|
|
180
217
|
*/
|
|
181
|
-
beforeRender?: (templateId:
|
|
218
|
+
beforeRender?: (templateId: keyof TMap, params: TMap[keyof TMap]) => void | Promise<void>;
|
|
182
219
|
/**
|
|
183
220
|
* Hook called after rendering.
|
|
184
221
|
*
|
|
@@ -188,7 +225,7 @@ type OgTemplateRenderer = {
|
|
|
188
225
|
* - Sending notifications
|
|
189
226
|
* - Updating metrics
|
|
190
227
|
*/
|
|
191
|
-
afterRender?: (templateId:
|
|
228
|
+
afterRender?: (templateId: keyof TMap, params: TMap[keyof TMap], imageBuffer: Buffer) => void | Promise<void>;
|
|
192
229
|
};
|
|
193
230
|
|
|
194
231
|
/**
|
|
@@ -217,7 +254,7 @@ type OgTemplateRenderer = {
|
|
|
217
254
|
* @returns true if validation passes
|
|
218
255
|
* @throws Error if any required field is missing or invalid
|
|
219
256
|
*/
|
|
220
|
-
declare function validateTemplate(config: OgTemplate): boolean;
|
|
257
|
+
declare function validateTemplate<TemplateParams>(config: OgTemplate<TemplateParams>): boolean;
|
|
221
258
|
/**
|
|
222
259
|
* Defines and validates a new OG template.
|
|
223
260
|
*
|
|
@@ -229,17 +266,19 @@ declare function validateTemplate(config: OgTemplate): boolean;
|
|
|
229
266
|
* - Type safety ensures correct template structure
|
|
230
267
|
* - Clear intent in code (self-documenting)
|
|
231
268
|
*
|
|
232
|
-
* @param config - Complete template configuration including
|
|
269
|
+
* @param config - Complete template configuration including renderer function and fonts
|
|
233
270
|
* @returns The validated template configuration
|
|
234
271
|
* @throws Error if validation fails
|
|
235
272
|
*/
|
|
236
|
-
declare function defineTemplate(config: OgTemplate): OgTemplate
|
|
273
|
+
declare function defineTemplate<TemplateParams>(config: OgTemplate<TemplateParams>): OgTemplate<TemplateParams>;
|
|
237
274
|
/**
|
|
238
275
|
* Template Handler class for managing multiple templates and rendering them to images.
|
|
239
276
|
*/
|
|
240
|
-
declare class TemplateRenderer {
|
|
277
|
+
declare class TemplateRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>> {
|
|
241
278
|
/** Configuration including global settings and lifecycle hooks */
|
|
242
279
|
private config;
|
|
280
|
+
/** Cache manager for fonts and icons */
|
|
281
|
+
private cacheManager;
|
|
243
282
|
/**
|
|
244
283
|
* Internal registry mapping template IDs to template definitions.
|
|
245
284
|
*
|
|
@@ -254,14 +293,14 @@ declare class TemplateRenderer {
|
|
|
254
293
|
*
|
|
255
294
|
* @param config - Handler configuration with templates and global settings
|
|
256
295
|
*/
|
|
257
|
-
constructor(config: OgTemplateRenderer);
|
|
296
|
+
constructor(config: OgTemplateRenderer<TMap>);
|
|
258
297
|
/**
|
|
259
298
|
* Registers multiple templates into the internal registry.
|
|
260
299
|
*
|
|
261
300
|
* Templates are indexed by their ID for fast lookup.
|
|
262
301
|
* If a template with the same ID already exists, it will be overwritten.
|
|
263
302
|
*
|
|
264
|
-
* @param templates -
|
|
303
|
+
* @param templates - Map of template definitions to register
|
|
265
304
|
*/
|
|
266
305
|
private registerTemplates;
|
|
267
306
|
/**
|
|
@@ -275,18 +314,7 @@ declare class TemplateRenderer {
|
|
|
275
314
|
* @param id - The template ID to look up
|
|
276
315
|
* @returns The template definition, or undefined if not found
|
|
277
316
|
*/
|
|
278
|
-
getTemplate(id:
|
|
279
|
-
/**
|
|
280
|
-
* Gets all registered template IDs.
|
|
281
|
-
*
|
|
282
|
-
* Useful for:
|
|
283
|
-
* - Building template selection dropdowns
|
|
284
|
-
* - Listing available templates in documentation
|
|
285
|
-
* - Debugging template registration
|
|
286
|
-
*
|
|
287
|
-
* @returns Array of template IDs
|
|
288
|
-
*/
|
|
289
|
-
getTemplateIds(): string[];
|
|
317
|
+
getTemplate<K extends keyof TMap>(id: K): OgTemplate<TMap[K]> | undefined;
|
|
290
318
|
/**
|
|
291
319
|
* Renders a template to a PNG image buffer.
|
|
292
320
|
*
|
|
@@ -308,17 +336,14 @@ declare class TemplateRenderer {
|
|
|
308
336
|
* - Throws if rendering fails (font loading, HTML generation, etc.)
|
|
309
337
|
*
|
|
310
338
|
* @param templateId - ID of the template to render
|
|
311
|
-
* @param params - Parameters to pass to the template
|
|
339
|
+
* @param params - Parameters (or function returning params) to pass to the template
|
|
312
340
|
* @param options - Optional rendering options
|
|
313
341
|
* @param options.width - Custom image width in pixels (default: 1200)
|
|
314
342
|
* @param options.height - Custom image height in pixels (default: 630)
|
|
315
343
|
* @returns Promise resolving to a PNG image buffer
|
|
316
344
|
* @throws Error if template is not found or rendering fails
|
|
317
345
|
*/
|
|
318
|
-
renderToImage(templateId:
|
|
319
|
-
width: number;
|
|
320
|
-
height: number;
|
|
321
|
-
}): Promise<Buffer>;
|
|
346
|
+
renderToImage<K extends keyof TMap>(templateId: K, params: TMap[K] | (() => Promise<TMap[K]>), options?: OgTemplateOptions): Promise<Buffer>;
|
|
322
347
|
}
|
|
323
348
|
/**
|
|
324
349
|
* Factory function to create a new TemplateRenderer instance.
|
|
@@ -329,7 +354,7 @@ declare class TemplateRenderer {
|
|
|
329
354
|
* @param config - Handler configuration with templates and settings
|
|
330
355
|
* @returns A new TemplateRenderer instance
|
|
331
356
|
*/
|
|
332
|
-
declare function
|
|
357
|
+
declare function createRenderer<TMap extends Record<string, OgTemplateParams> = Record<string, OgTemplateParams>>(config: OgTemplateRenderer<TMap>): TemplateRenderer<TMap>;
|
|
333
358
|
|
|
334
359
|
/**
|
|
335
360
|
* OG image rendering engine.
|
|
@@ -366,35 +391,15 @@ declare function createTemplateRenderer(config: OgTemplateRenderer): TemplateRen
|
|
|
366
391
|
* - SVG rendering fails
|
|
367
392
|
* - PNG conversion fails
|
|
368
393
|
*/
|
|
369
|
-
declare function renderTemplate(template: OgTemplate
|
|
370
|
-
width: number;
|
|
371
|
-
height: number;
|
|
372
|
-
}): Promise<Buffer>;
|
|
394
|
+
declare function renderTemplate<TParams = OgTemplateParams>(template: OgTemplate<TParams>, params: TParams, options?: OgTemplateOptions): Promise<Buffer>;
|
|
373
395
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
* Fetches a font file from a URL and returns it as an ArrayBuffer, with caching.
|
|
383
|
-
*
|
|
384
|
-
* This function is used to download font files from CDNs (like Google Fonts)
|
|
385
|
-
* so they can be embedded in OG images during server-side rendering.
|
|
386
|
-
* @param url - The URL of the font file to download
|
|
387
|
-
* @returns Promise resolving to the font data as an ArrayBuffer
|
|
388
|
-
*
|
|
389
|
-
* @throws Will throw if the network request fails or the URL is invalid
|
|
390
|
-
*
|
|
391
|
-
* @example
|
|
392
|
-
* // First call - downloads from network
|
|
393
|
-
* const fontData1 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
394
|
-
*
|
|
395
|
-
* // Second call - returns cached data (no network request)
|
|
396
|
-
* const fontData2 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
397
|
-
*/
|
|
398
|
-
declare const loadFontFromUrl: (url: string) => Promise<ArrayBuffer>;
|
|
396
|
+
type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
|
|
397
|
+
type ClassDictionary = Record<string, any>;
|
|
398
|
+
type ClassArray = ClassValue[];
|
|
399
|
+
declare function clsx(...inputs: ClassValue[]): string;
|
|
400
|
+
|
|
401
|
+
declare const objectToStyle: (style: Record<string, string | number | undefined | null> | undefined | null, options?: {
|
|
402
|
+
isRTL?: boolean;
|
|
403
|
+
}) => string;
|
|
399
404
|
|
|
400
|
-
export { type OgEmojiProvider, type OgFontConfig, type OgFontFormat, type OgTemplate, type OgTemplateOptions, type OgTemplateParams, type OgTemplateRenderer, TemplateRenderer,
|
|
405
|
+
export { type OgCacheConfig, type OgEmojiProvider, type OgFontConfig, type OgFontFormat, type OgTemplate, type OgTemplateOptions, type OgTemplateParams, type OgTemplateRenderer, TemplateRenderer, clsx, createRenderer, defineTemplate, objectToStyle, renderTemplate, validateTemplate };
|