Devour Client is a lightweight, framework-agnostic JavaScript client for consuming JSON:API compliant APIs. It simplifies the often painful process of manually serializing and deserializing JSON:API resources, providing clear conventions for pagination, filtering, sparse fields, and relationships. Currently stable at version 3.2.0, it focuses on offering a simple yet comprehensive feature set that differentiates it from other JavaScript JSON:API client implementations by abstracting away the complexities of the specification. While a specific release cadence isn't defined, the project sees regular maintenance and dependency updates. It provides a flexible middleware stack and configurable options for common API patterns like pluralization, trailing slashes, and authentication.
npm install devour-clientVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes Devour Client, defines two JSON:API models ('post' and 'comment'), and demonstrates common CRUD operations (findAll, find, create, update, destroy) and arbitrary requests against a hypothetical API endpoint.
Only use `disableErrorsForMissingResourceDefinitions: true` if you have a robust strategy for handling undefined resources. Otherwise, ensure all expected JSON:API resource types are defined using `jsonApi.define()`.
Pass `pluralize: false` to the `JsonApi` constructor to disable, or provide a custom pluralization function: `new JsonApi({apiUrl: '...', pluralize: myCustomPluralizeFunc})`.If you need to maintain builder state across calls, set `resetBuilderOnCall: false` in the `JsonApi` constructor options. Be mindful of managing the builder state manually if you disable this.
Regularly check the Snyk badge or run `npm audit` and `snyk test` on your project dependencies. Update `devour-client` to the latest version to ensure you have security patches for any known vulnerabilities.
Define the missing resource model: `jsonApi.define('comments', { comment: '', post: { jsonApi: 'belongsTo', type: 'posts' } })` or ensure `disableErrorsForMissingResourceDefinitions` is set to `true` if you wish to suppress these errors.Verify the `apiUrl` is correct and accessible. Check your browser's console for CORS errors. Ensure your API server is running and reachable from the client's environment.
Pass the necessary authentication details to the `JsonApi` constructor: `new JsonApi({ apiUrl: '...', bearer: 'YOUR_TOKEN' })` or `new JsonApi({ apiUrl: '...', auth: { username: 'user', password: 'pass' } })`.