Skip to content

Instantly share code, notes, and snippets.

@derekr
Created February 25, 2016 18:07
Show Gist options
  • Select an option

  • Save derekr/1d989b8c7a9ab8ebe1e6 to your computer and use it in GitHub Desktop.

Select an option

Save derekr/1d989b8c7a9ab8ebe1e6 to your computer and use it in GitHub Desktop.

Redux JSON-API Notes

  • Colocation of requests and where the properties at being used
  • Bubbling up/emrging duplicate requests (is this even really possible or good w/ json-api?)
  • Helpful warning messages for improving requests (eg "You're requesting all fields, but only selecting 3.")
  • Simplify selecting fields from the requested resource (eliminate caller needing to use safe-access)
  • Auto namespace requests
  • Select request state (use getLoadingFn internally)
  • Internalize all things like makeRefReducer… these are implementation details a caller shouldn't care about (maybe a separate module for all this tooling?)
  • Avoid abstracting away json-api details… it should be obvious that's the type of api we're working with?
  • How do we handle pagination?

Sketches

Sits on top of react-redux connect. Shooting for similar interface w/ some configuration around making json-api requests.

import { connectJsonApi } from 'connect-json-api'

import PostList from 'components/PostList'

const requests = {
    posts: { // used for namespacing requests… maybe prepend componentName as well?
        resourcePath: '/posts', // default to get
        includes: ['user'],
        fields: ['user.fullName'],
        filter: { name: 'drk' }, // and other json api query params
        props: { // if `props` ommitted return { posts, postsRequestStatus } by default?
            postsRequestStatus: 'requestStatuss', // special key? contains isLoading, isPending? 
            posts: function (ref) => {
                // if you want to do any custom mapping?
            },
            altPosts: [
                'user.fullName' // useful for grabbing exact fields when not specified on requests 
            ], // assumes accessing from the ref so access(ref, 'field.field')
            letFieldsDoTheWorkPosts: 'ref' // just grab all the things defined in the `fields` option
        }
    },
    updatePost: {
        resourcePath: '/posts',
        method: 'PATCH',
        dispatch: {
            updatePost: payload => payload // dispatches request actions etc
        },
        props: {
            requestStats: 'requestStatus',
            
        }
    }
}

export default connectJsonApi({
    requests
})(PostList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment