feat(api): add basic rest api

This commit is contained in:
Guz013
2023-06-29 15:16:38 -03:00
parent aecb212d0c
commit 16a13c2c6a
7 changed files with 64 additions and 42 deletions

View File

@@ -233,6 +233,7 @@ module.exports = {
'xml',
'jsconfig',
'vitest',
'matcher',
],
minLength: 4,
}],

View File

@@ -62,7 +62,6 @@
"Banner": true,
"load": true,
"data": true,
"PageData": true,
"GET": true
"PageData": true
}
}

View File

@@ -3,4 +3,7 @@ module.exports = {
extends: [
'./.eslintrc-auto-import.json',
],
rules: {
'@typescript-eslint/no-throw-literal': 'off',
},
};

View File

@@ -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']

View 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;

View File

@@ -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;

View 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;