TemplateRenderer instance.
For complete type definitions and interfaces, see the Type
Definitions page.
Function Signature
function createRenderer<
TMap extends Record<string, OgTemplateParams> = Record<
string,
OgTemplateParams
>
>(config: OgTemplateRenderer<TMap>): TemplateRenderer<TMap>;
Parameters
Handler configuration with templates and global settings
Show properties
Show properties
A map of template objects, keyed by a unique string ID
Default parameters that will be applied to all templates. Useful for brand consistency. Can be an object or a function returning a Promise. User-provided parameters take precedence over shared parameters during rendering.
Caching configuration for fonts and icons. If not provided, defaults to in-memory caching with
{ type: 'memory' }Hook called before rendering
(templateId: string, params: any) => void | Promise<void>
Hook called after rendering
(templateId: string, params: any, buffer: Buffer) => void | Promise<void>
Returns
The renderer instance with methods for managing templates and generating
images
Examples
import { createRenderer } from "@ogify/core";
import basicTemplate from "@ogify/templates/basic";
import type { TemplateParams } from "@ogify/templates/basic";
const renderer = createRenderer<{ basic: TemplateParams }>({
templates: { basic: basicTemplate },
sharedParams: {
brandName: "My Company",
brandLogo: "https://example.com/logo.png",
},
});
import { createRenderer } from "@ogify/core";
import basicTemplate from "@ogify/templates/basic";
import type { TemplateParams } from "@ogify/templates/basic";
const renderer = createRenderer<{ basic: TemplateParams }>({
templates: { basic: basicTemplate },
sharedParams: {
brandName: "My Company",
brandLogo: "https://example.com/logo.png",
},
cache: {
type: "memory",
ttl: 3600000, // 1 hour
max: 100,
},
});
import { createRenderer } from "@ogify/core";
import basicTemplate from "@ogify/templates/basic";
import type { TemplateParams } from "@ogify/templates/basic";
const renderer = createRenderer<{ basic: TemplateParams }>({
templates: { basic: basicTemplate },
sharedParams: {
brandName: "My Company",
brandLogo: "https://example.com/logo.png",
},
beforeRender: async (templateId, params) => {
console.log(`Rendering template: ${templateId}`);
},
afterRender: async (templateId, params, buffer) => {
console.log(`Generated ${buffer.length} bytes`);
},
});