Skip to content

Commit 18c5993

Browse files
authored
Merge pull request #66 from contentstack/feat/DX-654-updateAssetURLForGQL
feat: ✨ update-asset-url-for-gql fn added
2 parents a2d83ac + 9a0027e commit 18c5993

File tree

4 files changed

+197
-1
lines changed

4 files changed

+197
-1
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
export const gqlResponseForAssetUpdate = {
2+
"data": {
3+
"page_json_rte": {
4+
"title": "My First JSON-Rte Entry",
5+
"system": {
6+
"content_type_uid": "page_json_rte",
7+
"uid": "uid_page_json_rte"
8+
},
9+
"rte_2": {
10+
"embedded_itemsConnection": {
11+
"edges": [
12+
{
13+
"node": {
14+
"title": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
15+
"url": "https://images.contentstack.io/v3/assets/api_key/asset_uid_1341/66427e3816e6bfae8f58d971/pexels-pixabay-33109.jpg",
16+
"content_type": "image/jpeg",
17+
"filename": "pexels-pixabay-33109.jpg",
18+
"system": {
19+
"uid": "asset_uid_1341"
20+
}
21+
}
22+
}
23+
]
24+
},
25+
"json": {
26+
"type": "doc",
27+
"uid": "rte_uid",
28+
"attrs": {},
29+
"children": [
30+
{
31+
"uid": "rte_node_uid",
32+
"type": "reference",
33+
"attrs": {
34+
"display-type": "display",
35+
"asset-uid": "asset_uid_1341",
36+
"content-type-uid": "sys_assets",
37+
"asset-link": "https://images.contentstack.io/v3/assets/api_key/asset_uid_1341/6502bb17bb60f72316481aaa/FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
38+
"asset-name": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
39+
"asset-type": "image/jpeg",
40+
"type": "asset",
41+
"class-name": "embedded-asset",
42+
"alt": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
43+
"asset-alt": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
44+
"inline": false
45+
},
46+
"children": [
47+
{
48+
"text": ""
49+
}
50+
]
51+
},
52+
{
53+
"type": "p",
54+
"attrs": {},
55+
"uid": "rte_node_2_uid",
56+
"children": [
57+
{
58+
"text": ""
59+
}
60+
]
61+
}
62+
],
63+
"_version": 10
64+
}
65+
}
66+
}
67+
}
68+
}
69+
70+
export const gqlResponseForAssetUpdateWithoutSystemUid = {
71+
"data": {
72+
"page_json_rte": {
73+
"title": "My First JSON-Rte Entry",
74+
"system": {
75+
"content_type_uid": "page_json_rte",
76+
"uid": "uid_page_json_rte"
77+
},
78+
"rte_2": {
79+
"embedded_itemsConnection": {
80+
"edges": [
81+
{
82+
"node": {
83+
"title": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
84+
"url": "https://images.contentstack.io/v3/assets/api_key/asset_uid_1341/66427e3816e6bfae8f58d971/pexels-pixabay-33109.jpg",
85+
"content_type": "image/jpeg",
86+
"filename": "pexels-pixabay-33109.jpg",
87+
}
88+
}
89+
]
90+
},
91+
"json": {
92+
"type": "doc",
93+
"uid": "rte_uid",
94+
"attrs": {},
95+
"children": [
96+
{
97+
"uid": "rte_node_uid",
98+
"type": "reference",
99+
"attrs": {
100+
"display-type": "display",
101+
"asset-uid": "asset_uid_1341",
102+
"content-type-uid": "sys_assets",
103+
"asset-link": "https://images.contentstack.io/v3/assets/api_key/asset_uid_1341/6502bb17bb60f72316481aaa/FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
104+
"asset-name": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
105+
"asset-type": "image/jpeg",
106+
"type": "asset",
107+
"class-name": "embedded-asset",
108+
"alt": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
109+
"asset-alt": "FA23_TJ_FUEL_TJM_DEL_1_STITCHED.jpg",
110+
"inline": false
111+
},
112+
"children": [
113+
{
114+
"text": ""
115+
}
116+
]
117+
},
118+
{
119+
"type": "p",
120+
"attrs": {},
121+
"uid": "rte_node_2_uid",
122+
"children": [
123+
{
124+
"text": ""
125+
}
126+
]
127+
}
128+
],
129+
"_version": 10
130+
}
131+
}
132+
}
133+
}
134+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { updateAssetURLForGQL } from '../src/updateAssetURLForGQL';
2+
import { gqlResponseForAssetUpdate, gqlResponseForAssetUpdateWithoutSystemUid } from './mock/gql-asset-url-update-mock';
3+
4+
describe('updateAssetURLForGQL test', () => {
5+
6+
it('should update the asset URL in the GQL response when proper response is passed', done => {
7+
const testResponse = { ...gqlResponseForAssetUpdate };
8+
updateAssetURLForGQL(testResponse);
9+
10+
const rteField = testResponse.data.page_json_rte.rte_2;
11+
const assetLink = rteField.json.children[0].attrs['asset-link'];
12+
const expectedUrl = rteField.embedded_itemsConnection.edges[0].node.url;
13+
14+
expect(assetLink).toBe(expectedUrl);
15+
done();
16+
});
17+
18+
it('should throw error when system.uid is not present', done => {
19+
jest.spyOn(console, 'error').mockImplementation(() => {});
20+
21+
const testResponse = { ...gqlResponseForAssetUpdateWithoutSystemUid };
22+
updateAssetURLForGQL(testResponse);
23+
24+
expect(console.error).toHaveBeenCalledWith(
25+
'Error in updating asset URL for GQL response',
26+
expect.any(Error) // Expecting any Error object
27+
);
28+
29+
expect(console.error).toHaveBeenCalledWith(
30+
'Error in updating asset URL for GQL response',
31+
new Error('Asset UID not found in the response') // Expecting any Error object
32+
);
33+
done();
34+
});
35+
});

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export { default as Document } from './nodes/document'
1212
export { default as TextNode } from './nodes/text-node';
1313
export { jsonToHTML } from './json-to-html'
1414
export { GQL } from './gql'
15-
export { addTags as addEditableTags } from './entry-editable'
15+
export { addTags as addEditableTags } from './entry-editable'
16+
export { updateAssetURLForGQL } from './updateAssetURLForGQL'

src/updateAssetURLForGQL.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function updateAssetURLForGQL(gqlResponse:any) {
2+
try {
3+
const response = gqlResponse?.data;
4+
for (let contentType in response) {
5+
const entry = response[contentType]; // page_json_rte
6+
for (let field in entry) {
7+
const fieldData = entry[field];
8+
if (fieldData && fieldData.embedded_itemsConnection) { // rte field with embedded items e.g rte_2
9+
const edges = fieldData?.embedded_itemsConnection?.edges;
10+
edges.forEach((edge:any) => {
11+
const node = edge.node;
12+
if (node?.url && node?.filename) {
13+
14+
if (!node?.system?.uid) throw new Error('Asset UID not found in the response');
15+
16+
const correspondingAsset = fieldData?.json?.children?.find((child:any) => child.attrs['asset-uid'] === node.system.uid);
17+
correspondingAsset.attrs['asset-link'] = node.url;
18+
}
19+
});
20+
}
21+
}
22+
}
23+
} catch (error) {
24+
console.error('Error in updating asset URL for GQL response', error);
25+
}
26+
}

0 commit comments

Comments
 (0)