Skip to content

Commit

Permalink
feat(#71): add getRelated() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Sep 10, 2021
1 parent af0b44e commit f4d090f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-snakes-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt": minor
---

Added getRelated() method
29 changes: 29 additions & 0 deletions packages/druxt/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,35 @@ class DruxtClient {
return this.index
}

/**
* Get the related resources from a specified JSON:API resource.
*
* @param {string} type - The JSON:API Resource type.
* @param {string} id - The Drupal resource UUID.
* @param {string} related - The relationship name.
* @param {DruxtClientQuery} [query] - A correctly formatted JSON:API query string or object.
*
* @returns {object} The related JSON:API resource(s).
*/
async getRelated(type, id, related, query) {
if (!id || !type || !related) {
return false
}

let { href } = await this.getIndex(type)
if (!href) {
href = this.options.endpoint + '/' + type.replace('--', '/')
}

const url = this.buildQueryUrl(`${href}/${id}/${related}`, query)
try {
const related = await this.axios.get(url)
return related.data
} catch (e) {
return false
}
}

/**
* Get a JSON:API resource by type and ID.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/druxt/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ describe('DruxtClient', () => {
expect(cachedResourceIndex).toHaveProperty('href')
})

test('getRelated', async () => {
const mockArticle = await getMockResource('node--article')

const { type, id } = mockArticle.data
const related = await druxt.getRelated(type, id, 'field_media_image')
expect(related.data).toHaveProperty('type')
})

test('getResource', async () => {
const mockArticle = await getMockResource('node--article')
const entity = await druxt.getResource(mockArticle.data.type, mockArticle.data.id)
Expand Down
127 changes: 127 additions & 0 deletions test/__fixtures__/get/080ff38ba635ce6e565912bb104ad858.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": {
"type": "media--image",
"id": "201869c2-0cc4-405a-b596-93f488a48316",
"links": {
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316?resourceVersion=id%3A10"
}
},
"attributes": {
"drupal_internal__mid": 10,
"drupal_internal__vid": 10,
"langcode": "en",
"revision_created": "2021-05-01T06:24:09+00:00",
"revision_log_message": null,
"status": true,
"name": "Home Grown Herbs",
"created": "2021-05-01T06:24:09+00:00",
"changed": "2021-05-01T06:24:09+00:00",
"default_langcode": true,
"revision_translation_affected": true,
"path": {
"alias": null,
"pid": null,
"langcode": "en"
},
"content_translation_source": "und",
"content_translation_outdated": false
},
"relationships": {
"field_media_image": {
"data": {
"type": "file--file",
"id": "515d2bee-d29a-4bcb-a4ac-70b2c4faa5e3",
"meta": {
"alt": "Fresh cut herbs including mint, parsley, thyme and dill",
"title": null,
"width": 768,
"height": 512
}
},
"links": {
"related": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/field_media_image?resourceVersion=id%3A10"
},
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/relationships/field_media_image?resourceVersion=id%3A10"
}
}
},
"bundle": {
"data": {
"type": "media_type--media_type",
"id": "93c226a8-6e72-4caf-aae5-493066ef4f94"
},
"links": {
"related": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/bundle?resourceVersion=id%3A10"
},
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/relationships/bundle?resourceVersion=id%3A10"
}
}
},
"revision_user": {
"data": null,
"links": {
"related": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/revision_user?resourceVersion=id%3A10"
},
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/relationships/revision_user?resourceVersion=id%3A10"
}
}
},
"uid": {
"data": {
"type": "user--user",
"id": "f9e06c84-0999-4123-b1f1-b46b18590be4"
},
"links": {
"related": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/uid?resourceVersion=id%3A10"
},
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/relationships/uid?resourceVersion=id%3A10"
}
}
},
"thumbnail": {
"data": {
"type": "file--file",
"id": "515d2bee-d29a-4bcb-a4ac-70b2c4faa5e3",
"meta": {
"alt": "Fresh cut herbs including mint, parsley, thyme and dill",
"title": null,
"width": 768,
"height": 512
}
},
"links": {
"related": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/thumbnail?resourceVersion=id%3A10"
},
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/media/image/201869c2-0cc4-405a-b596-93f488a48316/relationships/thumbnail?resourceVersion=id%3A10"
}
}
}
}
},
"links": {
"self": {
"href": "https://demo-api.druxtjs.org/en/jsonapi/node/article/e4ced3c1-e14b-48f1-8c0a-c909e808de68/field_media_image"
}
}
}

0 comments on commit f4d090f

Please sign in to comment.