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

@vikhola/content-parser

v2.0.1

Published

Provides a simple infrastructure vikhola framework module for parsing request content

Downloads

20

Readme

@vikhola/content-parser

About

Provides a simple infrastructure vikhola framework module for parsing request content.

Installation

$ npm i @vikhola/content-parser

Usage

Package could be required as ES6 module

import { ContentType } from '@vikhola/content-parser'

Or as commonJS module.

const { ContentType } = require('@vikhola/content-parser');

ContentParser

To start using parser enough to initialize it and subscribe to the desirable scope of the vikhola framework. By default, parser already have several strategies and can parse requests with application/json and text/plain content types.

const server = new Server();
const parser = new ContentParser();

parser.parse(server);

server.post('/', (ctx) => {
	// some logic	
});

Parser will parse the request body by the strategy whose key exactly or as closely as possible matches the request Content-Type header by its type and every parameter.

parser.parse(server);

// POST / HTTP 1.1
// Content-Type: 'application/json; foo=bar; bar=foo'
server.post('/', (ctx) => {
	// some logic	
});

For the requests with Content-Type header other than application/json and text/plain the parser supports custom keys and strategies where the first ones is the Content-Type header or corresponding to it RegExp.

parser.set(key, strategy)

The parser.set() method binds the strategy for the passed key.

const strategy = new ContentParserStrategy();

parser.set('application/xml', strategy);

As mention before the strategy key could be Content-Type header or corresponding to it RegExp.

const strategy = new ContentParserStrategy();

parser.set(/^application\/xml/, strategy);

If the strategy key type of RegExp, parser will try to match it only if there is no strategies with a string key matching the request Content-Type header.

const strategyOne = { parse(request, source) { return 'bar' } };
const strategyTwo = { parse(request, source) { return 'foo' } };

parser.set(/^application\/xml/, strategyOne);
parser.set('application/xml; foo=bar; bar=baz', strategyTwo);

// POST / HTTP 1.1
// Content-Type: 'application/xml; foo=bar; bar=foo'
server.post('/', (ctx) => {
	// print: bar
	console.log(ctx.request.body);
});

Another strategy key is *. Strategy with this key will be executed if the request has a Content-Type that does not match any other strategy key.

const strategy = { parse(request, source) { return 'bar' } };

parser.set('*', strategy);

// POST / HTTP 1.1
// Content-Type: 'application/xml; foo=bar; bar=foo'
server.post('/', (ctx) => {
	// print: bar
	console.log(ctx.request.body);
});

There is no restriction to type of strategy, it could be any object that has parse() method.

const parser = new ContentParser();
const strategy = {
	parse(request, source) {
		// process data
	}
};

parser.set('application/xml', strategy);

parser.has(key)

The parser.has() method checks whether any strategy is associated with the passed key.

const strategy = new ContentParserStrategy();

// print: false
console.log(parser.has('application/xml'));

parser.set('application/xml', strategy);

// print: true
console.log(parser.has('application/xml'));

The method will compare the keys also by their payload, and if it is the same, true will be returned.

const strategy = new ContentParserStrategy();

parser.set('application/xml; foo=bar; bar=foo', strategy);

// print: true
console.log(parser.has('application/xml; bar=foo; foo=bar'));

parser.get(key)

The parser.get() method returns the strategy associated with the passed key.

const strategy = new ContentParserStrategy();

parser.set('application/xml', strategy);

// print: true
console.log(parser.get('application/xml') === strategy);

The method will compare the keys also by their payload, and if it is the same, strategy will be returned.

const strategy = new ContentParserStrategy();

parser.set('application/xml; foo=bar; bar=foo', strategy);

// print: true
console.log(parser.get('application/xml; bar=foo; foo=bar') === strategy);

parser.delete(key)

The parser.delete() method remove key and bound to it strategy from the parser.

const strategy = new ContentParserStrategy();

parser.set('application/xml', strategy);

parser.delete('application/xml');

// print: false
console.log(parser.has('application/xml'));

parser.parse(event)

The parser.parse() method subscribes with the provided parameters to the target kernel.parse event, during which parses its body using a strategy whose key matches exactly or as closely as possible the Content-Type of the request.

const strategy = { parse(request, source) { return 'bar' } };

parser.set('application/xml', strategy);

// POST / HTTP 1.1
// Content-Type: 'application/xml'
server.post('/', (ctx) => {
	// print: bar
	console.log(ctx.request.body);
});

parser.clear()

The parser.clear() method removes all key-value pairs from the parser.

const strategy = new ContentParserStrategy();

parser.set('application/xml', strategy);

parser.clear();

// print: false
console.log(parser.has('application/xml'));

ContentParserStrategy

Except parser, module also exports a default strategy and its more specialized versions as JSONContentParserStrategy and TextContentParserStrategy which helps parse the provided request body and return a promise with its contents. The strategy accepts an optional limit and type parameters.

const strategy = new BaseContentParserStrategy({ limit: '10mb', type: 'string' });

The limit parameter specifies the maximum content size that will cause an error if exceeded. The limit can be represented as a number of bytes or a string with a number and its units.

const strategy = new BaseContentParserStrategy({ limit: '1mb' });

The type parameter specifies the type of data returned by the parse() method. It could be buffer or string. By default its equal to buffer.

const strategy = new BaseContentParserStrategy({ type: 'string' });

strategy.parse(request, source)

The strategy.parse() method parses the provided source and return promise with its content.

strategy.parse(request, source);

For example, this method can be extended and the returned string or buffer processed by a more specific parser.

strategy.parse(request, source) {
	return super.parse(request, source).then(content => parseXML(content));
}

License

MIT