Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
saboooor committed Jun 13, 2024
1 parent 0c9cbb4 commit 7d9b4c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/routes/api/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export const onGet: RequestHandler = async ({ json }) => {
'/api/v2/rgb': {
POST: 'Generate a gradient.',
GET: 'Equivalent to POST, but with query parameters.',
WARNING: '/api/v2/rgb is deprecated. Please use /api/v3/rgb instead.',
},
},
WARNING: '/api/v2 is deprecated. Please use /api/v3 instead.',
});
};
23 changes: 14 additions & 9 deletions src/routes/api/v2/rgb/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { v3formats } from '~/components/util/PresetUtils';
import { generateOutput } from '~/components/util/RGBUtils';
import { rgbDefaults } from '~/routes/resources/rgb';

const v2rgbDefaults = {
rgbDefaults,
colors: rgbDefaults.colors.map(color => color.hex),
};

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

const keys = Object.keys(queryjson);
for (const key of keys) {
if (key == 'colors') {
Expand All @@ -22,17 +27,17 @@ export const onGet: RequestHandler = async ({ json, query }) => {
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);
output = await getOutput(queryjson);
}
catch (e: any) {
console.error(e);
throw json(400, { error: e.message });
}
throw json(200, output);
};

export const onPost: RequestHandler = async ({ json, parseBody }) => {
let output = {};
try {
const body = await parseBody() as any;
if (body?.colors) body.colors = body.colors.map((color: string, index: number) => {
Expand All @@ -41,14 +46,14 @@ export const onPost: RequestHandler = async ({ json, parseBody }) => {
pos: index / body.colors.length,
};
});
const output = await getOutput(body);

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

throw json(200, output);
};

async function getOutput(body: any) {
Expand All @@ -64,9 +69,9 @@ async function getOutput(body: any) {
default: rgbDefaults.text,
},
colors: {
type: 'array of hex colors',
type: 'array of (string)',
description: 'The colors to use for the gradient. Must be in hex format.',
default: rgbDefaults.colors,
default: v2rgbDefaults.colors,
},
format: {
type: 'format object - see data models in docs',
Expand Down
14 changes: 8 additions & 6 deletions src/routes/api/v3/rgb/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateOutput } from '~/components/util/RGBUtils';
import { rgbDefaults } from '~/routes/resources/rgb';

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

Expand All @@ -15,26 +16,27 @@ export const onGet: RequestHandler = async ({ json, query }) => {
else if (queryjson[key].startsWith('{') && queryjson[key].endsWith('}')) queryjson[key] = JSON.parse(queryjson[key]);
}

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

export const onPost: RequestHandler = async ({ json, parseBody }) => {
let output = {};
try {
const body = await parseBody();
const output = await getOutput(body);

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

throw json(200, output);
};

async function getOutput(body: any) {
Expand All @@ -50,7 +52,7 @@ async function getOutput(body: any) {
default: rgbDefaults.text,
},
colors: {
type: 'array of Color object - see data models in docs',
type: 'array of (Color object) - see data models in docs',
description: 'The colors to use for the gradient.',
default: rgbDefaults.colors,
},
Expand Down

0 comments on commit 7d9b4c1

Please sign in to comment.