{"_id":"@anchan.ashwithabg/twilio-function-helpers","_rev":"3-3bc58d1d5c5ed08585017b069dba8917","name":"@anchan.ashwithabg/twilio-function-helpers","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@anchan.ashwithabg/twilio-function-helpers","version":"1.0.0","keywords":["twilio","functions","helpers","transformations","serverless","error-handling"],"author":{"name":"Twilio"},"license":"MIT","_id":"@anchan.ashwithabg/twilio-function-helpers@1.0.0","maintainers":[{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"}],"homepage":"https://github.com/twilio/twilio-data-egress-domain#readme","bugs":{"url":"https://github.com/twilio/twilio-data-egress-domain/issues"},"dist":{"shasum":"6c57307390d19a169dce7dea076813ccd7729091","tarball":"https://registry.npmjs.org/@anchan.ashwithabg/twilio-function-helpers/-/twilio-function-helpers-1.0.0.tgz","fileCount":4,"integrity":"sha512-cf9VYtEPTOuycThKaG9W6GXE22sk9oo0i2tad/S8GH2EsuXdrXEQKDFbuW+W+jnpdBMEuy/2FZLkaDbW7/2OOQ==","signatures":[{"sig":"MEUCIQC5UmbNwdfVLhECNtiV3aZXZH79sw+3rY51ihLXGJa0cAIgQeIjspH7D2fD+Cuml6jqM9gGESO3N1IIX/UsFCHaeKU=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":8921},"main":"index.js","engines":{"node":">=14.0.0"},"gitHead":"9574c061931cf810040f0ec37fde1013f5f36f91","scripts":{"test":"node test.js","prepublishOnly":"npm test"},"_npmUser":{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"},"repository":{"url":"git+https://github.com/twilio/twilio-data-egress-domain.git","type":"git","directory":"tools/twilio-function-helpers"},"_npmVersion":"10.9.2","description":"Helper utilities for Twilio Functions with Segment-style error handling","directories":{},"_nodeVersion":"22.14.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/twilio-function-helpers_1.0.0_1777992425513_0.00482852175574644","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@anchan.ashwithabg/twilio-function-helpers","version":"1.0.1","keywords":["twilio","functions","helpers","transformations","serverless","error-handling"],"author":{"name":"Twilio"},"license":"MIT","_id":"@anchan.ashwithabg/twilio-function-helpers@1.0.1","maintainers":[{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"}],"dist":{"shasum":"f996194400bbdbbe19e610ebbff6d154745eb9a3","tarball":"https://registry.npmjs.org/@anchan.ashwithabg/twilio-function-helpers/-/twilio-function-helpers-1.0.1.tgz","fileCount":4,"integrity":"sha512-NIghlj/ff1flu/FzOyOCk7LFeyv/sIbg4q/2gs9TLvMDkxXQgyzG5951EZkCdT4jMrI+28UksajSzOybxTGIQw==","signatures":[{"sig":"MEUCIQD1Z1xRdTqlEJt+HuUlGB8MdBjRrGsLYZbVg643oQSfcgIgfy4H26iIcK6ipZSEKNAQbReNc0uf1ItQLZDyvHkUNaI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":8756},"main":"index.js","engines":{"node":">=14.0.0"},"gitHead":"9574c061931cf810040f0ec37fde1013f5f36f91","scripts":{"test":"node test.js","prepublishOnly":"npm test"},"_npmUser":{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"},"_npmVersion":"10.9.2","description":"Helper utilities for Twilio Functions with Segment-style error handling","directories":{},"_nodeVersion":"22.14.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/twilio-function-helpers_1.0.1_1777997437040_0.4520511070624995","host":"s3://npm-registry-packages-npm-production"}},"1.1.0":{"name":"@anchan.ashwithabg/twilio-function-helpers","version":"1.1.0","description":"Helper utilities for Twilio Functions with Segment-style error handling","main":"index.js","scripts":{"test":"node test.js","prepublishOnly":"npm test"},"keywords":["twilio","functions","helpers","transformations","serverless","error-handling"],"author":{"name":"Twilio"},"license":"MIT","engines":{"node":">=14.0.0"},"_id":"@anchan.ashwithabg/twilio-function-helpers@1.1.0","gitHead":"9574c061931cf810040f0ec37fde1013f5f36f91","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-wpC2cTu55HYCwf2zMPuJoq5QkklUl2I3o/eTv1+v2d6uPYGqk/EHlF7g0h+tqyruNBrpr+1Y2Ghoha/0Q3zRzg==","shasum":"df0ab800f255abe8f88484fe07d8eea890fc00ba","tarball":"https://registry.npmjs.org/@anchan.ashwithabg/twilio-function-helpers/-/twilio-function-helpers-1.1.0.tgz","fileCount":4,"unpackedSize":9881,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDhzjdAAWKJuwMg/Pvq7zUAMFSszYeWGqwGE8PKhZ4KfAIgJ0DUZwjmSD0J9NRlaBRpE3I8a5nIWBn60PLBBrXR7wU="}]},"_npmUser":{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"},"directories":{},"maintainers":[{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/twilio-function-helpers_1.1.0_1778003134779_0.8578828004658796"},"_hasShrinkwrap":false}},"time":{"created":"2026-05-05T14:47:05.352Z","modified":"2026-05-05T17:45:35.028Z","1.0.0":"2026-05-05T14:47:05.644Z","1.0.1":"2026-05-05T16:10:37.176Z","1.1.0":"2026-05-05T17:45:34.937Z"},"author":{"name":"Twilio"},"license":"MIT","keywords":["twilio","functions","helpers","transformations","serverless","error-handling"],"description":"Helper utilities for Twilio Functions with Segment-style error handling","maintainers":[{"name":"anchan.ashwithabg","email":"anchan.ashwithabg95@gmail.com"}],"readme":"# Twilio Function Helpers\n\nHelper utilities for Twilio Functions with Segment-style error handling.\n\n## Installation\n\n```bash\nnpm install twilio-function-helpers\n```\n\n## Usage\n\n### Before (Manual Response Handling)\n\n```javascript\nexports.handler = function(context, event, callback) {\n  const response = new Twilio.Response();\n  response.appendHeader('Content-Type', 'application/json');\n\n  const payload = event.event?.payload;\n\n  if (!payload) {\n    response.setStatusCode(400);\n    response.setBody({ error: \"Missing event.payload\" });\n    return callback(null, response);\n  }\n\n  if (payload.error_code === \"81018\") {\n    response.setStatusCode(422);\n    response.setBody({});\n    return callback(null, response);\n  }\n\n  response.setStatusCode(200);\n  response.setBody({ success: true });\n  return callback(null, response);\n};\n```\n\n### After (With Helpers)\n\n```javascript\nconst { wrapHandler, DropEvent, BadRequest } = require('twilio-function-helpers');\n\nexports.handler = wrapHandler(function(context, event, callback) {\n  // Note: event is automatically unwrapped from event.event to event\n  const payload = event.payload;\n\n  if (!payload) {\n    throw new BadRequest(\"Missing event.payload\");\n  }\n\n  if (payload.error_code === \"81018\") {\n    throw new DropEvent(); // Returns 422 automatically\n  }\n\n  return { success: true }; // Returns 200 automatically\n});\n```\n\n## API\n\n### `wrapHandler(handlerFn)`\n\nWraps your handler function to automatically handle responses and errors.\n\n- **Unwraps events**: Automatically converts `event.event` to `event` for cleaner access\n- **Auto-cleanup**: Removes sensitive fields (`request`, `dataschema`) from responses\n- **Returns data**: Automatically sends 200 with the cleaned data\n- **Throws FunctionResponse**: Automatically sends the appropriate status code\n- **Throws other errors**: Automatically sends 500\n\n### Error Classes\n\n#### `DropEvent`\n- **Status Code**: 422\n- **Use**: Filter/discard events without retry\n- **Example**: `throw new DropEvent('Filtered error 81018')`\n\n#### `BadRequest`\n- **Status Code**: 400\n- **Use**: Malformed requests that shouldn't be retried\n- **Example**: `throw new BadRequest('Missing required field')`\n\n#### `ValidationError`\n- **Status Code**: 422\n- **Use**: Well-formed but semantically invalid requests\n- **Example**: `throw new ValidationError('Invalid email format')`\n\n#### `RetryError`\n- **Status Code**: 429 (default) or 500\n- **Use**: Request retry\n- **Example**: `throw new RetryError('Downstream service unavailable', 503)`\n\n#### `InternalError`\n- **Status Code**: 500\n- **Use**: Unexpected errors that should trigger retry\n- **Example**: `throw new InternalError('Database connection failed')`\n\n## Complete Example\n\n```javascript\nconst { \n  wrapHandler, \n  DropEvent, \n  BadRequest, \n  ValidationError \n} = require('twilio-function-helpers');\n\nexports.handler = wrapHandler(function(context, event, callback) {\n  // Note: event is auto-unwrapped, so event.payload not event.event.payload\n  \n  // 1. Validate structure\n  const payload = event.payload;\n  if (!payload) {\n    throw new BadRequest(\"Malformed request: Missing event.payload\");\n  }\n\n  // 2. Filter specific events\n  if (payload.error_code === \"81018\") {\n    throw new DropEvent('Error 81018 filtered');\n  }\n\n  // 3. Validate semantics\n  if (!payload.id) {\n    throw new ValidationError('Missing required field: id');\n  }\n\n  // 4. Transform and return\n  let outboundEvent = JSON.parse(JSON.stringify(event));\n  outboundEvent.payload.service_name = \"twilio\";\n\n  // Note: dataschema and request fields are automatically removed\n  return { event: outboundEvent }; // Automatically returns 200\n});\n```\n\n## Status Code Reference\n\n| Class | Status Code | Retry? | Use Case |\n|-------|------------|--------|----------|\n| `DropEvent` | 422 | No | Filter/discard events |\n| `BadRequest` | 400 | No | Malformed requests |\n| `ValidationError` | 422 | No | Invalid data |\n| `RetryError` | 429/500 | Yes | Temporary failures |\n| `InternalError` | 500 | Yes | Unexpected errors |\n\n## License\n\nMIT\n","readmeFilename":"README.md"}