{"id":23510826,"url":"https://github.com/danielkov/aera-tools","last_synced_at":"2025-10-10T08:35:15.232Z","repository":{"id":57173924,"uuid":"86994451","full_name":"danielkov/aera-tools","owner":"danielkov","description":"Handy HTTP tools to use with Aera HTTP library.","archived":false,"fork":false,"pushed_at":"2017-04-05T11:45:35.000Z","size":283,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T07:45:16.935Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-02T15:12:32.000Z","updated_at":"2017-04-04T13:50:16.000Z","dependencies_parsed_at":"2022-09-02T10:22:22.089Z","dependency_job_id":null,"html_url":"https://github.com/danielkov/aera-tools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Faera-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Faera-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Faera-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Faera-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielkov","download_url":"https://codeload.github.com/danielkov/aera-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990503,"owners_count":21995776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-25T12:12:27.406Z","updated_at":"2025-10-10T08:35:10.183Z","avatar_url":"https://github.com/danielkov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Aera Tools\nHandy HTTP tools to use with Aera HTTP library.\n___\n\n[![Build Status](https://travis-ci.org/danielkov/aera-tools.svg?branch=master)](https://travis-ci.org/danielkov/aera-tools) [![Coverage Status](https://coveralls.io/repos/github/danielkov/aera-tools/badge.svg?branch=master)](https://coveralls.io/github/danielkov/aera-tools?branch=master) [![Dependencies](https://david-dm.org/danielkov/aera-tools.svg)](https://david-dm.org/danielkov/aera-tools) [![devDependencies Status](https://david-dm.org/danielkov/aera-tools/dev-status.svg)](https://david-dm.org/danielkov/aera-tools?type=dev) [![npm](https://img.shields.io/npm/v/aera-tools.svg?style=flat-square)](https://npmjs.com/package/aera-tools)\n\n## List of Tools\n\n  - [Http Error](#http-error) - create an Error object with HTTP specific proerties.\n  - [Body Parser](#body-parser) - turn request bodies into promises, resolving into JavaScript Objects.\n  - [Compose](#compose) - if you prefer middleware-based approach, it can be done with this module.\n  - [Middleware](#middleware) - create real `express`-like middleware.\n  - [Static](#static) - Serve static files even easier!\n  - [Cors](#cors) - configure Cross Origin Resource Sharing!\n\n## Http Error\n\nThis constructor creates an easy-to-use Error object, that is an instance of JavaScript `Error`, but also has a status property. Throw this instead of regular errors and Aera will automatically give better error responses.\n\n### Example usage\n\n```js\nconst { HttpError } = require('aera-tools')\n\nserver.get('/auth', () =\u003e new HttpError('You are not logged in.', 401)) // Response will be `You are not logged in.` with status 401.\n```\n\n## Body Parser\n\nRequest bodies are generally kind of hard to parse, especially of no proper content type has been set on the request (which this module does not help with at the moment, by the way). This tool takes in the request object and returns a promise, that resolves into the parsed body object, or rejects into an empty object for your convenience.\n\n### Example usage\n\n```js\nconst { parseBody } = require('aera-tools')\n\nserver.post('/', (req, res) =\u003e parseBody(req)) // this will return the body parsed into an object\n```\n\nYou can also do some stuff with the body, once it's been resolved.\n\n```js\nserver.post('/', (req, res) =\u003e bodyParser(req).then(createResourceInDb))\n```\n\n## Compose\n\nAera does not encourage middleware-based approach to writing your HTTP applications, however if you do want to provide multiple functions in sequence that you want executed, you can use this tool.\n\n### Example usage\n\n```js\nconst { compose } = require('aera-tools')\n\nserver.get('/', compose(req, res, function1, function2))\n```\n\nEach of these functions will have full access to request and response. E.g.:\n\n```js\nserver.get('/', compose(\n  (req, res) =\u003e res.setHeader('X-Custom-Header', 'value'),\n  (req, res) =\u003e console.log(res.getHeader('x-custom-header') === 'value'), // logs true\n  (req, res) =\u003e 'Hello, my app!'\n))\n```\n\nAs you can see, there is no calling `next` in this tool. That will be the [middleware](#middleware) tool.\n\n## Middleware\n\nIf you want to use real `express`-style middleware with Aera, you can do so, with this tool. Do note that this will somewhat decrease the performance of Aera, so if you've made your application logic truly decoupled from the feedback logic, this tool should not be needed.\n\n### Example usage\n\n```js\nconst { middleware } = require('aera-tools')\n\nserver.get('/', middleware(\n  (req, res, next) =\u003e {\n    let t1 = Date.now()\n    next()\n    console.log(`Request took: ${Date.now() - t1} milliseconds.`)\n  }, (req, res, next) =\u003e {\n    return 'Hello, World!'\n  }\n))\n```\n\n## Static\n\n**Note:** this will only work, from version `1.1.0` of Aera, due to how arguments are handled in the newer version.\n\nServes all files from the directory specified.\n\n### Example usage\n\n```js\nconst { static } = require('aera-tools')\n\nserver.get(static('/public', './my/file/folder'))\n```\n\nThe above example will serve requests coming to `/public` with the contents of `/my/file/folder`.\n\n## Cors\n\nConfigure a route to use Cross Origin Resouce Sharing policies properly. Note that this will only work for the specific route you called it in, but there is nothing stopping you from adding it to all the routes you want to allow CORS on.\n\nMake sure you also configure the OPTIONS method too.\n\n### Example usage\n\n```js\nconst { cors } =require('aera-tools')\n\nserver.get('/', (req, res) =\u003e {\n  cors(req, res)\n  return 'Hello, World!'\n})\n\nserver.options('/', cors)\n```\n\nCors also accepts options. Here is a list of them:\n\n  - `origin` defaults to *`true`*\n  - `expose` defaults to *`''`*\n  - `maxAge` defaults to *`false`*\n  - `credentials` defaults to *`false`*\n  - `methods` defaults to *`'*'`*\n  - `headers` defaults to *`*`*, falls back to Access-Control-Request-Headers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Faera-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielkov%2Faera-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Faera-tools/lists"}