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

ephery

v2.0.0

Published

Dead simple, in-memory, client-side, fake API service.

Downloads

49

Readme

Ephery

CircleCI codecov npm version

Ephery is a dead simple, in-memory, client-side, fake API service that mirrors redink-sdk. This tool is useful for prototyping client-side applications without hijacking Ajax. In particular, this tool was built to interact with React + Redux applications that rely on an API service, but Ephery can be useful for any framework that isn't too opinionated on data-fetching.

Getting started

npm install --save ephery

Define some basic schemas and fixtures.

// src/services/store.js
import Store from 'ephery';

const schemas = {
  user: {
    attributes: {
      name: true,
      email: true,
    },
    relationships: {
      blogs: {
        hasMany: 'blog',
      },
      comments: {
        hasMany: 'comment',
      },
    },
  },
  blog: {
    attributes: {
      title: true,
      createdAt: true,
    },
    relationships: {
      author: {
        belongsTo: 'user',
      },
      comments: {
        hasMany: 'comment',
      },
    },
  },
  comment: {
    attributes: {
      text: true,
      createdAt: true,
    },
    relationships: {
      user: {
        belongsTo: 'user',
      },
      blog: {
        belongsTo: 'blog',
      },
    },
  },
};

const fixtures = {
  user: {
    1: {
      name: 'John Doe',
      email: '[email protected]',
      blogs: ['1'],
      comments: ['1'],
    },
  },
  blog: {
    1: {
      title: 'Tabs vs. Spaces',
      author: '1',
      comments: ['1'],
    },
  },
  comment: {
    1: {
      text: 'First comment!',
      user: '1',
      blog: '1',
    },
  },
};

// Entity you want to use for authenticating
// Default is 'user'
const entity = 'user';

export default new Store(schemas, fixtures, entity);

Then somewhere in your application, you can invoke Ephery. All returned data is deeply nested JSON. authToken is used to authorize api calls, Ephery expects ${user.id}-token as a token.

import api from '../services/store';
const authToken = localStorage.token;

api.fetch(authToken, 'user', '1').then(user => {
  /*
  {
    name: 'John Doe',
    email: '[email protected]',
    blogs: [{
      id: '1',
      title: 'Tabs vs. Spaces',
      author: '1',
      comments: ['1'],
    }],
    comments: [{
      id: '1',
      text: 'First comment!',
      user: '1',
      blog: '1',
    }]
  }
   */
});

API

.create(authToken, type, record) -> Object async

api.create(authToken, 'user', {
  name: 'Dylan',
  email: '[email protected]',
}).then(user => {
  // created user
});

.fetch(authToken, type, id) -> Object async

api.fetch(authToken, 'user', '1').then(user => {
  // single user
});

.find(authToken, type, filters = {}) -> Object[] async

api.find(authToken, 'user', {
  name: 'Dylan',
}).then(users => {
  // all users
});

.update(authToken, type, id, data) -> Object async

api.update(authToken, 'user', '1', {
  name: 'Bob',
}).then(user => {
  // updated user
});

.archive(authToken, type, id) -> Object async

api.archive(authToken, 'user', '1').then(user => {
  // deleted user
});

.auth('signup', entity, data) -> Object async

This method simply creates a user and "hashes" the password. The password isn't actually hashed, but it simulates how it would happen on the server. Internally, the password is appended with "-secret", so that you can simulate users in fixtures by creating a user with the password field being "-secret".

api.auth('signup', 'user', {
  name: 'John Doe',
  email: '[email protected]',
  password: 'password',
}).then(user => {
  // created user with the password "hashed"
});

.auth('token', entity, data) -> Object async

This method exchanges an email/password combination for a token that can be stored (i.e. in localStorage).

api.auth('token', 'user', {
  email: '[email protected]',
  password: 'password',
}).then(response => {
  /*
  {
    token: 'f96776b7-19d1-44d8-8f78-f4c708b53c8a-token',
    user: {
      id: 'f96776b7-19d1-44d8-8f78-f4c708b53c8a',
      name: 'John Doe',
      email: '[email protected]',
    },
  }
  */
}).catch(err => {
  // invalid email/password combination
});

.verify(token) -> Object async

This method verifies that a token is valid. Internally, a token looks like "${id}-token", so that you can easily simulate tokens.

api.verify(token).then(response => {
  /*
  {
    verified: boolean,
  }
  */
})

React + Redux Example

Dan Abramov has an excellent course on Egghead.io titled "Idiomatic Redux," where he mocks a simple API service that is invoked by actions. Ephery can act as that service in applications that have more intensive CRUD requirements. Because Ephery returns deeply-nested JSON objects, responses can be easily normalized and merged with the state tree.

You'll need to create your own production-grade API service once your backend is finished, but Ephery can act as a drop-in replacement until then.

Current Gotchas

Relationships do not cascade right now. Meaning, if you delete a user entity that a blog's author relationship (a belongsTo relationship) points to, the blog is not deleted. I'm going to add this functionality soon. Also, if you're using Normalizr, you'll essentially have to duplicate schema definitions.

Also, the coverage could be a lot better, so more tests are coming as well.