@travetto/rest-upload
v5.0.2
Published
Provides integration between the travetto asset and rest module.
Downloads
611
Maintainers
Readme
Rest Upload Support
Provides integration between the travetto asset and rest module.
Install: @travetto/rest-upload
npm install @travetto/rest-upload
# or
yarn add @travetto/rest-upload
This module provides a clean and direct mechanism for processing uploads, built upon @fastify/busboy. The module also provides some best practices with respect to temporary file deletion.
Once the files are uploaded, they are exposed on RESTful API's request object as req.uploads
. The uploaded files are constructed as Blob
instances.
A simple example:
Code: Rest controller with upload support
import { Controller, Post, Get, Request } from '@travetto/rest';
import { Upload, UploadAll } from '@travetto/rest-upload';
@Controller('/simple')
export class Simple {
@Get('/age')
getAge() {
return { age: 50 };
}
@Post('/age')
getPage() {
return { age: 20 };
}
/**
* @param file A file to upload
*/
@Post('/file')
loadFile(@Upload() upload: File) {
return upload;
}
/**
* @param file A file to upload
*/
@Post('/files')
@UploadAll()
async loadFiles({ uploads }: Request) {
for (const [, upload] of Object.entries(uploads)) {
return upload;
}
}
}