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

parse-server-cloud-middleware

v1.0.7

Published

Are you looking for something like this: ```js Parse.Cloud.define('doSomethingA', UserModule.doSomethingA, ['checkVersion', 'validateActiveUser']); Parse.Cloud.define('doAnotherThingB', UserModule.doAnotherThingB, ['checkVersion', 'onlyAdmin', 'logRequest

Downloads

5

Readme

Finally middleware is here!

Are you looking for something like this:

Parse.Cloud.define('doSomethingA', UserModule.doSomethingA, ['checkVersion', 'validateActiveUser']);
Parse.Cloud.define('doAnotherThingB', UserModule.doAnotherThingB, ['checkVersion', 'onlyAdmin', 'logRequest']);

Including

Support Application-level middleware, middleware must be a custom middleware and following Usage part. This plugin is NOT compatible with expressJS style middleware.

Usage 1

First, require the package only once, before defining cloud functions

require('parse-server-cloud-middleware');

Then, you can now pass an Array of middleware function as third param of Parse.Cloud.define

Parse.Cloud.define('doSomethingA', UserModule.doSomethingA, [checkVersion, validateActiveUser]);

Unlike expressJS middleware style which is required to use next() function, a Parse middleware function can be written similar to a Cloud function, it has request object. You can:

  • Add more data/properties into request so next middlewares and main Cloud function can access to that information.
  • Throw an error.
const validateActiveUser = (request) => {
  const user = request.user;
  if (!user || user.get('status') !== 'ACTIVE') {
    throw new Parse.Error(403, 'FORBIDDEN')
  }
};
const checkVersion = (request) => {
  const { version } = request.params;
  if (!version) {
    throw new Error('missing version'); // new Parse.Error
  }
  if (version !== '1.1.1') {
    throw new Error('this cloud function is only for version 1.1.1') // new Parse.Error
  }
};
  • Early response (response without error but still prevent next middleware/cloud function). To do so, we need to use second param in the middleware function.
const withCachedMiddleware = async (request, response) => {
  const cachedData = cacheProvider.key(request.params.sample)
  if (cachedData.exists()) {
    return response(await cachedData.fetch()); // this is the end, main cloud function won't be reached.
  }

  return; // enter main cloud function, responded by main cloud function result
};
  • instead of using response as function. You can also use response similar to legacy style Parse server 2.x.x:
return response(await cachedData.fetch());
return response.success(await cachedData.fetch());
return response.error(); // Using "throw" statement is better than response.error()

Usage 2

It's basically the same, but you can inject the middleware function name-string instead!

const { init } = require('parse-server-cloud-middleware');
const MiddleWares = {
  checkVersion: require('./middlewares/checkVersion'),
  validateActiveUser: userModule.validateActiveUserMiddleware,
}

init(MiddleWares);

Parse.Cloud.define('doSomethingA', UserModule.doSomethingA, ['checkVersion', 'validateActiveUser']);