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

serverloose

v2.0.18

Published

serverless utilities

Downloads

24

Readme

serverloose

serverless utilities

npm install serverloose

Usage

In your serverless application, create request handlers by importing the handler function utility. responder is the first argument and should be a function. It can be asynchronous.

import handler from 'serverloose';

export default handler(() => ({
	message: 'hello'
}));

The default response content type is application/json. To change this, set the contentType property on the options object:

import handler from 'serverloose';

export default handler(() => (
	'<h1>hello</h1>'
), {contentType: 'text/html'});

To restrict available request methods, set the methods property on the options object:

import handler from 'serverloose';

export default handler(() => (
	'<h1>hello</h1>'
), {methods: ['post']});

Parsing URLs and incoming body

Parsing incoming URL

import handler, {getRequestUrl} from 'serverloose';

export default handler(({request}) => ({
	url: getRequestUrl(request)
}));

Parsing query strings

import handler, {getRequestQuery} from 'serverloose';

export default handler(({request}) => ({
	query: getRequestQuery(request, 'query')
}));
import handler, {getRequestQuery} from 'serverloose';

export default handler(({request}) => {
	const [id, name] = getRequestQuery(request, ['id', 'name']);
	return {id, name};
});
import handler, {getRequestQuery} from 'serverloose';

export default handler(({request}) => {
	const getQuery = getRequestQuery(request);

	if (getQuery('test')) {
		return {test: getQuery('test')};
	}

	return {test: false};
});

Parsing incoming body

import handler, {parseRequestJson} from 'serverloose';

export default handler(async ({request}) => {
	const body = await parseRequestJson(request);
	return {body, type: 'json'};
});
import handler, {parseRequestForm} from 'serverloose';

export default handler(async ({request}) => {
	const body = await parseRequestForm(request);
	return {body, type: 'form'};
});

Errors

Throwing inside the responder function is encouraged:

import handler from 'serverloose';

export default handler(() => {
	const error = new Error('Unauthorized');

	error.status = 401;
	error.code = error.type = 'unauthorized';

	throw error;
});

This will respond the following body with status 401:

{"success":false,"error":{"code":"unauthorized","message":"Unauthorized"}}

If error.type is not set, the output error will be obfuscated to prevent loss of critical information. The error is logged to the server.

{"success":false,"error":{"code":"unknown_error","message":"Unknown error"}}