griddo-sdk 1.2.4 → 1.2.6

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
@@ -1,214 +1,215 @@
1
- const {api} = require('./api');
2
- const {getToken} = require('./models/auth');
3
- const {getPages, getPage, savePage, deletePage, restorePage} = require('./models/pages');
1
+ const { api } = require("./api");
2
+ const { getToken } = require("./models/auth");
3
+ const { getPages, getPage, savePage, deletePage, restorePage } = require("./models/pages");
4
4
  const {
5
- getStructuredData,
6
- getStructuredDataByID,
7
- saveStructuredData,
8
- deleteStructuredData
9
- } = require('./models/structuredData');
10
- const {getLanguages} = require('./models/languages');
11
- const {getStatus} = require('./models/liveStatus');
12
- const {sendAlert} = require('./models/alerts');
5
+ getStructuredData,
6
+ getStructuredDataByID,
7
+ saveStructuredData,
8
+ deleteStructuredData
9
+ } = require("./models/structuredData");
10
+ const { getLanguages } = require("./models/languages");
11
+ const { getStatus } = require("./models/liveStatus");
12
+ const { sendAlert } = require("./models/alerts");
13
13
 
14
14
  class SDK {
15
- constructor() {
16
- this._env = {};
17
- this.connect = this.connect.bind(this);
18
- this.api = this.api.bind(this);
19
- this.getPage = this.getPage.bind(this);
20
- this.savePage = this.savePage.bind(this);
21
- this.deletePage = this.deletePage.bind(this);
22
- this.restorePage = this.restorePage.bind(this);
23
- this.mapPages = this.mapPages.bind(this);
24
- this.mapModulesFromPage = this.mapModulesFromPage.bind(this);
25
- this.mapModules = this.mapModules.bind(this);
26
- this.getStructuredDataByID = this.getStructuredDataByID.bind(this);
27
- this.getStructuredData = this.getStructuredData.bind(this);
28
- this.getPaginationStructuredData = this.getPaginationStructuredData.bind(this);
29
- this.saveStructuredData = this.saveStructuredData.bind(this);
30
- this.deleteStructuredData = this.deleteStructuredData.bind(this);
31
- this.mapStructuredData = this.mapStructuredData.bind(this);
32
- this.getLanguage = this.getLanguage.bind(this);
33
- this.getDefaultLanguage = this.getDefaultLanguage.bind(this);
34
- this.sendAlert = this.sendAlert.bind(this);
35
- this.languages = [];
36
- this.liveStatus = {};
37
- }
38
-
39
- async connect(environment) {
40
- const {
41
- api,
42
- publicApi,
43
- user,
44
- password,
45
- verbose,
46
- } = environment;
47
- if (!api || !user || !password) throw new Error('Connection object must contain the keys api, user and password.');
48
- if (!publicApi) console.log('WARNING: Connection object should contain the key publicApi (!!!).\n\n');
49
- this._env = {
50
- api: api.endsWith('/') ? api.slice(0, -1) : api,
51
- publicApi: publicApi && (publicApi.endsWith('/') ? publicApi.slice(0, -1) : publicApi),
52
- token: await getToken(environment),
53
- verbose,
54
- };
55
- this.languages = await getLanguages(this._env);
56
-
57
- const liveStatus = await getStatus(this._env);
58
- for (const item of liveStatus) this.liveStatus[item.status] = item.id;
59
- };
60
-
61
- getLanguage(key, value) {
62
- return this.languages.find(item => item[key] === value);
63
- };
64
-
65
- getDefaultLanguage() {
66
- return this.languages.find(item => item.isDefault);
67
- };
68
-
69
- async mapPages(callback, templateName) {
70
- if (this._env.verbose) console.log(`\nMapping ${templateName || 'all'} pages with callback function ${callback.name}...`);
71
- if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map pages without a callback function.');
72
- const pages = await getPages(this._env, templateName);
73
- if (!pages?.length) {
74
- if (this._env.verbose) console.log('- No pages found.');
75
- return;
15
+ constructor() {
16
+ this._env = {};
17
+ this.connect = this.connect.bind(this);
18
+ this.api = this.api.bind(this);
19
+ this.getPage = this.getPage.bind(this);
20
+ this.savePage = this.savePage.bind(this);
21
+ this.deletePage = this.deletePage.bind(this);
22
+ this.restorePage = this.restorePage.bind(this);
23
+ this.mapPages = this.mapPages.bind(this);
24
+ this.mapModulesFromPage = this.mapModulesFromPage.bind(this);
25
+ this.mapModules = this.mapModules.bind(this);
26
+ this.getStructuredDataByID = this.getStructuredDataByID.bind(this);
27
+ this.getStructuredData = this.getStructuredData.bind(this);
28
+ this.getPaginationStructuredData = this.getPaginationStructuredData.bind(this);
29
+ this.saveStructuredData = this.saveStructuredData.bind(this);
30
+ this.deleteStructuredData = this.deleteStructuredData.bind(this);
31
+ this.mapStructuredData = this.mapStructuredData.bind(this);
32
+ this.getLanguage = this.getLanguage.bind(this);
33
+ this.getDefaultLanguage = this.getDefaultLanguage.bind(this);
34
+ this.sendAlert = this.sendAlert.bind(this);
35
+ this.languages = [];
36
+ this.liveStatus = {};
76
37
  }
77
- for (const pageId of pages) {
78
- if (this._env.verbose) console.log('- Mapping page', pageId);
79
- const page = await getPage(this._env, pageId)
80
- const pageProtected = JSON.parse(JSON.stringify(page));
81
- const result = await callback(pageProtected);
82
- if (result && JSON.stringify(page) !== JSON.stringify(result)) {
83
- await savePage(this._env, result)
84
- }
38
+
39
+ async connect(environment) {
40
+ const {
41
+ api,
42
+ publicApi,
43
+ user,
44
+ password,
45
+ verbose
46
+ } = environment;
47
+ if (!api || !user || !password) throw new Error("Connection object must contain the keys api, user and password.");
48
+ if (!publicApi) console.log("WARNING: Connection object should contain the key publicApi (!!!).\n\n");
49
+ this._env = {
50
+ api: api.endsWith("/") ? api.slice(0, -1) : api,
51
+ publicApi: publicApi && (publicApi.endsWith("/") ? publicApi.slice(0, -1) : publicApi),
52
+ token: await getToken(environment),
53
+ verbose
54
+ };
55
+ this.languages = await getLanguages(this._env);
56
+
57
+ const liveStatus = await getStatus(this._env);
58
+ for (const item of liveStatus) this.liveStatus[item.status] = item.id;
59
+ };
60
+
61
+ getLanguage(key, value) {
62
+ return this.languages.find(item => item[key] === value);
63
+ };
64
+
65
+ getDefaultLanguage() {
66
+ return this.languages.find(item => item.isDefault);
67
+ };
68
+
69
+ async mapPages(callback, templateName) {
70
+ if (this._env.verbose) console.log(`\nMapping ${templateName || "all"} pages with callback function ${callback.name}...`);
71
+ if (!callback || typeof (callback) !== "function") throw new Error("Cannot map pages without a callback function.");
72
+ const pages = await getPages(this._env, templateName);
73
+ if (!pages?.length) {
74
+ if (this._env.verbose) console.log("- No pages found.");
75
+ return;
76
+ }
77
+ for (const pageId of pages) {
78
+ if (this._env.verbose) console.log("- Mapping page", pageId);
79
+ const page = await getPage(this._env, pageId);
80
+ const pageProtected = JSON.parse(JSON.stringify(page));
81
+ const result = await callback(pageProtected);
82
+ if (result && JSON.stringify(page) !== JSON.stringify(result)) {
83
+ await savePage(this._env, result);
84
+ }
85
+ }
86
+ };
87
+
88
+ async mapModulesFromPage(page, moduleName, callback) {
89
+ if (this._env.verbose) console.log(`\tMapping modules ${moduleName} with callback function ${callback.name}...`);
90
+ if (!callback || typeof (callback) !== "function") throw new Error("Cannot map modules without a callback function.");
91
+ if (!moduleName) throw new Error("Cannot map modules in page without selecting the module to map.");
92
+ if (!page || typeof (page) !== "object") throw new Error("Page to map modules not found.");
93
+ const protectedPage = JSON.parse(JSON.stringify(page));
94
+ let found = false;
95
+ const search = async (modulePiece) => {
96
+ for (const key of Object.keys(modulePiece)) {
97
+ const module = modulePiece[key];
98
+ if (!module || typeof (module) !== "object") continue;
99
+ if (module.component === moduleName) {
100
+ if (!found && this._env.verbose) console.log(`\tModule ${moduleName} found.`);
101
+ const newModule = await callback(JSON.parse(JSON.stringify(module)));
102
+ if (newModule) modulePiece[key] = newModule;
103
+ found = true;
104
+ }
105
+ await search(module);
106
+ }
107
+ };
108
+ await search(protectedPage);
109
+ if (!found && this._env.verbose) console.log(`\tModule ${moduleName} not found.`);
110
+ return protectedPage;
111
+ };
112
+
113
+ async mapModules(callback, moduleName, template) {
114
+ if (this._env.verbose) console.log(`\nMapping modules ${moduleName} from ${template || "all"} pages with callback function ${callback.name}...`);
115
+ if (!callback || typeof (callback) !== "function") throw new Error("Cannot map modules without a callback function.");
116
+ if (!moduleName) throw new Error("Cannot map modules in page without selecting the module to map.");
117
+ const fixPage = (page) => this.mapModulesFromPage(page, moduleName, callback);
118
+ await this.mapPages(fixPage, template);
119
+ };
120
+
121
+ async savePage(page) {
122
+ return await savePage(this._env, page);
123
+ };
124
+
125
+ async getPage(id) {
126
+ return await getPage(this._env, id);
127
+ };
128
+
129
+ async deletePage(id) {
130
+ return await deletePage(this._env, id);
131
+ };
132
+
133
+ async restorePage(id) {
134
+ return await restorePage(this._env, id);
135
+ };
136
+
137
+ async getStructuredDataByID(id) {
138
+ return await getStructuredDataByID(this._env, id);
85
139
  }
86
- };
87
-
88
- async mapModulesFromPage(page, moduleName, callback) {
89
- if (this._env.verbose) console.log(`\tMapping modules ${moduleName} with callback function ${callback.name}...`);
90
- if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
91
- if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
92
- if (!page || typeof (page) !== 'object') throw new Error('Page to map modules not found.');
93
- const protectedPage = JSON.parse(JSON.stringify(page));
94
- let found = false;
95
- const search = async (modulePiece) => {
96
- for (const key of Object.keys(modulePiece)) {
97
- const module = modulePiece[key];
98
- if (!module || typeof (module) !== 'object') continue;
99
- if (module.component === moduleName) {
100
- if (!found && this._env.verbose) console.log(`\tModule ${moduleName} found.`);
101
- const newModule = await callback(JSON.parse(JSON.stringify(module)));
102
- if (newModule) modulePiece[key] = newModule;
103
- found = true;
140
+
141
+ async getStructuredData(
142
+ contentType,
143
+ site = null,
144
+ language = null,
145
+ queryParams = {
146
+ page: 0,
147
+ itemsPerPage: 0,
148
+ pagination: false,
149
+ deleted: false,
150
+ includeDraft: true,
151
+ relatedFields: false,
152
+ query: null
104
153
  }
105
- await search(module);
106
- }
107
- };
108
- await search(protectedPage);
109
- if (!found && this._env.verbose) console.log(`\tModule ${moduleName} not found.`);
110
- return protectedPage;
111
- };
112
-
113
- async mapModules(callback, moduleName, template) {
114
- if (this._env.verbose) console.log(`\nMapping modules ${moduleName} from ${template || 'all'} pages with callback function ${callback.name}...`);
115
- if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
116
- if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
117
- const fixPage = (page) => this.mapModulesFromPage(page, moduleName, callback);
118
- await this.mapPages(fixPage, template);
119
- };
120
-
121
- async savePage(page) {
122
- return await savePage(this._env, page);
123
- };
124
-
125
- async getPage(id) {
126
- return await getPage(this._env, id);
127
- };
128
-
129
- async deletePage(id) {
130
- return await deletePage(this._env, id);
131
- };
132
-
133
- async restorePage(id) {
134
- return await restorePage(this._env, id);
135
- };
136
-
137
- async getStructuredDataByID(id) {
138
- return await getStructuredDataByID(this._env, id);
139
- }
140
-
141
- async getStructuredData(
142
- contentType,
143
- site = null,
144
- language = null,
145
- queryParams = {
146
- page: 0,
147
- itemsPerPage: 0,
148
- pagination: false,
149
- deleted: false,
150
- includeDraft: true,
151
- relatedFields: false,
152
- query: null,
153
- },
154
- ) {
155
- return await getStructuredData(this._env, contentType, site, language, queryParams);
156
- }
157
-
158
- async getPaginationStructuredData(
159
- contentType,
160
- site = null,
161
- language = null,
162
- queryParams = {
163
- page: 1,
164
- itemsPerPage: 50,
165
- pagination: true,
166
- deleted: false,
167
- includeDraft: false,
168
- relatedFields: false,
169
- query: null,
170
- },
171
- ) {
172
- return await getStructuredData(this._env, contentType, site, language, queryParams);
173
- }
174
-
175
- async saveStructuredData(structuredData) {
176
- return await saveStructuredData(this._env, structuredData);
177
- };
178
-
179
- async deleteStructuredData(id) {
180
- return await deleteStructuredData(this._env, id);
181
- };
182
-
183
- async mapStructuredData(callback, contentType) {
184
- if (this._env.verbose) console.log(`\nMapping ${contentType || 'all'} structured data with callback function ${callback.name}...`);
185
- if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map structured data without a callback function.');
186
- const content = await getStructuredData(this._env, contentType);
187
- if (!content?.length) {
188
- if (this._env.verbose) console.log('- No structured data found.');
189
- return;
154
+ ) {
155
+ return await getStructuredData(this._env, contentType, site, language, queryParams);
190
156
  }
191
- for (const structuredData of content) {
192
- if (this._env.verbose) console.log('- Mapping structured data', structuredData.id);
193
- const dataProtected = JSON.parse(JSON.stringify(structuredData));
194
- const result = await callback(dataProtected);
195
- if (result && JSON.stringify(structuredData) !== JSON.stringify(result)) {
196
- await saveStructuredData(this._env, result)
197
- }
157
+
158
+ async getPaginationStructuredData(
159
+ contentType,
160
+ site = null,
161
+ language = null,
162
+ queryParams = {
163
+ page: 1,
164
+ itemsPerPage: 50,
165
+ pagination: true,
166
+ deleted: false,
167
+ includeDraft: false,
168
+ relatedFields: false,
169
+ query: null,
170
+ related: null
171
+ }
172
+ ) {
173
+ return await (this._env, contentType, site, language, queryParams);
198
174
  }
199
- };
200
175
 
201
- async api(...params) {
202
- return await api(this._env, ...params);
203
- };
176
+ async saveStructuredData(structuredData) {
177
+ return await saveStructuredData(this._env, structuredData);
178
+ };
179
+
180
+ async deleteStructuredData(id) {
181
+ return await deleteStructuredData(this._env, id);
182
+ };
204
183
 
205
- showLog(...text) {
206
- console.log(`\t${text.join(' ')} `);
207
- };
184
+ async mapStructuredData(callback, contentType) {
185
+ if (this._env.verbose) console.log(`\nMapping ${contentType || "all"} structured data with callback function ${callback.name}...`);
186
+ if (!callback || typeof (callback) !== "function") throw new Error("Cannot map structured data without a callback function.");
187
+ const content = await getStructuredData(this._env, contentType);
188
+ if (!content?.length) {
189
+ if (this._env.verbose) console.log("- No structured data found.");
190
+ return;
191
+ }
192
+ for (const structuredData of content) {
193
+ if (this._env.verbose) console.log("- Mapping structured data", structuredData.id);
194
+ const dataProtected = JSON.parse(JSON.stringify(structuredData));
195
+ const result = await callback(dataProtected);
196
+ if (result && JSON.stringify(structuredData) !== JSON.stringify(result)) {
197
+ await saveStructuredData(this._env, result);
198
+ }
199
+ }
200
+ };
208
201
 
209
- async sendAlert(alert) {
210
- return await sendAlert(this._env, alert);
211
- };
202
+ async api(...params) {
203
+ return await api(this._env, ...params);
204
+ };
205
+
206
+ showLog(...text) {
207
+ console.log(`\t${text.join(" ")} `);
208
+ };
209
+
210
+ async sendAlert(alert) {
211
+ return await sendAlert(this._env, alert);
212
+ };
212
213
  };
213
214
 
214
215
  module.exports = SDK;
@@ -1,94 +1,102 @@
1
- const {api} = require('../api');
1
+ const { api } = require("../api");
2
2
 
3
3
  const getStructuredData = async (
4
- env,
5
- contentType,
6
- site = null,
7
- language = null,
8
- queryParams = {
9
- page: 0,
10
- itemsPerPage: 0,
11
- pagination: false,
12
- deleted: false,
13
- includeDraft: true,
14
- relatedFields: false,
15
- query: null,
16
- },
4
+ env,
5
+ contentType,
6
+ site = null,
7
+ language = null,
8
+ queryParams = {
9
+ page: 0,
10
+ itemsPerPage: 0,
11
+ pagination: false,
12
+ deleted: false,
13
+ includeDraft: true,
14
+ relatedFields: false,
15
+ query: null,
16
+ related: null
17
+ }
17
18
  ) => {
18
- const headers = {
19
- lang: language,
20
- };
19
+ const headers = {
20
+ lang: language
21
+ };
21
22
 
22
- if (!queryParams.pagination) {
23
- delete queryParams.page
24
- delete queryParams.itemsPerPage
25
- delete queryParams.deleted
26
- delete queryParams.relatedFields
27
- }
23
+ if (!queryParams.pagination) {
24
+ delete queryParams.page;
25
+ delete queryParams.itemsPerPage;
26
+ delete queryParams.deleted;
27
+ delete queryParams.relatedFields;
28
+ }
28
29
 
29
- if (!queryParams.query) {
30
- delete queryParams.query
31
- }
30
+ if (!queryParams.query) {
31
+ delete queryParams.query;
32
+ }
32
33
 
33
- const query = new URLSearchParams(queryParams).toString();
34
- const path = `${site ? `/site/${site}` : ''}/structured_data_contents/${contentType || ''}?${query}`
34
+ const query = new URLSearchParams(queryParams).toString();
35
+ const path = `${site ? `/site/${site}` : ""}/structured_data_contents/${contentType || ""}?${query}`;
35
36
 
36
- const items = await api(env, 'get', path, null, headers)
37
- .then(data => queryParams.pagination ? data : data.items);
38
- if (env.verbose) {
39
- console.log(`\tStructured data ${contentType} loaded.`);
40
- }
41
- return items;
37
+ const items = await api(env, "get", path, null, headers)
38
+ .then(data => {
39
+ if (queryParams.pagination) {
40
+ data.page = queryParams.page
41
+ return data
42
+ }
43
+
44
+ return data.items
45
+ });
46
+ if (env.verbose) {
47
+ console.log(`\tStructured data ${contentType} loaded.`);
48
+ }
49
+ return items;
42
50
  };
43
51
 
44
52
  const getStructuredDataByID = async (
45
- env,
46
- id
53
+ env,
54
+ id
47
55
  ) => {
48
- const headers = {};
49
- const path = `/structured_data_content/${id}`
56
+ const headers = {};
57
+ const path = `/structured_data_content/${id}`;
50
58
 
51
- const items = await api(env, 'get', path, null, headers)
52
- .then(data => data);
59
+ const items = await api(env, "get", path, null, headers)
60
+ .then(data => data);
53
61
 
54
- if (env.verbose) {
55
- console.log(`\tStructured data by content ID: ${id} loaded.`);
56
- }
62
+ if (env.verbose) {
63
+ console.log(`\tStructured data by content ID: ${id} loaded.`);
64
+ }
57
65
 
58
- return items;
59
- }
66
+ return items;
67
+ };
60
68
 
61
69
  const saveStructuredData = async (env, structuredData) => {
62
- const actions = {
63
- new: {
64
- method: 'post',
65
- endpoint: '/structured_data_content/',
66
- actionName: 'created',
67
- },
68
- update: {
69
- method: 'put',
70
- endpoint: `/structured_data_content/${structuredData.id}`,
71
- actionName: 'updated',
72
- },
73
- };
74
- const {
75
- method,
76
- endpoint,
77
- actionName,
78
- } = actions[structuredData.id ? 'update' : 'new'];
79
- const savedItem = await api(env, method, endpoint, structuredData);
80
- if (env.verbose) console.log(`\tStructured data ${structuredData?.content?.title || structuredData?.content?.label || 'untitled'} in ${structuredData.structuredData} ${actionName}.`);
81
- return savedItem;
70
+ const actions = {
71
+ new: {
72
+ method: "post",
73
+ endpoint: "/structured_data_content/",
74
+ actionName: "created"
75
+ },
76
+ update: {
77
+ method: "put",
78
+ endpoint: `/structured_data_content/${structuredData.id}`,
79
+ actionName: "updated"
80
+ }
81
+ };
82
+ const {
83
+ method,
84
+ endpoint,
85
+ actionName
86
+ } = actions[structuredData.id ? "update" : "new"];
87
+ const savedItem = await api(env, method, endpoint, structuredData);
88
+ if (env.verbose) console.log(`\tStructured data ${structuredData?.content?.title || structuredData?.content?.label || "untitled"} in ${structuredData.structuredData} ${actionName}.`);
89
+ return savedItem;
82
90
  };
83
91
 
84
92
  const deleteStructuredData = (env, id) => {
85
- if (env.verbose) console.log('\tDelete structured data', id);
86
- return api(env, 'delete', `/structured_data_content/${id}`);
93
+ if (env.verbose) console.log("\tDelete structured data", id);
94
+ return api(env, "delete", `/structured_data_content/${id}`);
87
95
  };
88
96
 
89
97
  module.exports = {
90
- getStructuredData,
91
- getStructuredDataByID,
92
- saveStructuredData,
93
- deleteStructuredData,
98
+ getStructuredData,
99
+ getStructuredDataByID,
100
+ saveStructuredData,
101
+ deleteStructuredData
94
102
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "griddo-sdk",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {