kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
52 wiersze
1.1 KiB
TypeScript
52 wiersze
1.1 KiB
TypeScript
import { createHash, randomUUID } from 'node:crypto'
|
|
|
|
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
|
import type { ZodSchema } from 'zod'
|
|
|
|
import { HttpError, ZodValidationError } from './errors'
|
|
|
|
export function sha256(input: string = randomUUID()) {
|
|
return createHash('sha256').update(input).digest('hex')
|
|
}
|
|
|
|
export function assert(expr: unknown, message?: string): asserts expr
|
|
export function assert(
|
|
expr: unknown,
|
|
statusCode?: ContentfulStatusCode,
|
|
message?: string
|
|
): asserts expr
|
|
export function assert(
|
|
expr: unknown,
|
|
statusCodeOrMessage?: ContentfulStatusCode | string,
|
|
message = 'Internal assertion failed'
|
|
): asserts expr {
|
|
if (expr) {
|
|
return
|
|
}
|
|
|
|
if (typeof statusCodeOrMessage === 'number') {
|
|
throw new HttpError({ statusCode: statusCodeOrMessage, message })
|
|
} else {
|
|
throw new Error(statusCodeOrMessage ?? message)
|
|
}
|
|
}
|
|
|
|
export function parseZodSchema<T>(
|
|
schema: ZodSchema<T>,
|
|
input: unknown,
|
|
{
|
|
error
|
|
}: {
|
|
error?: string
|
|
} = {}
|
|
): T {
|
|
try {
|
|
return schema.parse(input)
|
|
} catch (err) {
|
|
throw new ZodValidationError({
|
|
prefix: error,
|
|
cause: err
|
|
})
|
|
}
|
|
}
|