naystack - v1.5.10
    Preparing search index...

    Function setupFileUpload

    • Configures file upload for S3. Returns a PUT route handler (for the client to upload files) and server-side helpers.

      Mount the PUT handler on your file upload API route (e.g. app/api/(rest)/file/route.ts). The client can then use useFileUpload() from naystack/file/client to upload files with the auth token.

      AWS credentials are read from env vars: AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY_SECRET, AWS_REGION, AWS_BUCKET.

      Parameters

      Returns {
          deleteFile: (url: string) => Promise<boolean>;
          getDownloadURL: (keys: string | string[]) => string;
          getUploadURL: (keys: string | string[]) => Promise<string>;
          PUT: (
              req: NextRequest,
          ) => Promise<
              | NextResponse<{ error: string }>
              | NextResponse<{ onUploadResponse: object; url: string }>,
          >;
          uploadFile: (
              keys: string | string[],
              __namedParameters: { blob?: Blob; url?: string },
          ) => Promise<string | null>;
      }

      Object with:

      • PUT — Next.js route handler for file uploads (auth required, multipart form).
      • uploadFile(keys, { url?, blob? }) — Server-side: upload a file from URL or Blob to S3.
      • deleteFile(url) — Server-side: delete a file by its S3 URL.
      • getUploadURL(keys) — Server-side: get a presigned PUT URL (5-minute expiry).
      • getDownloadURL(keys) — Get the public download URL for a key.
      • deleteFile: (url: string) => Promise<boolean>
      • getDownloadURL: (keys: string | string[]) => string

        Builds the public download URL for one or more keys in the configured S3 bucket.

      • getUploadURL: (keys: string | string[]) => Promise<string>
      • PUT: (
            req: NextRequest,
        ) => Promise<
            | NextResponse<{ error: string }>
            | NextResponse<{ onUploadResponse: object; url: string }>,
        >
      • uploadFile: (
            keys: string | string[],
            __namedParameters: { blob?: Blob; url?: string },
        ) => Promise<string | null>
      // app/api/(rest)/file/route.ts
      import { setupFileUpload } from "naystack/file";

      export const { PUT } = setupFileUpload({
      onUpload: async ({ url, type, userId, data }) => {
      if (type === "DealDocument" && url) {
      const payload = data as { dealId: number; fileName: string };
      const [row] = await db.insert(DocumentsTable).values({
      dealId: payload.dealId, fileURL: url, fileName: payload.fileName,
      }).returning();
      return row ?? {};
      }
      return {};
      },
      // Optional: customize the S3 key (defaults to UUID)
      getKey: async ({ type, userId }) => `${type}/${userId}/${crypto.randomUUID()}`,
      });