Skip to content

Commit

Permalink
feat: When uploading, include record information (#478)
Browse files Browse the repository at this point in the history
* feat: When uploading, include record information, such as the resource ID

* add changeset

* modify docs

---------

Co-authored-by: Colin Regourd <[email protected]>
  • Loading branch information
huinalam and cregourd authored Nov 18, 2024
1 parent 46a3dba commit 9959cfd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-dodos-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

When uploading, include record information - Thanks to [@huinalam](https://github.com/huinalam)
6 changes: 4 additions & 2 deletions apps/docs/pages/docs/api/model-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,10 @@ When you define a field, use the field's name as the key and the following objec
an async function that is used only for formats <code>file</code> and{" "}
<code>data-url</code>. It takes a Buffer object and an information
object containing <code>name</code> and <code>type</code> properties
as parameters and must return a string. It can be useful to upload a
file to a remote provider
as parameters and must return a string. It has <code>context</code> as
a parameter, which holds record information such as the resource ID,
which does not exist during record creation. It can be useful to
upload a file to a remote provider
</>
),
},
Expand Down
2 changes: 1 addition & 1 deletion apps/example/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const options: NextAdminOptions = {
* for example you can upload the file to an S3 bucket.
* Make sure to return a string.
*/
upload: async (buffer, infos) => {
upload: async (buffer, infos, context) => {
return "https://raw.githubusercontent.com/premieroctet/next-admin/33fcd755a34f1ec5ad53ca8e293029528af814ca/apps/example/public/assets/logo.svg";
},
},
Expand Down
2 changes: 1 addition & 1 deletion apps/example/pageRouterOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const options: NextAdminOptions = {
* for example you can upload the file to an S3 bucket.
* Make sure to return a string.
*/
upload: async (buffer, infos) => {
upload: async (buffer, infos, context) => {
return "https://raw.githubusercontent.com/premieroctet/next-admin/33fcd755a34f1ec5ad53ca8e293029528af814ca/apps/example/public/assets/logo.svg";
},
},
Expand Down
10 changes: 9 additions & 1 deletion packages/next-admin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,22 @@ export type Handler<
get?: (input: T) => any;
/**
* an async function that is used only for formats `file` and `data-url`. It takes a buffer as parameter and must return a string. Useful to upload a file to a remote provider.
* @param file
* @param buffer
* @param infos
* @param context - This object contains record information, such as the resource ID.
* @returns
*/
upload?: (
buffer: Buffer,
infos: {
name: string;
type: string | null;
},
context: {
/**
* the resource ID if it exists, otherwise undefined
*/
resourceId: string | number | undefined;
}
) => Promise<string>;
/**
Expand Down
5 changes: 4 additions & 1 deletion packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,10 @@ export const formattedFormData = async <M extends ModelName>(
} else {
try {
const uploadResult = await uploadHandler(
...(formData[propertyName] as unknown as UploadParameters)
...(formData[propertyName] as unknown as UploadParameters),
{
resourceId,
}
);
if (typeof uploadResult !== "string") {
console.warn(
Expand Down

0 comments on commit 9959cfd

Please sign in to comment.