{"id":20993618,"url":"https://github.com/jblaak/fitted","last_synced_at":"2026-03-09T13:01:55.971Z","repository":{"id":76232516,"uuid":"59911358","full_name":"JBlaak/Fitted","owner":"JBlaak","description":"Simplifying http requests using ES decorators","archived":false,"fork":false,"pushed_at":"2016-12-23T12:58:15.000Z","size":107,"stargazers_count":135,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-07T10:46:01.123Z","etag":null,"topics":["ajax","decorators","http-requests"],"latest_commit_sha":null,"homepage":"","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/JBlaak.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-28T19:35:11.000Z","updated_at":"2024-01-04T16:05:05.000Z","dependencies_parsed_at":"2023-07-02T08:41:02.541Z","dependency_job_id":null,"html_url":"https://github.com/JBlaak/Fitted","commit_stats":{"total_commits":41,"total_committers":2,"mean_commits":20.5,"dds":"0.024390243902439046","last_synced_commit":"444861ff16ff4e46b22b84175274350bfc308e7d"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FFitted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FFitted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FFitted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlaak%2FFitted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JBlaak","download_url":"https://codeload.github.com/JBlaak/Fitted/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225309918,"owners_count":17454089,"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":["ajax","decorators","http-requests"],"created_at":"2024-11-19T07:15:33.111Z","updated_at":"2026-03-09T13:01:50.938Z","avatar_url":"https://github.com/JBlaak.png","language":"JavaScript","readme":"Fitted\n====\n\n[![Build Status](https://travis-ci.org/JBlaak/Fitted.svg?branch=master)](https://travis-ci.org/JBlaak/Fitted)\n\nUse ECMAScript decorators ([currently a Stage 2 proposal](https://github.com/tc39/proposals)) to\nexecute HTTP requests and manage processing of responses, an easy and readable way of managing\nhow data flows through the networking layer of your application.\n\nExample\n----\n\nTwo main parts, the method decorators which will actually do the request, and the class decorators\nwhich will allow you to handle the way responses from the server are transformed and handled.\n\nThe simplest example is just a fetch of data from a JSON endpoint:\n\n```javascript\nimport {get} from 'fitted';\n\nclass HackerNews {\n    @get('https://hacker-news.firebaseio.com/v0/topstories.json')\n    topstories (request, response) {\n      return request({}, response);\n    }\n}\n```\n\nAnd fetch:\n\n```javascript\nconst hackerNews = new HackerNews();\nconst topstories = await hackerNews.topstories();\nconsole.log(topstories);\n```\n\nUsage\n-----\n\n__Basic request__\n\nUsing the `get` decorator you can trigger a `GET` request:\n\n```javascript\nimport {get} from 'fitted';\n\nclass HackerNews {\n    @get('https://hacker-news.firebaseio.com/v0/topstories.json')\n    topstories (request, response) {\n      return request({}, response);\n    }\n}\n```\n\n**Merging params**\n\nTo merge params with the url we use [url-template](https://github.com/bramstein/url-template),\nwhich uses curly brackets to encapsulate a to be merged variable.\n\n```javascript\nimport {get} from 'fitted';\n\nclass HackerNews {\n      @get('https://hacker-news.firebaseio.com/v0/item/{id}.json')\n      item (id, request, response) {\n          return request({\n                template: {\n                    id: 123\n                 }\n          }, response);\n      }\n}\n```\n\n**Base url**\n\nMost of the time your endpoints will share the same base url, so Fitted\nallows you to set a base url which will be prefixed to all paths \nset in your method decorators.\n\n```javascript\nimport {base, get} from 'fitted';\n\n@base('https://hacker-news.firebaseio.com/v0/')\nclass HackerNews {\n      @get('item/{id}.json')\n      item (id, request, response) {\n          return request({\n                template: {\n                    id: 123\n                 }\n          }, response);\n      }\n}\n```\n\n__Sending data__\n\nTo add data to your request for `post`, `put` and `destroy` requests and\nspecifying a query string for your `get` request you add a `data` object\nto your request definition.\n\n```javascript\nimport {put} from 'fitted';\n\nclass HackerNews {\n      @put('https://hacker-news.firebaseio.com/v0/item/{id}.json')\n      item (id, name, request, response) {\n          return request({\n                template: {\n                    id: 123\n                },\n                data: {\n                   name: name\n                }\n          }, response);\n      }\n}\n```\n\n__Request decorating__\n\nOften all endpoints will have the same request requirements, requiring,\nfor example, all to have some header set. For this the `@request` \ndecorator can be used. It will get the config of each request\npassed before handing it over to the driver.\n\n```javascript\nimport {get, request} from 'fitted';\n\nconst myRequestHandler = config =\u003e {\n    config.headers = {\n        'accept': 'application/json' \n    }\n    \n    return config;\n}\n\n@request(myRequestHandler)\nclass HackerNews {\n    @get('item/{id}.json')\n    item (id, request, response) {\n      return request({\n            template: {\n                id: id\n            }\n        }, response);\n    }\n}\n```\n\n__Response handling__\n\nWhen the server responds with a `Content-Type` header containing `application/json` \nFitted will automatically feed it to the `JSON.parse` function so that\nthe resulting Promise will output the corresponding object.\n\nAny other `Content-Type` will result in an Error being thrown and require\nyou to implement your own handler.\n\n**Custom response processor**\n\nWhen your endpoint returns something that requires some pre-processing you\ncan define a processor for all endpoints in your api definition. This\nconsists of a function that receives the response from the server and\npasses the parsed data to the response object.\n\n```javascript\nimport {get, processor} from 'fitted';\n\nconst myProcessor = response =\u003e {\n    const data = JSON.parse(response.getBody());\n    response.setBody(data);\n    \n    return data;\n}\n\n@processor(myProcessor)\nclass HackerNews {\n    @get('item/{id}.json')\n    item (id, request, response) {\n      return request({\n            template: {\n                id: id\n            }\n        }, response);\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblaak%2Ffitted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjblaak%2Ffitted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblaak%2Ffitted/lists"}