refactor: strengthen upload boundary types
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export type DriveAction = 'new' | 'replace'
|
||||
|
||||
interface StagingRequestBody {
|
||||
stagingId: string
|
||||
}
|
||||
|
||||
interface DriveRequestBody extends StagingRequestBody {
|
||||
action: DriveAction
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null
|
||||
}
|
||||
|
||||
export function parseStagingRequestBody(value: unknown): StagingRequestBody {
|
||||
if (!isRecord(value) || typeof value.stagingId !== 'string' || value.stagingId.trim() === '') {
|
||||
throw new Error('stagingId manquant')
|
||||
}
|
||||
|
||||
return { stagingId: value.stagingId }
|
||||
}
|
||||
|
||||
export function parseDriveRequestBody(value: unknown): DriveRequestBody {
|
||||
const { stagingId } = parseStagingRequestBody(value)
|
||||
const action = isRecord(value) && value.action === 'replace' ? 'replace' : 'new'
|
||||
|
||||
return { stagingId, action }
|
||||
}
|
||||
Reference in New Issue
Block a user