{"id":19525805,"url":"https://github.com/maykinmedia/consumerjs","last_synced_at":"2025-08-04T09:32:58.191Z","repository":{"id":8337409,"uuid":"57900387","full_name":"maykinmedia/consumerjs","owner":"maykinmedia","description":"Consumerjs simplifies REST with an ORM like approach. Built on top of Axios.","archived":false,"fork":false,"pushed_at":"2023-01-06T01:39:57.000Z","size":2001,"stargazers_count":7,"open_issues_count":15,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-22T03:10:21.898Z","etag":null,"topics":["api","axios","consumer","datastore","javascript","rest"],"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/maykinmedia.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":"2016-05-02T15:34:42.000Z","updated_at":"2022-08-03T10:31:06.000Z","dependencies_parsed_at":"2023-01-13T14:45:13.058Z","dependency_job_id":null,"html_url":"https://github.com/maykinmedia/consumerjs","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/maykinmedia/consumerjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maykinmedia%2Fconsumerjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maykinmedia%2Fconsumerjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maykinmedia%2Fconsumerjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maykinmedia%2Fconsumerjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maykinmedia","download_url":"https://codeload.github.com/maykinmedia/consumerjs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maykinmedia%2Fconsumerjs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268675515,"owners_count":24288285,"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-04T02:00:09.867Z","response_time":79,"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":["api","axios","consumer","datastore","javascript","rest"],"created_at":"2024-11-11T01:07:07.780Z","updated_at":"2025-08-04T09:32:58.137Z","avatar_url":"https://github.com/maykinmedia.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/maykinmedia/consumerjs.svg?branch=master)](https://travis-ci.org/maykinmedia/consumerjs)\n[![Coverage Status](https://coveralls.io/repos/github/maykinmedia/consumerjs/badge.svg?branch=master)](https://coveralls.io/github/maykinmedia/consumerjs?branch=master)\n[![Code Climate](https://codeclimate.com/github/maykinmedia/consumerjs/badges/gpa.svg)](https://codeclimate.com/github/maykinmedia/consumerjs)\n\n[![NPM](https://nodei.co/npm/consumerjs.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/consumerjs/)\n[![Sauce Test Status](https://saucelabs.com/browser-matrix/consumerjs.svg)](https://saucelabs.com/u/consumerjs)\n\n# ConsumerJS\nConsumerjs simplifies [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) with an ORM like approach.\nBuilt on top of Axios.\n\nConsumerjs is an ORM-like repository/entity mapper that aims to make using RESTful JSON API's simple and \n[DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). It supports basic\n[CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations on remote resources and can easiy be\nextended with custom behaviour. \n\nConsumerJS out of the box supports ([Django](https://www.djangoproject.com))\n[CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection.\n\n## Installation\n\nInstall with [npm](https://www.npmjs.com/).\n\n```sh\n$ npm i consumerjs --save\n```\n\n*As of 2.0.0: [@babel/polyfill](https://babeljs.io/docs/en/babel-polyfill) needs to be installed in your project  in\norder to support older browsers like Internet Explorer 11.*\n\n## Usage\n\n*See [doc](doc/) for full API documentation.*\n\n**Example:**\n\n*data/post.js*\n```js\nimport { CrudConsumer, CrudConsumerObject } from 'consumerjs';\n\n\nclass Post extends CrudConsumerObject {}\n\n\nclass PostConsumer extends CrudConsumer {\n    constructor(endpoint='http://example.com/api/v1/posts/', objectClass=Post) {\n        super(endpoint, objectClass);\n    }\n;}\n\n\nexport default PostConsumer;\n```\n\n\n**examples/my-app.js**\n```js\nimport PostConsumer from '../data/post.js';\n\n\nlet postConsumer = new PostConsumer();\npostConsumer.create()\n    .then(someFunction)  // When promise resolves, call someFunction with new Post object\n    .catch(errorFunction);  // When promise rejects, call errorFunction\n\n    \n//\n// When a consumer receives a JSON array as result, an array of consumer objects is returned.\n//\n\n    \npostConsumer.read()\n    .then(someFunction)  // When promise resolves, call someFunction with all resolved Post objects\n    .catch(errorFunction);  // When promise rejects, call errorFunction\n    \n    \nlet id = 1;\npostConsumer.read(id)\n    .then(someFunction)  // When promise resolves, call someFunction with resolved Post (with id 1)\n    .catch(errorFunction);  // When promise rejects, call errorFunction\n```\n\n\n**examples/my-app2.js**\n```js\n// Internally, id is resolved using either \"pk\" or \"id\" field\n\npost.title = 'some new title';\npost.update()  // Saves changed fields to API using partial PATCH request\npost.save()  // Save all fields to API using full PUT request\npost.delete()  // Deletes this post using DELETE request\n```\n\n## Pagination\n\n \u003e TODO: Document the usage of various `List` types and pagination. For now please see the [tests](test/) for examples.\n\n\n## Concepts\n\n\nConsumerJS defines a few built-in classes, all of those should preferably be extended by a custom class implementing \ndomain specific behaviour (if any):\n\n- [Consumer](doc/consumer.md)\n- [CrudConsumer](doc/crud-consumer.md)\n- [ConsumerObject](doc/consumer-object.md)\n- [CrudConsumerObject](doc/crud-consumer-object.md)\n\n**Consumers (Consumer, CrudConsumer):**\n\n*\"Consumers\" are classes that define how to perform operations on the remote API. It converts the results to* *\"Consumer object\" which contains a statefull representation of the API result.*\n\nA consumer:\n- Acts a data store fore fetching remote data.\n- Can be used to convert human readable methods into DELETE, GET, PATCH POST and PUT requests.\n- All requests return promises.\n- Successfull API requests return promises for either an array (list) or a single consumer object (scalar).\n- Failed API requests cause the promise to reject.\n- Objects are cast to instances of a configurable consumer object class referenced by the consumers \"objectClass\" key.\n\n*Consumers should be preferably be extended, configured and optionally, methods can be overwritten to change default\nbehaviour. Configuration should preferably be done in de constructor method:*\n\n\n```js\n/**\n * Configures Consumer instance\n * @param {string} endpoint Base endpoint for this API\n * @param {AbstractConsumerObject} objectClass Class to cast results to\n * @param {Object} [options] Additional configuration\n */\nconstructor(endpoint='http://example.com/api/v1/posts/', objectClass=Post, options=null) {\n    super(endpoint, objectClass);\n}\n```\n\n- Consumer: Simple \"bare\" consumer intended for use with custom methods.\n- CrudConsumer: \"Consumer with base methods for common CRUD operations.\n    - `create([object])`, creates objecs.\n    - `read([id])`, fetches all objects or a single object by id.\n\n**Consumer objects (ConsumerObject, CrudConsumerObject):**\n\n*\"Consumer objects\" are classes that define how to perform object specific operations on the remote API.*\n*Consumer objects should be extended, configured and optionally methods can be overwritten to change default behaviour.*\n\nA consumer object:\n- Is the result of a resolved promise, gets passed to the promise's then() method.\n- If the API returns an array (list), an array of object classes is returned.\n- If the API returns a single object (scalar), a single object is returned.\n- The consumer object class can have methods.\n- The consumer object class keeps a reference to it's consumer using the \"\\__consumer__\" key, this allows methods to talk back to the API.\n\n\n*A reference to the consumer is kept using the \\_\\_consumer\\_\\_ property, (custom) methods can use this to communicate with the API.*\n\n```js\ncustomMethod(data) {\n    return this.__consumer.__.post('/path/', data);  // CrudConsumerObject instances can use this.getPath() as path.\n}\n```\n\n- ConsumerObject: Simple \"bare\" consumer object intended for use with custom methods.\n- CrudConsumerObject: \"Consumer object with base methods for common CRUD operations.\n    - `update()`, persists changes made to object.\n    - `save()`, fully saves object.\n    - `delete()`, deletes this object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaykinmedia%2Fconsumerjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaykinmedia%2Fconsumerjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaykinmedia%2Fconsumerjs/lists"}