griddo-sdk 1.0.7 → 1.0.10
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 +28 -4
- package/models/structuredData.js +8 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
const { api } = require('./api');
|
|
2
2
|
const { getToken } = require('./models/auth');
|
|
3
3
|
const { getPages, getPage, savePage, deletePage, restorePage } = require('./models/pages');
|
|
4
|
-
const { getStructuredData, saveStructuredData } = require('./models/structuredData');
|
|
4
|
+
const { getStructuredData, saveStructuredData, deleteStructuredData } = require('./models/structuredData');
|
|
5
5
|
const { getLanguages } = require('./models/languages');
|
|
6
6
|
const { getStatus } = require('./models/liveStatus');
|
|
7
7
|
|
|
8
8
|
class SDK {
|
|
9
9
|
constructor() {
|
|
10
10
|
this._env = {};
|
|
11
|
+
this.connect = this.connect.bind(this);
|
|
11
12
|
this.api = this.api.bind(this);
|
|
12
13
|
this.getPage = this.getPage.bind(this);
|
|
13
14
|
this.savePage = this.savePage.bind(this);
|
|
@@ -18,6 +19,7 @@ class SDK {
|
|
|
18
19
|
this.mapModules = this.mapModules.bind(this);
|
|
19
20
|
this.getStructuredData = this.getStructuredData.bind(this);
|
|
20
21
|
this.saveStructuredData = this.saveStructuredData.bind(this);
|
|
22
|
+
this.deleteStructuredData = this.deleteStructuredData.bind(this);
|
|
21
23
|
this.getLanguage = this.getLanguage.bind(this);
|
|
22
24
|
this.getDefaultLanguage = this.getDefaultLanguage.bind(this);
|
|
23
25
|
this.languages = [];
|
|
@@ -31,7 +33,7 @@ class SDK {
|
|
|
31
33
|
password,
|
|
32
34
|
verbose,
|
|
33
35
|
} = environment;
|
|
34
|
-
if (!api || !user || !password) throw new Error('
|
|
36
|
+
if (!api || !user || !password) throw new Error('Connection object must contain the keys api, user and password.');
|
|
35
37
|
this._env = {
|
|
36
38
|
api: api.endsWith('/') ? api.slice(0, -1) : api,
|
|
37
39
|
token: await getToken(environment),
|
|
@@ -119,14 +121,36 @@ class SDK {
|
|
|
119
121
|
return await restorePage(this._env, id);
|
|
120
122
|
};
|
|
121
123
|
|
|
122
|
-
async getStructuredData(
|
|
123
|
-
return await getStructuredData(this._env,
|
|
124
|
+
async getStructuredData(contentType, site = null, language = null) {
|
|
125
|
+
return await getStructuredData(this._env, contentType, site, language);
|
|
124
126
|
};
|
|
125
127
|
|
|
126
128
|
async saveStructuredData(structuredData) {
|
|
127
129
|
return await saveStructuredData(this._env, structuredData);
|
|
128
130
|
};
|
|
129
131
|
|
|
132
|
+
async deleteStructuredData(id) {
|
|
133
|
+
return await deleteStructuredData(this._env, id);
|
|
134
|
+
};
|
|
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
|
+
|
|
130
154
|
async api(...params) {
|
|
131
155
|
return await api(this._env, ...params);
|
|
132
156
|
};
|
package/models/structuredData.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const { api } = require('../api');
|
|
2
2
|
|
|
3
|
-
const getStructuredData = async (env,
|
|
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/${
|
|
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
9
|
if (env.verbose) console.log(`\tStructured data ${id} loaded.`);
|
|
10
10
|
return items;
|
|
@@ -33,7 +33,13 @@ const saveStructuredData = async (env, structuredData) => {
|
|
|
33
33
|
return savedItem;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
+
const deleteStructuredData = (env, id) => {
|
|
37
|
+
if (env.verbose) console.log('\tDelete structured data', id);
|
|
38
|
+
return api(env, 'delete', `/structured_data_content/${id}`);
|
|
39
|
+
};
|
|
40
|
+
|
|
36
41
|
module.exports = {
|
|
37
42
|
getStructuredData,
|
|
38
43
|
saveStructuredData,
|
|
44
|
+
deleteStructuredData,
|
|
39
45
|
};
|