7.0.2 • Published 1 year ago

json-api-client v7.0.2

Weekly downloads
468
License
Apache-2.0
Repository
github
Last release
1 year ago

JSON-API Client

A client for the pre-release version of the JSON API spec http://jsonapi.org/ used by Zooniverse Panoptes.

Requires a native or polyfilled Promise class.

Setting up a client

JSONAPIClient = require 'json-api-client'

PATH_TO_API_ROOT = 'https://example.com/api'

DEFAULT_HEADERS =
  'Content-Type': 'application/json'
  'Accept': 'application/vnd.api+json; version=1'

client = new JSONAPIClient PATH_TO_API_ROOT, DEFAULT_HEADERS

Working with resources

# Create a resource
brian = client.type('people').create name: 'Brian'

# Change a resource (locally)
brian.update name: 'Brian C.'

# Save a resource to the server
brian.save()

# Delete a resource from the server
brian.delete()

Retrieving existing resources

# Retrieve a resource by ID
client.type('people').get('1').then (person) ->

# Retrieve several resources by ID
client.type('people').get(['1', '2', '3']).then (people) ->

# Retrieve a resource by ID, skipping local cache
# (Any request with query params is passed to the server.)
client.type('people').get('1', {})).then (person) ->

# Retrieve a resource by query (likewise, this is never cached)
client.type('people').get(name: 'Brian').then (people) ->

# Chaining promised resource methods (experimental)
client.type('people').get(name: 'Brian').index(0).update(name: 'Brian C.').save().get('name').then (briansName) ->

Watching a resource for changes

HANDLER = ->
  console.log 'The resource changed.'

client.type('people').get('1').then (person) ->
  person.listen HANDLER
  person.stopListening HANDLER

Working with links

# Get a link (from local cache if, possible)
client.type('people').get('1').then (person) ->
  person.get('pets').then (personsPets) ->

# Or (experimental)
client.type('people').get('1').get('pets').then (personsPets) ->

# Skip the local cache (again, with query params)
person.get('pets', {}).then (personsPets) ->

# Set a link manually
client.type('animals').create(name: 'Spot').save().then (spot) ->
  client.type('people').get('1').then (person) ->
    person.update('links.pets': [spot.id]).save()

# Add an item to a link (instead of replacing the whole thing)
client.type('people').get('1').addLink 'pets', [rex.id, rover.id]

# Remove an item from a link
client.type('people').get('1').removeLink 'pets', spot.id

Getting response metadata

client.type('people').get('1').then (person) ->
  meta = person.getMeta()
  "Page #{meta.page} / #{meta.page_count}"
6.0.7

1 year ago

7.0.0

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

6.0.6

1 year ago

6.0.3

2 years ago

6.0.5

1 year ago

6.0.4

1 year ago

6.0.1

2 years ago

6.0.2

2 years ago

5.1.2

2 years ago

6.0.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

5.0.0-rc.0

4 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

6 years ago

4.0.0-0

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.2

7 years ago

3.2.1

8 years ago

3.2.0

8 years ago

3.1.1

8 years ago

3.1.0

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.1.0

8 years ago

2.0.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.14

9 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago