@travetto/asset-rest
v4.1.6
Published
Provides integration between the travetto asset and rest module.
Downloads
30
Maintainers
Readme
Asset Rest Support
Provides integration between the travetto asset and rest module.
Install: @travetto/asset-rest
npm install @travetto/asset-rest
# or
yarn add @travetto/asset-rest
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.files
. The uploaded files are constructed as Asset instances, which allows for integration with the Asset module.
A simple example:
Code: Rest controller with upload support
import { Controller, Post, Get, Request } from '@travetto/rest';
import { Asset } from '@travetto/asset';
import { AssetRestUtil, Upload, UploadAll } from '@travetto/asset-rest';
@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() file: Asset) {
return AssetRestUtil.downloadable(file);
}
/**
* @param file A file to upload
*/
@Post('/files')
@UploadAll()
loadFiles({ files }: Request) {
for (const [, file] of Object.entries(files)) {
return AssetRestUtil.downloadable(file); // return the first
}
}
}