A lightweight fetch wrapper for AWS Lambda functions
lambda-fetch is a simple utility library that provides a convenient wrapper around the Fetch API for use in AWS Lambda functions. It handles common concerns like error handling, timeouts, and response parsing.
- π Simple and intuitive API
- β‘ Built on the native Fetch API
- π‘οΈ Automatic error handling
- β±οΈ Configurable timeouts
- π¦ Zero dependencies
- π Supports all HTTP methods (GET, POST, PUT, DELETE, etc.)
npm install lambda-fetchOr using yarn:
yarn add lambda-fetchconst { fetch } = require('lambda-fetch');
exports.handler = async (event) => {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return {
statusCode: 200,
body: JSON.stringify(data)
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
};const { fetch } = require('lambda-fetch');
const data = await fetch('https://api.example.com/users');
const users = await data.json();const { fetch } = require('lambda-fetch');
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'John Doe',
email: 'john@example.com'
})
});
const newUser = await response.json();const { fetch } = require('lambda-fetch');
const response = await fetch('https://api.example.com/protected', {
headers: {
'Authorization': 'Bearer your-token-here',
'X-Custom-Header': 'value'
}
});const { fetch } = require('lambda-fetch');
const response = await fetch('https://api.example.com/slow-endpoint', {
timeout: 5000 // 5 seconds
});Makes an HTTP request to the specified URL.
url(string): The URL to fetchoptions(object, optional): Configuration optionsmethod(string): HTTP method (GET, POST, PUT, DELETE, etc.). Default: 'GET'headers(object): Request headersbody(string | object): Request bodytimeout(number): Request timeout in milliseconds. Default: 30000 (30 seconds)
Returns a Promise that resolves to a Response object.
- Node.js 18.x or higher
- npm or yarn
- Clone the repository:
git clone https://github.com/lukehedger/lambda-fetch.git
cd lambda-fetch- Install dependencies:
npm install- Run tests:
npm test- Build the project:
npm run buildlambda-fetch/
βββ src/ # Source code
βββ test/ # Test files
βββ dist/ # Built files (generated)
βββ package.json # Project configuration
βββ README.md # This file
Run the test suite:
npm testRun tests with coverage:
npm run test:coverageContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - see the LICENSE file for details
Luke Hedger (@lukehedger)
If you encounter any issues or have questions, please open an issue on GitHub.