X Tutup
Skip to content

MessingAround-test/brewing-data-api

Repository files navigation

# Brewing Data API

A TypeScript-based serverless API built with AWS Lambda, API Gateway, and DynamoDB, using AWS SDK v3.

## Local Development

### Prerequisites

- Node.js 16.x or later
- Docker (for local DynamoDB)
- AWS CLI (for local development)

### Setup

1. Install dependencies:
   ```bash
   npm install
   ```

2. Start local DynamoDB (in a separate terminal):
   ```bash
   docker run -p 8000:8000 amazon/dynamodb-local
   ```

3. Create the required tables:
   ```bash
   # Create API keys table
   aws dynamodb create-table \
     --endpoint-url http://localhost:8000 \
     --table-name local-api-keys \
     --attribute-definitions AttributeName=api_key,AttributeType=S \
     --key-schema AttributeName=api_key,KeyType=HASH \
     --billing-mode PAY_PER_REQUEST

   # Create example requests table
   aws dynamodb create-table \
     --endpoint-url http://localhost:8000 \
     --table-name example-requests \
     --attribute-definitions AttributeName=id,AttributeType=S \
     --key-schema AttributeName=id,KeyType=HASH \
     --billing-mode PAY_PER_REQUEST
   ```

4. Start the local development server:
   ```bash
   # For Windows
   set NODE_ENV=local && npx ts-node local-dev.ts
   
   # For macOS/Linux
   NODE_ENV=local npx ts-node local-dev.ts
   ```

### Testing the API

When running locally, API key validation is disabled for easier development and testing.

```bash
# Test the example endpoint (no API key required locally)
curl -X POST http://localhost:3456/example \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "age": 30}'
```

For production deployments, API key validation is automatically enabled. To test with API keys locally, you can set the `SKIP_API_KEY_VALIDATION` environment variable to `false`:

```bash
# For Windows
set SKIP_API_KEY_VALIDATION=false && npx ts-node local-dev.ts

# For macOS/Linux
SKIP_API_KEY_VALIDATION=false npx ts-node local-dev.ts
```

## Project Structure

- `src/` - Source code
  - `handlers/` - Lambda function handlers
    - `example/` - Example API endpoint
    - `BaseHandler.ts` - Base handler class with common functionality
  - `types/` - TypeScript type definitions
- `local-dev.ts` - Local development server
- `*.tf` - Terraform configuration files
- `package.json` - Project dependencies and scripts

## Deployment

1. Configure AWS credentials:
   ```bash
   aws configure
   ```

2. Deploy to AWS:
   ```bash
   npm run deploy
   ```

## AWS SDK v3 Migration Notes

This project uses AWS SDK v3, which provides several benefits over v2:
- Modular architecture (smaller bundle size)
- Improved TypeScript support
- Better error handling
- First-class async/await support

Key differences from v2:
- Import specific service clients instead of the entire SDK
- Commands are created using `new CommandName(params)`
- Use `await client.send(command)` instead of `client.command(params).promise()`

## License

MIT

About

Using windsurf to generate the template then trying to make some useful tools for myself

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

X Tutup