-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4611bf
commit f4bcc2b
Showing
3 changed files
with
56 additions
and
61 deletions.
There are no files selected for viewing
89 changes: 42 additions & 47 deletions
89
apps/web/app/api/v1/figma-plugin/generate-color-palette/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
); | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters