-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initialises trpc api package (#110)
- Loading branch information
Showing
43 changed files
with
1,606 additions
and
797 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,4 +2,6 @@ pnpm-lock.yaml | |
.next | ||
coverage | ||
storybook-static | ||
dist | ||
dist | ||
playwright-report | ||
test-results |
This file contains 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 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 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,12 @@ | ||
import { api } from '@/utils/api'; | ||
|
||
export const Greeting = () => { | ||
const { data, isLoading, error } = api.greeting.hello.useQuery({ | ||
name: 'John Doe', | ||
}); | ||
|
||
if (isLoading) return <p>Loading...</p>; | ||
if (error) return <p>{error.message}</p>; | ||
|
||
return <p>{data.message}</p>; | ||
}; |
This file contains 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 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 { createNextApiHandler } from '@trpc/server/adapters/next'; | ||
|
||
import { appRouter, createContext } from '@noodle/api'; | ||
|
||
export default createNextApiHandler({ | ||
router: appRouter, | ||
createContext, | ||
}); |
This file contains 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 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,11 +1,11 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { render, screen } from '@noodle/test-utils/renderer'; | ||
|
||
import Home from '@/pages/index'; | ||
|
||
describe('Home page', () => { | ||
it('should render the home page', () => { | ||
it('should render the home page', async () => { | ||
render(<Home />); | ||
|
||
expect(screen.getByText(/home page/i)).toBeInTheDocument(); | ||
expect(await screen.findByText(/hello john doe/i)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains 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,21 @@ | ||
import { httpBatchLink } from '@trpc/client'; | ||
import { createTRPCNext } from '@trpc/next'; | ||
import SuperJSON from 'superjson'; | ||
|
||
import { type AppRouter } from '@noodle/api'; | ||
|
||
import { getBaseUrl } from './getBaseUrl'; | ||
|
||
export const api = createTRPCNext<AppRouter>({ | ||
config() { | ||
return { | ||
transformer: SuperJSON, | ||
links: [ | ||
httpBatchLink({ | ||
url: `${getBaseUrl()}/api/trpc`, | ||
}), | ||
], | ||
}; | ||
}, | ||
ssr: false, | ||
}); |
This file contains 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 @@ | ||
export const getBaseUrl = () => { | ||
if (typeof window !== 'undefined') return ''; | ||
if (process.env['VERCEL_URL']) return `https://${process.env['VERCEL_URL']}`; | ||
return `http://localhost:${process.env['PORT'] ?? 3000}`; | ||
}; |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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,48 @@ | ||
{ | ||
"name": "@noodle/api", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "Noodle's tRPC API", | ||
"license": "AGPL-3.0", | ||
"author": "Ahmed Elsakaan", | ||
"type": "module", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"main": "./dist/index.js", | ||
"typesVersions": { | ||
"*": { | ||
".": [ | ||
"./dist/index.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"dev": "pnpm run build --watch", | ||
"lint": "eslint . --max-warnings 0 --report-unused-disable-directives", | ||
"lint:fix": "pnpm run lint --fix", | ||
"test": "vitest run", | ||
"test:coverage": "vitest run --coverage", | ||
"test:ui": "vitest --ui", | ||
"test:watch": "vitest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@trpc/next": "^10.32.0", | ||
"@trpc/server": "^10.32.0", | ||
"superjson": "^1.12.3", | ||
"zod": "^3.21.4" | ||
}, | ||
"devDependencies": { | ||
"@noodle/tsconfig": "workspace:^", | ||
"@noodle/vitest": "workspace:^", | ||
"@vitest/coverage-v8": "^0.32.2", | ||
"eslint-config-noodle": "workspace:^", | ||
"vite": "^4.3.9", | ||
"vitest": "^0.32.2" | ||
} | ||
} |
This file contains 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,2 @@ | ||
export * from './setup/context'; | ||
export * from './root'; |
This file contains 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 { greetingRouter } from './routers/greeting'; | ||
import { createRouter } from './setup/trpc'; | ||
|
||
export const appRouter = createRouter({ | ||
greeting: greetingRouter, | ||
}); | ||
|
||
export type AppRouter = typeof appRouter; |
This file contains 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 { greetingRouter } from '.'; | ||
import { createInnerContext } from '../../setup/context'; | ||
|
||
describe('Hello router', () => { | ||
it('should return hello world without inputs', async () => { | ||
const context = createInnerContext({}); | ||
const caller = greetingRouter.createCaller(context); | ||
|
||
const result = await caller.hello({}); | ||
|
||
expect(result).toEqual({ | ||
message: 'Hello World', | ||
}); | ||
}); | ||
}); |
This file contains 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,17 @@ | ||
import { z } from 'zod'; | ||
|
||
import { createRouter, publicProcedure } from '../../setup/trpc'; | ||
|
||
export const greetingRouter = createRouter({ | ||
hello: publicProcedure | ||
.input( | ||
z.object({ | ||
name: z.string().optional(), | ||
}), | ||
) | ||
.query(({ input }) => { | ||
return { | ||
message: `Hello ${input.name ?? 'World'}`, | ||
}; | ||
}), | ||
}); |
This file contains 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,19 @@ | ||
import { type CreateNextContextOptions } from '@trpc/server/adapters/next'; | ||
|
||
// TODO: add session as an option to provide to context type | ||
// eslint-disable-next-line | ||
type ContextOptions = {}; | ||
|
||
export const createInnerContext = (opts: ContextOptions) => { | ||
// TODO: create the db connection here and pass it in the return object | ||
return { | ||
...opts, | ||
}; | ||
}; | ||
|
||
// TODO: get the session here and pass it to the inner function | ||
export const createContext = (_: CreateNextContextOptions) => { | ||
return createInnerContext({}); | ||
}; | ||
|
||
export type Context = typeof createContext; |
This file contains 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,25 @@ | ||
import { initTRPC } from '@trpc/server'; | ||
import SuperJSON from 'superjson'; | ||
import { ZodError } from 'zod'; | ||
|
||
import { type Context } from './context'; | ||
|
||
export const t = initTRPC.context<Context>().create({ | ||
transformer: SuperJSON, | ||
errorFormatter({ shape, error }) { | ||
return { | ||
...shape, | ||
data: { | ||
...shape.data, | ||
zodError: | ||
error.cause instanceof ZodError ? error.cause.flatten() : null, | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export const { | ||
procedure: publicProcedure, | ||
router: createRouter, | ||
middleware, | ||
} = t; |
This file contains 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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"outDir": "dist", | ||
"declarationMap": true | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["src/**/*.test.ts", "src/**/*.spec.ts"] | ||
} |
This file contains 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,9 @@ | ||
{ | ||
"extends": "@noodle/tsconfig/base.json", | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"types": ["vitest/globals"] | ||
}, | ||
"exclude": ["./coverage/**/*"], | ||
"include": ["**/*.ts"] | ||
} |
Oops, something went wrong.