Skip to content

Commit

Permalink
feat(#317): add createResource method
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Oct 6, 2021
1 parent 84ca183 commit ff1ff47
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-students-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt": minor
---

Added createResource method to DruxtClient
31 changes: 31 additions & 0 deletions packages/druxt/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ class DruxtClient {
}
}

/**
* Create a JSON:API resource.
*
* @param {object} resource - The JSON:API resource object
*
* @returns {object} The response data
*/
async createResource(resource) {
const { href } = await this.getIndex(resource.type)
if (!href) {
return false
}

let response
try {
response = await this.axios.post(
href,
{ data: resource },
{
headers: {
'Content-Type': 'application/vnd.api+json'
}
}
)
} catch (err) {
response = (err.response || {}).data || err.message
}

return response
}

/**
* Get a collection of resources from the JSON:API server.
*
Expand Down

0 comments on commit ff1ff47

Please sign in to comment.