feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
|
|
@ -1,4 +1,12 @@
|
|||
import { isUuid } from '@d3ro/core/guards'
|
||||
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '@d3ro/core/supabase-config'
|
||||
import {
|
||||
assertRecordWith,
|
||||
parseBoundedStringWith,
|
||||
parseIsoTimestampWith,
|
||||
parseSafeIntegerWith,
|
||||
parseUuidWith,
|
||||
} from '../shared/service-guards'
|
||||
|
||||
export const ADMIN_ROLES = ['user', 'manager', 'admin', 'super_admin'] as const
|
||||
export const ADMIN_TIERS = ['free', 'pro', 'pro_plus'] as const
|
||||
|
|
@ -130,7 +138,6 @@ interface RequestOptions {
|
|||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
const MAX_RESPONSE_BYTES = 1024 * 1024
|
||||
const REQUEST_TIMEOUT_MS = 20_000
|
||||
|
|
@ -139,10 +146,7 @@ const TIER_SET = new Set<string>(ADMIN_TIERS)
|
|||
const STATUS_SET = new Set<string>(ADMIN_SUBSCRIPTION_STATUSES)
|
||||
|
||||
function recordValue(value: unknown): Record<string, unknown> {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
throw invalidResponse()
|
||||
}
|
||||
return value as Record<string, unknown>
|
||||
return assertRecordWith(value, invalidResponse)
|
||||
}
|
||||
|
||||
function assertOnlyKeys(value: Record<string, unknown>, allowed: readonly string[]): void {
|
||||
|
|
@ -151,8 +155,7 @@ function assertOnlyKeys(value: Record<string, unknown>, allowed: readonly string
|
|||
}
|
||||
|
||||
function requiredString(value: unknown, maximum = 2048): string {
|
||||
if (typeof value !== 'string' || value.length < 1 || value.length > maximum) throw invalidResponse()
|
||||
return value
|
||||
return parseBoundedStringWith(value, invalidResponse, { maxLength: maximum })
|
||||
}
|
||||
|
||||
function nullableString(value: unknown, maximum = 4096): string | null {
|
||||
|
|
@ -161,15 +164,11 @@ function nullableString(value: unknown, maximum = 4096): string | null {
|
|||
}
|
||||
|
||||
function uuid(value: unknown): string {
|
||||
const normalized = requiredString(value, 36)
|
||||
if (!UUID_PATTERN.test(normalized)) throw invalidResponse()
|
||||
return normalized.toLowerCase()
|
||||
return parseUuidWith(requiredString(value, 36), invalidResponse)
|
||||
}
|
||||
|
||||
function isoDateTime(value: unknown): string {
|
||||
const normalized = requiredString(value, 40)
|
||||
if (!Number.isFinite(Date.parse(normalized))) throw invalidResponse()
|
||||
return normalized
|
||||
return parseIsoTimestampWith(requiredString(value, 40), invalidResponse, { maxLength: 40 })
|
||||
}
|
||||
|
||||
function nullableIsoDateTime(value: unknown): string | null {
|
||||
|
|
@ -177,13 +176,11 @@ function nullableIsoDateTime(value: unknown): string | null {
|
|||
}
|
||||
|
||||
function nonNegativeInteger(value: unknown, maximum = 1_000_000): number {
|
||||
if (!Number.isSafeInteger(value) || Number(value) < 0 || Number(value) > maximum) throw invalidResponse()
|
||||
return Number(value)
|
||||
return parseSafeIntegerWith(value, invalidResponse, { min: 0, max: maximum })
|
||||
}
|
||||
|
||||
function positiveInteger(value: unknown, maximum = Number.MAX_SAFE_INTEGER): number {
|
||||
if (!Number.isSafeInteger(value) || Number(value) < 1 || Number(value) > maximum) throw invalidResponse()
|
||||
return Number(value)
|
||||
return parseSafeIntegerWith(value, invalidResponse, { min: 1, max: maximum })
|
||||
}
|
||||
|
||||
function role(value: unknown): AdminRole {
|
||||
|
|
@ -517,7 +514,7 @@ export async function getAdminUser(
|
|||
userId: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<AdminUserDetail> {
|
||||
if (!UUID_PATTERN.test(userId)) throw new AdminServiceError('validation', null, false)
|
||||
if (!isUuid(userId)) throw new AdminServiceError('validation', null, false)
|
||||
const payload = await requestAdmin(`admin-users${queryString({ userId })}`, { accessToken, signal })
|
||||
const body = recordValue(payload)
|
||||
assertOnlyKeys(body, ['profile', 'subscription', 'account'])
|
||||
|
|
@ -566,7 +563,7 @@ export async function listAdminAudit(options: {
|
|||
const page = options.page ?? 1
|
||||
const limit = options.limit ?? 20
|
||||
validatedPage(page, limit)
|
||||
if (options.targetId !== undefined && !UUID_PATTERN.test(options.targetId)) {
|
||||
if (options.targetId !== undefined && !isUuid(options.targetId)) {
|
||||
throw new AdminServiceError('validation', null, false)
|
||||
}
|
||||
const payload = await requestAdmin(`admin-audit-log${queryString({
|
||||
|
|
@ -583,7 +580,7 @@ export async function getAdminPaymentHistory(
|
|||
userId: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<AdminPaymentHistory> {
|
||||
if (!UUID_PATTERN.test(userId)) throw new AdminServiceError('validation', null, false)
|
||||
if (!isUuid(userId)) throw new AdminServiceError('validation', null, false)
|
||||
const payload = await requestAdmin(`admin-payments${queryString({ userId, source: 'db' })}`, {
|
||||
accessToken,
|
||||
signal,
|
||||
|
|
@ -609,7 +606,7 @@ export async function getAdminPaymentHistory(
|
|||
}
|
||||
|
||||
function validatedIdempotencyKey(value: string): string {
|
||||
if (!UUID_PATTERN.test(value)) throw new AdminServiceError('validation', null, false)
|
||||
if (!isUuid(value)) throw new AdminServiceError('validation', null, false)
|
||||
return value.toLowerCase()
|
||||
}
|
||||
|
||||
|
|
@ -627,7 +624,7 @@ export async function changeAdminUserRole(options: {
|
|||
memo: string
|
||||
signal?: AbortSignal
|
||||
}): Promise<{ userId: string; newRole: AdminRole }> {
|
||||
if (!UUID_PATTERN.test(options.userId) || !ROLE_SET.has(options.newRole)) {
|
||||
if (!isUuid(options.userId) || !ROLE_SET.has(options.newRole)) {
|
||||
throw new AdminServiceError('validation', null, false)
|
||||
}
|
||||
const idempotencyKey = validatedIdempotencyKey(options.idempotencyKey)
|
||||
|
|
@ -697,7 +694,7 @@ async function mutateAdminSubscription(
|
|||
signal?: AbortSignal
|
||||
},
|
||||
): Promise<AdminSubscription> {
|
||||
if (!UUID_PATTERN.test(options.userId)) throw new AdminServiceError('validation', null, false)
|
||||
if (!isUuid(options.userId)) throw new AdminServiceError('validation', null, false)
|
||||
const idempotencyKey = validatedIdempotencyKey(options.idempotencyKey)
|
||||
const body = action === 'delete'
|
||||
? { memo: validatedMemo(options.mutation.memo) }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue