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