feat(WEB-127): add trpc to handle requests both serverside and clientside

This commit is contained in:
Simon Emanuelsson
2024-03-20 16:39:11 +01:00
parent 2087ac6c91
commit ec4da5798b
31 changed files with 422 additions and 40 deletions

View File

@@ -0,0 +1,5 @@
import { mergeRouters } from "@/server/trpc"
import { userQueryRouter } from "./query"
export const userRouter = mergeRouters(userQueryRouter)

View File

@@ -0,0 +1,3 @@
/**
* Add route inputs (both query & mutation)
*/

View File

@@ -0,0 +1,3 @@
/**
* Add User mutations
*/

View File

@@ -0,0 +1,27 @@
import { z } from "zod"
/**
* Return value from jsonplaceholder.com/users/1
* Add proper user object expectation when fetching
* from Scandic API
*/
export const getUserSchema = z.object({
address: z.object({
city: z.string(),
geo: z.object({}),
street: z.string(),
suite: z.string(),
zipcode: z.string(),
}),
company: z.object({
bs: z.string(),
catchPhrase: z.string(),
name: z.string(),
}),
email: z.string().email(),
id: z.number(),
name: z.string(),
phone: z.string(),
username: z.string(),
website: z.string(),
})

View File

@@ -0,0 +1,25 @@
import { badRequestError, internalServerError } from "@/server/errors"
import { protectedProcedure, router } from "@/server/trpc"
import { getUserSchema } from "./output"
export const userQueryRouter = router({
get: protectedProcedure.query(async function (opts) {
// TODO: Make request to get user data from Scandic API
const response = await fetch(
"https://jsonplaceholder.typicode.com/users/1",
{
cache: "no-store",
}
)
if (!response.ok) {
throw internalServerError()
}
const json = await response.json()
const validJson = getUserSchema.parse(json)
if (!validJson) {
throw badRequestError()
}
return validJson
}),
})