Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
barclayd committed Dec 8, 2024
0 parents commit 127afb0
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .turbo/turbo-test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

$ bun test
bun test v1.1.38 (bf2f153f)

src/index.test.ts:
✓ swipe saves data to database [1.78ms]

 1 pass
 0 fail
2 expect() calls
Ran 1 tests across 1 files. [20.00ms]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To install dependencies:
```sh
bun install
```

To run:
```sh
bun run dev
```

open http://localhost:3000
37 changes: 37 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noForEach": "off"
},
"correctness": {
"noUnusedVariables": "error",
"noUnusedImports": "error"
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
}
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "server",
"scripts": {
"dev": "bun run --hot src/index.ts",
"lint": "bunx biome check --write .",
"test": "bun test",
"types": "tsc --noEmit"
},
"dependencies": {
"@trpc/server": "^11.0.0-rc.660",
"hono": "^4.6.13",
"uuid": "^11.0.3",
"zod": "^3.23.8"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@hono/trpc-server": "0.3.4",
"@types/bun": "latest"
}
}
19 changes: 19 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Context as HonoContext } from 'hono';
import { setCookie } from 'hono/cookie';
import type { BlankEnv, BlankInput } from 'hono/types';

export async function createContext(
c?: HonoContext<BlankEnv, '/trpc/*', BlankInput>,
) {
return {
c,
setUserIdCookie: (userId: string) => {
if (c === undefined) {
return;
}
setCookie(c, 'userId', userId, {});
},
};
}

export type Context = Awaited<ReturnType<typeof createContext>>;
16 changes: 16 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from 'bun:test';
import { createContext } from './context';
import { createCaller } from './router';
import { updateSchema } from './schemas';

test('swipe saves data to database', async () => {
const ctx = await createContext();
const caller = createCaller(ctx);

const data = await caller.get();

const result = updateSchema.safeParse(data);

expect(result.data?.status).toBe('active');
expect(result.data?.id).toBe(4);
});
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { Hono } from 'hono';
import { createContext } from './context';
import { appRouter } from './router';

const app = new Hono();

app.all('/trpc/*', async (c) => {
return await fetchRequestHandler({
endpoint: '/trpc',
req: c.req.raw,
router: appRouter,
createContext: () => createContext(c),
});
});

export default app;
23 changes: 23 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { initTRPC } from '@trpc/server';
import type { Context } from './context';
import { updateSchema } from './schemas';

const t = initTRPC.context<Context>().create();

const publicProcedure = t.procedure;
const router = t.router;
export const createCallerFactory = t.createCallerFactory;

export const appRouter = router({
update: publicProcedure.input(updateSchema).mutation(() => {
}),
get: publicProcedure.query(() => {
return {
id: 4,
status: 'active'
}
}),
});

export const createCaller = createCallerFactory(appRouter);
export type AppRouter = typeof appRouter;
7 changes: 7 additions & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod';
import { status } from './types';

export const updateSchema = z.object({
id: z.number(),
status: z.enum(status),
});
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const status = ['active'] as const;
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
}
}

0 comments on commit 127afb0

Please sign in to comment.