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

@jayrbolton/flyd-crud

v0.0.1

Published

flyd ajax crud functionality

Downloads

3

Readme

flyd-crud

Easily manage a ajax in your UI logic of data using event streams.

crud({options})

options is an object that can contain any number of properties for each different kind of ajax request that you want. The property name is an arbitrary name you can pick (eg 'create', 'index', 'read', 'fetch', 'update', 'destroy', etc, etc). The value of each property is a set of request options

The only reserved property name is any. This allows you to set defaults for all the other requests. The properties in your any options will get merged into the other request options.

Request options can include these properties:

  • method: (required) the HTTP method for this request ('get', 'patch', 'put', 'post', 'delete', etc)
  • params$: (required) a stream of data that triggers a new ajax request, with this data being sent as JSON in the request
  • path: (required) string path for this request, or a function that receives the data from the params$ stream and should return a string path
  • headers: (optional) an object of header keys and values for the request
  • url: (optional) the base url for this request, which the path gets appended to
  • onSuccess: (optional) give an array of string names of other requests that you want to trigger when this request completes successfully (status === 200)
  • onStart: (optional) an array of names of other requests that you want to trigger when this request starts
  • onFail: (optional) an array of names of other requests that you want to trigger when this request fails (status !== 200)
// Stream of new user objects, maybe from some form data
const newUser$ = flyd.stream()
// A stream of user objects that we want to delete on the server
const userToDelete$ = flyd.stream()

const users = flyd.crud({
  // Options in "any" get merged into all the request below
  // Here we are setting some global headers
  any: {
    headers: {'Content-Type': 'application/json'}
  }
, create: {
    method: 'post'
  , params$: newUser$
  , path: u => `/users/${u.id}`
  , onSuccess: ['index']
  }
, index: {
    method: 'get'
  , path: '/users'
  , params$: userQuery$
  }
, delete: {
    method: 'delete'
  , params$: userToDelete$
  , path: u => `/users/delete?id=${u.id}`
  , onSuccess: ['index']
  }
})

userUpdates$({name: "Bob"}) 
users.loading$() // returns true
users.create.loading$() // returns true
// ... wait until request is finished
users.loading$() // returns false
users.create.loading$() // returns false
users.index.body$ // will receive the result of indexing users, triggered by the update using `onSuccess`
users.index.error$

userUpdates$({what: 'what'})
users.loading$() // true
// ...
users.loading$() // false
users.error$() // "Name is required"
users.body$() // -> [{name: "Bob", id: 1}]

Return value

The return value of calling flyd.crud({options}) is an object that contains helpful streams that indicate the status of your requests:

  • result.loading$: boolean, whether any request still pending
  • result.error$: the response body of an error result from the last request that had status !== 200
  • result.body$: the response body of the last request that succeeded (where status === 200)

The returned object also has properties for each request name that you gave. Each property has its own status streams for loading$, body$, and error$. For example, if you had a request called fetch, then the resulting object will have result.fetch.loading$, result.fetch.body$, and result.fetch.error$