Skip to content

Commit

Permalink
feat: Next API Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyr33x committed Feb 6, 2025
1 parent bf9ad6c commit 63e2801
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 210 deletions.
194 changes: 0 additions & 194 deletions apps/api/src/data/hooks.json

This file was deleted.

3 changes: 2 additions & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { react, root, next } from "./routes";
import { swagger } from "@elysiajs/swagger";
import { Server } from "./classes/server";
import { react, root } from "./routes";
import { ENV } from "./schema/server";
import { cors } from "@elysiajs/cors";
import { Elysia } from "elysia";
Expand All @@ -26,6 +26,7 @@ const app = new Elysia()
.use(cors())
.use(root)
.use(react)
.use(next)
.listen(server.port);

console.table(server);
1 change: 1 addition & 0 deletions apps/api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { react } from "./react";
export { next } from "./next";
export { root } from "./root";
18 changes: 18 additions & 0 deletions apps/api/src/routes/next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getHooks } from "../services/hooks.service";
import { Elysia, t } from "elysia";

export const next = new Elysia()
.get(
"/next",
({ query }) =>
getHooks({ search: query.search, limit: query.limit, type: "next" }),
{
query: t.Object({
search: t.Optional(t.String()),
limit: t.Optional(t.Integer()),
}),
},
)
.get("/next/:title", ({ params }) =>
getHooks({ search: params.title, type: "next" }),
);
7 changes: 5 additions & 2 deletions apps/api/src/routes/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { Elysia, t } from "elysia";
export const react = new Elysia()
.get(
"/react",
({ query }) => getHooks({ search: query.search, limit: query.limit }),
({ query }) =>
getHooks({ search: query.search, limit: query.limit, type: "react" }),
{
query: t.Object({
search: t.Optional(t.String()),
limit: t.Optional(t.Integer()),
}),
},
)
.get("/react/:title", ({ params }) => getHooks({ search: params.title }));
.get("/react/:title", ({ params }) =>
getHooks({ search: params.title, type: "react" }),
);
1 change: 1 addition & 0 deletions apps/api/src/routes/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const returnRoot = {
version: "1.0.0",
endpoints: {
react: ["/react", "/react/:title"],
next: ["/next", "/next/:title"],
},
};

Expand Down
33 changes: 25 additions & 8 deletions apps/api/src/services/hooks.service.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import { createFilter, createLimit } from "../utilities/creators";
import type { Hook } from "../types/hook";
import type { React, Next, CondHooks } from "../types/hook";

const path = "src/data/hooks.json";
const file = Bun.file(path);
const reactPath = "react.json";
const nextPath = "next.json";
const reactFile = Bun.file(reactPath);
const nextFile = Bun.file(nextPath);

type QueryParams = {
search?: string;
limit?: number;
type: "react" | "next";
};

type Response = Hook[];

async function getHooks({ search, limit }: QueryParams): Promise<Response> {
const hooks: Hook[] = await file.json();
type Response = CondHooks[];

async function getHooks({
search,
limit,
type,
}: QueryParams): Promise<Response> {
let hooks: CondHooks[];
if (type === "react") {
hooks = await reactFile.json();
} else if (type === "next") {
hooks = await nextFile.json();
} else {
const reactHooks: React[] = await reactFile.json();
const nextHooks: Next[] = await nextFile.json();
hooks = [...reactHooks, ...nextHooks];
}

const applySearch = search ? createFilter("title")(search) : () => true;
const filteredHooks = hooks.filter(applySearch);

const limitedHooks = limit
? createLimit(limit)(filteredHooks)
: filteredHooks;

if (!limitedHooks.length) {
throw new Error("Couldn't find the requsted hook.");
throw new Error("Couldn't find the requested hook.");
}

return limitedHooks;
Expand Down
16 changes: 15 additions & 1 deletion apps/api/src/types/hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
export type Hook = Readonly<{
export type React = Readonly<{
id: number;
title: string;
description: string;
content: string;
}>;

export type Next = Readonly<{
id: number;
title: string;
description: string;
content: Opts;
}>;

type Opts = {
server: string;
client: string;
};

export type CondHooks = Next | React;
9 changes: 5 additions & 4 deletions apps/api/src/utilities/creators.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { Hook } from "~/types/hook";
import type { CondHooks } from "~/types/hook";

const createFilter =
<T extends keyof Hook>(key: T) =>
<T extends keyof CondHooks>(key: T) =>
(search: string) =>
(hook: Hook) => {
(hook: CondHooks) => {
const value = hook[key];
if (typeof value === "string") {
return value.toLowerCase().includes(search.toLowerCase());
}
return false;
};

const createLimit = (limit: number) => (hooks: Hook[]) => hooks.slice(0, limit);
const createLimit = (limit: number) => (hooks: CondHooks[]) =>
hooks.slice(0, limit);

export { createFilter, createLimit };

0 comments on commit 63e2801

Please sign in to comment.