Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: When uploading, include record information #478

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading