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 🙏

© 2025 – Pkg Stats / Ryan Hefner

relae

v1.6.3

Published

A Relay inspired library for React and RESTful backends

Readme

Relä

A Relay inspired library for React and RESTful backends

"Relä" is the Swedish word for "Relay".

Installation

You'll need both React and Relä:

npm install --save react relae

Usage

Create a React component and wrap it using the createContainer function:

import React from 'react';
import Relä from 'relae';

class MyComponent extends React.Component {
  render() {
    const data = this.props.data;
    return <div>{JSON.stringify(data)}</div>;
  }
}

export default Relä.createContainer(MyComponent, /* `Container Configuration` goes here */);

Container Configuration

Type: Object

Key options

Specifies optional options that are passed to the communication adapter.

{
  options: {
    // Relä options here...
  }
}

Option baseUrl

Type: String

Example:

{
  options: {
    baseUrl: "http://the.location.of/your/rest/api"
  }
}

Key queries

Type: Object

Specifies what data to fetch before rendering the wrapped component.

A query consists of three parts: a name, a path and a filter:

{
  queries: {
    name: {path: {/* filter */}}
  }
}

The name is the name of the props property to be set for the wrapped component with the data from the query result. The path is the resource path in your API, e.g. having baseUrl = "http://localhost" and path = "item" would give the full resource URL: "http://localhost/item". See below about filter.

Key queryParams

Type: Object

The queryParams object is merged with the created Relä container's props and then passed to the query filters, see more on filter params below.

Example:

{
  queryParams: {
    page: 1
  }
}

Key mutations

Type: Object

Specifies what mutative actions a wrapped component can do, the syntax is similar to that of queries but also specifies a mutation type:

{
  mutations: {
    name: {path: {type: {/* filter */}}
  }
}

The name of the action is used to create a mutation function that's passed to the wrapped component as props. The path is the resource path in the API, see queries above. type can be any of $create (makes a POST request), $update (PUT request) and $delete (DELETE request). See below about filter.

API

Relae.createContainer(Component, config)

| Parameter | Type | Description | |-----------|------|-------------| | Component | React.Component | The component to wrap with a Relä container | | config | Object | The container configuration |

Wraps a React component with a Relä container, to get all the Relä goodness.

Relae.dump()

Returns a JSON stringified dump of all the data in the internal store. Can be used in conjunction with Relae.boostrap().

Relae.bootstrap(data)

| Parameter | Type | Description | |-----------|------|-------------| | data | String | JSON stringified data |

Fills the internal store with the provided data. Should be used in conjunction with Relae.dump().

Relae.setIdProperty(name)

| Parameter | Type | Description | Default | |-----------|------|-------------|---------| | name | String | The name of the id property to use | "id" |

Relä identifies objects in a collection by its id property, which defaults to "id". If you're using Relä with e.g. MongoDB you should set this to "_id" with: Relae.setIdProperty('_id').

Relae.onChange(listener)

| Parameter | Type | Description | |-----------|------|-------------| | listener | Function | The function which will be triggered when the internal store changes |

Registers listeners that are triggered when the internal store is changed.

Filter

The filter is a MongoDB like filter that's passed as a query string to the API (for GET and DELETE requests), it's also used to query the internal cache store using sift.

Example:

{
  options: {
    baseUrl: "http://localhost"
  },
  queries: {
    items: {items: {parentItemId: {$exists: false}}}
  }
}

A container configuration like that would make a GET http://localhost/items?parentItemId={$exists:false} request and set props.item to the result of the request for the wrapped component.

Filter parameters

A filter can also contain parameters, using the <param-name> syntax:

{
  options: {
    baseUrl: "http://localhost"
  },
  queryParams: {
    page: 1
  },
  queries: {
    items: {items: {page: '<page>', parentItemId: '<parentItemId>'}}
  }
}

Because only page is specified in the queryParams object the parentItemId must be provided using props to the Relä container, i.e:

class ItemList extends React.Component {
  render() {
    return <ul>{this.props.items.map((item, i) => <li key={i}>{item}</li>)}</ul>;
  }
}

let ItemListCntainer = Relä.createContainer(ItemList, /* configuration from above */);

React.render(<ItemListContainer parentItemId={2} />, document);

Which would then trigger a GET http://localhost/items?page=1&parentItemId=2 request.

Nested filter parameters

Getting parameter values from nested queryParams or props can be done using dot notation, e.g. <parent.id> will search in queryParams.parent.id or props.parent.id.

More examples

Have a look at the tests for now...

Work in progress - TODO

  • [x] Cache request data in a universal store to not make unnecessary requests
  • [x] Add setQueryParams method to retrigger dependent requests
  • [ ] More usage examples
  • [ ] Make sure the components are updated optimistically for mutative actions
  • [ ] Handle errors and maybe retries in a good way
  • [ ] Work more on the isomorphism of the module (using the dump and bootstrap functions)
  • [ ] Intelligently group similar requests together to minimize number of requests
  • [ ] Extract Relä REST adapter to own module
  • [ ] Create a Relä Websockets adapter

License

MIT