{"library":"rest","title":"rest.js HTTP Client","description":"rest.js is an HTTP client library designed for both browser and Node.js environments, emphasizing extensibility through its interceptor-based architecture. It provides core functionality for making HTTP requests and normalizing request/response objects, with advanced features like MIME type conversion, error handling, and hypermedia API traversal implemented as composable interceptors. The current stable version is 2.0.0, which notably moved from a hard dependency on `when.js` to native ES6 Promises and completely dropped AMD module support. Releases historically occurred somewhat frequently for major feature additions in 1.x, with 2.0.0 being a significant breaking change. It allows developers to configure tailored HTTP clients by wrapping a basic client with only the necessary features, promoting a lightweight and modular approach to client-side HTTP interactions, differentiating it from monolithic HTTP libraries.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install rest"],"cli":null},"imports":["const rest = require('rest');","const mime = require('rest/interceptor/mime');","const errorCode = require('rest/interceptor/errorCode');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const rest = require('rest');\nconst mime = require('rest/interceptor/mime');\nconst errorCode = require('rest/interceptor/errorCode');\nconst defaultRequest = require('rest/interceptor/defaultRequest');\nconst pathPrefix = require('rest/interceptor/pathPrefix');\n\n// Assume a base URL and an API key for a hypothetical service\nconst API_BASE_URL = 'https://api.example.com';\nconst API_KEY = process.env.MY_API_KEY ?? 'your-secret-api-key'; // Use process.env for robustness\n\n// Configure a client with common interceptors\n// pathPrefix to handle base URL\n// defaultRequest to add common headers like Authorization\n// mime to automatically parse JSON responses\n// errorCode to handle HTTP error statuses gracefully\nconst client = rest\n    .wrap(pathPrefix, { prefix: API_BASE_URL })\n    .wrap(defaultRequest, {\n        headers: {\n            'Authorization': `Bearer ${API_KEY}`,\n            'Accept': 'application/json'\n        }\n    })\n    .wrap(mime, { mime: 'application/json' })\n    .wrap(errorCode, { code: 400 }); // Treat 400 and above as errors\n\n// Make a request to fetch user data\nclient({ path: '/users/123' }).then(\n    function(response) {\n        console.log('User data:', response.entity);\n        console.log('Status:', response.status.code);\n    },\n    function(error) {\n        console.error('Request failed:', error.entity || error.message);\n        console.error('Error status:', error.status && error.status.code);\n    }\n);\n\n// Example of a POST request\nclient({\n    path: '/users',\n    method: 'POST',\n    entity: { name: 'John Doe', email: 'john.doe@example.com' }\n}).then(\n    function(response) {\n        console.log('New user created:', response.entity);\n    },\n    function(error) {\n        console.error('Failed to create user:', error.entity || error.message);\n    }\n);","lang":"javascript","description":"This quickstart demonstrates how to create an advanced `rest.js` client by composing multiple interceptors, including `pathPrefix` for base URL, `defaultRequest` for adding common headers, `mime` for automatic JSON parsing, and `errorCode` for robust error handling. It shows both GET and POST requests, using `process.env` for API key management.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}