# @datx/jsonapi

> DatX mixin for JSON API support

Latest version **3.0.0** (published 2023-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @datx/jsonapi
pnpm add @datx/jsonapi
yarn add @datx/jsonapi
bun add @datx/jsonapi
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-06-29 |
| First published | 2021-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 331.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 146 |
| Author | Infinum JavaScript Team |
| Maintainers | danipavic, kristian240, infinumcom, darkokukovec, safo6m, fvoska, ivicabatinic |
| Keywords | datx, jsonapi |

## Links

- npm: https://www.npmjs.com/package/@datx/jsonapi
- Repository: https://github.com/infinum/datx
- Homepage: https://github.com/infinum/datx#readme
- Issues: https://github.com/infinum/datx/issues
- npm.io page: https://npm.io/package/@datx/jsonapi

## Dependencies (3)

- [@datx/core](https://npm.io/package/@datx/core.md) 3.0.0
- [@datx/utils](https://npm.io/package/@datx/utils.md) 3.0.0
- [@datx/network](https://npm.io/package/@datx/network.md) 3.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.0 (latest) — 2023-06-29
- 2.6.2-beta.2 (beta) — 2023-08-07
- 3.0.0-alpha.0 (alpha) — 2022-03-29
- 2.6.2-beta.1 — 2023-07-20
- 2.6.2-beta.0 — 2023-07-11
- 3.0.0-beta.1 — 2023-06-29
- 2.6.1 — 2023-06-12
- 2.5.1 — 2023-04-20
- 2.5.0 — 2023-04-20
- 2.5.0-beta.11 — 2023-03-23
- 2.4.14 — 2023-03-07
- 2.4.12 — 2023-01-25
- 2.4.11 — 2023-01-19
- 2.5.0-beta.6 — 2022-12-14
- 2.4.10 — 2022-11-28
- … 19 more at https://npm.io/package/@datx/jsonapi/versions

## README

# @datx/jsonapi

DatX is an opinionated data store. It features support for references to other models and first-class TypeScript support.

`@datx/jsonapi` is a datx mixin that adds [JSON API](https://jsonapi.org/) support.

---

## Basic usage

```typescript
import { Collection, Model, Attribute } from '@datx/core';
import { jsonapiCollection, jsonapiModel } from '@datx/jsonapi';

class Person extends jsonapiModel(Model) {
  public static type = 'person'; // Unique name of the model class

  @Attribute()
  public name: string; // A normal attribute without a default value

  @Attribute()
  public surname: string;

  @Attribute({ toOne: Person })
  public spouse?: Person; // A reference to a Person model

  public get fullName() {
    return `${this.name} ${this.surname}`;
  }
}

class AppData extends jsonapiCollection(Collection) {
  public static types = [Person]; // A list of models available in the collection
}

const store = new AppData();
const john = store.add(new Person({ name: 'John', surname: 'Smith' })); // Add a model instance to the store
const jane = store.add({ name: 'Jane', surname: 'Smith', spouse: john }, Person); // Add a model to the store

await john.save(); // POST to the server
const people = await store.fetchAll(Person); // Get all people from the server
```

## Getting started

```bash
npm install --save @datx/jsonapi
```

- [Basic configuration](https://datx.dev/docs/jsonapi/jsonapi-basic-configuration)
- [Network configuration](https://datx.dev/docs/jsonapi/jsonapi-network-configuration)
- [Network usage](https://datx.dev/docs/jsonapi/jsonapi-network-usage)
- [Spec compliance](https://datx.dev/docs/jsonapi/jsonapi-spec-compliance)

### Polyfilling

The lib makes use of the following features that are not yet available everywhere. Based on your browser support, you might want to polyfill them:

- [Symbol.for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
- [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
- [Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
- [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
- [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)

[How to add the polyfills](https://datx.dev/docs/troubleshooting/known-issues#the-library-doesnt-work-in-internet-explorer-11).
Note: Fetch API is not included in the polyfills mentioned in the Troubleshooting page. Instead, you need to add it as a separate library. If you don't have any special requirements (like server-side rendering), you can use the [window.fetch polyfill](https://github.com/github/fetch#installation).

## API reference

- [Model](https://datx.dev/docs/jsonapi/jsonapi-model)
- [Collection](https://datx.dev/docs/jsonapi/jsonapi-collection)
- [View](https://datx.dev/docs/jsonapi/jsonapi-view)
- [Response](https://datx.dev/docs/jsonapi/jsonapi-response)
- [Config](https://datx.dev/docs/jsonapi/jsonapi-config)
- [Utils](https://datx.dev/docs/jsonapi/jsonapi-utils)
- [TypeScript Interfaces](https://datx.dev/docs/jsonapi/jsonapi-typescript-interfaces)

## Troubleshooting

Having issues with the library? Check out the [troubleshooting](https://datx.dev/docs/troubleshooting/known-issues) page or [open](https://github.com/infinum/datx/issues/new/choose) an issue.

---

[![Build Status](https://travis-ci.org/infinum/datx.svg?branch=master)](https://travis-ci.org/infinum/datx)
[![npm version](https://badge.fury.io/js/@datx/jsonapi.svg)](https://badge.fury.io/js/@datx/jsonapi)
[![Dependency Status](https://david-dm.org/infinum/datx.svg?path=packages/@datx/jsonapi)](https://david-dm.org/infinum/datx?path=packages/@datx/jsonapi)
[![devDependency Status](https://david-dm.org/infinum/datx/dev-status.svg?path=packages/@datx/jsonapi)](https://david-dm.org/infinum/datx?path=packages/@datx/jsonapi#info=devDependencies)

## License

The [MIT License](LICENSE)

## Credits

datx-jsonapi is maintained and sponsored by
[Infinum](https://www.infinum.com).

<p align="center">
  <a href='https://infinum.com'>
    <picture>
        <source srcset="https://assets.infinum.com/brand/logo/static/white.svg" media="(prefers-color-scheme: dark)">
        <img src="https://assets.infinum.com/brand/logo/static/default.svg">
    </picture>
  </a>
</p>

---
_Source: https://npm.io/package/@datx/jsonapi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
