Skip to content
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

feat: use r2 for data storage #15

Merged
merged 8 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "api.mojis.dev",
"type": "module",
"private": true,
"packageManager": "[email protected].4",
"packageManager": "[email protected].5",
"scripts": {
"dev": "wrangler dev",
"build": "wrangler deploy --dry-run --outdir=dist",
Expand All @@ -18,20 +18,20 @@
"dependencies": {
"@hono/zod-openapi": "^0.19.2",
"@mojis/internal-utils": "^0.0.5",
"@scalar/hono-api-reference": "^0.7.1",
"@scalar/hono-api-reference": "^0.7.2",
"hono": "^4.7.4",
"zod": "^3.24.2"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.8.1",
"@cloudflare/vitest-pool-workers": "^0.8.2",
"@luxass/eslint-config": "^4.17.1",
"@stoplight/spectral-cli": "^6.14.3",
"eslint": "^9.22.0",
"eslint-plugin-format": "^1.0.1",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"vitest": "^3.0.9",
"wrangler": "^4.1.0"
"wrangler": "^4.2.0"
},
"pnpm": {
"onlyBuiltDependencies": [
Expand Down
58 changes: 29 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/cache.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/routes/gateway_github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { HonoContext } from "../types";
import { OpenAPIHono } from "@hono/zod-openapi";
import { cache, createError } from "../utils";
import { cache } from "hono/cache";
import { createError } from "../utils";
import { GITHUB_EMOJIS_ROUTE } from "./gateway_github.openapi";

export const GATEWAY_GITHUB_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/gateway/github");

GATEWAY_GITHUB_ROUTER.get("*", cache({
cacheName: "github-emojis",
cacheControl: "max-age=3600, immutable",
}));
GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {
const response = await fetch("https://api.github.com/emojis", {
headers: {
Expand All @@ -24,7 +29,5 @@ GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {

const emojis = await response.json<Record<string, string>>();

cache(c, 3600, true);

return c.json(emojis, 200);
});
8 changes: 6 additions & 2 deletions src/routes/random-emoji.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { HonoContext } from "../types";
import { Hono } from "hono";
import { cache } from "../utils";
import { cache } from "hono/cache";

export const RANDOM_EMOJI_ROUTER = new Hono<HonoContext>();

RANDOM_EMOJI_ROUTER.get("/random-emoji.png", cache({
cacheName: "random-emoji",
cacheControl: "max-age=3600, immutable",
}));

RANDOM_EMOJI_ROUTER.get("/random-emoji.png", async (c) => {
cache(c, 60 * 60);
return c.redirect("https://image.luxass.dev/api/image/emoji");
});
25 changes: 17 additions & 8 deletions src/routes/v1_categories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { HonoContext } from "../types";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { cache } from "hono/cache";
import { HTTPException } from "hono/http-exception";
import { versionMiddleware } from "../middlewares/version";
import { EmojiCategorySchema } from "../schemas";
import { createError } from "../utils";
Expand All @@ -9,19 +11,24 @@ export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/ap

V1_CATEGORIES_ROUTER.use(versionMiddleware);

V1_CATEGORIES_ROUTER.get("*", cache({
cacheName: "v1-categories",
cacheControl: "max-age=3600, immutable",
}));

V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
const version = c.req.param("version");

const res = await fetch(`https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${version}/groups.json`);

if (!res.ok) {
return createError(c, 500, "failed to fetch categories");
const res = await c.env.EMOJI_DATA.get(`v${version}/groups.json`);
if (res == null) {
throw new HTTPException(500, {
message: "failed to fetch categories",
});
}

const data = await res.json();

const result = z.array(EmojiCategorySchema).safeParse(data);

if (!result.success) {
return createError(c, 500, "failed to parse categories");
}
Expand All @@ -38,10 +45,12 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
const version = c.req.param("version");
const categorySlug = c.req.param("category");

const res = await fetch(`https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${version}/groups.json`);
const res = await c.env.EMOJI_DATA.get(`v${version}/groups.json`);

if (!res.ok) {
return createError(c, 500, "failed to fetch categories");
if (res == null) {
throw new HTTPException(500, {
message: "failed to fetch categories",
});
}

const data = await res.json();
Expand Down
12 changes: 0 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,3 @@ export async function getAvailableVersions(): Promise<EmojiLock["versions"]> {

return result.data.versions;
}

export function cache<TCtx extends Context>(ctx: TCtx, age: number, immutable = false) {
if (age === -1) {
ctx.header("Expires", "0");
ctx.header("Pragma", "no-cache");
ctx.header("Cache-Control", "no-cache, no-store, must-revalidate");
return;
}

ctx.header("Expires", new Date(Date.now() + age * 1000).toUTCString());
ctx.header("Cache-Control", ["public", `max-age=${age}`, immutable ? "immutable" : null].filter((x) => !!x).join(", "));
};
29 changes: 28 additions & 1 deletion test/routes/v1_categories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,36 @@ import {
env,
waitOnExecutionContext,
} from "cloudflare:test";
import { describe, expect, it } from "vitest";
import { beforeAll, describe, expect, it } from "vitest";
import worker from "../../src";

beforeAll(async () => {
await env.EMOJI_DATA.put("v15.1/groups.json", JSON.stringify([
{
name: "Smileys & Emotion",
slug: "smileys-emotion",
subgroups: [
"face-smiling",
"face-affection",
"face-tongue",
"face-hand",
"face-neutral-skeptical",
"face-sleepy",
"face-unwell",
"face-hat",
"face-glasses",
"face-concerned",
"face-negative",
"face-costume",
"cat-face",
"monkey-face",
"heart",
"emotion",
],
},
]));
});

describe("v1_categories", () => {
it("should return 404 for non-existent version", async () => {
const request = new Request("https://api.mojis.dev/api/v1/categories/999.0");
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"resolveJsonModule": true,
"types": [
"./worker-configuration.d.ts",
"./worker-configuration-test.d.ts",
// for `cloudflare:test` types
"@cloudflare/vitest-pool-workers"
],
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineWorkersProject({
poolOptions: {
workers: {
singleWorker: true,
isolatedStorage: true,
miniflare: {
compatibilityFlags: ["nodejs_compat"],
bindings: {
Expand Down
5 changes: 5 additions & 0 deletions worker-configuration-test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "cloudflare:test" {
// eslint-disable-next-line ts/no-empty-object-type
interface ProvidedEnv extends CloudflareBindings {
}
}
3 changes: 2 additions & 1 deletion worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Generated by Wrangler by running `wrangler types --env-interface CloudflareBindings` (hash: e71cf0de9c927e7e3f2e02a5b583aba3)
// Generated by Wrangler by running `wrangler types --env-interface CloudflareBindings` (hash: c476c03844aa0429b0aacf2d2d7c8815)
// Runtime types generated with [email protected] 2025-03-13
declare namespace Cloudflare {
interface Env {
API_VERSION: "x.y.z";
ENVIRONMENT: "preview" | "production";
GITHUB_TOKEN: string;
EMOJI_DATA: R2Bucket;
}
}
interface CloudflareBindings extends Cloudflare.Env {}
Expand Down
Loading