🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

parse-server-cloud-middleware

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-server-cloud-middleware

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

1.0.7
latest
Source
npm
Version published
Maintainers
1
Created
Source

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']);

Keywords

parse-middleware

FAQs

Package last updated on 06 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts