Skip to content

Commit

Permalink
Add json-api-client to this repo
Browse files Browse the repository at this point in the history
Move the `json-api-client` code into `lib/json-api-client` and require it from there. Remove `json-api-client` as a dependency.
  • Loading branch information
eatyourgreens committed Jan 20, 2023
1 parent f0e1459 commit 4bf4d65
Show file tree
Hide file tree
Showing 18 changed files with 1,120 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/api-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var JSONAPIClient = require('json-api-client');
var JSONAPIClient = require('./json-api-client');
var config = require('./config');

var apiClient = new JSONAPIClient(config.host + '/api', {
Expand Down
2 changes: 1 addition & 1 deletion lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var JSONAPIClient = require('json-api-client');
var JSONAPIClient = require('./json-api-client');
var Model = JSONAPIClient.Model;
var makeHTTPRequest = JSONAPIClient.makeHTTPRequest;
var makeCredentialHTTPRequest = JSONAPIClient.makeCredentialHTTPRequest;
Expand Down
2 changes: 1 addition & 1 deletion lib/csrf-token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var config = require('./config');
var apiClient = require('./api-client');
var JSONAPIClient = require('json-api-client');
var JSONAPIClient = require('./json-api-client');
var makeCredentialHTTPRequest = JSONAPIClient.makeCredentialHTTPRequest;

function getCSRFToken(host) {
Expand Down
114 changes: 114 additions & 0 deletions lib/json-api-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 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

```js
import JSONAPIClient from './json-api-client'

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

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

const client = new JSONAPIClient(PATH_TO_API_ROOT, DEFAULT_HEADERS)
```

## Working with resources

```js
# Create a resource
const 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

```js
# Retrieve a resource by ID
client.type('people').get('1').then( person => { console.log(person) })

# Retrieve several resources by ID
client.type('people').get(['1', '2', '3']).then(people => { console.log(...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 => { console.log(person) })

# Retrieve a resource by query (likewise, this is never cached)
client.type('people').get({ name: 'Brian' }).then(people => { const [brian] = people })

# Chaining promised resource methods (experimental)
client.type('people')
.get({ name: 'Brian' })
.index(0)
.update({ name: 'Brian C.' })
.save()
.get('name')
.then(briansName => { console.log(brainsName) })
```

### Watching a resource for changes

```js
function HANDLER() {
console.log('The resource changed.')
}

client.type('people').get('1').then(person => {
person.listen('change', HANDLER)
person.stopListening('change', HANDLER)
})
```

### Working with links

```js
# Get a link (from local cache if, possible)
client.type('people').get('1')
.then(person => person.get('pets'))
.then (personsPets => {
console.log(personsPets)
})

# Or (experimental)
client.type('people').get('1').get('pets').then(personsPets => { console.log(personsPets)})

# Skip the local cache (again, with query params)
person.get('pets', {}).then(personsPets => { console.log(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

```js
client.type('people').get('1').then(person => {
const meta = person.getMeta()
console.log(`Page ${meta.page} / ${meta.page_count}`)
})
```
5 changes: 5 additions & 0 deletions lib/json-api-client/default-headers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions lib/json-api-client/emitter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4bf4d65

Please sign in to comment.