2.0.18 • Published 1 year ago

serverloose v2.0.18

Weekly downloads
11
License
MIT
Repository
github
Last release
1 year ago

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"}}
2.0.15

1 year ago

2.0.16

1 year ago

2.0.17

1 year ago

2.0.18

1 year ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.11

2 years ago

2.0.12

2 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.0

6 years ago