Skip to content

Commit 9d26182

Browse files
authored
Merge pull request #688 from akeneo/API-1935-API-1894-retrieve-pim-structure-js-snippets
API-1935: Add NodeJS snippets for guide Retrieve PIM structure
2 parents 6dde755 + 6456287 commit 9d26182

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

content/tutorials/guides/how-to-retrieve-pim-structure.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ $client = new \GuzzleHttp\Client([
7272
]);
7373
```
7474

75+
```javascript [activate:NodeJS]
76+
77+
// Install the node-fetch library by following the official documentation:
78+
// https://www.npmjs.com/package/node-fetch
79+
80+
import fetch from 'node-fetch';
81+
```
82+
7583
### 1. Get the PIM structure by fetching a channel from API
7684

7785
Workflow
@@ -96,6 +104,24 @@ $response = $client->get(
96104
$channel = json_decode($response->getBody()->getContents(), true);
97105
```
98106

107+
```javascript [activate:NodeJS]
108+
109+
import fetch from 'node-fetch';
110+
111+
const pimUrl = 'https://url-of-your-pim.com';
112+
const accessToken = 'your_app_token'; // Token provided during oAuth steps
113+
114+
const channelCode = 'ecommerce';
115+
116+
const response = await fetch(`${pimUrl}/api/rest/v1/channels/${channelCode}`, {
117+
headers: {
118+
'Authorization': `Bearer ${accessToken}`
119+
}
120+
});
121+
122+
const channel = await response.json();
123+
```
124+
99125
The retrieved channel resource looks like this:
100126

101127
```php [activate:PHP]
@@ -124,6 +150,32 @@ var_export($channel);
124150
]
125151
```
126152

153+
```javascript [activate:NodeJS]
154+
155+
console.log(channel);
156+
157+
// Output
158+
{
159+
code: 'ecommerce',
160+
currencies: [
161+
'USD',
162+
'EUR'
163+
],
164+
locales: [
165+
'de_DE',
166+
'en_US',
167+
'fr_FR'
168+
],
169+
category_tree: 'master',
170+
conversion_units: {},
171+
labels: {
172+
en_US: 'Ecommerce',
173+
de_DE: 'Ecommerce',
174+
fr_FR: 'Ecommerce'
175+
}
176+
}
177+
````
178+
127179
### 2. Store the PIM structure
128180

129181
We advise storing in your App locales, currencies, and root category.
@@ -137,3 +189,10 @@ storeCurrencies($channel['currencies']);
137189
storeLocales($channel['locales']);
138190
storeCategoryTree($channel['category_tree']);
139191
```
192+
193+
```javascript [activate:NodeJS]
194+
195+
storeCurrencies(channel.currencies);
196+
storeLocales(channel.locales);
197+
storeCategoryTree(channel.category_tree);
198+
```

0 commit comments

Comments
 (0)