Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/outlandishideas/wp-api-response-modify

Makes WP API response JSON sensible
https://github.com/outlandishideas/wp-api-response-modify

Last synced: about 1 month ago
JSON representation

Makes WP API response JSON sensible

Awesome Lists containing this project

README

        

# wp-api-response-modify

> Makes WP API response JSON sensible

Made with ❤ at [@outlandish](http://www.twitter.com/outlandish)

npm version
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

Makes response JSON from the WP API sensible. By default it...

- transforms properties to camel-case
- flattens objects with a single property `rendered`
- lifts embedded entities (author, featured media, etc.)
- removes the `_links` property

See the [list of effects](#effects) for more.

## Install

```sh
npm install --save wp-api-response-modify
```

## Import

```js
// ES2015
import modify from 'wp-api-response-modify'

// CommonJS
var modify = require('wp-api-response-modify')
```

## Usage

### `modify(response[, effects]) : Object`

Make a WP API response sensible.

- __response__ {Object} Response from WP-API
- [__effects__] {Array} _(optional)_ Effects to apply to the response

Returns the modified response.

## Effects

All effects are available at `modify.effects`, e.g. `modify.effects.flattenRenderedProps`.

Example: `featured_media => featuredMedia`

### `flattenRenderedProps`

Flatten any object that has a single property `rendered`.

Example:

```js
{ content: { rendered: 'content string' } }
// becomes...
{ content: 'content string' }
```

### `liftEmbeddedAuthor`

Replace the author field with the embedded author entity.

Example:

```js
{ author: 12, _embedded: { author: { id: 12, ... } } }
// becomes...
{ author: { id: 12, ... } }
```

### `liftEmbeddedFeaturedMedia`

Replace the `featured_media` field with the value of the embedded `wp:featuredmedia`.

### `removeLinks`

Remove the `_links` property.

### `camelize`

Transform all property names to camel-case in the response.

## Contributing

All pull requests and issues welcome!

If you're not sure how, check out Kent C. Dodds'
[great video tutorials on egghead.io](https://egghead.io/lessons/javascript-identifying-how-to-contribute-to-an-open-source-project-on-github)!

## Author & License

`wp-api-response-modify` was created by [Outlandish](https://twitter.com/outlandish) and is released under the MIT license.