mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-14 08:06:40 +00:00
* chore(deps-dev): bump @antfu/eslint-config in /frontend Bumps [@antfu/eslint-config](https://github.com/antfu/eslint-config) from 9.5.1 to 10.0.0. - [Release notes](https://github.com/antfu/eslint-config/releases) - [Commits](https://github.com/antfu/eslint-config/compare/v9.5.1...v10.0.0) --- updated-dependencies: - dependency-name: "@antfu/eslint-config" dependency-version: 10.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * chore(frontend): migrate formatting to ESLint v10 Co-authored-by: Codex <noreply@openai.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Zyy <zyycn@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com>
36 lines
966 B
TypeScript
36 lines
966 B
TypeScript
import { t } from '@/i18n'
|
|
|
|
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
}
|
|
|
|
type JsonObjectResult
|
|
= | { value: Record<string, unknown>, error: undefined }
|
|
| { value: undefined, error: string }
|
|
|
|
export function parseJsonObject(text: string, maximumBytes?: number): JsonObjectResult {
|
|
let value: unknown
|
|
try {
|
|
value = JSON.parse(text)
|
|
}
|
|
catch {
|
|
return {
|
|
value: undefined,
|
|
error: t('common.validation.invalidJson'),
|
|
}
|
|
}
|
|
if (!isRecord(value)) {
|
|
return {
|
|
value: undefined,
|
|
error: t('common.validation.jsonObject'),
|
|
}
|
|
}
|
|
if (maximumBytes !== undefined && new TextEncoder().encode(JSON.stringify(value)).byteLength > maximumBytes) {
|
|
return {
|
|
value: undefined,
|
|
error: t('common.validation.sizeLimit', { value: maximumBytes / 1024 }),
|
|
}
|
|
}
|
|
return { value, error: undefined }
|
|
}
|