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
Changes from 5 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
23 changes: 16 additions & 7 deletions src/routes/v1_categories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { HonoContext } from "../types";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { HTTPException } from "hono/http-exception";
import { versionMiddleware } from "../middlewares/version";
import { EmojiCategorySchema } from "../schemas";
import { createError } from "../utils";
import { cache, createError } from "../utils";
import { ALL_CATEGORIES_ROUTE, GET_CATEGORY_ROUTE } from "./v1_categories.openapi";

export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/v1/categories/:version");
@@ -12,10 +13,12 @@ V1_CATEGORIES_ROUTER.use(versionMiddleware);
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`);
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();
@@ -28,6 +31,8 @@ V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {

const categories = result.data;

cache(c, 3600, true);

return c.json(
categories,
200,
@@ -38,10 +43,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();
@@ -54,6 +61,8 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {

const category = result.data.find((c) => c.slug === categorySlug);

cache(c, 3600, true);

return c.json(
category,
200,
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 workerd@1.20250317.0 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 {}
22 changes: 20 additions & 2 deletions wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -13,6 +13,12 @@
"GITHUB_TOKEN": "",
"API_VERSION": "x.y.z"
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis"
}
],
"placement": { "mode": "smart" },
"env": {
"preview": {
@@ -24,7 +30,13 @@
"route": {
"custom_domain": true,
"pattern": "api.preview.mojis.dev"
}
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis-preview"
}
]
},
"production": {
"name": "mojis-api",
@@ -35,7 +47,13 @@
"route": {
"custom_domain": true,
"pattern": "api.mojis.dev"
}
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis"
}
]
}
}
}

Unchanged files with check annotations Beta

const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);

Check failure on line 42 in test/routes/v1_categories.test.ts

GitHub Actions / deploy

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ test/routes/v1_categories.test.ts:42:29
const data = await response.json() as EmojiCategory[];
expect(Array.isArray(data)).toBe(true);
expect(data[0]).toMatchObject({
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);

Check failure on line 58 in test/routes/v1_categories.test.ts

GitHub Actions / deploy

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ test/routes/v1_categories.test.ts:58:29
const data = await response.json();
expect(data).toMatchObject({
name: expect.any(String),