npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonapi-metal-client

v0.2.1

Published

Bare metal TypeScript and JavaScript client for web API implementing JSON:API v1.0.

Downloads

5

Readme

JSON:API metal client

Build npm version

Bare metal TypeScript and JavaScript client for web API implementing JSON:API v1.0.

  • Zero dependency
  • Type-safe
  • Isomorphic

Documentations


Table of Contents

Requirements

  • ES6 (ECMAScript 2015)

Installation

Using npm:

npm install -s jsonapi-metal-client

Using yarn:

yarn add jsonapi-metal-client

With ES6 modules:

import { HttpAdapters, JsonApi } from 'jsonapi-metal-client';

With CommonJS modules:

const { HttpAdapters, JsonApi } = require('jsonapi-metal-client');

Usage

const httpAdapter = new HttpAdapters.FetchHttpAdapter(window.fetch.bind(window));
const client = new JsonApi.Client(httpAdapter);

// Fetch a resource collection.
client.fetch('https://example.com/articles');
client.fetchResource('https://example.com/articles');
client.fetchResourceCollection('https://example.com/articles');

// Fetch an individual resource.
client.fetch('https://example.com/articles/1');
client.fetchResource('https://example.com/articles/1');
client.fetchResourceIndividual('https://example.com/articles/1');

// Fetch relationship data representing a to-one relationship.
client.fetch('https://example.com/articles/1/relationships/author');
client.fetchRelationship('https://example.com/articles/1/relationships/author');
client.fetchRelationshipToOne('https://example.com/articles/1/relationships/author');

// Fetch relationship data representing a to-many relationship.
client.fetch('https://example.com/articles/1/relationships/comments');
client.fetchRelationship('https://example.com/articles/1/relationships/comments');
client.fetchRelationshipToMany('https://example.com/articles/1/relationships/comments');

// Create a resource.
client.createResource(
  'https://example.com/comments',
  {
    data: {
      type: 'photos',
      attributes: {
        title: 'Ember Hamster',
        src: 'http://example.com/images/productivity.png'
      },
      relationships: {
        photographer: {
          data: { type: 'people', id: '9' }
        }
      }
    }
  }
);

// Update a resource.
client.updateResource(
  'https://example.com/articles/1',
  {
    data: {
      type: 'articles',
      id: '1',
      attributes: {
        title: 'To TDD or Not'
      }
    }
  }
);

// Delete a resource.
client.deleteResource('https://example.com/photos/1');

// Update a to-one relationship.
client.updateRelationshipToOne(
  'https://example.com/articles/1/relationships/author',
  {
    data: { type: 'people', id: '12' }
  }
);

// Add a member to a to-many relationship.
client.createRelationshipToMany(
  'https://example.com/articles/1/relationships/comments',
  {
    data: [
      { type: 'comments', id: '123' }
    ]
  }
);

// Replace all members of a to-many relationship.
client.updateRelationshipToMany(
  'https://example.com/articles/1/relationships/tags',
  {
    data: []
  }
);

// Remove members from a to-many relationship.
client.deleteRelationshipToMany(
  'https://example.com/articles/1/relationships/comments',
  {
    data: [
      { type: 'comments', id: '12' },
      { type: 'comments', id: '13' }
    ]
  }
);

Documentation

HTTP adapter

Using axios

HTTP Adapter using the axios.

import axios from 'axios'

const httpAdapter = new HttpAdapters.AxiosHttpAdapter(
  axios
);

Using custom instance:

import axios from 'axios'

const instance = axios.create({ /* ... */ });

const httpAdapter = new HttpAdapters.AxiosHttpAdapter(
  instance
);

Using fetch

HTTP Adapter using the Fetch API.

const httpAdapter = new HttpAdapters.FetchHttpAdapter(
  window.fetch.bind(window)
);

For Node.js you can install cross-fetch, or other implementations.

const fetch = require('cross-fetch');

const httpAdapter = new HttpAdapters.FetchHttpAdapter(
  fetch
);
Set defaultInit

Set a default request init options that will be merged with all the request options made by the adapter:

const defaultInit = { mode: 'cors' };
const httpAdapter = new HttpAdapters.FetchHttpAdapter(fetch, defaultInit);

client.defaultInit.credentials = 'include';

console.log(client.defaultInit);
// { mode: 'cors', credentials: 'include' }

Client

Set the HTTP adapter.

const client = new JsonApi.Client(httpAdapter);

Configure custom HTTP headers

Set default HTTP headers to apply to each HTTP requests.

const username = 'username';
const password = 'password';
const defaultHttpHeaders = { 'Authorization': 'Basic ' + btoa(username + ':' + password) };

const client = new JsonApi.Client(httpAdapter, defaultHttpHeaders);

client.defaultHttpHeaders['x-foo'] = 'bar';

console.log(client.defaultHttpHeaders);
// { Authorization: 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'x-foo': 'bar' }

Result

The client returns a Promise that resolves with a Result for JSON:API operations with contains the properties:

| Name | Type | Description | |-------------|-----------|-------------| | isSuccess | boolean | whether the operation is successful. | | document | object | JSON:API document. | | request | object | HTTP request representation. | | response | object | HTTP response representation. |

const result = await client.fetch('http://examples.com/articles/1');
console.log(result)
// {
//   isSuccess: true,
//   document: {
//     links: {
//       self: "http://example.com/articles/1"
//     },
//     data: {
//       type: "articles",
//       id: "1",
//       attributes: {
//         title: "JSON:API paints my bikeshed!"
//       },
//       relationships: {
//         author: {
//           links: {
//             related: "http://example.com/articles/1/author"
//           }
//         }
//       }
//     }
//   },
//   request: {
//     url: 'http://examples.com/articles/1',
//     headers: {
//       Accept: 'application/vnd.api+json'
//     },
//     method: 'GET',
//     body: null
//   },
//   response: {
//     status: 200,
//     body: '{"type":"articles","id":"1","attributes":{"title":"JSON:API paints my bikeshed!"},"relationships":{"author":{"links":{"related":"http://example.com/articles/1/author"}}}}',
//     headers: {
//       'content-type': 'application/vnd.api+json; charset=utf-8',
//       etag: 'W/"47a7cbaefdec0639404a5946676f6e95"'
//     }
//   }
// }

Specification

There are some type guards (type predicats) that can be used to inspect the kind of JSON:API document.

import { JsonApi } from 'jsonapi-metal-client';

const {
  isFetchResponse
  isFetchResourceResponse
  isFetchResourceIndividualResponse
  isErrorDocument
} = JsonApi.Specification.TypeGuards

const result = await client.fetch('http://examples.com/articles/1');
if (result.isSuccess) {
  isFetchResponse(result.document)
  isFetchResourceResponse(result.document)
  isFetchResourceIndividualResponse(result.document)
  // true
} else {
  if (result.document) {
    isErrorDocument(result.document)
    // true
  }
}

Development

Requirements:

  • Yarn

Type Guards

Generate type guards (type predicats).

yarn type-guards

Documentation

Generate documentation using TypeDoc:

yarn documentation

Lint

Executing lint check using ESLint:

yarn lint

Executing lint fix using ESLint:

yarn format

Test

Executing Jest test suite:

yarn test

License

MIT