feat(api): ✨ add basic rest api
This commit is contained in:
@@ -233,6 +233,7 @@ module.exports = {
|
||||
'xml',
|
||||
'jsconfig',
|
||||
'vitest',
|
||||
'matcher',
|
||||
],
|
||||
minLength: 4,
|
||||
}],
|
||||
|
||||
@@ -62,7 +62,6 @@
|
||||
"Banner": true,
|
||||
"load": true,
|
||||
"data": true,
|
||||
"PageData": true,
|
||||
"GET": true
|
||||
"PageData": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,7 @@ module.exports = {
|
||||
extends: [
|
||||
'./.eslintrc-auto-import.json',
|
||||
],
|
||||
rules: {
|
||||
'@typescript-eslint/no-throw-literal': 'off',
|
||||
},
|
||||
};
|
||||
|
||||
2
apps/www/src/lib/imports.d.ts
vendored
2
apps/www/src/lib/imports.d.ts
vendored
@@ -5,7 +5,6 @@
|
||||
export {}
|
||||
declare global {
|
||||
const Banner: typeof import('./Banner')['default']
|
||||
const GET: typeof import('../routes/api.svg/+server')['GET']
|
||||
const afterUpdate: typeof import('svelte')['afterUpdate']
|
||||
const backIn: typeof import('svelte/easing')['backIn']
|
||||
const backInOut: typeof import('svelte/easing')['backInOut']
|
||||
@@ -41,7 +40,6 @@ declare global {
|
||||
const getContext: typeof import('svelte')['getContext']
|
||||
const hasContext: typeof import('svelte')['hasContext']
|
||||
const linear: typeof import('svelte/easing')['linear']
|
||||
const load: typeof import('../routes/+page')['load']
|
||||
const onDestroy: typeof import('svelte')['onDestroy']
|
||||
const onMount: typeof import('svelte')['onMount']
|
||||
const quadIn: typeof import('svelte/easing')['quadIn']
|
||||
|
||||
11
apps/www/src/params/layout.ts
Normal file
11
apps/www/src/params/layout.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { ParamMatcher } from '@sveltejs/kit';
|
||||
|
||||
export const match = ((param) => {
|
||||
if (
|
||||
param === 'horizontal.svg'
|
||||
|| param === 'vertical.svg'
|
||||
|| param === 'horizontal'
|
||||
|| param === 'vertical'
|
||||
) return true;
|
||||
else return false;
|
||||
}) satisfies ParamMatcher;
|
||||
@@ -1,38 +0,0 @@
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import newBanner from '@marknow/banners';
|
||||
|
||||
export const GET = (async ({ fetch }): Promise<Response> => {
|
||||
const banner = await newBanner({
|
||||
title: 'Hello world',
|
||||
subtitle: 'This is a test!',
|
||||
icon: 'solar:hand-shake-bold-duotone',
|
||||
colors: {
|
||||
background: '#000000',
|
||||
foreground: '#ffffff',
|
||||
},
|
||||
fonts: {
|
||||
title: {
|
||||
data: await (await fetch('/Mona-Sans-SemiBold.woff')).arrayBuffer(),
|
||||
name: 'Mona Sans',
|
||||
weight: 600,
|
||||
style: 'normal',
|
||||
},
|
||||
subtitle: {
|
||||
data: await (await fetch('/Mona-Sans-Regular.woff')).arrayBuffer(),
|
||||
name: 'Mona Sans',
|
||||
weight: 400,
|
||||
style: 'normal',
|
||||
},
|
||||
},
|
||||
libConfig: {
|
||||
fetcher: fetch,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(`${banner.toString()}`, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-type': 'image/svg+xml',
|
||||
},
|
||||
});
|
||||
}) satisfies RequestHandler;
|
||||
48
apps/www/src/routes/v1/banners/[layout=layout]/+server.ts
Normal file
48
apps/www/src/routes/v1/banners/[layout=layout]/+server.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { BannerOptions } from '@marknow/banners';
|
||||
import createBanner from '@marknow/banners';
|
||||
import { type RequestHandler, error } from '@sveltejs/kit';
|
||||
|
||||
export const GET = (async ({ params, url, fetch }) => {
|
||||
const title: string | null = url.searchParams.get('title');
|
||||
if (!title)
|
||||
throw error(400, { message: 'Please provide a title parameter for the banner' });
|
||||
|
||||
const layout = <BannerOptions['layout']>params.layout?.replace('.svg', '');
|
||||
const subtitle: string | undefined = url.searchParams.get('subtitle') ?? undefined;
|
||||
const icon: string | undefined = url.searchParams.get('icon') ?? undefined;
|
||||
const rtl: boolean = url.searchParams.get('rtl') !== 'false' && url.searchParams.get('rtl') !== null;
|
||||
|
||||
const colors: BannerOptions['colors'] = {
|
||||
foreground: url.searchParams.get('foreground') ?? '#000000',
|
||||
background: url.searchParams.get('background') ?? '#ffffff',
|
||||
};
|
||||
|
||||
const banner = await createBanner({
|
||||
title,
|
||||
layout,
|
||||
subtitle,
|
||||
icon,
|
||||
rtl,
|
||||
colors,
|
||||
fonts: {
|
||||
title: {
|
||||
data: await (await fetch('/Mona-Sans-SemiBold.woff')).arrayBuffer(),
|
||||
name: 'Mona Sans',
|
||||
},
|
||||
subtitle: {
|
||||
data: await (await fetch('/Mona-Sans-Regular.woff')).arrayBuffer(),
|
||||
name: 'Mona Sans',
|
||||
},
|
||||
},
|
||||
libConfig: {
|
||||
fetcher: fetch,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(banner.svg, {
|
||||
headers: {
|
||||
'Content-type': 'image/svg+xml',
|
||||
},
|
||||
status: 200,
|
||||
});
|
||||
}) satisfies RequestHandler;
|
||||
Reference in New Issue
Block a user