feat: Add support for uploading media in admin (#7564)
This commit is contained in:
@@ -314,4 +314,65 @@ export class Admin {
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
public uploads = {
|
||||
// Note: The creation/upload flow be made more advanced, with support for streaming and progress, but for now we keep it simple
|
||||
create: async (
|
||||
body: HttpTypes.AdminUploadFile,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
const form = new FormData()
|
||||
if (body instanceof FileList) {
|
||||
Array.from(body).forEach((file) => {
|
||||
form.append("files", file)
|
||||
})
|
||||
} else {
|
||||
body.files.forEach((file) => {
|
||||
form.append(
|
||||
"files",
|
||||
"content" in file
|
||||
? new Blob([file.content], {
|
||||
type: "text/plain",
|
||||
})
|
||||
: file,
|
||||
file.name
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return this.client.fetch<{ files: HttpTypes.AdminFile[] }>(
|
||||
`/admin/uploads`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...headers,
|
||||
// Let the browser determine the content type.
|
||||
"content-type": null,
|
||||
},
|
||||
body: form,
|
||||
query,
|
||||
}
|
||||
)
|
||||
},
|
||||
retrieve: async (
|
||||
id: string,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) => {
|
||||
return this.client.fetch<{ file: HttpTypes.AdminFile }>(
|
||||
`/admin/uploads/${id}`,
|
||||
{
|
||||
query,
|
||||
headers,
|
||||
}
|
||||
)
|
||||
},
|
||||
delete: async (id: string, headers?: ClientHeaders) => {
|
||||
return this.client.fetch<DeleteResponse<"file">>(`/admin/uploads/${id}`, {
|
||||
method: "DELETE",
|
||||
headers,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +164,11 @@ export class Client {
|
||||
}
|
||||
// We use `headers.set` in order to ensure headers are overwritten in a case-insensitive manner.
|
||||
Object.entries(customHeaders).forEach(([key, value]) => {
|
||||
headers.set(key, value)
|
||||
if (value === null) {
|
||||
headers.delete(key)
|
||||
} else {
|
||||
headers.set(key, value)
|
||||
}
|
||||
})
|
||||
|
||||
let normalizedInput: RequestInfo | URL = input
|
||||
|
||||
@@ -23,7 +23,7 @@ export type FetchParams = Parameters<typeof fetch>
|
||||
|
||||
export type ClientHeaders =
|
||||
// The `tags` header is specifically added for nextJS, as they follow a non-standard header format
|
||||
Record<string, string | { tags: string[] }>
|
||||
Record<string, string | null | { tags: string[] }>
|
||||
|
||||
export type FetchInput = FetchParams[0]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user