{"_id":"@abstraks-dev/lambda-responses","_rev":"2-2fc6a9b511456e5b9fcc3b4d3a5eaf3d","name":"@abstraks-dev/lambda-responses","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@abstraks-dev/lambda-responses","version":"1.0.0","keywords":["aws","lambda","api-gateway","cors","responses","http","microservices"],"author":{"name":"Abstraks"},"license":"MIT","_id":"@abstraks-dev/lambda-responses@1.0.0","maintainers":[{"name":"abstraks-dev","email":"contactabstraks@gmail.com"}],"homepage":"https://github.com/Abstraks-co/shared-modules#readme","bugs":{"url":"https://github.com/Abstraks-co/shared-modules/issues"},"dist":{"shasum":"23c431c24a206044876755d92aa3b0a7803dad38","tarball":"https://registry.npmjs.org/@abstraks-dev/lambda-responses/-/lambda-responses-1.0.0.tgz","fileCount":5,"integrity":"sha512-ugB0PzgUPa9LsIWSofzzR7Ansf5/C/7I5wlB1X+mTAtycrsPNjzSRKKrnct2QQQbEDu/LV7u0ZyoLok+ubLN8Q==","signatures":[{"sig":"MEQCIBF9jyDls5097vlBLR3G2xFncgyx+uUyMkb+2EUBHY5+AiAS4BXC96Z+yvzpMlhA9OhoOQ6g0ZkZjwkDJ/VALLX7Kg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":15543},"main":"src/index.js","type":"module","engines":{"node":">=20.0.0"},"gitHead":"8aa41df62a281863efc707a08e35120b759f358e","scripts":{"test":"NODE_OPTIONS=--experimental-vm-modules jest","test:watch":"NODE_OPTIONS=--experimental-vm-modules jest --watch","test:coverage":"NODE_OPTIONS=--experimental-vm-modules jest --coverage"},"_npmUser":{"name":"abstraks-dev","email":"contactabstraks@gmail.com"},"repository":{"url":"git+https://github.com/Abstraks-co/shared-modules.git","type":"git","directory":"packages/lambda-responses"},"_npmVersion":"10.8.2","description":"Standardized Lambda response helpers with CORS support for AWS API Gateway","directories":{},"_nodeVersion":"20.19.5","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/lambda-responses_1.0.0_1763506076467_0.6985829831166035","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@abstraks-dev/lambda-responses","version":"1.0.1","description":"Standardized Lambda response helpers with CORS support for AWS API Gateway","main":"src/index.js","type":"module","scripts":{"test":"NODE_OPTIONS=--experimental-vm-modules jest","test:watch":"NODE_OPTIONS=--experimental-vm-modules jest --watch","test:coverage":"NODE_OPTIONS=--experimental-vm-modules jest --coverage"},"keywords":["aws","lambda","api-gateway","cors","responses","http","microservices"],"author":{"name":"Abstraks"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/Abstraks-co/shared-modules.git","directory":"packages/lambda-responses"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"engines":{"node":">=20.0.0"},"_id":"@abstraks-dev/lambda-responses@1.0.1","gitHead":"2b247a175ceba75cf8a499d613edc07eaebcb147","bugs":{"url":"https://github.com/Abstraks-co/shared-modules/issues"},"homepage":"https://github.com/Abstraks-co/shared-modules#readme","_nodeVersion":"20.19.5","_npmVersion":"10.8.2","dist":{"integrity":"sha512-y6GDf0579rPK0bxtx771niHXMgKDWhgIeKHk9v0JdcRm8Scj6+5FEpBxWQXE+4HOHHShMbAO3XynfJTBAs4xcQ==","shasum":"cd4e91cdebd6b07bd131b19d4b40a323b33d87be","tarball":"https://registry.npmjs.org/@abstraks-dev/lambda-responses/-/lambda-responses-1.0.1.tgz","fileCount":5,"unpackedSize":15543,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIDySNZWVqgLYl8awbF+8tiKb4F2GrpTCZRdEw78UqSzDAiEAolzfmSbHM7uj2Bzd21Nf5oqmcCjuA9hyqFWkvcXOHKY="}]},"_npmUser":{"name":"abstraks-dev","email":"contactabstraks@gmail.com"},"directories":{},"maintainers":[{"name":"abstraks-dev","email":"contactabstraks@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/lambda-responses_1.0.1_1763657695994_0.1560430109596802"},"_hasShrinkwrap":false}},"time":{"created":"2025-11-18T22:47:56.396Z","modified":"2025-11-20T16:54:56.368Z","1.0.0":"2025-11-18T22:47:56.666Z","1.0.1":"2025-11-20T16:54:56.170Z"},"bugs":{"url":"https://github.com/Abstraks-co/shared-modules/issues"},"author":{"name":"Abstraks"},"license":"MIT","homepage":"https://github.com/Abstraks-co/shared-modules#readme","keywords":["aws","lambda","api-gateway","cors","responses","http","microservices"],"repository":{"type":"git","url":"git+https://github.com/Abstraks-co/shared-modules.git","directory":"packages/lambda-responses"},"description":"Standardized Lambda response helpers with CORS support for AWS API Gateway","maintainers":[{"name":"abstraks-dev","email":"contactabstraks@gmail.com"}],"readme":"# @abstraks-dev/lambda-responses\n\nStandardized Lambda response helpers with CORS support for AWS API Gateway.\n\n## Installation\n\n```bash\nnpm install @abstraks-dev/lambda-responses\n```\n\n## Features\n\n- ✅ Consistent response format across all Lambda functions\n- ✅ Built-in CORS headers for all responses\n- ✅ Type-safe error handling\n- ✅ Common HTTP status code helpers\n- ✅ Automatic error response conversion\n- ✅ Zero dependencies\n\n## Usage\n\n### Basic Success Response\n\n```javascript\nimport { createSuccessResponse } from '@abstraks-dev/lambda-responses';\n\nexport const handler = async (event) => {\n\tconst data = { message: 'Hello World', timestamp: new Date().toISOString() };\n\treturn createSuccessResponse(data);\n};\n\n// Returns:\n// {\n//   statusCode: 200,\n//   headers: { /* CORS headers */ },\n//   body: '{\"message\":\"Hello World\",\"timestamp\":\"2024-01-01T00:00:00.000Z\"}'\n// }\n```\n\n### Error Responses\n\n```javascript\nimport {\n\tcreateBadRequestResponse,\n\tcreateUnauthorizedResponse,\n\tcreateNotFoundResponse,\n\tcreateServerErrorResponse,\n} from '@abstraks-dev/lambda-responses';\n\n// Bad Request (400)\nexport const handler = async (event) => {\n\tif (!event.body) {\n\t\treturn createBadRequestResponse('Request body is required');\n\t}\n\t// ... your logic\n};\n\n// Unauthorized (401)\nif (!isAuthenticated) {\n\treturn createUnauthorizedResponse('Invalid token');\n}\n\n// Not Found (404)\nif (!user) {\n\treturn createNotFoundResponse('User not found');\n}\n\n// Server Error (500)\ntry {\n\t// ... your logic\n} catch (error) {\n\treturn createServerErrorResponse('Database connection failed');\n}\n```\n\n### Error Response from Error Object\n\n```javascript\nimport { createErrorResponse } from '@abstraks-dev/lambda-responses';\n\nexport const handler = async (event) => {\n\ttry {\n\t\t// ... your logic\n\t} catch (error) {\n\t\t// Automatically uses error.statusCode if present, defaults to 500\n\t\treturn createErrorResponse(error);\n\t}\n};\n\n// Custom status code:\nconst error = new Error('Resource not found');\nerror.statusCode = 404;\nreturn createErrorResponse(error);\n```\n\n### Validation Errors\n\n```javascript\nimport { createValidationErrorResponse } from '@abstraks-dev/lambda-responses';\n\nconst validationErrors = [\n\t{ field: 'email', message: 'Invalid email format' },\n\t{ field: 'password', message: 'Password must be at least 8 characters' },\n];\n\nreturn createValidationErrorResponse(validationErrors);\n\n// Returns:\n// {\n//   statusCode: 422,\n//   headers: { /* CORS headers */ },\n//   body: '{\"error\":\"Validation failed\",\"details\":[...]}'\n// }\n```\n\n### CORS Preflight (OPTIONS)\n\n```javascript\nimport { createOptionsResponse } from '@abstraks-dev/lambda-responses';\n\nexport const handler = async (event) => {\n\tif (event.httpMethod === 'OPTIONS') {\n\t\treturn createOptionsResponse();\n\t}\n\t// ... your logic\n};\n```\n\n### Error Handling Middleware\n\n```javascript\nimport { withErrorHandling } from '@abstraks-dev/lambda-responses';\n\nconst myHandler = async (event, context) => {\n\t// If this throws an error, it will automatically be converted to an error response\n\tconst data = await fetchData();\n\treturn createSuccessResponse(data);\n};\n\n// Wrap with automatic error handling\nexport const handler = withErrorHandling(myHandler);\n```\n\n### Custom Status Codes\n\n```javascript\nimport { createSuccessResponse } from '@abstraks-dev/lambda-responses';\n\n// Created (201)\nreturn createSuccessResponse({ id: '123', created: true }, 201);\n\n// Accepted (202)\nreturn createSuccessResponse({ queued: true }, 202);\n\n// No Content (204)\nreturn createSuccessResponse({}, 204);\n```\n\n### Custom Headers\n\n```javascript\nimport {\n\tcreateSuccessResponse,\n\tCORS_HEADERS,\n} from '@abstraks-dev/lambda-responses';\n\nconst customHeaders = {\n\t...CORS_HEADERS,\n\t'X-Custom-Header': 'value',\n\t'Cache-Control': 'max-age=3600',\n};\n\nreturn createSuccessResponse(data, 200, customHeaders);\n```\n\n## API Reference\n\n### Response Creators\n\n- **`createSuccessResponse(data, statusCode?, headers?)`** - Create success response (default 200)\n- **`createErrorResponse(error, headers?)`** - Create error response from Error object\n- **`createBadRequestResponse(message?, headers?)`** - Create 400 response\n- **`createUnauthorizedResponse(message?, headers?)`** - Create 401 response\n- **`createForbiddenResponse(message?, headers?)`** - Create 403 response\n- **`createNotFoundResponse(message?, headers?)`** - Create 404 response\n- **`createValidationErrorResponse(errors, headers?)`** - Create 422 response\n- **`createServerErrorResponse(message?, headers?)`** - Create 500 response\n- **`createOptionsResponse(headers?)`** - Create OPTIONS response for CORS preflight\n\n### Utilities\n\n- **`createResponse(statusCode, body, headers?)`** - Low-level response creator\n- **`createErrorResponseWithCode(statusCode, message, headers?)`** - Create error with specific code\n- **`withErrorHandling(handler)`** - Wrap handler with automatic error handling\n- **`CORS_HEADERS`** - Default CORS headers object\n\n## Response Format\n\nAll responses follow this structure:\n\n```javascript\n{\n  statusCode: number,\n  headers: {\n    'Access-Control-Allow-Origin': '*',\n    'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',\n    'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',\n    'Access-Control-Allow-Credentials': 'true',\n  },\n  body: string // JSON stringified\n}\n```\n\n### Success Response Body\n\n```json\n{\n\t// Your data directly\n}\n```\n\n### Error Response Body\n\n```json\n{\n\t\"error\": \"Error message\"\n}\n```\n\n### Validation Error Response Body\n\n```json\n{\n\t\"error\": \"Validation failed\",\n\t\"details\": [{ \"field\": \"email\", \"message\": \"Invalid format\" }]\n}\n```\n\n## Migration from Existing Code\n\n### Before\n\n```javascript\n// Old code in each service\nexport const createErrorResponse = (error) => {\n\treturn {\n\t\tstatusCode: error.statusCode || 500,\n\t\theaders: {\n\t\t\t'Access-Control-Allow-Origin': '*',\n\t\t\t'Access-Control-Allow-Headers':\n\t\t\t\t'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',\n\t\t\t'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',\n\t\t\t'Access-Control-Allow-Credentials': 'true',\n\t\t},\n\t\tbody: JSON.stringify({ error: error.message }),\n\t};\n};\n```\n\n### After\n\n```javascript\n// New code using shared module\nimport { createErrorResponse } from '@abstraks-dev/lambda-responses';\n\n// That's it! Same functionality, maintained in one place\n```\n\n## Testing\n\nThe package includes comprehensive tests with 100% coverage:\n\n```bash\nnpm test\n```\n\n## License\n\nMIT\n\n## Contributing\n\nSee the main [shared-modules repository](https://github.com/Abstraks-co/shared-modules) for contribution guidelines.\n","readmeFilename":"README.md"}