Giter VIP home page Giter VIP logo

drupal-jsonapi-client's Introduction

npm downloads gzip size version

Drupal JSON:API Client

This package makes manipulating Drupal entities and resources easier via the JSON:API module which is now in Drupal core (8.7.x).

Installation

npm i --save drupal-jsonapi-client
yarn add drupal-jsonapi-client

Usage

ES6 module

import { GlobalClient, Entity ... } from 'drupal-jsonapi-client'

UMD

<script src="https://unpkg.com/[email protected]/lib/Browser.min.js"></script>
<script>
  const Entity = window.DrupalJsonApi.Entity
  
  Entity
  .Load('node', 'article', 'uuid')
  .then(entity => {
    console.log(entity)
  })
</script>

Key features

  • Lightweight - HTTP library agnostic, defaults to axios (3kb)
  • Cross platform - works in node.js and the browser
  • Drupal specific - abstracts away the nuances of working with Drupal's JSON:API implementation
  • Object oriented - leverages ES6 classes to neatly package JSON:API objects
  • Familiar - takes inspiration from Drupal's Entity API syntax

It's still in an early stage and contributions are welcome. The general idea is to maintain a base Entity class which can be extended to provide more context specific uses ie. Article extends Entity.

Here's some syntax sugar to sink your teeth into that illustrates the vision:

import { GlobalClient, Entity } from 'drupal-jsonapi-client'

GlobalClient.baseUrl = 'https://www.example.com'
GlobalClient.sendCookies = true // use this when running code on the same origin as Drupal

const doRequest = async () => {
  // Update an existing entity
  const entity = await Entity.Load('node', 'article', 'uuid')
  entity.title = 'Drupal JSON:API rocks!'
  // PATCH the existing entity
  await entity.save()

  // Create a new entity
  const newEntity = new Entity('node', 'article')
  // .setAttribute must be used here as Entity
  // doesn't know whether title should be
  // an attribute or a relationship
  newEntity.setAttribute('title', 'A brand new article!')
  // Future references to "title" can use .title
  newEntity.title = 'We changed the title'
  // POST the new entity
  await newEntity.save()
}
doRequest()

Examples

For more detailed usage, see the examples.

Environment support

Node.js

node.js >= 6.0.0

Browsers

The library works in all modern browsers.

https://netmarketshare.com

We bundle regenerator-runtime and proxy-polyfill so that the library works out of the box in IE11.

Sponsors

Desarol

drupal-jsonapi-client's People

Contributors

auspicus avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

drupal-jsonapi-client's Issues

Documentation/examples for error handling.

Thanks for supplying some examples to get started. Please can you provide a typical template for error handling if Drupal fails to deliver or passes back an error message.

Are there any base assumptions we can make/document about error handling?

Consider replacing axios for devour to abstract away JSON:API logic

It would be much less work on our end if we didn't have to maintain a JSON:API client at all and just focused on the Drupal specific configuration we are looking for.

Unfortunately devour requires some advanced configuration before you can start sending entities which would mean users would need to do some kind of prefetch of their entity configuration just to be able to use the library.

Create a product roadmap

Few things so far:

  • New API to follow JSON:API semantics religiously
  • Provide a way to pull entity definitions locally (default values, etc) and cache them for continued usage across page refreshes
  • Integrate devour-client to reduce / remove heavy lifting of serialization, etc

Create an easy way to fetch Entity configuration

We want to be able to provide an export like:

import { EntityStorage, GlobalClient } from 'drupal-jsonapi-client'

GlobalClient.baseUrl = 'https://example.com' // point the cannon
GlobalClient.authorization = `Basic ${btoa('username:password')}` // if required

await EntityStorage.fetch()
const Article = EntityStorage.NodeArticle
const article = new Article()
article.title = 'asdf'
article.body.value = '1234'
article.body.format = 'basic_html'
await article.save() // create a new NodeArticle

do you support cross site requests & fetch configuration?

Do you support cross-site requests? The example code in the readme looks like its assuming same-origin.

  const Entity = window.DrupalJsonApi.Entity
  
  Entity
  .Load('node', 'article', 'uuid')
  .then(entity => {
    console.log(entity)
  })

This looks like quite a high level API (which is great!) but can I configure it and specify the URL and set up the necessary headers to make this work?

Thanks

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.