2
0

♻️ (viewer) Remove barrel exports and flatten folder arch

This commit is contained in:
Baptiste Arnaud
2023-03-15 12:21:52 +01:00
parent 44d7a0bcb8
commit f3af07b7ff
113 changed files with 398 additions and 426 deletions

View 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 } },
},
},
})

View 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)

View 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>

View 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)
)

View 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

View 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)

View 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 */
}
}