2.15.0 • Published 6 years ago

directus-sdk-javascript v2.15.0

Weekly downloads
507
License
GPL-3.0
Repository
github
Last release
6 years ago

directus-sdk-javascript

Directus SDK for JavaScript (Node and Browser)

Installation

Install the package using npm or Yarn: npm install directus-sdk-javascript --production or yarn add directus-sdk-javascript

Or download the repo and include /dist/remote.js into your document: <script src="/dist/remote.js"></script>

Or use a service that distributes npm packages like unpkg: <script src="https://unpkg.com/directus-sdk-javascript/dist/remote.js"></script>

Usage

Create a new client passing it the options needed to create a connection:

// Only in Node / non-bundled version:
const RemoteInstance = require('directus-sdk-javascript/remote');

// Or (es6+):
import { RemoteInstance } from 'directus-sdk-javascript';

const client = new RemoteInstance({
  url: 'http://instance.directus.io/',
  version: '1.1', // optional, only need to update if different from default
  accessToken: [user-token] // optional, can be used without on public routes
});

The client provides methods for each API endpoint. Every endpoint returns a promise which resolves the APIs JSON on success and rejects on an error:

client.getItems('projects')
  .then(res => console.log(res))
  .catch(err => console.log(err));

Get and update the current logged in user:

client.getMe()
  .then(res => console.log(res))
  .catch(err => console.log(err));

client.updateMe({first_name: 'John', last_name: 'Doe'})
  .then(res => console.log(res))
  .catch(err => console.log(err));

Custom api endpoints, implemented on the server under customs/endpoints, are also available. Here is an example for calling the http://instance.directus.io/api/example endpoint:

client.getApi('example')
  .then(res => console.log(res))
  .catch(err => console.log(err));

client.postApi('example', {custom_var: 'value'})
  .then(res => console.log(res))
  .catch(err => console.log(err));

Since the SDK uses promises, you can also use it with async/await:

const projects = await client.getItems('projects');

Check the official API docs for a complete overview of all endpoints and available methods

2.15.0

6 years ago

2.14.0

6 years ago

2.13.3

6 years ago

2.12.3

6 years ago

2.12.2

6 years ago

2.12.1

6 years ago

2.12.0

6 years ago

2.11.1

6 years ago

2.11.0

6 years ago

2.10.0

6 years ago

3.0.0-19

6 years ago

3.0.0-18

6 years ago

3.0.0-17

6 years ago

3.0.0-16

6 years ago

3.0.0-15

6 years ago

3.0.0-14

6 years ago

3.0.0-13

6 years ago

2.9.0

6 years ago

2.8.0

6 years ago

2.7.0

6 years ago

2.6.1

6 years ago

3.0.0-12

6 years ago

3.0.0-11

6 years ago

3.0.0-10

6 years ago

3.0.0-9

6 years ago

3.0.0-8

6 years ago

3.0.0-7

6 years ago

3.0.0-6

6 years ago

2.6.0

6 years ago

2.5.1

6 years ago

2.4.0

6 years ago

2.3.2

6 years ago

3.0.0-5

6 years ago

3.0.0-4

6 years ago

3.0.0-3

6 years ago

3.0.0-2

6 years ago

3.0.0-1

6 years ago

3.0.0-0

6 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.2.0-1

7 years ago

2.2.0-0

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

2.0.0-0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago