griddo-sdk 1.0.6 → 1.0.9
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 +18 -10
- package/models/auth.js +3 -2
- package/models/pages.js +4 -4
- package/models/structuredData.js +9 -3
- 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 = [];
|
|
@@ -28,12 +30,14 @@ class SDK {
|
|
|
28
30
|
const {
|
|
29
31
|
api,
|
|
30
32
|
user,
|
|
31
|
-
password
|
|
33
|
+
password,
|
|
34
|
+
verbose,
|
|
32
35
|
} = environment;
|
|
33
|
-
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.');
|
|
34
37
|
this._env = {
|
|
35
38
|
api: api.endsWith('/') ? api.slice(0, -1) : api,
|
|
36
39
|
token: await getToken(environment),
|
|
40
|
+
verbose,
|
|
37
41
|
};
|
|
38
42
|
this.languages = await getLanguages(this._env);
|
|
39
43
|
|
|
@@ -50,15 +54,15 @@ class SDK {
|
|
|
50
54
|
};
|
|
51
55
|
|
|
52
56
|
async mapPages(callback, templateName) {
|
|
53
|
-
console.log(`\nMapping ${templateName || 'all'} pages with callback function ${callback.name}...`);
|
|
57
|
+
if (this._env.verbose) console.log(`\nMapping ${templateName || 'all'} pages with callback function ${callback.name}...`);
|
|
54
58
|
if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map pages without a callback function.');
|
|
55
59
|
const pages = await getPages(this._env, templateName);
|
|
56
60
|
if (!pages?.length) {
|
|
57
|
-
console.log('- No pages found.');
|
|
61
|
+
if (this._env.verbose) console.log('- No pages found.');
|
|
58
62
|
return;
|
|
59
63
|
}
|
|
60
64
|
for (const pageId of pages) {
|
|
61
|
-
console.log('- Mapping page', pageId);
|
|
65
|
+
if (this._env.verbose) console.log('- Mapping page', pageId);
|
|
62
66
|
const page = await getPage(this._env, pageId)
|
|
63
67
|
const pageProtected = JSON.parse(JSON.stringify(page));
|
|
64
68
|
const result = await callback(pageProtected);
|
|
@@ -69,7 +73,7 @@ class SDK {
|
|
|
69
73
|
};
|
|
70
74
|
|
|
71
75
|
async mapModulesFromPage(page, moduleName, callback) {
|
|
72
|
-
console.log(`\tMapping modules ${moduleName} with callback function ${callback.name}...`);
|
|
76
|
+
if (this._env.verbose) console.log(`\tMapping modules ${moduleName} with callback function ${callback.name}...`);
|
|
73
77
|
if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
|
|
74
78
|
if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
|
|
75
79
|
if (!page || typeof (page) !== 'object') throw new Error('Page to map modules not found.');
|
|
@@ -80,7 +84,7 @@ class SDK {
|
|
|
80
84
|
const module = modulePiece[key];
|
|
81
85
|
if (!module || typeof (module) !== 'object') continue;
|
|
82
86
|
if (module.component === moduleName) {
|
|
83
|
-
if (!found) console.log(`\tModule ${moduleName} found.`);
|
|
87
|
+
if (!found && this._env.verbose) console.log(`\tModule ${moduleName} found.`);
|
|
84
88
|
const newModule = await callback(JSON.parse(JSON.stringify(module)));
|
|
85
89
|
if (newModule) modulePiece[key] = newModule;
|
|
86
90
|
found = true;
|
|
@@ -89,12 +93,12 @@ class SDK {
|
|
|
89
93
|
}
|
|
90
94
|
};
|
|
91
95
|
await search(protectedPage);
|
|
92
|
-
if (!found) console.log(`\tModule ${moduleName} not found.`);
|
|
96
|
+
if (!found && this._env.verbose) console.log(`\tModule ${moduleName} not found.`);
|
|
93
97
|
return protectedPage;
|
|
94
98
|
};
|
|
95
99
|
|
|
96
100
|
async mapModules(callback, moduleName, template) {
|
|
97
|
-
console.log(`\nMapping modules ${moduleName} from ${template || 'all'} pages with callback function ${callback.name}...`);
|
|
101
|
+
if (this._env.verbose) console.log(`\nMapping modules ${moduleName} from ${template || 'all'} pages with callback function ${callback.name}...`);
|
|
98
102
|
if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
|
|
99
103
|
if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
|
|
100
104
|
const fixPage = (page) => this.mapModulesFromPage(page, moduleName, callback);
|
|
@@ -125,6 +129,10 @@ class SDK {
|
|
|
125
129
|
return await saveStructuredData(this._env, structuredData);
|
|
126
130
|
};
|
|
127
131
|
|
|
132
|
+
async deleteStructuredData(id) {
|
|
133
|
+
return await deleteStructuredData(this._env, id);
|
|
134
|
+
};
|
|
135
|
+
|
|
128
136
|
async api(...params) {
|
|
129
137
|
return await api(this._env, ...params);
|
|
130
138
|
};
|
package/models/auth.js
CHANGED
|
@@ -5,11 +5,12 @@ const getToken = async (params) => {
|
|
|
5
5
|
api: apiKey,
|
|
6
6
|
user: email,
|
|
7
7
|
password,
|
|
8
|
+
verbose,
|
|
8
9
|
} = params;
|
|
9
|
-
console.log('Logging to api...');
|
|
10
|
+
if (verbose) console.log('Logging to api...');
|
|
10
11
|
const token = await api({ api: apiKey }, 'post', '/login_check', { email, password })
|
|
11
12
|
.then(data => data.token);
|
|
12
|
-
console.log('Authenticated.');
|
|
13
|
+
if (verbose) console.log('Authenticated.');
|
|
13
14
|
return token;
|
|
14
15
|
};
|
|
15
16
|
|
package/models/pages.js
CHANGED
|
@@ -4,7 +4,7 @@ const getPages = (env, template = null) => api(env, 'get', `/pages/all/id/${temp
|
|
|
4
4
|
|
|
5
5
|
const getPage = async (env, pageId) => {
|
|
6
6
|
const page = await api(env, 'get', `/page/${pageId}`)
|
|
7
|
-
console.log('\tLoaded page', page.title);
|
|
7
|
+
if (env.verbose) console.log('\tLoaded page', page.title);
|
|
8
8
|
return page;
|
|
9
9
|
};
|
|
10
10
|
|
|
@@ -27,17 +27,17 @@ const savePage = async (env, page) => {
|
|
|
27
27
|
actionName,
|
|
28
28
|
} = actions[page.id ? 'update' : 'new'];
|
|
29
29
|
const savedPage = await api(env, method, endpoint, page);
|
|
30
|
-
console.log(`\tPage ${actionName}.`);
|
|
30
|
+
if (env.verbose) console.log(`\tPage ${actionName}.`);
|
|
31
31
|
return savedPage;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
const deletePage = (env, id) => {
|
|
35
|
-
console.log('\tDelete page', id);
|
|
35
|
+
if (env.verbose) console.log('\tDelete page', id);
|
|
36
36
|
return api(env, 'delete', `/page/${id}`);
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
const restorePage = (env, id) => {
|
|
40
|
-
console.log('\tRestore page', id);
|
|
40
|
+
if (env.verbose) console.log('\tRestore page', id);
|
|
41
41
|
return api(env, 'delete', `/page/${id}/undo`);
|
|
42
42
|
};
|
|
43
43
|
|
package/models/structuredData.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const { api } = require('../api');
|
|
2
2
|
|
|
3
|
-
const getStructuredData = async (env, id, site = null, language = null) => {
|
|
3
|
+
const getStructuredData = async (env, id, site = null, language = null, verbose) => {
|
|
4
4
|
const headers = {
|
|
5
5
|
lang: language,
|
|
6
6
|
};
|
|
7
7
|
const items = await api(env, 'get', `${site ? `/site/${site}` : ''}/structured_data_contents/${id}?pagination=false&includeDraft=true`, { headers })
|
|
8
8
|
.then(data => data.items);
|
|
9
|
-
console.log(`\tStructured data ${id} loaded.`);
|
|
9
|
+
if (env.verbose) console.log(`\tStructured data ${id} loaded.`);
|
|
10
10
|
return items;
|
|
11
11
|
};
|
|
12
12
|
|
|
@@ -29,11 +29,17 @@ const saveStructuredData = async (env, structuredData) => {
|
|
|
29
29
|
actionName,
|
|
30
30
|
} = actions[structuredData.id ? 'update' : 'new'];
|
|
31
31
|
const savedItem = await api(env, method, endpoint, structuredData);
|
|
32
|
-
console.log(`\tStructured data ${structuredData?.content?.title || structuredData?.content?.label || 'untitled'} in ${structuredData.structuredData} ${actionName}.`);
|
|
32
|
+
if (env.verbose) console.log(`\tStructured data ${structuredData?.content?.title || structuredData?.content?.label || 'untitled'} in ${structuredData.structuredData} ${actionName}.`);
|
|
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
|
};
|