fix: camel case photo api responses

This commit is contained in:
Innei
2025-10-30 20:26:07 +08:00
parent b7289cc513
commit 4aff1b4e9b
2 changed files with 56 additions and 5 deletions

View File

@@ -0,0 +1,40 @@
const CAMELIZE_PATTERN = /[_.-](\w|$)/g
const camelCase = (value: string): string => {
if (
!value ||
(!value.includes('_') && !value.includes('-') && !value.includes('.'))
) {
return value
}
return value.replaceAll(CAMELIZE_PATTERN, (_match, group: string) =>
group.toUpperCase(),
)
}
const isPlainObject = (value: unknown): value is Record<string, unknown> => {
if (typeof value !== 'object' || value === null) {
return false
}
const prototype = Object.getPrototypeOf(value)
return prototype === Object.prototype || prototype === null
}
export const camelCaseKeys = <T>(input: unknown): T => {
if (Array.isArray(input)) {
return input.map((item) => camelCaseKeys(item)) as unknown as T
}
if (isPlainObject(input)) {
const entries = Object.entries(input).map(([key, val]) => [
camelCase(key),
camelCaseKeys(val),
])
return Object.fromEntries(entries) as T
}
return input as T
}

View File

@@ -1,4 +1,5 @@
import { coreApi } from '~/lib/api-client'
import { camelCaseKeys } from '~/lib/case'
import type {
PhotoAssetListItem,
@@ -10,18 +11,24 @@ import type {
export const runPhotoSync = async (
payload: RunPhotoSyncPayload,
): Promise<PhotoSyncResult> => {
return await coreApi<PhotoSyncResult>('/data-sync/run', {
const result = await coreApi<PhotoSyncResult>('/data-sync/run', {
method: 'POST',
body: { dryRun: payload.dryRun ?? false },
})
return camelCaseKeys<PhotoSyncResult>(result)
}
export const listPhotoAssets = async (): Promise<PhotoAssetListItem[]> => {
return await coreApi<PhotoAssetListItem[]>('/photos/assets')
const assets = await coreApi<PhotoAssetListItem[]>('/photos/assets')
return camelCaseKeys<PhotoAssetListItem[]>(assets)
}
export const getPhotoAssetSummary = async (): Promise<PhotoAssetSummary> => {
return await coreApi<PhotoAssetSummary>('/photos/assets/summary')
const summary = await coreApi<PhotoAssetSummary>('/photos/assets/summary')
return camelCaseKeys<PhotoAssetSummary>(summary)
}
export const deletePhotoAssets = async (ids: string[]): Promise<void> => {
@@ -53,7 +60,9 @@ export const uploadPhotoAssets = async (
},
)
return response.assets
const data = camelCaseKeys<{ assets: PhotoAssetListItem[] }>(response)
return data.assets
}
export const getPhotoStorageUrl = async (
@@ -64,5 +73,7 @@ export const getPhotoStorageUrl = async (
query: { key: storageKey },
})
return result.url
const data = camelCaseKeys<{ url: string }>(result)
return data.url
}