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

sulu-headless-api-client

v0.4.0

Published

An API client for SuluHeadlessBundle.

Downloads

9

Readme

Sulu Headless API Client

This module provides a Promise based way to work with the SuluHeadlessBundle JSON API.

Installation

# With npm
npm install sulu-headless-api-client

# Or yarn
yarn add sulu-headless-api-client

Basic usage

import Client from 'sulu-headless-api-client';

// Create client
const client = new Client();

// Get page data
client
    .getPageByPath(window.location.pathname)
    .then((page) => {
        // Do something with page data...
    });

Advanced usage

import Client from 'sulu-headless-api-client';
import sortNavigation from './helpers/sortNavigation';

// Create client
const navigationClient = new Client({
    baseUrl: process.env.SULU_BASE_URL,
    removeEmbedded: true,
    onResponse: (response) => sortNavigation(response),
});

// Get navigation data
navigationClient
    .getNavigationByKey('main', {
        depth: 3,
        excerpt: true,
        flat: true,
    })
    .then((navigation) => {
        // Do something with navigation data...
    });

Or with Axios.

import Client from 'sulu-headless-api-client';
import axios from 'axios';
import sortNavigation from './helpers/sortNavigation';

// Create client
const navigationClient = new Client({
    baseUrl: process.env.SULU_BASE_URL,
    fetchClient: axios,
    fetchOptions: {
        transformResponse: (response) => sortNavigation(response),
    },
    removeEmbedded: true,
});

// Get navigation data
navigationClient
    .getNavigationByKey('main', {
        depth: 3,
        excerpt: true,
        flat: true,
    })
    .then((navigation) => {
        // Do something with navigation data...
    });

Non-browser environments

By default, Client is intended to work in a browser environment. For non-browser environments, like Static Site Generation with Next.js or Astro, you need to opt-out from using the default baseUrl and fetchClient which rely on the window object. For example:

import Client from 'sulu-headless-api-client';

// Create client
const client = new Client({
    baseUrl: process.env.SULU_BASE_URL,
    fetchClient: fetch,
});

Error handling

import Client from 'sulu-headless-api-client';
import handleError from './helpers/handleError';
import handleSearchError from './helpers/handleSearchError';

// Client wide
const client = new Client({
    onError: (error) => handleError(error),
});

// Or per method
client
    .getPageByPath(window.location.pathname)
    .then((page) => {
        // Do something with page data...
    })
    .catch((error) => handleError(error));

client
    .search(query)
    .then((hits) => {
        // Do something with hits...
    })
    .catch((error) => handleSearchError(error));

Constructor

const client = new Client(options)

Creates a client.

Parameters

| Parameter | Required? | Type | Default | Description | |:-------------------------|:----------|:--------------------|:---------------------------|:----------------------------------------------------------------------| | options | No | Object | {} | The options for the client. | | options.basePath | No | String | /api | The base path of the API. | | options.baseUrl | No | String | window.location.pathname | The base URL of the API. | | options.fetchClient | No | Function | fetch.bind(window) | The fetch client to use. Tested with fetch and axios. | | options.fetchOptions | No | Object | {} | The options for the fetch client. | | options.locale | No | String | '' | The locale for every request. | | options.onError | No | Function(Error) | () => {} | The function to call on error. | | options.onResponse | No | Function(Reponse) | (r) => r | The function to call on response. | | options.removeEmbedded | No | Boolean | false | Whether to remove the _embedded layer from the response if present. |

Instance methods

client.getPageByPath(path)

Retrieves page data from the given path.

Parameters

| Parameter | Required? | Type | Default | Description | |:----------|:----------|:---------|:--------|:----------------------------------| | path | Yes | String | - | The path of the page to retrieve. |

Return value

A Promise that resolves to the page data.

client.getNavigationByKey(key[, params])

Retrieves navigation data with the given key and optional query parameters.

Parameters

| Parameter | Required? | Type | Default | Description | |:-----------------|:----------|:----------|:--------|:-------------------------------------------| | key | Yes | String | - | The key of the navigation to retrieve. | | params | No | Object | - | The query parameters for the request. | | params.depth | No | Number | 1 | The maximum depth of the navigation. | | params.excerpt | No | Boolean | false | Whether to include excerpt data. | | params.flat | No | Boolean | false | Whether to return as list instead of tree. |

Return value

A Promise that resolves to the navigation data.

client.getSnippetByArea(area[, params])

Retrieves snippet data with the given area name and optional query parameters.

Parameters

| Parameter | Required? | Type | Default | Description | |:--------------------------|:----------|:----------|:--------|:------------------------------------------| | area | Yes | String | - | The name of the snippet area to retrieve. | | params | No | Object | - | The query parameters for the request. | | params.includeExtension | No | Boolean | false | Whether to include extension data. |

Return value

A Promise that resolves to the snippet area data.

client.search(query)

Performs a search with the given query.

Parameters

| Parameter | Required? | Type | Default | Description | |:----------|:----------|:---------|:--------|:------------------| | query | Yes | String | - | The search query. |

Return value

A Promise that resolves to the search results.

Testing

Run yarn test to fire up the Mockoon CLI mock API and run all Playwright tests. Don't worry, everything stops after testing has finished.

License

Licensed under MIT.