{"id":21065088,"url":"https://github.com/prolificinteractive/glenlivet-request","last_synced_at":"2025-03-14T01:43:10.861Z","repository":{"id":24648016,"uuid":"28057857","full_name":"prolificinteractive/glenlivet-request","owner":"prolificinteractive","description":"Adds the results of an HTTP request to a glenlivet processing pipeline.","archived":false,"fork":false,"pushed_at":"2014-12-15T22:08:10.000Z","size":89,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-14T15:22:15.883Z","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/prolificinteractive.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":"2014-12-15T21:48:51.000Z","updated_at":"2014-12-15T22:08:10.000Z","dependencies_parsed_at":"2022-08-23T05:01:13.424Z","dependency_job_id":null,"html_url":"https://github.com/prolificinteractive/glenlivet-request","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/prolificinteractive%2Fglenlivet-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prolificinteractive%2Fglenlivet-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prolificinteractive%2Fglenlivet-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prolificinteractive%2Fglenlivet-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prolificinteractive","download_url":"https://codeload.github.com/prolificinteractive/glenlivet-request/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243508986,"owners_count":20302106,"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-11-19T17:53:32.178Z","updated_at":"2025-03-14T01:43:10.830Z","avatar_url":"https://github.com/prolificinteractive.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# glenlivet-request\n\nAdds the results of an HTTP request to a glenlivet processing pipeline.\n\nCombines especially well with glenlivet plugins that filter HTML data, such as glenlivet-html-to-json.\n\n## Installation\n\n`npm install glenlivet-request`\n\n## Usage\n\nThe plugin's main job is to resolve a `request` options object, then add the resulting response to the glenlivet data object. `pluginDefaults` on barrel configuration allows for a convenient way to setup default options.\n\nExample:\n\n```javascript\nvar fakeStore = glenlivet.createBarrel({\n  plugins: [\n    require('glenlivet-request'),\n    require('glenlivet-html-to-json')\n  ],\n  pluginDefaults: {\n    request: {\n      protocol: 'https:',\n      host: 'store.prolificinteractive.com',\n      jar: function (data) {\n        if (data.session) {\n          return data.session.jar;\n        }\n      }\n    }\n  }\n});\n\nfakeStore.bottle('getProduct', {\n  request: {\n    method: 'GET',\n    pathname: function (data) {\n      return '/products/' + data.id;\n    }\n  },\n  htmlToJson: {\n    'id': function () {\n      return this.data.id;\n    },\n    'name': function ($doc) {\n      return $doc.find('#name').text().trim();\n    },\n    'price': function ($doc) {\n      return $doc.find('#price').text().trim().replace('$', '');\n    }\n  }\n});\n\napp.get('/products/:id', function (req, resp) {\n  fakeStore.getProduct({\n    id: req.param('id')\n  }).done(function (data) {\n    resp.json(data.json);\n  }, handleError);\n});\n```\n\n### Dynamic Options\n\nThe plugin resolves request options recursively, resolving functions into values at any level in the options object. For example:\n\n```javascript\nfakeSite.bottle('checkout', {\n  request: {\n    method: 'POST',\n    pathname: function (data) {\n      return '/orders/' + data.orderId + '/checkout';\n    },\n    headers: {\n      'X-Checkout-Token': function (data) {\n        return data.checkoutToken;\n      }\n    }\n  },\n  htmlToJson: {\n    'success': function ($doc) {\n      return $doc.find('#error-msg').length === 0;\n    }\n  }\n});\n```\n\n### Asynchronous Options\n\nThis plugin also supports option values that resolve asynchronously. For example, in scraped APIs you might need to grab the CSRF token from a page before submitting a form:\n\n```javascript\nvar getCsrfToken = fakeStore.bottle({\n  request: {\n    method: 'GET',\n    pathname: function (data) {\n      return data.page || '/';\n    }\n  },\n  htmlToJson: {\n    'csrf': function ($doc) {\n      return $doc.find('input#csrf').eq(0).attr('value');\n    }\n  }\n}).method('json.csrf');\n\nfakeStore.bottle('addToCart', {\n  session: {\n    required: true\n  },\n  request: {\n    method: 'POST',\n    pathname: '/cart',\n    jar: function (data) {\n      return data.session.jar;\n    },\n    headers: {\n      'csrf': function () {\n        return getCsrfToken({\n          page: '/cart'\n        });\n      }\n    }\n  }\n});\n\nfakeStore.addToCart({\n  productId: '109883'\n});\n```\n\n### Response Content Types\n\nIn addition to putting the response object at `data.request.response`, it will detect the content type from the response, filter the body, and place it at its corresponding key on the data object. For example, a JSON body will be assigned to `data.json`, an HTML body to `data.html`, etc.\n\n```javascript\nfakeSite.getHomepage(function (err, data) {\n  doSomethingWithHtml(data.html);\n});\n\nfakeSite.makeCallToSomeAjaxEndpoint(function (err, data) {\n  doSomethingWithJson(data.json);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprolificinteractive%2Fglenlivet-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprolificinteractive%2Fglenlivet-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprolificinteractive%2Fglenlivet-request/lists"}