{"id":13455203,"url":"https://github.com/cstrnt/next-multiparty","last_synced_at":"2025-03-18T21:31:49.090Z","repository":{"id":40589781,"uuid":"443971859","full_name":"cstrnt/next-multiparty","owner":"cstrnt","description":"Easy \u0026 Simple File Uploads for Next.js","archived":false,"fork":false,"pushed_at":"2023-03-27T05:48:08.000Z","size":257,"stargazers_count":20,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-08T17:43:59.520Z","etag":null,"topics":["file-upload","form","formdata","image-upload","incomingform","multipart","multipart-parser","mutlipart-formdata","nextjs","serverless"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cstrnt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-01-03T07:11:29.000Z","updated_at":"2025-02-25T14:57:43.000Z","dependencies_parsed_at":"2023-07-14T05:50:39.616Z","dependency_job_id":null,"html_url":"https://github.com/cstrnt/next-multiparty","commit_stats":{"total_commits":31,"total_committers":3,"mean_commits":"10.333333333333334","dds":0.06451612903225812,"last_synced_commit":"0da98c4bda40396e906a058a577731d356fbdd0a"},"previous_names":["cstrnt/next-multipart"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstrnt%2Fnext-multiparty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstrnt%2Fnext-multiparty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstrnt%2Fnext-multiparty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstrnt%2Fnext-multiparty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cstrnt","download_url":"https://codeload.github.com/cstrnt/next-multiparty/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243950813,"owners_count":20373664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-upload","form","formdata","image-upload","incomingform","multipart","multipart-parser","mutlipart-formdata","nextjs","serverless"],"created_at":"2024-07-31T08:01:02.397Z","updated_at":"2025-03-18T21:31:47.676Z","avatar_url":"https://github.com/cstrnt.png","language":"TypeScript","readme":"# next-multiparty\n\nnext-multiparty is a small utility library to ease the process of file uploads with Next.js.\nIt uses [formidable](https://github.com/node-formidable/formidable) under the hood, but with much less work to do and a modern API\n\n## Installation\n\nInstallation is pretty straight forward. Simply run one of the following commands to install\nit to your Next.js app.\n\n```\nyarn add next-multiparty\n```\n```\nnpm i next-multiparty\n```\n```\npnpm add next-multiparty\n```\n\n## Usage\n\n**TL;DR:**\n\n1. `import { withFileUpload, getConfig } from 'next-multiparty'`\n2. Simply wrap any api route with `withFileUpload`\n3. `export const config = getConfig()`\n4. You now can access `req.file` (if the request contained one file field) or `req.files`\n5. Call `await req.file.toBuffer()` to load the file into the memory\n\n## API\n\n### withFileUpload\nThe `withFileUpload` function is a higher-order function which should be wrapped around\nan api route from next.js:\n\n```js\nimport { withFileUpload } from 'next-multiparty';\nexport default withFileUpload(async (req, res) =\u003e {\n    res.json({test: 1})\n})\n```\n\nBy default it will attach the files and files which were posted to that endpoint to the `NextApiRequest` (in this case `req`) **if** the method was `POST`, `PATCH` or `PUT` **and** the `Content-Type` header was set to `multipart/form-data`.\n\nIf the request includes files they will be saved to the disk in the `os.tmpdir()` directory.\nAfter the execution of the handler all files will be cleaned up automatically.\n\nThe following properties are added to the Request:\n\n- `files`: Array of [EnhancedFile](#EnhancedFile)\n- `file`: Single [EnhancedFile](#EnhancedFile). Will be `undefined` if there are no files\n- `fields`: Object containing the name of the field as the key and the value of the field as the value\n\nYou can also pass a second parameter `options` to `withFileUpload`. Options is an object with the following values:\n\n```ts\n// Methods which should be allowed. Defaults to ['POST', 'PATCH', 'PUT']\nallowedMethods?: HTTP_METHOD[];\n\n// Flag whether the files should be removed after the execution of the handler. Defaults to true. You will probably not need to touch this.\ncleanupFiles?: boolean;\n\n// Options to change the behavior of formidable (e.g. max file size). Please refer to the https://github.com/node-formidable/formidable#options\nformidableOptions?: formidable.Options\n```\n\n\n### EnhancedFile\nBasically just [formidable.File](https://github.com/node-formidable/formidable#file) but with two added helper functions:\n\n```ts\n// Loads the file asynchronously from the file system and loads it into the memory\n// will throw if the file doesn't exists anymore\ntoBuffer: () =\u003e Promise\u003cBuffer\u003e\n\n// Deletes the file from the file system if it exists\ndestroy: () =\u003e Promise\u003cvoid\u003e\n```\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcstrnt%2Fnext-multiparty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcstrnt%2Fnext-multiparty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcstrnt%2Fnext-multiparty/lists"}