griddo-sdk 1.0.0

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/api/index.js ADDED
@@ -0,0 +1,25 @@
1
+ const axios = require('axios');
2
+
3
+ const api = (env, method, endpoint, body = null, headers = {}) => {
4
+ const {
5
+ api,
6
+ token,
7
+ } = env;
8
+ if (!api) throw new Error('Class SDK should be initialized with connect method.');
9
+ const noBodyMethods = [
10
+ 'get',
11
+ 'delete',
12
+ ];
13
+ if (token) headers.authorization = token;
14
+ const params = [
15
+ `${api}${endpoint.startsWith('/') ? '' : '/'}${endpoint}`,
16
+ ...(noBodyMethods.includes(method.toLowerCase()) ? [] : [body]),
17
+ { headers },
18
+ ];
19
+ return axios[method](...params)
20
+ .then(response => response.data);
21
+ };
22
+
23
+ module.exports={
24
+ api,
25
+ };
package/index.js ADDED
@@ -0,0 +1,137 @@
1
+ const { api } = require('./api')
2
+ const { getToken } = require('./models/auth');
3
+ const { getPages, getPage, savePage, deletePage, restorePage } = require('./models/pages');
4
+ const { getStructuredData, saveStructuredData } = require('./models/structuredData');
5
+ const { getLanguages } = require('./models/languages');
6
+ const { getStatus } = require('./models/liveStatus');
7
+
8
+ class SDK {
9
+ constructor() {
10
+ this._env = {};
11
+ this.api = this.api.bind(this);
12
+ this.getPage = this.getPage.bind(this);
13
+ this.savePage = this.savePage.bind(this);
14
+ this.deletePage = this.deletePage.bind(this);
15
+ this.restorePage = this.restorePage.bind(this);
16
+ this.mapPages = this.mapPages.bind(this);
17
+ this.mapModulesFromPage = this.mapModulesFromPage.bind(this);
18
+ this.mapModules = this.mapModules.bind(this);
19
+ this.getStructuredData = this.getStructuredData.bind(this);
20
+ this.saveStructuredData = this.saveStructuredData.bind(this);
21
+ this.getLanguage = this.getLanguage.bind(this);
22
+ this.getDefaultLanguage = this.getDefaultLanguage.bind(this);
23
+ this.languages = [];
24
+ this.liveStatus = {};
25
+ }
26
+
27
+ async connect(environment) {
28
+ const {
29
+ api,
30
+ user,
31
+ password
32
+ } = environment;
33
+ if (!api || !user || !password) throw new Error('Environment JSON file should contain the keys api, user and password.');
34
+ this._env = {
35
+ api: api.endsWith('/') ? api.slice(0, -1) : api,
36
+ token: await getToken(environment),
37
+ };
38
+ this.languages = await getLanguages(this._env);
39
+
40
+ const liveStatus = await getStatus(this._env);
41
+ for (const item of liveStatus) this.liveStatus[item.status] = item.id;
42
+ };
43
+
44
+ getLanguage(key, value) {
45
+ return this.languages.find(item => item[key] === value);
46
+ };
47
+
48
+ getDefaultLanguage() {
49
+ return this.languages.find(item => item.isDefault);
50
+ };
51
+
52
+ async mapPages(callback, templateName) {
53
+ console.log(`\nMapping ${templateName || 'all'} pages with callback function ${callback.name}...`);
54
+ if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map pages without a callback function.');
55
+ const pages = await getPages(this._env, templateName);
56
+ if (!pages?.length) {
57
+ console.log('- No pages found.');
58
+ return;
59
+ }
60
+ for (const pageId of pages) {
61
+ console.log('- Mapping page', pageId);
62
+ const page = await getPage(this._env, pageId)
63
+ const pageProtected = JSON.parse(JSON.stringify(page));
64
+ const result = await callback(pageProtected);
65
+ if (result && JSON.stringify(page) !== JSON.stringify(result)) {
66
+ await savePage(this._env, result)
67
+ }
68
+ }
69
+ };
70
+
71
+ async mapModulesFromPage(page, moduleName, callback) {
72
+ console.log(`\tMapping modules ${moduleName} with callback function ${callback.name}...`);
73
+ if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
74
+ if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
75
+ if (!page || typeof (page) !== 'object') throw new Error('Page to map modules not found.');
76
+ const protectedPage = JSON.parse(JSON.stringify(page));
77
+ let found = false;
78
+ const search = async (modulePiece) => {
79
+ for (const key of Object.keys(modulePiece)) {
80
+ const module = modulePiece[key];
81
+ if (!module || typeof (module) !== 'object') continue;
82
+ if (module.component === moduleName) {
83
+ if (!found) console.log(`\tModule ${moduleName} found.`);
84
+ const newModule = await callback(JSON.parse(JSON.stringify(module)));
85
+ if (newModule) modulePiece[key] = newModule;
86
+ found = true;
87
+ }
88
+ await search(module);
89
+ }
90
+ };
91
+ await search(protectedPage);
92
+ if (!found) console.log(`\tModule ${moduleName} not found.`);
93
+ return protectedPage;
94
+ };
95
+
96
+ async mapModules(callback, moduleName, template) {
97
+ console.log(`\nMapping modules ${moduleName} from ${template || 'all'} pages with callback function ${callback.name}...`);
98
+ if (!callback || typeof (callback) !== 'function') throw new Error('Cannot map modules without a callback function.');
99
+ if (!moduleName) throw new Error('Cannot map modules in page without selecting the module to map.');
100
+ const fixPage = (page) => this.mapModulesFromPage(page, moduleName, callback);
101
+ await this.mapPages(fixPage, template);
102
+ };
103
+
104
+ async savePage(page) {
105
+ return await savePage(this._env, page);
106
+ };
107
+
108
+ async getPage(id) {
109
+ return await getPage(this._env, id);
110
+ };
111
+
112
+ async deletePage(id) {
113
+ return await deletePage(this._env, id);
114
+ };
115
+
116
+ async restorePage(id) {
117
+ return await restorePage(this._env, id);
118
+ };
119
+
120
+ async getStructuredData(id, site = null, language = null) {
121
+ return await getStructuredData(this._env, id, site, language);
122
+ };
123
+
124
+ async saveStructuredData(structuredData) {
125
+ return await saveStructuredData(this._env, structuredData);
126
+ };
127
+
128
+ async api(...params) {
129
+ return await api(this._env, ...params);
130
+ };
131
+
132
+ showLog(...text) {
133
+ console.log(`\t${text.join(' ')} `);
134
+ };
135
+ };
136
+
137
+ module.exports = SDK;
package/models/auth.js ADDED
@@ -0,0 +1,18 @@
1
+ const api = require('../api');
2
+
3
+ const getToken = async (params) => {
4
+ const {
5
+ api: apiKey,
6
+ user: email,
7
+ password,
8
+ } = params;
9
+ console.log('Logging to api...');
10
+ const token = await api({ api: apiKey }, 'post', '/login_check', { email, password })
11
+ .then(data => data.token);
12
+ console.log('Authenticated.')
13
+ return token;
14
+ };
15
+
16
+ module.exports = {
17
+ getToken,
18
+ };
@@ -0,0 +1,8 @@
1
+ const api = require('../api');
2
+
3
+ const getLanguages = env => api(env, 'get', '/languages')
4
+ .then(data => data.items);
5
+
6
+ module.exports = {
7
+ getLanguages,
8
+ };
@@ -0,0 +1,7 @@
1
+ const api = require('../api');
2
+
3
+ const getStatus = env => api(env, 'get', '/live-status');
4
+
5
+ module.exports={
6
+ getStatus,
7
+ };
@@ -0,0 +1,50 @@
1
+ const api = require('../api');
2
+
3
+ const getPages = (env, template = null) => api(env, 'get', `/pages/all/id/${template ? `?template=${template}` : ''}`);
4
+
5
+ const getPage = async (env, pageId) => {
6
+ const page = await api(env, 'get', `/page/${pageId}`)
7
+ console.log('\tLoaded page', page.title);
8
+ return page;
9
+ };
10
+
11
+ const savePage = async (env, page) => {
12
+ const actions = {
13
+ new: {
14
+ method: 'post',
15
+ endpoint: '/page/',
16
+ actionName: 'created',
17
+ },
18
+ update: {
19
+ method: 'put',
20
+ endpoint: `/page/${page.id}`,
21
+ actionName: 'updated',
22
+ },
23
+ };
24
+ const {
25
+ method,
26
+ endpoint,
27
+ actionName,
28
+ } = actions[page.id ? 'update' : 'new'];
29
+ const savedPage = await api(env, method, endpoint, page);
30
+ console.log(`\tPage ${actionName}.`);
31
+ return savedPage;
32
+ };
33
+
34
+ const deletePage = (env, id) => {
35
+ console.log('\tDelete page', id);
36
+ return api(env, 'delete', `/page/${id}`);
37
+ };
38
+
39
+ const restorePage = (env, id) => {
40
+ console.log('\tRestore page', id);
41
+ return api(env, 'delete', `/page/${id}/undo`);
42
+ };
43
+
44
+ module.exports = {
45
+ getPages,
46
+ getPage,
47
+ savePage,
48
+ deletePage,
49
+ restorePage,
50
+ };
@@ -0,0 +1,39 @@
1
+ const api = require('../api');
2
+
3
+ const getStructuredData = async (env, id, site = null, language = null) => {
4
+ const headers = {
5
+ lang: language,
6
+ };
7
+ const items = await api(env, 'get', `${site ? `/site/${site}` : ''}/structured_data_contents/${id}?pagination=false&includeDraft=true`, { headers })
8
+ .then(data => data.items);
9
+ console.log(`\tStructured data ${id} loaded.`);
10
+ return items;
11
+ };
12
+
13
+ const saveStructuredData = async (env, structuredData) => {
14
+ const actions = {
15
+ new: {
16
+ method: 'post',
17
+ endpoint: '/structured_data_content/',
18
+ actionName: 'created',
19
+ },
20
+ update: {
21
+ method: 'put',
22
+ endpoint: `/structured_data_content/${structuredData.id}`,
23
+ actionName: 'updated',
24
+ },
25
+ };
26
+ const {
27
+ method,
28
+ endpoint,
29
+ actionName,
30
+ } = actions[structuredData.id ? 'update' : 'new'];
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}.`);
33
+ return savedItem;
34
+ };
35
+
36
+ module.exports = {
37
+ getStructuredData,
38
+ saveStructuredData,
39
+ };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "griddo-sdk",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/griddo/griddo-sdk.git"
12
+ },
13
+ "author": "",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/griddo/griddo-sdk/issues"
17
+ },
18
+ "homepage": "https://github.com/griddo/griddo-sdk#readme",
19
+ "dependencies": {
20
+ "axios": "^0.26.0"
21
+ }
22
+ }