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

Modifing response format #39

Open
andrewmclagan opened this issue Dec 15, 2016 · 0 comments
Open

Modifing response format #39

andrewmclagan opened this issue Dec 15, 2016 · 0 comments

Comments

@andrewmclagan
Copy link

andrewmclagan commented Dec 15, 2016

Is there anyway to modify the response format from the standard?

The current standard is:

{
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [
    {
      "id": 1,
      "type": "post",
      "slug": "hello-world",
      "url": "http:\/\/localhost\/wordpress\/?p=1",
      "title": "Hello world!",
      "title_plain": "Hello world!",
      "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
      "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
      "date": "2009-11-11 12:50:19",
      "modified": "2009-11-11 12:50:19",
      "categories": [],
      "tags": [],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      },
      "comments": [
        {
          "id": 1,
          "name": "Mr WordPress",
          "url": "http:\/\/wordpress.org\/",
          "date": "2009-11-11 12:50:19",
          "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.<\/p>\n",
          "parent": 0
        }
      ],
      "comment_count": 1,
      "comment_status": "open"
    }
  ]
}

We would like to use wp-json-api as a "headless CMS" integrated into our current API. Thus we would want to transform our responses as per the rest of our API:

{
  "data": {
    "posts": [
      {
        "id": 1,
        "title": "Hello world!",
        "title_plain": "Hello world!",
        "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
        "categories": [92,61,21,6,32,7],
        "tags": [22,3,1,12],        
        "comments": [1], // see here
        "author": 1, // see here
      },
      {
        "id": 2,
        "title": "Another Post!",
        "title_plain": "Another Post!",
        "content": "<p>Another Post...<\/p>\n",
        "categories": [6,32,7],
        "tags": [17,1,44],        
        "comments": [], // see here
        "author": 1, // see here
      },      
    ],
    "comments": [
      {
        "id": 1,
        "name": "Mr WordPress",
        "url": "http:\/\/wordpress.org\/",
        "date": "2009-11-11 12:50:19",
        "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.<\/p>\n",
        "parent": 0
      },
    ],  
    "authors": [
      {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      }
    ], 
    "tags": [
      // ... you get the idea
    ],           
    "categories": [
      // ... you get the idea
    ],               
  }
}
  • As a side note we consume this API in react/redux apps.
  • The above response is "normalised" and is thus smaller on network traffic.
  • We already have a transformer written for the PHP Fractal API transformer library
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

No branches or pull requests

1 participant