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

statamic-client

v1.0.5

Published

Javascript client to interact with the [Statamic API](https://statamic.dev/rest-api).

Downloads

1,256

Readme

Statamic Client

Javascript client to interact with the Statamic API.

Installation

npm install statamic-client

Configuration

The client needs to be configured with the base URL (https://yourwebsite.test/api/) of the Statamic API. This can be done in two ways:

  • Set the apiUrl option in the Client constructor.
  • Set the STATAMIC_API_URL environment variable.

Make sure the Statamic API is enabled in your .env file:

STATAMIC_API_ENABLED=true

Each resource that is requested from the API needs to be enabled in the config/statamic/api.php file.

Usage

There are two ways to use the client. The first is to use the StatamicClient class directly. The second is to use the provided hooks. These hooks make use of SWR to cache the data and can only be used in React components.

Using the StatamicClient class directly:

import { Client } from 'statamic-client';

const client = new Client({
  apiUrl: 'https://statamic.test/api/',
});

const entry = await client.getEntry('pages', 'home');

console.log(entry.title);

Using the hooks:

import { useEntry } from 'statamic-client';

export function Component() {
    const { data: entry, error, isLoading } = useEntry('pages', 'home');

    if (error) {
        return <div>Error: {error.message}</div>;
    }
    
    if (isLoading) {
        return <div>Loading...</div>;
    }

    if (!entry) {
        return <div>Loading...</div>;
    }

    return <div>{entry.title}</div>;
}

Hook components need to be wrapped in a ClientContextProvider:

import { ClientContextProvider } from 'statamic-client';

export function App() {
    return (
        <ClientContextProvider apiUrl="https://statamic.test/api/">
            <Component />
        </ClientContextProvider>
    );
}

See the SWR documentation for more information.

API responses

The client will return unmodified responses from the Statamic API, except for the following endpoints:

  • Entry
  • CollectionTree
  • NavigationTree
  • TaxonomyTerm
  • Globals
  • Global
  • Forms
  • Form
  • User
  • Asset

These responses usually only contain an object with a data property. The client will return the data property directly.

{
  "title": "My First Day"
}

All other responses are paginated and unmodified:

{
  
  "data": [
    {
      "title": "Music"
    },
    {
      "title": "Movies"
    },
    {
      "title": "Books"
    }
  ],
  "links": {
    "first": "https://statamic.test/api/collections/pages/entries?page=1",
    "last": "https://statamic.test/api/collections/pages/entries?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 1,
    "links": [
      {
        "url": null,
        "label": "&laquo; Previous",
        "active": false
      },
      {
        "url": "https://statamic.test/api/collections/pages/entries?page=1",
        "label": "1",
        "active": true
      },
      {
        "url": null,
        "label": "Next &raquo;",
        "active": false
      }
    ],
    "path": "https://statamic.test/api/collections/pages/entries",
    "per_page": 50,
    "to": 1,
    "total": 1
  }
}

Parameters

Filtering

Make sure to enable filtering for the resource you want to filter.

See the Statamic documentation for more information.

const entries = await client.getEntries('pages', {
    filter: {
        /**
         * A string describing the field to filter by.
         */
        field: 'title',
        /**
         * A string describing the condition to filter by.
         * If omitted, defaults to `equals` in Statamic.
         * See the `Condition` enum for all available conditions.
         */
        condition: Condition.EQUALS,
        /**
         * A string used as value to compare the field against.
         */
        value: 'Home'
    },
});

Multiple filters

const entries = await client.getEntries('pages', {
    filter: [
        {
            field: 'title',
            condition: Condition.EQUALS,
            value: 'Home'
        },
        {
            field: 'slug',
            condition: Condition.EQUALS,
            value: 'home'
        }
    ],
});

Selecting a field

See the Statamic documentation for more information.

const entries = await client.getEntries('pages', {
  select: 'title',
});

Selecting multiple fields

const entries = await client.getEntries('pages', {
  select: [
        'title',
        'slug',
    ],
});

Sorting

See the Statamic documentation for more information.

const entries = await client.getEntries('pages', {
  sort: {
        /**
         * A string describing the field to sort by.
         */
        field: 'fieldA',
        /**
         * A boolean describing whether the sort should be reversed.
         */
        reverse: true,
    },
});

Sorting by multiple fields

const entries = await client.getEntries('pages', {
  sort: [
        {
            field: 'fieldA',
            reverse: true,
        },
        {
            field: 'fieldB',
        }
    ],
});

Pagination

const entries = await client.getEntries('pages', {
  /**
   * A number describing which page of results to return.
   */
  page: 2,
  /**
   * A number describing how many results to return per page.
   * Set to 25 by default by Statamic.
   */
  limit: 10,
});

Site locale

const entries = await client.getEntries('pages', {
  site: 'nl',
});

Maximum tree depth

const tree = await client.getCollectionTree('pages', {
  /**
   * A number describing the maximum depth of the tree to return.
   */
  maxDepth: 2,
});