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

superagent-declare

v1.0.1

Published

Provides a declarative and uncurried API to superagent

Downloads

34

Readme

superagent-declare

NPM

Summary

Go from this:

const request = require('superagent') ;

request
.post('/upload')
.field('user[name]', 'Tobi')
.field('user[email]', '[email protected]')
.field('friends[]', ['loki', 'jane'])
.attach('image', 'path/to/tobi.png')
.then(callback) ;

to this:

const superagent = require('superagent') ;
const request = require('superagent-declare') ;
request.use(superagent) ;

request({
	post: '/upload',
	field: [
		['user[name]', 'Tobi'],
		['user[email]', '[email protected]'],
		['friends[]', ['loki', 'jane'] ],
	],
	attach: ['image', 'path/to/tobi.png'],
	then: callback
}) ;

Overview

Superagent-declare provides a declarative and uncurried API for the fantastic superagent module.

Why? The primary driver for writing this module is to be able to store a declarative representation of all the request parameters without having to define them as lines of javascript code. As an example, you are able to store these request parameters in a configuration file. Furthermore, the request parameters declared in the data structure can be specified in any order.

Installation

The usual:

npm install superagent-declarative superagent

Superagent-declare does not itself include superagent as a dependency. Although it does for dev-dependencies (primarily for unit testing). So you will need to install both alongside one another in your module/app.

Usage

The interface to superagent-declare is very simple. Use the following pattern:

const superagent = require('superagent') ;
const request = require('superagent-declare') ;
request.use(superagent) ;

request({ /* Usual superagent API options here */ }) ;

// or without calling request.use():

request({ /* Usual superagent API options here */ }, superagent) ;

The request call above will return an instance of superagent with the given methods invoked, but can then be further mutated. In other words, you do not need to define all the options for your request using superagent-declare, you can programmatically augment the resulting object. i.e. based on example in summary:

request({
	post: '/upload',
	field: [
		['user[name]', 'Tobi'],
		['user[email]', '[email protected]'],
		['friends[]', ['loki', 'jane'] ],
	],
	attach: ['image', 'path/to/tobi.png']
})
.then(callback) ;

Use of superagent and declare APIs can be intermixed - you are not locked into the declare API. i.e.

const superagent = require('superagent') ;
const request = require('superagent-declare') ;
request.use(superagent) ;

request({
	post: '/upload',
	field: [
		['user[name]', 'Tobi'],
		['user[email]', '[email protected]'],
		['friends[]', ['loki', 'jane'] ],
	],
	attach: ['image', 'path/to/tobi.png'],
	then: callback
}) ;

// and can also call this way in same codebase

superagent
.post('/upload')
.field('user[name]', 'Tobi')
.field('user[email]', '[email protected]')
.field('friends[]', ['loki', 'jane'])
.attach('image', 'path/to/tobi.png')
.then(callback) ;

Superagent-declare API Syntax

This section outlines how the superagent API translates into a declarative JS data structure that can be passed to superagent-declare.

The following definitions are used throughout:

  • method = superagent method name (e.g. send() or field())
  • argument = an argument or parameter passed to a method (e.g. '/upload' in post('/upload') )

The object literal passed to superagent-declare has these forms.

Call a method with no arguments or a single argument

  • method: [] Call method once with no arguments
  • method: 'argument' Call method once with one argument
  • method: {'key': argument} Call method once with one argument
  • method: [ [ [1, 2, 3] ] ] Call method once with one argument being an array
request({
	end: [], // Invoke the request and ignore the result
	post: '/upload',
	field: { 'friends[]': ['loki', 'jane'] },
	send: [ [ [1, 2, 3] ] ], //Send an array of values as body of request
}) ;

Call a method with two or more arguments

  • method: [ 'arg1', 'arg2', ... ] Call method once with two or more arguments
  • method: [ 'arg1', ['arg2 element 0', 'arg2 element 1'], ... ] Call method once with two or more arguments
request({
	// ...
	set: ['X-Foo', 'bar'],
	// ...
}) ;

Call a method multiple times with one or multiple arguments

  • method: [ ['call1 arg1', 'call1 arg2'], ['call2 arg1', 'call2 arg2'], ... ] Call method two or more times with sub-array arguments
request({
	// ...
	use: [ [uuid], [prefix] ],
	set: [ ['X-Foo', 'bar'], ['X-Bar', 'baz'] ],
	// ...
}) ;

Examples

Following are many examples and patterns that show how to use the declarative API.

request.get('/search')

request({
	get: '/search'
}) ;
//-------------------------------------------------------------------------------
request.query({ email: '[email protected]' })

request({
	query: { email: '[email protected]' }
}) ;
//-------------------------------------------------------------------------------
request
.query('search=Manny')
.query('range=1..5')

request({
	query: [ ['search=Manny'], ['range=1..5'] ]
}) ;
//-------------------------------------------------------------------------------
request
.query({ query: 'Manny' })
.query({ range: '1..5' })
.query({ order: 'desc' })

request({
	query: [ [{ query: 'Manny' }], [{ range: '1..5' }], [{ order: 'desc' }] ]
}) ;
//-------------------------------------------------------------------------------
request.set('API-Key', 'foobar')

request({
	set: ['API-Key', 'foobar']
}) ;
//-------------------------------------------------------------------------------
request
.set('API-Key', 'foobar')
.set('Accept', 'application/json')

request({
	set: [ ['API-Key', 'foobar'], ['Accept', 'application/json'] ]
}) ;
//-------------------------------------------------------------------------------
request
.post('/upload')
.field('user[name]', 'Tobi')
.field('user[email]', '[email protected]')
.field('friends[]', ['loki', 'jane'])
.attach('image', 'path/to/tobi.png')
.then(callback);

request({
	post: '/upload',
	field: [
		['user[name]', 'Tobi'],
		['user[email]', '[email protected]'],
		['friends[]', ['loki', 'jane'] ],
	],
	attach: ['image', 'path/to/tobi.png'],
	then: callback
}) ;

Contributions

Contributions are most welcome.

Licence

Copyright (c) 2018 Damien Clark

Licensed under the Apache License, Version 2.0

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Acknowledgements / Attribution

Many thanks to the team who created and maintain the superagent project.