Skip to content

Commit

Permalink
add back ability to use query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
saboooor committed Jun 9, 2024
1 parent c075126 commit 3cfa0b8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "birdflop",
"version": "8.10.1",
"version": "8.10.2",
"homepage": "https://www.birdflop.com",
"description": "The only 501(c)(3) nonprofit Minecraft server host.",
"repository": {
Expand Down
6 changes: 2 additions & 4 deletions src/routes/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ export const onGet: RequestHandler = async ({ json }) => {
throw json(200, {
endpoints: {
'/api/v2': {
type: 'GET',
description: 'View the v2 API endpoints.',
GET: 'View the v2 API endpoints.',
},
'/api/gradient': {
type: 'GET',
description: 'Generate a gradient.',
GET: 'Generate a gradient.',
WARNING: '/api/gradient is deprecated. Please use /api/v2/rgb instead.',
},
},
Expand Down
16 changes: 9 additions & 7 deletions src/routes/api/v2/docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useEndpoints = routeLoader$(async ({ url }) => {
for (const path of paths) {
const endpointData = await fetch(url.origin + path);
const endpointJson = await endpointData.json();
json.endpoints[path] = { ...json.endpoints[path], options: endpointJson.options };
json.endpoints[path] = { methods: json.endpoints[path], options: endpointJson.options };
}
return json;
});
Expand Down Expand Up @@ -42,13 +42,15 @@ export default component$(() => {
</h2>
<div>
{Object.keys(endpoints).map((path) => <div key={path}>
<h3 class="flex gap-4 items-center text-gray-100 text-lg sm:text-2xl font-bold mb-1 drop-shadow-lg">
{endpoints[path].type} {path}
<h3 class="flex gap-4 items-center text-gray-100 text-lg sm:text-2xl font-bold mb-2 drop-shadow-lg">
{path}
</h3>
<h4 class="flex gap-4 items-center text-gray-400 sm:text-lg mb-2 drop-shadow-lg">
{endpoints[path].description}
</h4>
<h4 class="flex gap-4 items-center text-gray-400 sm:text-lg mb-2 drop-shadow-lg">
{Object.entries(endpoints[path].methods).map(([method, description]: any) =>
<p key={method} class="flex gap-4 items-center text-gray-400 sm:text-lg drop-shadow-lg">
{method}: {description}
</p>,
)}
<h4 class="flex gap-4 items-center text-gray-400 sm:text-lg my-2 drop-shadow-lg">
Options
</h4>
<Card>
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const onGet: RequestHandler = async ({ json }) => {
throw json(200, {
endpoints: {
'/api/v2/rgb': {
type: 'POST',
description: 'Generate a gradient.',
POST: 'Generate a gradient.',
GET: 'Equivalent to POST, but with query parameters.',
},
},
});
Expand Down
47 changes: 40 additions & 7 deletions src/routes/api/v2/rgb/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,42 @@ import { v3formats } from '~/components/util/PresetUtils';
import { generateOutput } from '~/components/util/RGBUtils';
import { rgbDefaults } from '~/routes/resources/rgb';

export const onGet: RequestHandler = async ({ json, query }) => {
try {
const queryjson: any = Object.fromEntries(query);

const keys = Object.keys(queryjson);
for (const key of keys) {
if (key == 'colors') queryjson.colors = queryjson.colors.split(',');
else if (queryjson[key] == 'true') queryjson[key] = true;
else if (queryjson[key] == 'false') queryjson[key] = false;
else if (queryjson[key].startsWith('{') && queryjson[key].endsWith('}')) queryjson[key] = JSON.parse(queryjson[key]);
}

const output = await getOutput(queryjson);
throw json(200, output);
}
catch (e: any) {
console.error(e);
throw json(400, { error: e.message });
}
};

export const onPost: RequestHandler = async ({ json, parseBody }) => {
const body = await parseBody() as any;
const { text, colors, prefixsuffix, trimspaces, bold, italic, underline, strikethrough, silent } = body ?? {};
try {
const body = await parseBody();
const output = await getOutput(body);

const options = silent ? {} : {
throw json(200, output);
}
catch (e: any) {
console.error(e);
throw json(400, { error: e.message });
}
};

async function getOutput(body: any) {
const options = body?.silent ? {} : {
input: {
...rgbDefaults,
...body,
Expand Down Expand Up @@ -73,8 +104,10 @@ export const onPost: RequestHandler = async ({ json, parseBody }) => {
format = v3formats.find(f => f.color == format.color) ?? { ...format, char: '&' };
}

throw json(200, {
output: generateOutput(text, colors, format, prefixsuffix, trimspaces, bold, italic, underline, strikethrough),
const { text, colors, prefixsuffix, trimspaces, bold, italic, underline, strikethrough } = body ?? {};
const output = generateOutput(text, colors, format, prefixsuffix, trimspaces, bold, italic, underline, strikethrough);
return {
output,
...options,
});
};
};
}

0 comments on commit 3cfa0b8

Please sign in to comment.