kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
ac2d0c41a7
commit
903d052548
|
@ -5,6 +5,7 @@ import * as middleware from '@/lib/middleware'
|
|||
|
||||
import { registerHealthCheck } from './health-check'
|
||||
import { registerV1ProjectsGetProject } from './projects/get-project'
|
||||
import { registerV1ProjectsListProjects } from './projects/list-projects'
|
||||
import { registerV1TeamsCreateTeam } from './teams/create-team'
|
||||
import { registerV1TeamsDeleteTeam } from './teams/delete-team'
|
||||
import { registerV1TeamsGetTeam } from './teams/get-team'
|
||||
|
@ -44,6 +45,7 @@ registerV1TeamsMembersDeleteTeamMember(pri)
|
|||
|
||||
// Projects crud
|
||||
registerV1ProjectsGetProject(pri)
|
||||
registerV1ProjectsListProjects(pri)
|
||||
|
||||
// Setup routes and middleware
|
||||
apiV1.route('/', pub)
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
import { createRoute, type OpenAPIHono } from '@hono/zod-openapi'
|
||||
|
||||
import type { AuthenticatedEnv } from '@/lib/types'
|
||||
import {
|
||||
db,
|
||||
eq,
|
||||
// populateProjectSchema,
|
||||
schema
|
||||
} from '@/db'
|
||||
import { db, eq, populateProjectSchema, schema } from '@/db'
|
||||
import { acl } from '@/lib/acl'
|
||||
import { assert, parseZodSchema } from '@/lib/utils'
|
||||
|
||||
|
@ -20,8 +15,8 @@ const route = createRoute({
|
|||
path: 'projects/{projectId}',
|
||||
security: [{ bearerAuth: [] }],
|
||||
request: {
|
||||
params: ProjectIdParamsSchema
|
||||
// query: populateProjectSchema
|
||||
params: ProjectIdParamsSchema,
|
||||
query: populateProjectSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
|
@ -42,11 +37,11 @@ export function registerV1ProjectsGetProject(
|
|||
) {
|
||||
return app.openapi(route, async (c) => {
|
||||
const { projectId } = c.req.valid('param')
|
||||
// const { populate = [] } = c.req.valid('query')
|
||||
const { populate = [] } = c.req.valid('query')
|
||||
|
||||
const project = await db.query.projects.findFirst({
|
||||
where: eq(schema.projects.id, projectId)
|
||||
// with: Object.fromEntries(populate.map((field) => [field, true]))
|
||||
where: eq(schema.projects.id, projectId),
|
||||
with: Object.fromEntries(populate.map((field) => [field, true]))
|
||||
})
|
||||
assert(project, 404, `Project not found "${projectId}"`)
|
||||
await acl(c, project, { label: 'Project' })
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
import { createRoute, type OpenAPIHono, z } from '@hono/zod-openapi'
|
||||
|
||||
import type { AuthenticatedEnv } from '@/lib/types'
|
||||
import { db, eq, paginationSchema, schema } from '@/db'
|
||||
import { parseZodSchema } from '@/lib/utils'
|
||||
|
||||
const route = createRoute({
|
||||
description: 'Lists projects the authenticated user has access to.',
|
||||
tags: ['projects'],
|
||||
operationId: 'listProjects',
|
||||
method: 'get',
|
||||
path: 'projects',
|
||||
security: [{ bearerAuth: [] }],
|
||||
request: {
|
||||
query: paginationSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: 'A list of projects',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: z.array(schema.projectSelectSchema)
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
// ...openApiErrorResponses
|
||||
}
|
||||
})
|
||||
|
||||
export function registerV1ProjectsListProjects(
|
||||
app: OpenAPIHono<AuthenticatedEnv>
|
||||
) {
|
||||
return app.openapi(route, async (c) => {
|
||||
const {
|
||||
offset = 0,
|
||||
limit = 10,
|
||||
sort = 'desc',
|
||||
sortBy = 'createdAt'
|
||||
} = c.req.valid('query')
|
||||
|
||||
const user = c.get('user')
|
||||
const teamMember = c.get('teamMember')
|
||||
const isAdmin = user.role === 'admin'
|
||||
|
||||
const projects = await db.query.projects.findMany({
|
||||
where: isAdmin
|
||||
? undefined
|
||||
: teamMember
|
||||
? eq(schema.projects.teamId, teamMember.teamId)
|
||||
: eq(schema.projects.userId, user.id),
|
||||
with: {
|
||||
lastPublishedDeployment: true
|
||||
},
|
||||
orderBy: (projects, { asc, desc }) => [
|
||||
sort === 'desc' ? desc(projects[sortBy]) : asc(projects[sortBy])
|
||||
],
|
||||
offset,
|
||||
limit
|
||||
})
|
||||
|
||||
return c.json(parseZodSchema(z.array(schema.projectSelectSchema), projects))
|
||||
})
|
||||
}
|
|
@ -6,10 +6,11 @@ import {
|
|||
pgTable,
|
||||
text
|
||||
} from '@fisch0920/drizzle-orm/pg-core'
|
||||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { deployments } from './deployment'
|
||||
import { projects } from './project'
|
||||
import { users } from './user'
|
||||
import { deployments, deploymentSelectSchema } from './deployment'
|
||||
import { projects, projectSelectSchema } from './project'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -114,24 +115,43 @@ const stripeValidSubscriptionStatuses = new Set([
|
|||
])
|
||||
|
||||
export const consumerSelectSchema = createSelectSchema(consumers)
|
||||
.openapi('Consumer')
|
||||
.omit({
|
||||
_stripeCustomerId: true
|
||||
})
|
||||
.extend({
|
||||
user: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' }),
|
||||
|
||||
export const consumerInsertSchema = createInsertSchema(consumers).pick({
|
||||
token: true,
|
||||
plan: true,
|
||||
env: true,
|
||||
coupon: true,
|
||||
source: true,
|
||||
userId: true,
|
||||
projectId: true,
|
||||
deploymentId: true
|
||||
})
|
||||
project: z
|
||||
.lazy(() => projectSelectSchema)
|
||||
.optional()
|
||||
.openapi('Project', { type: 'object' }),
|
||||
|
||||
export const consumerUpdateSchema = createUpdateSchema(consumers).refine(
|
||||
(consumer) => {
|
||||
deployment: z
|
||||
.lazy(() => deploymentSelectSchema)
|
||||
.optional()
|
||||
.openapi('Deployment', { type: 'object' })
|
||||
})
|
||||
.openapi('Consumer')
|
||||
|
||||
export const consumerInsertSchema = createInsertSchema(consumers)
|
||||
.pick({
|
||||
token: true,
|
||||
plan: true,
|
||||
env: true,
|
||||
coupon: true,
|
||||
source: true,
|
||||
userId: true,
|
||||
projectId: true,
|
||||
deploymentId: true
|
||||
})
|
||||
.strict()
|
||||
|
||||
export const consumerUpdateSchema = createUpdateSchema(consumers)
|
||||
.strict()
|
||||
.refine((consumer) => {
|
||||
return {
|
||||
...consumer,
|
||||
enabled:
|
||||
|
@ -139,5 +159,4 @@ export const consumerUpdateSchema = createUpdateSchema(consumers).refine(
|
|||
(consumer.status &&
|
||||
stripeValidSubscriptionStatuses.has(consumer.status))
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
|
|
@ -10,14 +10,14 @@ import {
|
|||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { projects } from './project'
|
||||
import { teams } from './team'
|
||||
import { teams, teamSelectSchema } from './team'
|
||||
import {
|
||||
type Coupon,
|
||||
couponSchema,
|
||||
type PricingPlan,
|
||||
pricingPlanSchema
|
||||
} from './types'
|
||||
import { users } from './user'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -114,6 +114,17 @@ export const deploymentSelectSchema = createSelectSchema(deployments, {
|
|||
.omit({
|
||||
_url: true
|
||||
})
|
||||
.extend({
|
||||
user: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' }),
|
||||
|
||||
team: z
|
||||
.lazy(() => teamSelectSchema)
|
||||
.optional()
|
||||
.openapi('Team', { type: 'object' })
|
||||
})
|
||||
.openapi('Deployment')
|
||||
|
||||
export const deploymentInsertSchema = createInsertSchema(deployments, {
|
||||
|
@ -133,13 +144,17 @@ export const deploymentInsertSchema = createInsertSchema(deployments, {
|
|||
// env: z.object({}),
|
||||
pricingPlans: z.array(pricingPlanSchema),
|
||||
coupons: z.array(couponSchema).optional()
|
||||
}).omit({ id: true, createdAt: true, updatedAt: true })
|
||||
|
||||
export const deploymentUpdateSchema = createUpdateSchema(deployments).pick({
|
||||
enabled: true,
|
||||
published: true,
|
||||
version: true,
|
||||
description: true
|
||||
})
|
||||
.omit({ id: true, createdAt: true, updatedAt: true })
|
||||
.strict()
|
||||
|
||||
export const deploymentUpdateSchema = createUpdateSchema(deployments)
|
||||
.pick({
|
||||
enabled: true,
|
||||
published: true,
|
||||
version: true,
|
||||
description: true
|
||||
})
|
||||
.strict()
|
||||
|
||||
// TODO: add admin select schema which includes all fields?
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { relations } from '@fisch0920/drizzle-orm'
|
||||
import { index, pgTable, text } from '@fisch0920/drizzle-orm/pg-core'
|
||||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { consumers } from './consumer'
|
||||
import { deployments } from './deployment'
|
||||
import { projects } from './project'
|
||||
import { users } from './user'
|
||||
import { consumers, consumerSelectSchema } from './consumer'
|
||||
import { deployments, deploymentSelectSchema } from './deployment'
|
||||
import { projects, projectSelectSchema } from './project'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -78,11 +79,34 @@ export const logEntriesRelations = relations(logEntries, ({ one }) => ({
|
|||
})
|
||||
}))
|
||||
|
||||
export const logEntrySelectSchema =
|
||||
createSelectSchema(logEntries).openapi('LogEntry')
|
||||
export const logEntrySelectSchema = createSelectSchema(logEntries)
|
||||
.extend({
|
||||
user: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' }),
|
||||
|
||||
export const logEntryInsertSchema = createInsertSchema(logEntries).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
})
|
||||
project: z
|
||||
.lazy(() => projectSelectSchema)
|
||||
.optional()
|
||||
.openapi('Project', { type: 'object' }),
|
||||
|
||||
deployment: z
|
||||
.lazy(() => deploymentSelectSchema)
|
||||
.optional()
|
||||
.openapi('Deployment', { type: 'object' }),
|
||||
|
||||
consumer: z
|
||||
.lazy(() => consumerSelectSchema)
|
||||
.optional()
|
||||
.openapi('Consumer', { type: 'object' })
|
||||
})
|
||||
.openapi('LogEntry')
|
||||
|
||||
export const logEntryInsertSchema = createInsertSchema(logEntries)
|
||||
.omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
})
|
||||
.strict()
|
||||
|
|
|
@ -12,10 +12,10 @@ import { z } from '@hono/zod-openapi'
|
|||
|
||||
import { getProviderToken } from '@/lib/auth/get-provider-token'
|
||||
|
||||
import { deployments } from './deployment'
|
||||
import { teams } from './team'
|
||||
import { deployments, deploymentSelectSchema } from './deployment'
|
||||
import { teams, teamSelectSchema } from './team'
|
||||
import { type Webhook } from './types'
|
||||
import { users } from './user'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -144,11 +144,11 @@ export const projectRelationsSchema: z.ZodType<ProjectRelationFields> = z.enum([
|
|||
'team',
|
||||
'lastPublishedDeployment',
|
||||
'lastDeployment'
|
||||
// 'deployments',
|
||||
// 'publishedDeployments'
|
||||
])
|
||||
|
||||
export const projectSelectSchema = createSelectSchema(projects, {
|
||||
applicationFeePercent: (schema) => schema.nonnegative(),
|
||||
|
||||
stripeMetricProductIds: z.record(z.string(), z.string()).optional()
|
||||
// _webhooks: z.array(webhookSchema),
|
||||
// _stripeCouponIds: z.record(z.string(), z.string()).optional(),
|
||||
|
@ -171,6 +171,27 @@ export const projectSelectSchema = createSelectSchema(projects, {
|
|||
_stripePlanIds: true,
|
||||
_stripeAccountId: true
|
||||
})
|
||||
.extend({
|
||||
user: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' }),
|
||||
|
||||
team: z
|
||||
.lazy(() => teamSelectSchema)
|
||||
.optional()
|
||||
.openapi('Team', { type: 'object' }),
|
||||
|
||||
lastPublishedDeployment: z
|
||||
.lazy(() => deploymentSelectSchema)
|
||||
.optional()
|
||||
.openapi('Deployment', { type: 'object' }),
|
||||
|
||||
lastDeployment: z
|
||||
.lazy(() => deploymentSelectSchema)
|
||||
.optional()
|
||||
.openapi('Deployment', { type: 'object' })
|
||||
})
|
||||
.openapi('Project')
|
||||
|
||||
export const projectInsertSchema = createInsertSchema(projects, {
|
||||
|
@ -189,6 +210,7 @@ export const projectInsertSchema = createInsertSchema(projects, {
|
|||
name: true,
|
||||
userId: true
|
||||
})
|
||||
.strict()
|
||||
.refine((data) => {
|
||||
return {
|
||||
...data,
|
||||
|
@ -197,7 +219,7 @@ export const projectInsertSchema = createInsertSchema(projects, {
|
|||
})
|
||||
|
||||
// TODO: narrow update schema
|
||||
export const projectUpdateSchema = createUpdateSchema(projects)
|
||||
export const projectUpdateSchema = createUpdateSchema(projects).strict()
|
||||
|
||||
export const projectDebugSelectSchema = createSelectSchema(projects).pick({
|
||||
id: true,
|
||||
|
|
|
@ -5,9 +5,10 @@ import {
|
|||
pgTable,
|
||||
primaryKey
|
||||
} from '@fisch0920/drizzle-orm/pg-core'
|
||||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { teams } from './team'
|
||||
import { users } from './user'
|
||||
import { teams, teamSelectSchema } from './team'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -59,14 +60,29 @@ export const teamMembersRelations = relations(teamMembers, ({ one }) => ({
|
|||
})
|
||||
}))
|
||||
|
||||
export const teamMemberSelectSchema =
|
||||
createSelectSchema(teamMembers).openapi('TeamMember')
|
||||
export const teamMemberSelectSchema = createSelectSchema(teamMembers)
|
||||
.extend({
|
||||
user: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' }),
|
||||
|
||||
export const teamMemberInsertSchema = createInsertSchema(teamMembers).pick({
|
||||
userId: true,
|
||||
role: true
|
||||
})
|
||||
team: z
|
||||
.lazy(() => teamSelectSchema)
|
||||
.optional()
|
||||
.openapi('Team', { type: 'object' })
|
||||
})
|
||||
.openapi('TeamMember')
|
||||
|
||||
export const teamMemberUpdateSchema = createUpdateSchema(teamMembers).pick({
|
||||
role: true
|
||||
})
|
||||
export const teamMemberInsertSchema = createInsertSchema(teamMembers)
|
||||
.pick({
|
||||
userId: true,
|
||||
role: true
|
||||
})
|
||||
.strict()
|
||||
|
||||
export const teamMemberUpdateSchema = createUpdateSchema(teamMembers)
|
||||
.pick({
|
||||
role: true
|
||||
})
|
||||
.strict()
|
||||
|
|
|
@ -6,9 +6,10 @@ import {
|
|||
text,
|
||||
uniqueIndex
|
||||
} from '@fisch0920/drizzle-orm/pg-core'
|
||||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { teamMembers } from './team-member'
|
||||
import { users } from './user'
|
||||
import { users, userSelectSchema } from './user'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
|
@ -45,16 +46,27 @@ export const teamsRelations = relations(teams, ({ one, many }) => ({
|
|||
members: many(teamMembers)
|
||||
}))
|
||||
|
||||
export const teamSelectSchema = createSelectSchema(teams).openapi('Team')
|
||||
export const teamSelectSchema = createSelectSchema(teams)
|
||||
.extend({
|
||||
owner: z
|
||||
.lazy(() => userSelectSchema)
|
||||
.optional()
|
||||
.openapi('User', { type: 'object' })
|
||||
})
|
||||
.openapi('Team')
|
||||
|
||||
export const teamInsertSchema = createInsertSchema(teams, {
|
||||
slug: (schema) =>
|
||||
schema.refine((slug) => validators.team(slug), {
|
||||
message: 'Invalid team slug'
|
||||
})
|
||||
}).omit({ id: true, createdAt: true, updatedAt: true, ownerId: true })
|
||||
|
||||
export const teamUpdateSchema = createUpdateSchema(teams).pick({
|
||||
name: true,
|
||||
ownerId: true
|
||||
})
|
||||
.omit({ id: true, createdAt: true, updatedAt: true, ownerId: true })
|
||||
.strict()
|
||||
|
||||
export const teamUpdateSchema = createUpdateSchema(teams)
|
||||
.pick({
|
||||
name: true,
|
||||
ownerId: true
|
||||
})
|
||||
.strict()
|
||||
|
|
|
@ -89,6 +89,7 @@ export const userInsertSchema = createInsertSchema(users, {
|
|||
lastName: true,
|
||||
image: true
|
||||
})
|
||||
.strict()
|
||||
.refine((user) => {
|
||||
return {
|
||||
...user,
|
||||
|
@ -105,6 +106,7 @@ export const userUpdateSchema = createUpdateSchema(users)
|
|||
password: true,
|
||||
isStripeConnectEnabledByDefault: true
|
||||
})
|
||||
.strict()
|
||||
.refine((user) => {
|
||||
return {
|
||||
...user,
|
||||
|
|
|
@ -44,3 +44,5 @@ export type ConsumerWithProjectAndDeployment = BuildQueryResult<
|
|||
Tables['consumers'],
|
||||
{ with: { project: true; deployment: true } }
|
||||
>
|
||||
|
||||
export type LogEntry = z.infer<typeof schema.logEntrySelectSchema>
|
||||
|
|
|
@ -7,9 +7,6 @@ import { env } from '@/lib/env'
|
|||
|
||||
import { assert } from '../utils'
|
||||
|
||||
const token = await jwt.sign({ userId: 'test', type: 'user' }, env.JWT_SECRET)
|
||||
console.log({ token })
|
||||
|
||||
export const authenticate = createMiddleware<AuthenticatedEnv>(
|
||||
async function authenticateMiddleware(ctx, next) {
|
||||
const credentials = ctx.req.raw.headers.get('Authorization')
|
||||
|
|
Ładowanie…
Reference in New Issue