{"id":22468565,"url":"https://github.com/projectweekend/node-api-utils","last_synced_at":"2025-08-23T02:17:06.915Z","repository":{"id":27018599,"uuid":"30482995","full_name":"projectweekend/Node-API-Utils","owner":"projectweekend","description":"A collection of things I like to use when building web APIs in Node.js","archived":false,"fork":false,"pushed_at":"2015-04-11T14:02:47.000Z","size":328,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-13T05:48:12.498Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/projectweekend.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-08T06:21:41.000Z","updated_at":"2015-04-11T14:02:48.000Z","dependencies_parsed_at":"2022-08-21T09:30:42.570Z","dependency_job_id":null,"html_url":"https://github.com/projectweekend/Node-API-Utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/projectweekend/Node-API-Utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/projectweekend%2FNode-API-Utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/projectweekend%2FNode-API-Utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/projectweekend%2FNode-API-Utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/projectweekend%2FNode-API-Utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/projectweekend","download_url":"https://codeload.github.com/projectweekend/Node-API-Utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/projectweekend%2FNode-API-Utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271732362,"owners_count":24811309,"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","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-06T11:17:42.200Z","updated_at":"2025-08-23T02:17:06.876Z","avatar_url":"https://github.com/projectweekend.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Install it\n\n```\nnpm install projectweekend/Node-API-Utils\n```\n\n------------\n\n\n### Authentication\n\n#### Generate JSON web token\n\n```javascript\nvar authUtils = require( \"api-utils\" ).authentication;\n\n\nvar token = authUtils.generateJWT( user, [ \"id\", \"email\", \"role\" ] );\n```\n\n**Parameters:**\n\n* A user object to serialize into a token\n* An array of user properties to include in the serialization\n\n\n#### Middleware requiring a system-wide API key\n\n```javascript\nvar authUtils = require( \"api-utils\" ).authentication;\n\n\n// Assuming an Express or Restify server is defined as 'server'\nserver.use( authUtils.systemAPIKey( [ \"/skip-this-route\" ] ) );\n```\n\n**Parameters:**\n\n* An array of routes to skip API check (optional)\n\n**Notes:**\n\n* `SYSTEM_API_KEY` environment variable must be defined\n\n------------\n\n\n### Blitz.io\n\n#### Authorization route for Blitz.io load testing\n\n```javascript\nvar blitzio = require( \"api-utils\" ).blitzio;\n\n\n// Assuming an Express or Restify server is defined as 'server'\nserver.get( blitzio.url(), blitzio.handler );\n```\n\n**Parameters: None**\n\n**Notes:**\n\n* `BLITZ_KEY` environment variable must be defined\n\n------------\n\n\n### Database\n\n#### Mongoose connection with Docker-Compose fallback\n\n```javascript\nvar databaseUtils = require( \"api-utils\" ).database;\n\n\nvar db = databaseUtils.mongooseConnection( \"DB_PORT\" );\n```\n\n**Parameters: None**\n\n**Notes:**\n\n`mongooseConnection` takes an optional parameter which is the name of the environment variable provided by a linked Docker Compose service. If this variable name is not provided then the `MONGO_URL` environment variable must be defined.\n\n------------\n\n\n### Caching\n\n#### Middleware for setting Cache-Control header\n\n```javascript\nvar cacheUtils = require( \"api-utils\" ).cache;\n\n\nvar oneMonth = 60 * 60 * 24 * 30;\n\n\n// Apply to all routes\nserver.use( cacheUtils.cacheControl( oneMonth ) );\n\n\n// Apply of a specific route only\nserver.get( \"/\", cacheUtils.cacheControl( oneMonth ), indexHandler.get );\n```\n\n**Parameters:**\n\n* An integer representing a number os seconds to keep cache alive\n\n------------\n\n\n### Responses\n\n#### Common HTTP Response Callbacks\n\n```javascript\nvar responses = require( \"api-utils\" ).responses;\n\n\nexports.create = function ( req, res, next ) {\n\n    // Send a 201 and the database result, or pass error to handler middleware\n    CreateSomethingInDatabase( thingToCreate, responses.createdResponse( res, next ) );\n\n};\n\n// Other responses:\n\n// Send a 201 and the database result, or pass error to handler middleware.\n// Behaves like a detail response and sends 404 when parent document is not found\nresponses.nestedCreateResponse\n\n// Send 200 and result, or pass error to handler\nresponses.listResponse\n\n// Send 200 and result, or pass error to handler. Sends a 404 when the result is empty.\nresponses.detailResponse\n\n// Send 204 and no body, or pass error to handler. Sends a 404 when the result is empty.\nresponses.deleteResponse\n```\n\n\n### Errors\n\n#### Common HTTP errors\n\n```javascript\nvar errors = require( \"api-utils\" ).errors;\n\n\nerrors.system( \"Something went wrong on the server\" );\n\nerrors.auth( \"Invalid credentials\" );\n\nerrors.notAuthorized( \"You can't do that!\" );\n\nerrors.resourceNotFound( \"That doesn't exist\" );\n\nerrors.conflict( \"Email is in use\" );\n```\n\n**Notes:**\n\n* Each error function takes a `message` parameter and returns a JavaScript `Error` with the `status` property set to the appropriate HTTP code, so you can pass these to `next()` and let Express middleware do the rest.\n\n------------\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprojectweekend%2Fnode-api-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprojectweekend%2Fnode-api-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprojectweekend%2Fnode-api-utils/lists"}