kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
83b8d9e31c
commit
3f1d1a4d4f
|
@ -17,7 +17,10 @@ import { registerV1UsersUpdateUser } from './users/update-user'
|
|||
|
||||
export const apiV1 = new OpenAPIHono()
|
||||
|
||||
// Public routes
|
||||
const pub = new OpenAPIHono()
|
||||
|
||||
// Private, authenticated routes
|
||||
const pri = new OpenAPIHono<AuthenticatedEnv>()
|
||||
|
||||
registerHealthCheck(pub)
|
||||
|
|
|
@ -1,21 +1,11 @@
|
|||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { teamSlugSchema } from '@/db'
|
||||
|
||||
export const TeamSlugParamsSchema = z.object({
|
||||
team: teamSlugSchema.openapi({
|
||||
param: {
|
||||
description: 'Team slug',
|
||||
name: 'team',
|
||||
in: 'path'
|
||||
}
|
||||
})
|
||||
})
|
||||
import { userIdSchema } from '@/db'
|
||||
|
||||
export const TeamMemberUserIdParamsSchema = z.object({
|
||||
userId: z.string().openapi({
|
||||
userId: userIdSchema.openapi({
|
||||
param: {
|
||||
description: 'Team member user id',
|
||||
description: 'Team member user ID',
|
||||
name: 'userId',
|
||||
in: 'path'
|
||||
}
|
||||
|
|
|
@ -1,28 +1,21 @@
|
|||
import { createRoute, type OpenAPIHono, z } from '@hono/zod-openapi'
|
||||
import { createRoute, type OpenAPIHono } from '@hono/zod-openapi'
|
||||
|
||||
import type { AuthenticatedEnv } from '@/lib/types'
|
||||
import { db, eq, schema, userIdSchema } from '@/db'
|
||||
import { db, eq, schema } from '@/db'
|
||||
import { acl } from '@/lib/acl'
|
||||
import { assert, parseZodSchema } from '@/lib/utils'
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
userId: userIdSchema.openapi({
|
||||
param: {
|
||||
name: 'userId',
|
||||
in: 'path'
|
||||
}
|
||||
})
|
||||
})
|
||||
import { UserIdParamsSchema } from './schemas'
|
||||
|
||||
const route = createRoute({
|
||||
description: 'Gets a user by ID.',
|
||||
description: 'Gets a user',
|
||||
tags: ['users'],
|
||||
operationId: 'getUser',
|
||||
method: 'get',
|
||||
path: 'users/{userId}',
|
||||
security: [{ bearerAuth: [] }],
|
||||
request: {
|
||||
params: ParamsSchema
|
||||
params: UserIdParamsSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import { z } from '@hono/zod-openapi'
|
||||
|
||||
import { userIdSchema } from '@/db'
|
||||
|
||||
export const UserIdParamsSchema = z.object({
|
||||
userId: userIdSchema.openapi({
|
||||
param: {
|
||||
description: 'User ID',
|
||||
name: 'userId',
|
||||
in: 'path'
|
||||
}
|
||||
})
|
||||
})
|
|
@ -1,18 +1,11 @@
|
|||
import { createRoute, type OpenAPIHono, z } from '@hono/zod-openapi'
|
||||
import { createRoute, type OpenAPIHono } from '@hono/zod-openapi'
|
||||
|
||||
import type { AuthenticatedEnv } from '@/lib/types'
|
||||
import { db, eq, schema, userIdSchema } from '@/db'
|
||||
import { db, eq, schema } from '@/db'
|
||||
import { acl } from '@/lib/acl'
|
||||
import { assert, parseZodSchema } from '@/lib/utils'
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
userId: userIdSchema.openapi({
|
||||
param: {
|
||||
name: 'userId',
|
||||
in: 'path'
|
||||
}
|
||||
})
|
||||
})
|
||||
import { UserIdParamsSchema } from './schemas'
|
||||
|
||||
const route = createRoute({
|
||||
description: 'Updates a user',
|
||||
|
@ -22,7 +15,7 @@ const route = createRoute({
|
|||
path: 'users/{userId}',
|
||||
security: [{ bearerAuth: [] }],
|
||||
request: {
|
||||
params: ParamsSchema,
|
||||
params: UserIdParamsSchema,
|
||||
body: {
|
||||
required: true,
|
||||
content: {
|
||||
|
|
|
@ -3,8 +3,7 @@ import {
|
|||
boolean,
|
||||
index,
|
||||
pgTable,
|
||||
primaryKey,
|
||||
text
|
||||
primaryKey
|
||||
} from '@fisch0920/drizzle-orm/pg-core'
|
||||
|
||||
import { teams } from './team'
|
||||
|
@ -15,6 +14,7 @@ import {
|
|||
createUpdateSchema,
|
||||
cuid,
|
||||
teamMemberRoleEnum,
|
||||
teamSlug,
|
||||
timestamp,
|
||||
timestamps
|
||||
} from './utils'
|
||||
|
@ -27,7 +27,7 @@ export const teamMembers = pgTable(
|
|||
userId: cuid()
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: 'cascade' }),
|
||||
teamSlug: text()
|
||||
teamSlug: teamSlug()
|
||||
.notNull()
|
||||
.references(() => teams.slug, { onDelete: 'cascade' }),
|
||||
teamId: cuid()
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
createUpdateSchema,
|
||||
cuid,
|
||||
id,
|
||||
teamSlug,
|
||||
timestamps
|
||||
} from './utils'
|
||||
|
||||
|
@ -24,7 +25,7 @@ export const teams = pgTable(
|
|||
id,
|
||||
...timestamps,
|
||||
|
||||
slug: text().notNull().unique(),
|
||||
slug: teamSlug().notNull().unique(),
|
||||
name: text().notNull(),
|
||||
|
||||
ownerId: cuid().notNull()
|
||||
|
|
|
@ -21,6 +21,7 @@ import {
|
|||
stripeId,
|
||||
timestamp,
|
||||
timestamps,
|
||||
username,
|
||||
userRoleEnum
|
||||
} from './utils'
|
||||
|
||||
|
@ -30,7 +31,7 @@ export const users = pgTable(
|
|||
id,
|
||||
...timestamps,
|
||||
|
||||
username: text().notNull().unique(),
|
||||
username: username().notNull().unique(),
|
||||
role: userRoleEnum().default('user').notNull(),
|
||||
|
||||
email: text().unique(),
|
||||
|
|
|
@ -13,6 +13,8 @@ import { createSchemaFactory } from '@fisch0920/drizzle-zod'
|
|||
import { z } from '@hono/zod-openapi'
|
||||
import { createId } from '@paralleldrive/cuid2'
|
||||
|
||||
const usernameAndTeamSlugLength = 64 as const
|
||||
|
||||
/**
|
||||
* `cuid2`
|
||||
*/
|
||||
|
@ -46,6 +48,18 @@ export function deploymentId<U extends string, T extends Readonly<[U, ...U[]]>>(
|
|||
return varchar({ length: 160, ...config })
|
||||
}
|
||||
|
||||
export function username<U extends string, T extends Readonly<[U, ...U[]]>>(
|
||||
config?: PgVarcharConfig<T | Writable<T>, never>
|
||||
): PgVarcharBuilderInitial<'', Writable<T>, 64> {
|
||||
return varchar({ length: usernameAndTeamSlugLength, ...config })
|
||||
}
|
||||
|
||||
export function teamSlug<U extends string, T extends Readonly<[U, ...U[]]>>(
|
||||
config?: PgVarcharConfig<T | Writable<T>, never>
|
||||
): PgVarcharBuilderInitial<'', Writable<T>, 64> {
|
||||
return varchar({ length: usernameAndTeamSlugLength, ...config })
|
||||
}
|
||||
|
||||
/**
|
||||
* Default `id` primary key as a cuid2
|
||||
*/
|
||||
|
|
|
@ -22,6 +22,12 @@ export const deploymentIdSchema = z
|
|||
message: 'Invalid deployment id'
|
||||
})
|
||||
|
||||
export const usernameSchema = z
|
||||
.string()
|
||||
.refine((username) => validators.username(username), {
|
||||
message: 'Invalid username'
|
||||
})
|
||||
|
||||
export const teamSlugSchema = z
|
||||
.string()
|
||||
.refine((slug) => validators.team(slug), {
|
||||
|
|
Ładowanie…
Reference in New Issue