mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-24 23:05:05 +00:00
fix: camel case photo api responses
This commit is contained in:
40
be/apps/dashboard/src/lib/case.ts
Normal file
40
be/apps/dashboard/src/lib/case.ts
Normal 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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user