Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API and form adapters #42

Merged
merged 4 commits into from
Mar 28, 2016
Merged

Add API and form adapters #42

merged 4 commits into from
Mar 28, 2016

Conversation

randycoulman
Copy link
Contributor

Forms

With formApiAdapter, you can wrap a redux-api-middleware action
creator in such a way that it will work as expected by redux-form. The
promise returned by the adapter will:

  • Resolve with a successful payload
  • Reject with a failing payload

If the failing payload is a 422 error, the rejection contains error
information in the format expected by redux-form.

The error formatting is Rails-specific for now. It would be possible
to pass the error formatter into formApiAdapter and rename our
current formatter as railsErrorFormatter or something like that.

API calls

Provide a callApi helper that wraps a redux-api-middleware action and
augments it as follows:

  • Call an onSuccess callback on a successful call. This allows for
    follow-up actions to be taken and/or dispatched when the API call
    succeeds.
  • Default the request method to GET. This can still be overridden.
  • Add a Content-Type: ‘application/json’ header to the request.
  • Convert any keys in the body to snake_case
  • Convert keys in a success or failure response back to camelCase
  • Convert ’true’ and ’false’ strings in success payloads to booleans

Other

Import specHelper in every spec file (for now).

There’s an issue with mocha-webpack where requiring the spec helper in the config file doesn’t make its changes visible to the actual specs. See zinserjan/mocha-webpack#1.

I forgot to git pair on this; I paired with @awc737. This PR supercedes the earlier #34 that @julianrogmans worked on because the project code that this was based on changed significantly since he opened that PR.

There’s an issue with mocha-webpack where requiring the spec helper in
the config file doesn’t make its changes visible to the actual specs.
See zinserjan/mocha-webpack#1.
With `formApiAdapter`, you can wrap a redux-api-middleware action
creator in such a way that it will work as expected by redux-form.  The
promise returned by the adapter will:

* Resolve with a successful payload
* Reject with a failing payload

If the failing payload is a 422 error, the rejection contains error
information in the format expected by redux-form.

The error formatting is Rails-specific for now.  It would be possible
to pass the error formatter into `formApiAdapter` and rename our
current formatter as `railsErrorFormatter` or something like that.
Provide a `callApi` helper that wraps a redux-api-middleware action and
augments it as follows:

* Call an `onSuccess` callback on a successful call.  This allows for
follow-up actions to be taken and/or dispatched when the API call
succeeds.

* Default the request method to `GET`.  This can still be overridden.

* Add a `Content-Type: ‘application/json’` header to the request.

* Convert any keys in the body to snake_case

* Convert keys in a success or failure response back to camelCase

* Convert `’true’` and `’false’` strings in success payloads to booleans
@lexun
Copy link
Contributor

lexun commented Mar 28, 2016

Looks good to me. Hopefully the require thing will be resolved soon.

@randycoulman randycoulman merged commit a8fe227 into master Mar 28, 2016
@randycoulman randycoulman deleted the features/api-adapters branch March 28, 2016 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants