@travetto/asset-rest

4.0.11 • Public • Published

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
    }
  }
}

Package Sidebar

Install

npm i @travetto/asset-rest

Homepage

travetto.io

Weekly Downloads

10

Version

4.0.11

License

MIT

Unpacked Size

26.7 kB

Total Files

10

Last publish

Collaborators

  • arcsine