New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

express-partial-response

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-partial-response

Express middleware for filtering-out parts of JSON responses based on the `fields` query-string.

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

Express Partial Response Middleware NPM version

This Express Middleware will allow you to send a subset of a JSON object instead of the entire object from your HTTP services. To do so, your services will begin accepting the ?fields= query-string that, using a simple language, will specify which fields and sub-fields to keep and which to ignore.

If you've used the Google APIs, provided a ?fields= query-string to get a Partial Response, and wanted to do the same for your own server, now you can do so with this middleware.

Underneath, this middleware uses json-mask. Use it directly without this middleware if you need more flexibility.

Installation

npm install express-partial-response

Usage

const express = require('express');
const partialResponse = require('express-partial-response');
const app = express();

app.use(partialResponse());

app.get('/', (req, res) => {
  res.json({
    firstName: 'Mohandas',
    lastName: 'Gandhi',
    aliases: [
      {
        firstName: 'Mahatma',
        lastName: 'Gandhi'
      },
      {
        firstName: 'Bapu'
      }
    ]
  });
});

app.listen(4000);

Let's test it:

$ curl 'http://localhost:4000'
{"firstName":"Mohandas","lastName":"Gandhi","aliases":[{"firstName":"Mahatma","lastName":"Gandhi"},{"firstName":"Bapu"}]}

$ # Let's just get the first name
$ curl 'http://localhost:4000?fields=lastName'
{"lastName":"Gandhi"}

$ # Now, let's just get the first names directly as well as from aliases
$ curl 'http://localhost:4000?fields=firstName,aliases(firstName)'
{"firstName":"Mohandas","aliases":[{"firstName":"Mahatma"},{"firstName":"Bapu"}]}

Note: take a look at /example.

Syntax

Look at json-mask for the available syntax of the fields param.

Options

query specifies the query-string to use. Defaults to fields

app.use(
  partialResponse({
    query: 'filter'
  })
);

License

MIT. See LICENSE

Keywords

express

FAQs

Package last updated on 30 Oct 2020

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