griddo-sdk 1.2.6 → 1.2.7
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/examples/paginationStructuredData.js +50 -0
- package/examples/runTest.js +12 -0
- package/index.js +201 -201
- package/models/structuredData.js +78 -79
- package/package.json +4 -3
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const dotenv = require("dotenv");
|
|
2
|
+
const SDK = require('./../index');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Para probar el ejemplo hay que crearse un .env con las siguientes variables:
|
|
6
|
+
* - API_URL
|
|
7
|
+
* - PUBLIC_API_URL
|
|
8
|
+
* - USER_EMAIL
|
|
9
|
+
* - USER_PASSWORD
|
|
10
|
+
* - VERBOSE
|
|
11
|
+
*/
|
|
12
|
+
(async () => {
|
|
13
|
+
dotenv.config();
|
|
14
|
+
|
|
15
|
+
const sdk = new SDK();
|
|
16
|
+
|
|
17
|
+
const { API_URL, PUBLIC_API_URL, USER_EMAIL, USER_PASSWORD, VERBOSE } =
|
|
18
|
+
process.env;
|
|
19
|
+
|
|
20
|
+
if (!API_URL || !PUBLIC_API_URL || !USER_EMAIL || !USER_PASSWORD) {
|
|
21
|
+
throw new Error("Environment variables must be set");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Conectar al entorno
|
|
25
|
+
await sdk.connect({
|
|
26
|
+
api: API_URL,
|
|
27
|
+
publicApi: PUBLIC_API_URL,
|
|
28
|
+
user: USER_EMAIL,
|
|
29
|
+
password: USER_PASSWORD,
|
|
30
|
+
verbose: VERBOSE === "true",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const queryParams = {
|
|
34
|
+
page: 1,
|
|
35
|
+
itemsPerPage: 2,
|
|
36
|
+
pagination: true,
|
|
37
|
+
includeDraft: false,
|
|
38
|
+
relatedFields: true,
|
|
39
|
+
deleted: false,
|
|
40
|
+
query: "prueba",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const paginationStructuredData = await sdk.getPaginationStructuredData(
|
|
44
|
+
"ALUMNI",
|
|
45
|
+
"8",
|
|
46
|
+
"2",
|
|
47
|
+
queryParams,
|
|
48
|
+
);
|
|
49
|
+
console.log("Structured Data:", paginationStructuredData);
|
|
50
|
+
})();
|
package/index.js
CHANGED
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
const {
|
|
2
|
-
const {
|
|
3
|
-
const {
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} = require(
|
|
10
|
-
const {
|
|
11
|
-
const {
|
|
12
|
-
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');
|
|
13
13
|
|
|
14
14
|
class SDK {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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;
|
|
37
76
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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);
|
|
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
|
+
}
|
|
139
85
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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;
|
|
153
104
|
}
|
|
154
|
-
|
|
155
|
-
|
|
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
|
+
related: null,
|
|
171
|
+
},
|
|
172
|
+
) {
|
|
173
|
+
return await getStructuredData(this._env, contentType, site, language, queryParams);
|
|
174
|
+
}
|
|
175
|
+
|
|
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
|
+
};
|
|
183
|
+
|
|
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;
|
|
156
191
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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);
|
|
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
|
+
}
|
|
174
199
|
}
|
|
200
|
+
};
|
|
175
201
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
async deleteStructuredData(id) {
|
|
181
|
-
return await deleteStructuredData(this._env, id);
|
|
182
|
-
};
|
|
202
|
+
async api(...params) {
|
|
203
|
+
return await api(this._env, ...params);
|
|
204
|
+
};
|
|
183
205
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
};
|
|
206
|
+
showLog(...text) {
|
|
207
|
+
console.log(`\t${text.join(' ')} `);
|
|
208
|
+
};
|
|
201
209
|
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
};
|
|
210
|
+
async sendAlert(alert) {
|
|
211
|
+
return await sendAlert(this._env, alert);
|
|
212
|
+
};
|
|
213
213
|
};
|
|
214
214
|
|
|
215
215
|
module.exports = SDK;
|
package/models/structuredData.js
CHANGED
|
@@ -1,102 +1,101 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const {api} = require('../api');
|
|
2
2
|
|
|
3
3
|
const getStructuredData = async (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
},
|
|
18
18
|
) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const headers = {
|
|
20
|
+
lang: language,
|
|
21
|
+
};
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
if (!queryParams.pagination) {
|
|
24
|
+
delete queryParams.page
|
|
25
|
+
delete queryParams.itemsPerPage
|
|
26
|
+
delete queryParams.deleted
|
|
27
|
+
delete queryParams.relatedFields
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
if (!queryParams.query) {
|
|
31
|
+
delete queryParams.query
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const query = new URLSearchParams(queryParams).toString();
|
|
35
|
+
const path = `${site ? `/site/${site}` : ''}/structured_data_contents/${contentType || ''}?${query}`
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
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
|
+
return data.items
|
|
44
|
+
});
|
|
45
|
+
if (env.verbose) {
|
|
46
|
+
console.log(`\tStructured data ${contentType} loaded.`);
|
|
47
|
+
}
|
|
48
|
+
return items;
|
|
50
49
|
};
|
|
51
50
|
|
|
52
51
|
const getStructuredDataByID = async (
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
env,
|
|
53
|
+
id
|
|
55
54
|
) => {
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
const headers = {};
|
|
56
|
+
const path = `/structured_data_content/${id}`
|
|
58
57
|
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const items = await api(env, 'get', path, null, headers)
|
|
59
|
+
.then(data => data);
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
if (env.verbose) {
|
|
62
|
+
console.log(`\tStructured data by content ID: ${id} loaded.`);
|
|
63
|
+
}
|
|
65
64
|
|
|
66
|
-
|
|
67
|
-
}
|
|
65
|
+
return items;
|
|
66
|
+
}
|
|
68
67
|
|
|
69
68
|
const saveStructuredData = async (env, structuredData) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
69
|
+
const actions = {
|
|
70
|
+
new: {
|
|
71
|
+
method: 'post',
|
|
72
|
+
endpoint: '/structured_data_content/',
|
|
73
|
+
actionName: 'created',
|
|
74
|
+
},
|
|
75
|
+
update: {
|
|
76
|
+
method: 'put',
|
|
77
|
+
endpoint: `/structured_data_content/${structuredData.id}`,
|
|
78
|
+
actionName: 'updated',
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const {
|
|
82
|
+
method,
|
|
83
|
+
endpoint,
|
|
84
|
+
actionName,
|
|
85
|
+
} = actions[structuredData.id ? 'update' : 'new'];
|
|
86
|
+
const savedItem = await api(env, method, endpoint, structuredData);
|
|
87
|
+
if (env.verbose) console.log(`\tStructured data ${structuredData?.content?.title || structuredData?.content?.label || 'untitled'} in ${structuredData.structuredData} ${actionName}.`);
|
|
88
|
+
return savedItem;
|
|
90
89
|
};
|
|
91
90
|
|
|
92
91
|
const deleteStructuredData = (env, id) => {
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
if (env.verbose) console.log('\tDelete structured data', id);
|
|
93
|
+
return api(env, 'delete', `/structured_data_content/${id}`);
|
|
95
94
|
};
|
|
96
95
|
|
|
97
96
|
module.exports = {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
getStructuredData,
|
|
98
|
+
getStructuredDataByID,
|
|
99
|
+
saveStructuredData,
|
|
100
|
+
deleteStructuredData,
|
|
102
101
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "griddo-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test
|
|
7
|
+
"test:example": "node examples/runTest.js",
|
|
8
8
|
"release": "bumpp --commit --push --tag"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"axios": "0.26.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"bumpp": "^9.4.1"
|
|
24
|
+
"bumpp": "^9.4.1",
|
|
25
|
+
"dotenv": "^16.4.5"
|
|
25
26
|
}
|
|
26
27
|
}
|