{"_id":"@apicase/fluent","_rev":"2-98190441fcf4b5434c6bd1fa2eb607bf","name":"@apicase/fluent","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@apicase/fluent","version":"0.1.0","description":"Fluent interface for your Apicase services","main":"index.js","repository":{"type":"git","url":"git@github.com:apicase/fluent.git"},"author":{"name":"Anton Kosykh","email":"kelin2025@yandex.ru"},"license":"MIT","dependencies":{"schematic-fluent":"^0.5.0"},"scripts":{"build":"babel index.js --out-file=index.dist.js"},"peerDependencies":{"@apicase/adapter-fetch":"^0.14.3"},"_id":"@apicase/fluent@0.1.0","dist":{"shasum":"a52b0eb0071effcac408c294a924a506051b219a","tarball":"https://registry.npmjs.org/@apicase/fluent/-/fluent-0.1.0.tgz","fileCount":5,"unpackedSize":6820,"integrity":"sha512-NvlEN6+Ki72zhUz4NXhxz+eDppxqKWJkZoEg7YDgx2MlIv7w8MtLMre/X14KpU+wpcJeyY4Qu1HshDSvOhpR4w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDDChzzfjZY4kcYkYxLAMAsunhGRVKXvcZ9jx/UBABdlQIgOX74LRHXqWaHRJfm+VFvToDSscF3TJL/Q7w/dnBnz14="}]},"maintainers":[{"name":"kelin2025","email":"vasya-petruha@yandex.ru"}],"_npmUser":{"name":"kelin2025","email":"vasya-petruha@yandex.ru"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fluent_0.1.0_1530554421168_0.7880564990892125"},"_hasShrinkwrap":false}},"time":{"created":"2018-07-02T18:00:20.900Z","0.1.0":"2018-07-02T18:00:21.235Z","modified":"2022-06-12T14:43:39.699Z"},"maintainers":[{"name":"kelin2025","email":"vasya-petruha@yandex.ru"}],"description":"Fluent interface for your Apicase services","repository":{"type":"git","url":"git@github.com:apicase/fluent.git"},"author":{"name":"Anton Kosykh","email":"kelin2025@yandex.ru"},"license":"MIT","readme":"# @apicase/fluent\n\nFluent interface for Apicase services\n\n## Benefits\n\n- Like Apicase and unlike another solutions, it's not API-specific\n- Highly customizable. You can easily add your own methods and even whole schemas (based on [schematic-fluent](https://github.com/Kelin2025/schematic-fluent))\n- Fluent instance is **immutable** and don't modify current instance but returns the new one\n\n## Installation\n\n```bash\nnpm i @apicase/fluent  # or yarn add @apicase/fluent\n# If you're using fetch adapter\nnpm i @apicase/adapter-fetch\n```\n\n## Usage guide\n\n### Create fluent Apicase service\n\n```js\nimport fetchSchema from \"@apicase/fluent/fetch\"\nimport fluentApicase from \"@apicase/fluent\"\nimport { ApiService } from \"@apicase/services\"\n\n// Create fluent instance with fetchSchema\n// You can use any service as \"base\" for requests\nconst FluentFetch = fluentApicase(fetchSchema).service(new ApiService())\n\n// GET /photos?foo=bar\nFluentFetch.get(\"/photos\")\n  .query(\"foo\", \"bar\")\n  .doRequest()\n  .then(console.log)\n```\n\n### Service methods\n\nAll `ApiService` methods are available in fluent interface:\n\n```js\nFluentFetch.doRequest()\nFluentFetch.pushRequest()\nFluentFetch.doSingleRequest()\nFluentFetch.doUniqueRequest()\n```\n\nAlso, there are additional methods\n\n```js\nFluentFetch.getPayload() // Returns payload\nFluentFetch.getService() // Returns service.extend(payload)\n```\n\n### Add hooks or events\n\n```js\nconst addToken = ({ payload, next }) => {\n  payload.headers = payload.headers || {}\n  payload.headers.token = localStorage.token\n  next(payload)\n}\n\nconst WithAuth = FluentFetch.on(\"done\", console.log).hook(\"before\", addToken)\n```\n\n## Available methods\n\n### `apicaseFluent()` methods\n\n`apicaseFluent` function just extends schema with the following methods:\n```js\nRoot\n  // Adapter and service\n  .adapter(SomeAdapter)\n  .service(SomeService)\n\n  // Meta info\n  .meta('foo', 'bar')\n  .meta({ foo: 'bar' })\n\n  // Listen to events (requires service)\n  .on('evtName', cb)\n\n  // Hook or a lot of hooks\n  .hook('hookType', cb)\n  .hooks({\n    done: cb,\n    fail: cb\n  })\n```\n\n### `fetchSchema` methods\n\nTo reduce boilerplate code, we provide ready-to-use schema for fetch adapter.  \nFetch schema has the following methods:\n\n```js\nRoot\n  // Request methods + url\n  .get(\"/photos/:id\")\n  .post(\"/photos\")\n  .put(\"/photos/:id\")\n  .patch(\"/photos/:id\")\n  .delete(\"/photos/:id\")\n\n  // Set URL and method separately\n  .url(\"/foo/bar\")\n  .method(\"GET\")\n\n  // Query string ?foo=bar\n  .query(\"foo\", \"bar\")\n  .query({ foo: \"bar\" })\n\n  // URL params (e.g. /photos/:id -> /photos/1)\n  .param(\"id\", 1)\n  .param({ id: 1 })\n\n  // Request headers\n  .header(\"token\", \"12345\")\n  .header({ token: \"12345\" })\n\n  // Body\n  .body(\"something\")\n  .body(new FormData())\n  .body({ foo: \"bar\" })\n```\n\n## Customization\n\nWe use [schematic-fluent](https://github.com/Kelin2025/schematic-fluent) for schemas.  \nYou can check out its README for more info\n\n### Extend schema or instance\n\n```js\nimport fetchSchema from \"@apicase/fluent/fetch\"\nimport apicaseFluent from \"@apicase/fluent\"\n\n// Extend schema\nconst jsonApiSchema = fetchSchema.extend({\n  methods: {\n    include: (ctx, { method }) => include => \n      method(\"query\", { include }),\n\n    page: (ctx, { method }) => (number, limit) =>\n      method(\"query\", { page: { number, limit } })\n  }\n})\n\nconst Root = apicaseFluent(jsonApiSchema)\n\n// Extend wrapped service\nconst GetPost = Root\n  .get('/post')\n  .extend({\n    method: {\n      id: (ctx, { method }) => id => method('param', id)\n    }\n  })\n\nGetPost\n  .include(['author'])\n  .id(1)\n  .doRequest()\n  .then(console.log)\n```\n\n### Use your own schema\n\nDocs coming soon...\n","readmeFilename":"README.md"}