-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/lospec updated api #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
688dfc7
Refactor API to support sorting and pagination, update database integ…
wernerbihl 6c2f880
Implement API structure with health and Lospec palette routes, add CO…
wernerbihl 7950e7a
Add deployment workflow for development environment and update TODO list
wernerbihl 143db03
Add tests for app functionality, models, services, and syncing palettes
wernerbihl 034b974
Remove generated code coverage reports and related files
wernerbihl d8cb0f7
Update rate limiting to 10 requests per minute and adjust error messa…
wernerbihl b4641dd
Update rate limit exceeded error message to reflect a one-minute wait
wernerbihl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Test Dev | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| # Allows you to run this workflow manually from the Actions tab on GitHub. | ||
| workflow_dispatch: | ||
|
|
||
| # Allow this job to clone the repo and create a page deployment | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test-api: | ||
| name: Test API | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout your repository using git | ||
| uses: actions/checkout@v6.0.2 | ||
|
|
||
| - name: Set up Bun | ||
| uses: oven-sh/setup-bun@v2.2.0 | ||
|
|
||
| - name: Install dependencies | ||
| run: bun install | ||
|
|
||
| - name: Run tests with coverage gate | ||
| run: bun run test |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -26,4 +26,5 @@ dist-ssr | |
| # Cloudflare/Wrangler | ||
| .wrangler | ||
|
|
||
| .env | ||
| .env | ||
| coverage | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## Reporting a Vulnerability | ||
|
|
||
| Please contact admin@2dtiler.com with information about the security vulnerability. | ||
|
|
||
| We highly appreciate any vulnerability feedback! |
This file contains hidden or 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,3 +1,3 @@ | ||
| CI/CD | ||
| Create API endpoint for fetching releases | ||
| https://lospec.com/palette-list/load?colorNumberFilterType=any&page=0&tag=&sortingType=newest | ||
| Add unit tests and add to CI/CD | ||
| Update README.md | ||
| Push and deploy |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Hono } from "hono"; | ||
|
|
||
| import { registerHealthRoutes } from "./app/routes/health"; | ||
| import { registerLospecPaletteRoutes } from "./app/routes/lospec-palettes"; | ||
| import { corsMiddleware } from "./app/middleware/cors"; | ||
| import type { AppBindings } from "./config/types"; | ||
|
|
||
| const app = new Hono<AppBindings>(); | ||
|
|
||
| app.use("*", corsMiddleware); | ||
|
|
||
| registerHealthRoutes(app); | ||
| registerLospecPaletteRoutes(app); | ||
|
|
||
| export default app; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { Context } from "hono"; | ||
|
|
||
| import type { AppBindings } from "../../config/types"; | ||
|
|
||
| export function getHealth(c: Context<AppBindings>): Response { | ||
| return c.json({}); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { Context } from "hono"; | ||
|
|
||
| import { LOSPEC_PALETTES_PAGE_SIZE } from "../../config/constants"; | ||
| import type { AppBindings } from "../../config/types"; | ||
| import { | ||
| mapRowToResponse, | ||
| type ListLospecPalettesOptions, | ||
| } from "../models/lospec-palette"; | ||
| import { listPalettes } from "../services/lospec-palettes-repository"; | ||
|
|
||
| function parsePage(value: string | undefined): number | null { | ||
| if (value === undefined) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (!/^\d+$/.test(value)) { | ||
| return null; | ||
| } | ||
|
|
||
| return Number.parseInt(value, 10); | ||
| } | ||
|
|
||
| function normalizeQueryValue(value: string | undefined): string | undefined { | ||
| const trimmed = value?.trim(); | ||
| return trimmed ? trimmed : undefined; | ||
| } | ||
|
|
||
| function parseListLospecPalettesOptions( | ||
| query: Record<string, string | undefined>, | ||
| ): ListLospecPalettesOptions | null { | ||
| const page = parsePage(query.page); | ||
| if (page === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| page, | ||
| search: normalizeQueryValue(query.search), | ||
| tag: normalizeQueryValue(query.tags), | ||
| }; | ||
| } | ||
|
|
||
| export async function getLospecPalettes( | ||
| c: Context<AppBindings>, | ||
| ): Promise<Response> { | ||
| const options = parseListLospecPalettesOptions(c.req.query()); | ||
| if (!options) { | ||
| return c.json( | ||
| { | ||
| error: `Invalid page parameter. Expected a non-negative integer with ${LOSPEC_PALETTES_PAGE_SIZE} results per page.`, | ||
| }, | ||
| 400, | ||
| ); | ||
| } | ||
|
|
||
| const ip = c.req.raw.headers.get("CF-Connecting-IP") ?? "unknown"; | ||
| const { success } = await c.env.RATE_LIMITER.limit({ key: ip }); | ||
| if (!success) { | ||
| return c.json( | ||
| { error: "Rate limit exceeded. Try again in a minute." }, | ||
| 429, | ||
| ); | ||
| } | ||
|
|
||
| const palettes = await listPalettes(c.env.DB, options); | ||
| return c.json(palettes.map(mapRowToResponse)); | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import type { MiddlewareHandler } from "hono"; | ||
|
|
||
| import { | ||
| ALLOWED_ORIGINS, | ||
| INTERNAL_API_KEY_HEADER, | ||
| } from "../../config/constants"; | ||
| import type { AppBindings } from "../../config/types"; | ||
|
|
||
| function applyCorsHeaders(headers: Headers, origin: string): void { | ||
| headers.set("Access-Control-Allow-Origin", origin); | ||
| headers.set("Access-Control-Allow-Methods", "GET, OPTIONS"); | ||
| headers.set("Access-Control-Allow-Headers", "Content-Type"); | ||
| headers.set("Vary", "Origin"); | ||
| } | ||
|
|
||
| export const corsMiddleware: MiddlewareHandler<AppBindings> = async ( | ||
| c, | ||
| next, | ||
| ) => { | ||
| const origin = c.req.header("Origin"); | ||
| const internalApiKey = c.req.header(INTERNAL_API_KEY_HEADER); | ||
|
|
||
| if (c.env.INTERNAL_API_KEY && internalApiKey === c.env.INTERNAL_API_KEY) { | ||
| await next(); | ||
| return; | ||
| } | ||
|
|
||
| if (!origin) { | ||
| await next(); | ||
| return; | ||
| } | ||
|
|
||
| if (!ALLOWED_ORIGINS.has(origin)) { | ||
| return c.json({ error: "Forbidden origin" }, 403); | ||
| } | ||
|
|
||
| if (c.req.method === "OPTIONS") { | ||
| const headers = new Headers(); | ||
| applyCorsHeaders(headers, origin); | ||
| return new Response(null, { status: 204, headers }); | ||
| } | ||
|
|
||
| await next(); | ||
| applyCorsHeaders(c.res.headers, origin); | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { LOSPEC_CDN_BASE_URL } from "../../config/constants"; | ||
|
|
||
| export interface LospecPaletteApiItem { | ||
| _id: string; | ||
| title?: string; | ||
| slug?: string; | ||
| description?: string; | ||
| tags?: unknown; | ||
| user?: unknown; | ||
| colors?: unknown; | ||
| examples?: unknown; | ||
| publishedAt?: string; | ||
| } | ||
|
|
||
| export interface LospecPaletteRow { | ||
| id: string; | ||
| title: string | null; | ||
| slug: string | null; | ||
| description: string | null; | ||
| tags: string | null; | ||
| user: string | null; | ||
| colors: string | null; | ||
| examples: string | null; | ||
| published_at: string | null; | ||
| } | ||
|
|
||
| export interface LospecPaletteExample { | ||
| image: string; | ||
| description: string | null; | ||
| } | ||
|
|
||
| export interface LospecPaletteResponse { | ||
| id: string; | ||
| title: string | null; | ||
| slug: string | null; | ||
| description: string | null; | ||
| tags: unknown | null; | ||
| user: string | null; | ||
| colors: unknown | null; | ||
| examples: LospecPaletteExample[] | null; | ||
| published_at: string | null; | ||
| } | ||
|
|
||
| export interface ListLospecPalettesOptions { | ||
| page: number; | ||
| search?: string; | ||
| tag?: string; | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
|
|
||
| export function isLospecPaletteApiItem( | ||
| value: unknown, | ||
| ): value is LospecPaletteApiItem { | ||
| return isRecord(value) && typeof value._id === "string"; | ||
| } | ||
|
|
||
| function asNullableString(value: unknown): string | null { | ||
| return typeof value === "string" ? value : null; | ||
| } | ||
|
|
||
| function normalizePaletteUser(value: unknown): string | null { | ||
| if (typeof value === "string") { | ||
| return value; | ||
| } | ||
|
|
||
| if (!isRecord(value)) { | ||
| return null; | ||
| } | ||
|
|
||
| return asNullableString(value.name); | ||
| } | ||
|
|
||
| function normalizePaletteExampleImage(value: string): string { | ||
| return new URL(value, LOSPEC_CDN_BASE_URL).toString(); | ||
| } | ||
|
|
||
| function normalizePaletteExamples( | ||
| value: unknown, | ||
| ): LospecPaletteExample[] | null { | ||
| if (!Array.isArray(value)) { | ||
| return null; | ||
| } | ||
|
|
||
| return value.flatMap((example) => { | ||
| if (!isRecord(example)) { | ||
| return []; | ||
| } | ||
|
|
||
| const image = asNullableString(example.image); | ||
| if (!image) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| image: normalizePaletteExampleImage(image), | ||
| description: asNullableString(example.description), | ||
| }, | ||
| ]; | ||
| }); | ||
| } | ||
|
|
||
| function serializeJsonField(value: unknown): string | null { | ||
| if (value === undefined || value === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return JSON.stringify(value); | ||
| } | ||
|
|
||
| function deserializeJsonField(value: string | null): unknown | null { | ||
| if (!value) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(value) as unknown; | ||
| } catch { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| export function mapPaletteToRow( | ||
| palette: LospecPaletteApiItem, | ||
| ): LospecPaletteRow { | ||
| return { | ||
| id: palette._id, | ||
| title: asNullableString(palette.title), | ||
| slug: asNullableString(palette.slug), | ||
| description: asNullableString(palette.description), | ||
| tags: serializeJsonField(palette.tags), | ||
| user: normalizePaletteUser(palette.user), | ||
| colors: serializeJsonField(palette.colors), | ||
| examples: serializeJsonField(normalizePaletteExamples(palette.examples)), | ||
| published_at: asNullableString(palette.publishedAt), | ||
| }; | ||
| } | ||
|
|
||
| export function mapRowToResponse(row: LospecPaletteRow): LospecPaletteResponse { | ||
| const user = normalizePaletteUser(deserializeJsonField(row.user)); | ||
| const examples = normalizePaletteExamples(deserializeJsonField(row.examples)); | ||
|
|
||
| return { | ||
| id: row.id, | ||
| title: row.title, | ||
| slug: row.slug, | ||
| description: row.description, | ||
| tags: deserializeJsonField(row.tags), | ||
| user, | ||
| colors: deserializeJsonField(row.colors), | ||
| examples, | ||
| published_at: row.published_at, | ||
| }; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { Hono } from "hono"; | ||
|
|
||
| import type { AppBindings } from "../../config/types"; | ||
| import { getHealth } from "../controllers/health"; | ||
|
|
||
| export function registerHealthRoutes(app: Hono<AppBindings>): void { | ||
| app.get("/", getHealth); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { Hono } from "hono"; | ||
|
|
||
| import type { AppBindings } from "../../config/types"; | ||
| import { getLospecPalettes } from "../controllers/lospec-palettes"; | ||
|
|
||
| export function registerLospecPaletteRoutes(app: Hono<AppBindings>): void { | ||
| app.get("/lospec_palettes", getLospecPalettes); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.