Skip to content

Type Issue for Response #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2022
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Install `@ethercorps/sveltekit-og`, then use it inside a server endpoint route (
```typescript
// /routes/og/+server.ts
import { ImageResponse } from '@ethercorps/sveltekit-og';
import type { RequestHandler } from './$types';

const template = `
<div tw="bg-gray-50 flex w-full h-full items-center justify-center">
Expand All @@ -33,7 +32,7 @@ const template = `
const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
const fontData: ArrayBuffer = await fontFile.arrayBuffer();

export const GET: RequestHandler = async () => {
export const GET: () => Promise<ImageResponse> = async () => {
return new ImageResponse(template, {
height: 250,
width: 500,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ethercorps/sveltekit-og",
"version": "0.1.3",
"version": "0.1.4",
"private": false,
"scripts": {
"dev": "vite dev",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const resSvgWasm = initWasm(fetch('https://sveltekit-og.ethercorps.io/resvg.wasm
const fontFile = await fetch('https://sveltekit-og.ethercorps.io/noto-sans.ttf');
const fontData: ArrayBuffer = await fontFile.arrayBuffer();

export const ImageResponse = class {
export class ImageResponse {
constructor(htmlTemplate: string, optionsByUser: ImageResponseOptions) {
const options = Object.assign({ width: 1200, height: 630, debug: !1 }, optionsByUser);
const png = new ReadableStream({
Expand Down Expand Up @@ -45,14 +45,14 @@ export const ImageResponse = class {
statusText: options.statusText
});
}
};
}

export const componentToImageResponse = class {
export class componentToImageResponse {
constructor(component: typeof SvelteComponent, props = {}, optionsByUser: ImageResponseOptions) {
const htmlTemplate = componentToMarkup(component, props)
return new ImageResponse(htmlTemplate, optionsByUser);
}
};
}

const componentToMarkup = (component: typeof SvelteComponent, props={}) => {
const SvelteRenderedMarkup = (component as any).render(props);
Expand Down
3 changes: 1 addition & 2 deletions src/routes/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ImageResponse } from '$lib';
import type { RequestHandler } from './$types';

const template = `
<div tw="bg-gray-50 flex w-full h-full items-center justify-center">
Expand All @@ -24,7 +23,7 @@ const fontFile700 = await fetch('https://og-playground.vercel.app/inter-latin-ex
const fontData400: ArrayBuffer = await fontFile400.arrayBuffer();
const fontData700: ArrayBuffer = await fontFile700.arrayBuffer();

export const GET: RequestHandler = async () => {
export const GET: () => Promise<ImageResponse> = async () => {
return new ImageResponse(template, {
height: 250,
width: 500,
Expand Down
3 changes: 1 addition & 2 deletions src/routes/component-og/+server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import OG from "./OG.svelte";
import {componentToImageResponse} from "$lib";
import type {RequestHandler} from "./$types";


const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-700-normal.woff');
const fontData: ArrayBuffer = await fontFile.arrayBuffer();

export const GET: RequestHandler = async () => {
export const GET:() => Promise<componentToImageResponse>= async () => {
return new componentToImageResponse(OG, {text: 'Ready to dive in?', spanText: 'Start your free trial today.'}, {
height: 250,
width: 500,
Expand Down