npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tsrt/mparty-express

v0.8.0

Published

Mparty Express Middleware for usage handling file uploads to FileSystem, AWS S3 and whatever

Downloads

19

Readme

Typescript Reusable Tools: Mparty Express Middleware

npm version GitHub license Size Downloads

An Mparty Express middleware.

Includes updated Express Request typings.

All available limits & options are available here.

Usage

import { Router } from 'express';
import { createMpartyMiddleware } from '@tsrt/mparty-express';

const mpartyMiddleware = createMpartyMiddleware({ destination: 'some/files/path' });

const router = Router();

// Allow uploading any files
router.post('/upload1', mpartyMiddleware(), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

// Restrict uploading files only for next fields: `fileField1`, `fileField1`.
router.post('/upload2', mpartyMiddleware(['fileField1', 'fileField2']), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

// Provide some route specific limitations (same as above example)
router.post('/upload3', mpartyMiddleware({ limits: { allowedFiles: ['fileField1', 'fileField2'] } }), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

// Limit files amount
router.post('/upload4', mpartyMiddleware({ limits: { allowedFiles: ['fileField1'], files: 1 } }), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

// ... etc

export default router;

Dynamic destionation config

import { Router, Request } from 'express';
import { createMpartyMiddleware } from '@tsrt/mparty-express';

async function destination(req: Request): Promise<string> {
  // ...make any async/sync calculations or get data from Request
  return `/some/${req.method}/path`;
}
const mpartyMiddleware = createMpartyMiddleware({ destination });

const router = Router();

// ... Same usage as from example above

export default router;
... or dynamic adapter. For example AwsAdapter:
import { Router, Request } from 'express';
import { IAdapter } from '@tsrt/mparty';
import { createMpartyMiddleware } from '@tsrt/mparty-express';
import { AwsAdapter } from '@tsrt/mparty-aws';

import { SomeAwsConfigService } from '@lib/services';

async function adapter(req: Request): Promise<IAdapter> {
  const awsConfig = await SomeAwsConfigService.getConfigFromRequestContext(req);
  return new AwsAdapter(awsConfig);
}
const mpartyMiddleware = createMpartyMiddleware({ adapter });

const router = Router();

// ... Same usage as from example above

export default router;

Use cases

Some options or default limits could be provided right here. The will be applied by default unless reassigned manually in specific route.

For example fileNameFactory:

import { Router } from 'express';
import { createMpartyMiddleware } from '@tsrt/mparty-express';

const fileNameFactory: FileNameFactory = async (req, _file, { originalFileName }) => `${req.method}/${Date.now()}_${originalFileName}`;

const mpartyMiddleware = createMpartyMiddleware({ destination: 'some/files/path', fileNameFactory });

export const router = Router();

// Use default options
router.post('/upload1', mpartyMiddleware(), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

// Reassign destionstion for this route specific
router.post('/upload1', mpartyMiddleware({ destionation: 'some/another/path' }), (req, res) => {
  console.log(req.files);
  console.log(req.file);
});

Todo

  • [ ] Update allowedFiles signature after Mparty will implement it. The puspose is next: upload(['file1', ['files', min, max], ['files', 1, 5]])

License

This project is licensed under the terms of the MIT license.