♻️ (viewer) Remove barrel exports and flatten folder arch
This commit is contained in:
58
apps/viewer/src/helpers/api/dbRules.ts
Normal file
58
apps/viewer/src/helpers/api/dbRules.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import {
|
||||
CollaborationType,
|
||||
Prisma,
|
||||
User,
|
||||
WorkspaceRole,
|
||||
} from '@typebot.io/prisma'
|
||||
import { env } from '@typebot.io/lib'
|
||||
|
||||
const parseWhereFilter = (
|
||||
typebotIds: string[] | string,
|
||||
user: User,
|
||||
type: 'read' | 'write'
|
||||
): Prisma.TypebotWhereInput => ({
|
||||
OR: [
|
||||
{
|
||||
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
|
||||
collaborators: {
|
||||
some: {
|
||||
userId: user.id,
|
||||
type: type === 'write' ? CollaborationType.WRITE : undefined,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
|
||||
workspace:
|
||||
(type === 'read' && user.email === process.env.ADMIN_EMAIL) ||
|
||||
env('E2E_TEST') === 'true'
|
||||
? undefined
|
||||
: {
|
||||
members: {
|
||||
some: { userId: user.id, role: { not: WorkspaceRole.GUEST } },
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
export const canReadTypebot = (typebotId: string, user: User) =>
|
||||
parseWhereFilter(typebotId, user, 'read')
|
||||
|
||||
export const canWriteTypebot = (typebotId: string, user: User) =>
|
||||
parseWhereFilter(typebotId, user, 'write')
|
||||
|
||||
export const canReadTypebots = (typebotIds: string[], user: User) =>
|
||||
parseWhereFilter(typebotIds, user, 'read')
|
||||
|
||||
export const canWriteTypebots = (typebotIds: string[], user: User) =>
|
||||
parseWhereFilter(typebotIds, user, 'write')
|
||||
|
||||
export const canEditGuests = (user: User, typebotId: string) => ({
|
||||
id: typebotId,
|
||||
workspace: {
|
||||
members: {
|
||||
some: { userId: user.id, role: { not: WorkspaceRole.GUEST } },
|
||||
},
|
||||
},
|
||||
})
|
19
apps/viewer/src/helpers/authenticateUser.ts
Normal file
19
apps/viewer/src/helpers/authenticateUser.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { User } from '@typebot.io/prisma'
|
||||
import { NextApiRequest } from 'next'
|
||||
import prisma from '@/lib/prisma'
|
||||
|
||||
export const authenticateUser = async (
|
||||
req: NextApiRequest
|
||||
): Promise<User | undefined> => authenticateByToken(extractBearerToken(req))
|
||||
|
||||
const authenticateByToken = async (
|
||||
apiToken?: string
|
||||
): Promise<User | undefined> => {
|
||||
if (!apiToken) return
|
||||
return (await prisma.user.findFirst({
|
||||
where: { apiTokens: { some: { token: apiToken } } },
|
||||
})) as User
|
||||
}
|
||||
|
||||
const extractBearerToken = (req: NextApiRequest) =>
|
||||
req.headers['authorization']?.slice(7)
|
35
apps/viewer/src/helpers/server/context.ts
Normal file
35
apps/viewer/src/helpers/server/context.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { inferAsyncReturnType } from '@trpc/server'
|
||||
import * as trpcNext from '@trpc/server/adapters/next'
|
||||
import { User } from '@typebot.io/prisma'
|
||||
import { NextApiRequest } from 'next'
|
||||
|
||||
export async function createContext(opts: trpcNext.CreateNextContextOptions) {
|
||||
const user = await getAuthenticatedUser(opts.req)
|
||||
|
||||
return {
|
||||
user,
|
||||
}
|
||||
}
|
||||
|
||||
const getAuthenticatedUser = async (
|
||||
req: NextApiRequest
|
||||
): Promise<User | undefined> => {
|
||||
const bearerToken = extractBearerToken(req)
|
||||
if (!bearerToken) return
|
||||
return authenticateByToken(bearerToken)
|
||||
}
|
||||
|
||||
const authenticateByToken = async (
|
||||
apiToken: string
|
||||
): Promise<User | undefined> => {
|
||||
if (typeof window !== 'undefined') return
|
||||
return (await prisma.user.findFirst({
|
||||
where: { apiTokens: { some: { token: apiToken } } },
|
||||
})) as User
|
||||
}
|
||||
|
||||
const extractBearerToken = (req: NextApiRequest) =>
|
||||
req.headers['authorization']?.slice(7)
|
||||
|
||||
export type Context = inferAsyncReturnType<typeof createContext>
|
15
apps/viewer/src/helpers/server/generateOpenApi.ts
Normal file
15
apps/viewer/src/helpers/server/generateOpenApi.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { generateOpenApiDocument } from 'trpc-openapi'
|
||||
import { writeFileSync } from 'fs'
|
||||
import { appRouter } from './routers/v1/_app'
|
||||
|
||||
const openApiDocument = generateOpenApiDocument(appRouter, {
|
||||
title: 'Chat API',
|
||||
version: '1.0.0',
|
||||
baseUrl: 'https://typebot.io/api/v1',
|
||||
docsUrl: 'https://docs.typebot.io/api',
|
||||
})
|
||||
|
||||
writeFileSync(
|
||||
'./openapi/chat/_spec_.json',
|
||||
JSON.stringify(openApiDocument, null, 2)
|
||||
)
|
8
apps/viewer/src/helpers/server/routers/v1/_app.ts
Normal file
8
apps/viewer/src/helpers/server/routers/v1/_app.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { chatRouter } from '@/features/chat/api/router'
|
||||
import { router } from '../../trpc'
|
||||
|
||||
export const appRouter = router({
|
||||
chat: chatRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
22
apps/viewer/src/helpers/server/trpc.ts
Normal file
22
apps/viewer/src/helpers/server/trpc.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { initTRPC } from '@trpc/server'
|
||||
import { OpenApiMeta } from 'trpc-openapi'
|
||||
import superjson from 'superjson'
|
||||
import { Context } from './context'
|
||||
|
||||
const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
|
||||
transformer: superjson,
|
||||
})
|
||||
|
||||
const injectUser = t.middleware(({ next, ctx }) => {
|
||||
return next({
|
||||
ctx: {
|
||||
user: ctx.user,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const middleware = t.middleware
|
||||
|
||||
export const router = t.router
|
||||
|
||||
export const publicProcedure = t.procedure.use(injectUser)
|
17
apps/viewer/src/helpers/sessionStorage.ts
Normal file
17
apps/viewer/src/helpers/sessionStorage.ts
Normal file
@ -0,0 +1,17 @@
|
||||
const sessionStorageKey = 'resultId'
|
||||
|
||||
export const getExistingResultFromSession = () => {
|
||||
try {
|
||||
return sessionStorage.getItem(sessionStorageKey)
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
}
|
||||
|
||||
export const setResultInSession = (resultId: string) => {
|
||||
try {
|
||||
return sessionStorage.setItem(sessionStorageKey, resultId)
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user