JSPM

  • Created
  • Published
  • Downloads 4521
  • Score
    100M100P100Q126973F
  • License MIT

Package Exports

  • @clipboard-health/json-api
  • @clipboard-health/json-api/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@clipboard-health/json-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@clipboard-health/json-api

Utilities for adhering to the JSON:API specification.

Table of Contents

Install

npm install @clipboard-health/json-api

Usage

Query helpers

From the client, call toSearchParams to convert from JsonApiQuery to URLSearchParams:

// ./examples/toSearchParams.ts

import { toSearchParams } from "@clipboard-health/json-api";

import { type JsonApiQuery } from "../src/lib/types";

const query: JsonApiQuery = {
  fields: { dog: ["age", "name"] },
  filter: { age: ["2", "5"] },
  include: ["vet"],
  page: { size: "10" },
  sort: ["-age"],
};

console.log(toSearchParams(query).toString());
// Note: actual result is URL-encoded, but unencoded below for readability
// => fields[dog]=age,name&filter[age]=2,5&include=vet&page[size]=10&sort=-age

From the server, call toJsonApiQuery to convert from URLSearchParams to JsonApiQuery:

// ./examples/toJsonApiQuery.ts

import { toJsonApiQuery } from "@clipboard-health/json-api";

const searchParams = new URLSearchParams(
  "fields%5Bdog%5D=age%2Cname&filter%5Bage%5D=2%2C5&include=vet&page%5Bsize%5D=10&sort=-age",
);

console.log(toJsonApiQuery(searchParams));
// => {
//   fields: { dog: ["age", "name"] },
//   filter: { age: ["2", "5"] },
//   include: ["vet"],
//   page: { size: "10" },
//   sort: ["-age"],
// }

Local development commands

See package.json scripts for a list of commands.