1- import express , { type Request , type Response } from 'express' ;
21import { nodeAdapter } from '@og-engine/adapter-node' ;
32import { createHandler } from '@og-engine/core' ;
4- import type { OGRequest , MetaRequest } from '@og-engine/types' ;
3+ import type { MetaRequest , OGRequest , PlatformSize } from '@og-engine/types' ;
4+ import express , { type Request , type Response } from 'express' ;
55
66const app = express ( ) ;
77const port = process . env . PORT ?? 3000 ;
@@ -12,23 +12,50 @@ const adapter = nodeAdapter({
1212} ) ;
1313
1414const handler = createHandler ( { platform : adapter } ) ;
15+ const PLATFORM_SIZES : ReadonlyArray < PlatformSize > = [
16+ 'twitter-og' ,
17+ 'facebook-og' ,
18+ 'linkedin-og' ,
19+ 'ig-post' ,
20+ 'ig-story' ,
21+ 'discord' ,
22+ 'whatsapp' ,
23+ 'github' ,
24+ 'og'
25+ ] ;
26+
27+ function parseSize ( value : unknown ) : PlatformSize {
28+ if ( typeof value === 'string' && PLATFORM_SIZES . includes ( value as PlatformSize ) ) {
29+ return value as PlatformSize ;
30+ }
31+
32+ return 'og' ;
33+ }
34+
35+ function parseFormat ( value : unknown ) : OGRequest [ 'format' ] {
36+ if ( value === 'jpeg' || value === 'png' ) {
37+ return value ;
38+ }
39+
40+ return 'png' ;
41+ }
1542
1643app . get ( '/health' , ( _request : Request , response : Response ) => {
1744 response . json ( { ok : true } ) ;
1845} ) ;
1946
2047app . get ( '/api/og' , async ( req : Request , res : Response ) => {
2148 try {
22- const { template, size = 'og' , format = 'png' , ...params } = req . query ;
49+ const { template, size, format, ...params } = req . query ;
2350
24- if ( ! template ) {
51+ if ( ! template || typeof template !== 'string' ) {
2552 return res . status ( 400 ) . send ( 'Missing template parameter' ) ;
2653 }
2754
2855 const ogReq : OGRequest = {
29- template : template as string ,
30- size : size as any ,
31- format : format as any ,
56+ template,
57+ size : parseSize ( size ) ,
58+ format : parseFormat ( format ) ,
3259 params : params as Record < string , string >
3360 } ;
3461
@@ -40,34 +67,30 @@ app.get('/api/og', async (req: Request, res: Response) => {
4067 } ) ;
4168 res . send ( buffer ) ;
4269 } catch ( error ) {
43- console . error ( 'Render error:' , error ) ;
4470 res . status ( 500 ) . send ( error instanceof Error ? error . message : 'Internal Server Error' ) ;
4571 }
4672} ) ;
4773
4874app . get ( '/api/meta' , async ( req : Request , res : Response ) => {
4975 try {
50- const { template, size = 'og' , ...params } = req . query ;
76+ const { template, size, ...params } = req . query ;
5177
52- if ( ! template ) {
78+ if ( ! template || typeof template !== 'string' ) {
5379 return res . status ( 400 ) . send ( 'Missing template parameter' ) ;
5480 }
5581
5682 const metaReq : MetaRequest = {
57- template : template as string ,
58- size : size as any ,
83+ template,
84+ size : parseSize ( size ) ,
5985 params : params as Record < string , string > ,
6086 baseUrl : `${ req . protocol } ://${ req . get ( 'host' ) } `
6187 } ;
6288
6389 const meta = await handler . handleMetaRequest ( metaReq ) ;
6490 res . json ( meta ) ;
6591 } catch ( error ) {
66- console . error ( 'Meta error:' , error ) ;
6792 res . status ( 500 ) . send ( error instanceof Error ? error . message : 'Internal Server Error' ) ;
6893 }
6994} ) ;
7095
71- app . listen ( port , ( ) => {
72- console . log ( `Server listening on port ${ port } ` ) ;
73- } ) ;
96+ app . listen ( port ) ;
0 commit comments