https://github.com/shgysk8zer0/netlify-func-utils
A collection of helpful functions for building Netlify Functions
https://github.com/shgysk8zer0/netlify-func-utils
aws-lambda form-data netlify netlify-functions
Last synced: 6 days ago
JSON representation
A collection of helpful functions for building Netlify Functions
- Host: GitHub
- URL: https://github.com/shgysk8zer0/netlify-func-utils
- Owner: shgysk8zer0
- License: mit
- Created: 2023-09-13T23:14:12.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2026-02-26T22:59:08.000Z (12 days ago)
- Last Synced: 2026-02-27T05:00:08.200Z (12 days ago)
- Topics: aws-lambda, form-data, netlify, netlify-functions
- Language: JavaScript
- Homepage: https://npmjs.com/package/@shgysk8zer0/netlify-func-utils
- Size: 8.88 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# @shgysk8zer0/netlify-func-utils
A collection of helpful functions for building Netlify Functions
> [!IMPORTANT]
> This package relies on `FormData` and `File` support, which was added in Node 20
> and will be available on Netlify and AWS Lambda sometime after it reaches LTS
> on or about 2023-10-24. In Node 18, `FormData` is supported, but uploads will
be `Blob`s instead of `File`s.
## Features
- Custom `NetlifyRequest` class extending `Request`
- Created from a `HandlerEvent` event
- Adds convenient `searchParams` property (`URLSearchParams`)
- Adds `cookies` property as a `Map` from the `Cookie` header
- A `createHandler` function
- Accepts an object of HTTP Methods and callbacks
- Automatically handles errors
- Callbacks are passed a `NetlifyRequest` object and must return a `Response`
- Automatically adds CORS headers (can be disabled)
## Example
```js
import { createHandler } from '@shgysk8zer0/netlify-func-utils`;
import { BAD_REQUEST, NOT_AUTHORIZED } from '@shgysk8zer0/http/status';
import { HttpError } from '@shgysk8zer0/http/error';
export const handler = createHandler({
get: async req => {
if (req.searchParams.has('id')) {
return Response.json(await getItem(req.searchParams.get('id)));
}
},
post: async req = {
if (! req.cookies.has('token')) {
throw new HTTPError('You must be signed-in.', { status: NOT_AUTHORIZED });
} else {
const data = await req.formData(); // `FormData`, including `File` objects
if (! data.has('required-field')) {
throw new HTTPError('Missing requied field.', { status: BAD_REQUEST });
} else {
// Maybe save something to a DB.
return Response.json({ created: item.id });
}
}
},
delete: async req => {
//
},
}, {
cors: true,
headers: new Headers({ 'X-UID': crypto.randomUUID() }),
allowHeaders: ['X-Foo', 'X-UID'],
}
```