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

relient

v5.2.0

Published

Relient is an util set working closely with Relient-Cli. It will be useful if your project is built on React, Redux and of course Relient-Cli

Downloads

110

Readme

Relient

Relient is an util set working closely with Relient-CLI. It will be useful if your project is built on React, Redux and of course Relient-CLI.

Install

$ npm install relient --save

TOC

Redux utils

Relient provides action, middleware, reducer utils to help you handle API request and merge the payload. Check the API document

Create action

actions/account.ts

import { createAction, actionTypeCreator, post, read, put, del } from 'relient/actions';

const actionType = actionTypeCreator(__filename);

export const READ_ALL = actionType('READ_ALL');
export const CREATE = actionType('CREATE');
export const UPDATE = actionType('UPDATE');
export const REMOVE = actionType('REMOVE');

export const readAll = createAction(
  READ_ALL,
  () => read('/account/all'),
);

export const create = createAction(
  CREATE,
  ({ username, password }) => post('/account', { username, password }),
);

export const update = createAction(
  UPDATE,
  ({ id, username, password }) => put(`/account/${id}`, { username, password }),
);

export const remove = createAction(REMOVE, ({ id }) => del(`/account/${id}`));

Create reducer

reducers/account.ts

import { merge, remove, handleActions, combineActions } from 'relient/reducers';
import { account } from '../schema';
import { UPDATE, READ_ALL, CREATE, REMOVE } from '../actions/account';

export default {
  account: handleActions({
    [combineActions(
      UPDATE,
      CREATE,
    )]: merge({ schema: account }),

    [READ_ALL]: merge({ schema: account }),

    [REMOVE]: remove(account),

  }, {}),
};

reducers/index.ts

import { createEntitiesReducer, history, serverError } from 'relient/reducers';
import { combineReducers } from 'redux';
import { account } from './account'

export default combineReducers({
  ...createEntitiesReducer([ account ]),
  history,
  serverError,
});

Create store

import { fetch, history, serverError } from 'relient/middleware'
import { createStore, applyMiddleware } from 'redux';
import fetchInstance from 'isomorphic-fetch/fetch-npm-browserify' // or 'isomorphic-fetch/fetch-npm-node' in server side
import reducers from './reducers';

const initialState = {};

export default createStore(
  reducers,
  initialState,
  applyMiddleware({
    fetch({
      fetch: fetchInstance,
      apiDomain: 'https://your-api-domain',
      getDefaultHeader: ({ getState, withoutAuth }) => {
        // Return your default header object
      },
    })
    serverError({
      onUnauthorized: ({ payload, dispatch, getState }) => {
        // Handle 401 and 403 error
      },
      onGlobalWarning: ({ payload, dispatch, getState }) => {
        // Handle global API error
      }
    }),
  }),
);

Config

Relient config provide consistent configuration between server side and client side based on node-config.

You need to inject the global configs in client side:

html.js

import React from 'react';
import getClientConfig from 'relient/config/client-config';

export default () => (
  <html className="no-js" lang="en">
    <head>
    	...
    </head>
    <body>
      <script dangerouslySetInnerHTML={{ __html: getClientConfig(['slogan']) }} />
      ...
    </body>
  </html>
)

Then you can use getConfig in your code:

header.js

import getConfig from 'relient/config';

export default () => (
  <div>{getConfig('slogan')}</div>
)

Formatters

Relient provides common formatters to display date, time, price and percentage. Check the API document

import { date, time, price, percentage } from 'relient/formatters'

const originalDate = new Date('2018-01-01');
const formattedDate = date('YYYY-M-D')(originalDate); // 2018-1-1

const originalTime = new Date('2018-01-01 01:01:01');
const formattedDate = date('YYYY-M-D H:m:s')(originalTime); // 2018-1-1 1:1:1

const originalPrice = 100;
const formattedPrice = price()(originalPrice); // 100.00

const originalPercentage = 0.2;
const formattedPercentage = percentage()(originalPercentage); // 20%

I18N

Relient provides a simple i18n mechanism, which compatible with React: Check the API document

import React from 'react';
import i18n from 'relient/i18n';

const messages = {
  title: 'The total mount is {total}, and the top one is {top}',
}

export default () => (
  <div>{i18n(messages)('total', { total: 1, top: <b>Alex</b> })}</div>
)

DOM

Relient provides a simple head elements maintenance utils: Check the API document

import { updateMeta, updateCustomMeta, updateLink } from 'relient/dom';

updateMeta('description', 'web site description');
updateCustomMeta('og:url', 'canonical url');
updateLink('canonical', 'canonical url');