(data: unknown, context: string)
| 126 | * response is missing a presigned URL or `fileInfo.path`. |
| 127 | */ |
| 128 | export const normalizePresignedData = (data: unknown, context: string): PresignedUploadInfo => { |
| 129 | const d = (data ?? {}) as Record<string, unknown> |
| 130 | const presignedUrl = (d.presignedUrl as string) || (d.uploadUrl as string) || '' |
| 131 | const fileInfo = d.fileInfo as Record<string, unknown> | undefined |
| 132 | const directUploadSupported = d.directUploadSupported !== false |
| 133 | |
| 134 | if (!directUploadSupported) { |
| 135 | return { |
| 136 | fileName: (d.fileName as string) || context, |
| 137 | presignedUrl: '', |
| 138 | fileInfo: { path: '', key: '', name: context, size: 0, type: '' }, |
| 139 | directUploadSupported: false, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!presignedUrl || !fileInfo?.path) { |
| 144 | throw new DirectUploadError( |
| 145 | `Invalid presigned response for ${context}`, |
| 146 | 'PRESIGNED_URL_ERROR', |
| 147 | data |
| 148 | ) |
| 149 | } |
| 150 | |
| 151 | return { |
| 152 | fileName: (d.fileName as string) || (fileInfo.name as string) || context, |
| 153 | presignedUrl, |
| 154 | fileInfo: { |
| 155 | path: fileInfo.path as string, |
| 156 | key: (fileInfo.key as string) || '', |
| 157 | name: (fileInfo.name as string) || context, |
| 158 | size: (fileInfo.size as number) || (d.fileSize as number) || 0, |
| 159 | type: (fileInfo.type as string) || (d.contentType as string) || '', |
| 160 | }, |
| 161 | uploadHeaders: (d.uploadHeaders as Record<string, string>) || undefined, |
| 162 | directUploadSupported: true, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | interface GetPresignedOptions { |
| 167 | endpoint: string |
no outgoing calls
no test coverage detected