Skip to content

Commit

Permalink
Merge pull request #38 from cosmicjs/mr-objects
Browse files Browse the repository at this point in the history
MR Objects
  • Loading branch information
jazibsawar authored Jan 22, 2021
2 parents 4d2803d + 80e9b63 commit 6e1e150
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 5 deletions.
16 changes: 16 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ bucket.getObjects(params).then(data => {
})
```

### Get Merge Request Objects [[View Blog Announcement](https://www.cosmicjs.com/blog/introducing-merge-requests)]
Get Objects included in a [Merge Request](https://www.cosmicjs.com/blog/introducing-merge-requests). Same query params options available as `getObjects`.
```javascript
const Cosmic = require('cosmicjs')
const api = Cosmic()
const bucket = api.bucket({
slug: 'target-bucket-slug',
read_key: 'target-bucket-read-key'
})
const data = await bucket.getMergeRequestObjects({
id: 'merge-request-id-found-in-dashboard',
props: 'slug,title,content' // use props to limit response payload
})
```
Use these Objects to then overright Objects from the target Bucket response based on unique `slug` and `locale` identifiers. An example of this logic can be found in the [Next Merge template](https://github.com/cosmicjs/next-merge/blob/master/lib/merge.js#L19).

#### Get Single Object [[View Docs](https://docs.cosmicjs.com/rest-api/objects.html#get-object)]
Returns a single Object from your Bucket.
```javascript
Expand Down
2 changes: 1 addition & 1 deletion cosmicjs.browser.min.js

Large diffs are not rendered by default.

85 changes: 84 additions & 1 deletion dist/bucket/object.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "cosmicjs",
"version": "3.3.1",
"description": "The official client module for Cosmic JS. This module helps you easily add dynamic content to your website or application.",
"description": "The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.",
"keywords": [
"headlesscms",
"cms",
"node",
"content",
Expand Down
64 changes: 64 additions & 0 deletions src/bucket/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,70 @@ const objectMethods = (bucket_config) => ({
}
return requestHandler(HTTP_METHODS.GET, endpoint)
},
getMergeRequestObjects: (params) => {
let endpoint = `${URI}/${bucket_config.slug}/merge-requests/${params.id}/objects?read_key=${bucket_config.read_key}`
if (params && params.limit) {
endpoint += `&limit=${params.limit}`
}
if (params && params.skip) {
endpoint += `&skip=${params.skip}`
}
if (params && params.locale) {
endpoint += `&locale=${params.locale}`
}
if (params && params.status) {
endpoint += `&status=${params.status}`
}
if (params && params.sort) {
endpoint += `&sort=${params.sort}`
}
// Type param
if (params && params.type) {
endpoint += `&type=${params.type}`
}
// Search params
if (params && params.q) {
endpoint += `&q=${params.q}`
}
if (params && params.metafield_key) {
endpoint += `&metafield_key=${params.metafield_key}`
}
if (params && params.metafield_value) {
endpoint += `&metafield_value=${params.metafield_value}`
}
if (params && params.metafield_object_id) {
endpoint += `&metafield_object_id=${params.metafield_object_id}`
}
if (params && params.hide_metafields) {
endpoint += `&hide_metafields=${params.hide_metafields}`
}
if (params && params.pretty) {
endpoint += `&pretty=${params.pretty}`
}
if (params && params.filters) {
Object.keys(params.filters).forEach((key) => {
endpoint += `&filters[${key}]=${params.filters[key]}`
})
}
if (params && params.metadata) {
Object.keys(params.metadata).forEach((key) => {
endpoint += `&metadata[${key}]=${params.metadata[key]}`
})
}
if (params && params.props) {
endpoint += `&props=${params.props}`
}
if (params && typeof params.created_by !== 'undefined') {
endpoint += `&created_by=${params.created_by}`
}
if (params && typeof params.depth !== 'undefined') {
endpoint += `&depth=${params.depth}`
}
if (params && params.query) {
endpoint += `&query=${encodeURI(JSON.stringify(params.query))}`
}
return requestHandler(HTTP_METHODS.GET, endpoint)
},
addObject: (params) => {
const endpoint = `${URI}/${bucket_config.slug}/add-object`
if (bucket_config.write_key) {
Expand Down

0 comments on commit 6e1e150

Please sign in to comment.