Skip to content

Instantly share code, notes, and snippets.

@hhsnopek
Created June 16, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhsnopek/956c31fc07691102a3dc9709507c5616 to your computer and use it in GitHub Desktop.
Save hhsnopek/956c31fc07691102a3dc9709507c5616 to your computer and use it in GitHub Desktop.

Post Requst.

Payload

{
	"data": {
		"type": "users",
		"attributes": {
			"name": "Henry Snopek",
			"email": "hhsnopek@gmail.com",
			"bio": "Dev"
		},
		"relationships": {}
	}
}

Response

{
    "data": {
        "id": "4",
        "type": "users",
        "attributes": {}
    },
    "links": {
        "self": "http://localhost:4000/users"
    },
    "jsonapi": {
        "version": "1.0"
    }
}

Terminal Output from modified format.js fn

res POST { name: 'Henry Snopek', email: 'hhsnopek@gmail.com', bio: 'Dev' }
res POST {}
res POST { type: 'users',
  attributes: { name: 'Henry Snopek', email: 'hhsnopek@gmail.com', bio: 'Dev' },
  relationships: {} }
res POST { data:
   { type: 'users',
     attributes: { name: 'Henry Snopek', email: 'hhsnopek@gmail.com', bio: 'Dev' },
     relationships: {} } }
res POST {}
[2017-06-16T17:26:26.982Z] INSERT INTO "users" ("created_at", "updated_at") VALUES ('2017-06-16 13:26:26.973', '2017-06-
16 13:26:26.973')

Notice the console.log near the end of the function.

/**
 * @private
 */
export default function format(params: Object, method: Request$method): Object {
  const result = entries(params).reduce((obj, param) => {
    const [, value] = param;
    let [key] = param;

    key = key.replace(BRACKETS, '');

    switch (typeof value) {
      case 'object':
        return {
          ...obj,
          [key]: isNull(value) ? null : formatObject(value, method, format)
        };

      case 'string':
        return {
          ...obj,
          [key]: formatString(value, key === 'id' ? 'GET' : method)
        };

      default:
        return {
          ...obj,
          [key]: value
        };
    }
  }, {});

  console.log('res', method, camelizeKeys(result, true))
  return camelizeKeys(result, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment