Skip to content

Commit

Permalink
chore: formatted api code
Browse files Browse the repository at this point in the history
  • Loading branch information
fluid-design-io committed Jun 24, 2024
1 parent 66de982 commit 23b2a98
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 59 deletions.
3 changes: 2 additions & 1 deletion apps/web/app/api/color-names/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { differenceEuclidean, nearest } from "culori";

import colorNameList from "../../../lib/converted_colors.json";
import { nearest, differenceEuclidean } from "culori";

export async function POST(req: Request) {
const { colors } = await req.json();
Expand Down
89 changes: 42 additions & 47 deletions apps/web/app/api/figma-plugin/generate-color-palette/route.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,90 @@
import { NextResponse } from "next/server";
import jwt from "jsonwebtoken";
import { BaseColorTypes, BaseColors, ColorMode, RawColor } from "@/types/app";
import { generateColorPalette } from "@/lib/colorCalculator";
import { generateColorPalette } from '@/lib/colorCalculator'
import { BaseColorTypes, BaseColors, ColorMode } from '@/types/app'
import jwt from 'jsonwebtoken'
import { NextResponse } from 'next/server'

const headers = (origin: string) => ({
"Access-Control-Allow-Origin": origin || "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Content-Type, Authorization, Accept",
});
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Accept',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Origin': origin || '*',
})

export async function OPTIONS(request: Request) {
const origin = request.headers.get("origin");
const origin = request.headers.get('origin')

return new Response(null, {
status: 200,
headers: headers(origin),
});
status: 200,
})
}

export async function GET(req: Request) {
const origin = req.headers.get("origin");
const origin = req.headers.get('origin')

const { searchParams } = new URL(req.url);
const token = searchParams.get("token");
const authSecret = process.env.FIGMA_AUTH_SECRET;
const { searchParams } = new URL(req.url)
const token = searchParams.get('token')
const authSecret = process.env.FIGMA_AUTH_SECRET
// decode the token using the same secret key
// we should get the base colors
let baseColors: Partial<BaseColors>;
let baseColors: Partial<BaseColors>
try {
baseColors = jwt.verify(token, authSecret);
baseColors = jwt.verify(token, authSecret)
} catch (error) {
console.log(`====> Error:`, error);
console.log(`====> Error:`, error)
return NextResponse.json(
{
data: null,
error: {
message: "Invalid token",
message: 'Invalid token',
},
},
{
status: 400,
headers: headers(origin),
},
);
status: 400,
}
)
}
const { primary, secondary, accent } = baseColors as BaseColors;
const { accent, primary, secondary } = baseColors as BaseColors
if (!primary || !secondary || !accent) {
return NextResponse.json(
{
data: null,
error: {
message: "Missing primary, secondary or accent color",
message: 'Missing primary, secondary or accent color',
},
},
{
status: 400,
headers: headers(origin),
},
);
status: 400,
}
)
}
// !TODO: get mode from query params
const mode = ColorMode.HEX;
const [primaryPalette, secondaryPalette, accentPalette, grayPalette] = [
"primary",
"secondary",
"accent",
"gray",
].map((color) =>
generateColorPalette({
color: color === "gray" ? baseColors.primary : baseColors[color],
type: color as BaseColorTypes,
colorMode: mode,
}),
);
const mode = ColorMode.HEX
const [primaryPalette, secondaryPalette, accentPalette, grayPalette] = ['primary', 'secondary', 'accent', 'gray'].map(
(color) =>
generateColorPalette({
color: color === 'gray' ? baseColors.primary : baseColors[color],
type: color as BaseColorTypes,
})
)
return NextResponse.json(
{
error: null,
data: {
baseColors,
colorMode: mode,
colorPalettes: {
primary: primaryPalette,
secondary: secondaryPalette,
accent: accentPalette,
gray: grayPalette,
primary: primaryPalette,
secondary: secondaryPalette,
},
colorMode: mode,
},
error: null,
},
{
headers: headers(origin),
},
);
}
)
}
22 changes: 11 additions & 11 deletions apps/web/app/api/figma-plugin/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { BaseColors, RawColor } from "@/types/app";
import jwt from "jsonwebtoken";
import { NextResponse } from "next/server";
import tinycolor from "tinycolor2";
import jwt from "jsonwebtoken";
import { BaseColors, RawColor } from "@/types/app";

const headers = (origin: string) => ({
"Access-Control-Allow-Origin": origin || "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Content-Type, Authorization, Accept",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Origin": origin || "*",
});

export async function OPTIONS(request: Request) {
const origin = request.headers.get("origin");

return new Response(null, {
status: 200,
headers: headers(origin),
status: 200,
});
}

Expand Down Expand Up @@ -64,12 +64,12 @@ export async function GET(req: Request) {
},
},
{
status: 400,
headers: headers(origin),
status: 400,
},
);
}
const { primary, secondary, accent } = baseColors as BaseColors;
const { accent, primary, secondary } = baseColors as BaseColors;
if (!primary || !secondary || !accent) {
return NextResponse.json(
{
Expand All @@ -79,12 +79,12 @@ export async function GET(req: Request) {
},
},
{
status: 400,
headers: headers(origin),
status: 400,
},
);
}
const v = (color: string | RawColor) => tinycolor(color).isValid();
const v = (color: RawColor | string) => tinycolor(color).isValid();
if (!v(primary) || !v(secondary) || !v(accent)) {
return NextResponse.json(
{
Expand All @@ -94,14 +94,14 @@ export async function GET(req: Request) {
},
},
{
status: 400,
headers: headers(origin),
status: 400,
},
);
}
return NextResponse.json(
{
data: { primary, secondary, accent },
data: { accent, primary, secondary },
error: null,
},
{
Expand Down

0 comments on commit 23b2a98

Please sign in to comment.