From be2979861a4b20d31a54616a7951607c0f973579 Mon Sep 17 00:00:00 2001 From: hyesungoh Date: Tue, 31 Oct 2023 00:57:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20image=20response=20api=20route=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/dna-image.page.tsx | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/pages/api/dna-image.page.tsx diff --git a/src/pages/api/dna-image.page.tsx b/src/pages/api/dna-image.page.tsx new file mode 100644 index 00000000..dfd76432 --- /dev/null +++ b/src/pages/api/dna-image.page.tsx @@ -0,0 +1,103 @@ +/* eslint-disable unicorn/filename-case */ +import { ImageResponse } from 'next/server'; + +type ImageOptions = ConstructorParameters[1]; + +export const config = { + runtime: 'edge', +}; + +// ref: https://github.com/vercel/examples/blob/main/edge-functions/vercel-og-nextjs/pages/api/custom-font.tsx +// TODO: group, nickname, position 받아서 이미지 생성 +export default async function handler() { + const notoSansScFont700 = await fetchFont('Noto+Sans+KR', 700); + if (!notoSansScFont700) + return { + base64: '', + }; + + const imageOptions: ImageOptions = { + width: 375, + height: 666, + fonts: [ + { + name: 'Noto Sans KR', + data: notoSansScFont700, + weight: 700, + style: 'normal', + }, + ], + }; + + return new ImageResponse( + ( +
+ {/* eslint-disable-next-line @next/next/no-img-element */} + {/* {'dna_' */} + + nick + + + position + +
+ ), + imageOptions, + ); +} + +async function fetchFont(fontFamily = 'Noto+Sans+KR', fontWeight = 700): Promise { + const fontUrl = `https://fonts.googleapis.com/css2?family=${fontFamily}:wght@${fontWeight}`; + + const css = await (await fetch(fontUrl)).text(); + + const resource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/); + + if (!resource) return null; + + const res = await fetch(resource[1]); + + return res.arrayBuffer(); +} + +// const HOISTING_IMAGE_BY_GROUP: Record = { +// A: `https://github.com/depromeet/na-lab-client/assets/49177223/0b224b08-3858-4305-8323-1a6082dbb4f7`, +// B: `https://github.com/depromeet/na-lab-client/assets/49177223/fec4951a-270f-40e8-8520-43eecb89416f`, +// C: `https://github.com/depromeet/na-lab-client/assets/49177223/faefd0ee-7048-4578-8ec5-b70713e6efd9`, +// D: `https://github.com/depromeet/na-lab-client/assets/49177223/4dbecfeb-5492-4c6b-b4a7-cd933cd5621e`, +// E: `https://github.com/depromeet/na-lab-client/assets/49177223/1eb9e7e4-801d-475a-8037-aa35a2776441`, +// F: `https://github.com/depromeet/na-lab-client/assets/49177223/8667e31c-9722-490b-9def-3b952d115275`, +// };