Giter VIP home page Giter VIP logo

directus-sdk-javascript's Introduction

directus-sdk-javascript

Directus SDK for JavaScript (Node and Browser)

Directus Logo

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

directus-sdk-javascript's People

Contributors

rijkvanzanten avatar u12206050 avatar arboehme avatar csprance avatar recumbentbirder avatar flo-f avatar honzabilek4 avatar lucaperret avatar padle avatar milasevicius avatar simonasdev avatar wellingguzman avatar arblickspule avatar nstcactus avatar thebliz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.