griddo-sdk 1.0.9 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -121,8 +121,8 @@ class SDK {
121
121
  return await restorePage(this._env, id);
122
122
  };
123
123
 
124
- async getStructuredData(id, site = null, language = null) {
125
- return await getStructuredData(this._env, id, site, language);
124
+ async getStructuredData(contentType, site = null, language = null) {
125
+ return await getStructuredData(this._env, contentType, site, language);
126
126
  };
127
127
 
128
128
  async saveStructuredData(structuredData) {
@@ -133,6 +133,24 @@ class SDK {
133
133
  return await deleteStructuredData(this._env, id);
134
134
  };
135
135
 
136
+ async mapStructuredData(callback, contentType) {
137
+ if (this._env.verbose) console.log(`\nMapping ${contentType || 'all'} structured data with callback function ${callback.name}...`);
138
+ if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map structured data without a callback function.');
139
+ const content = await getStructuredData(this._env, contentType);
140
+ if (!content?.length) {
141
+ if (this._env.verbose) console.log('- No structured data found.');
142
+ return;
143
+ }
144
+ for (const structuredData of content) {
145
+ if (this._env.verbose) console.log('- Mapping structured data', structuredData.id);
146
+ const dataProtected = JSON.parse(JSON.stringify(structuredData));
147
+ const result = await callback(dataProtected);
148
+ if (result && JSON.stringify(structuredData) !== JSON.stringify(result)) {
149
+ await saveStructuredData(this._env, result)
150
+ }
151
+ }
152
+ };
153
+
136
154
  async api(...params) {
137
155
  return await api(this._env, ...params);
138
156
  };
@@ -1,12 +1,12 @@
1
1
  const { api } = require('../api');
2
2
 
3
- const getStructuredData = async (env, id, site = null, language = null, verbose) => {
3
+ const getStructuredData = async (env, contentType, site = null, language = null) => {
4
4
  const headers = {
5
5
  lang: language,
6
6
  };
7
- const items = await api(env, 'get', `${site ? `/site/${site}` : ''}/structured_data_contents/${id}?pagination=false&includeDraft=true`, { headers })
7
+ const items = await api(env, 'get', `${site ? `/site/${site}` : ''}/structured_data_contents/${contentType||''}?pagination=false&includeDraft=true`, { headers })
8
8
  .then(data => data.items);
9
- if (env.verbose) console.log(`\tStructured data ${id} loaded.`);
9
+ if (env.verbose) console.log(`\tStructured data ${contentType} loaded.`);
10
10
  return items;
11
11
  };
12
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "griddo-sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {