-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
DefaultSheet.ts
181 lines (165 loc) 路 5.07 KB
/
DefaultSheet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
ErrorCode,
Extensible,
HandleRequest,
JovoError,
Log,
Plugin,
PluginConfig,
} from 'jovo-core';
import _merge = require('lodash.merge');
import { GoogleSheetsCMS } from './GoogleSheetsCMS';
export interface GoogleSheetsSheet extends PluginConfig {
name?: string;
range?: string;
spreadsheetId?: string;
type?: string;
access?: string;
entity?: string;
position?: number;
caching?: boolean;
}
export class DefaultSheet implements Plugin {
config: GoogleSheetsSheet = {
caching: true,
enabled: true,
name: undefined,
range: 'A:B',
};
cms?: GoogleSheetsCMS;
constructor(config?: GoogleSheetsSheet) {
if (config) {
this.config = _merge(this.config, config);
}
this.config.entity = this.config.entity || this.config.name;
}
install(extensible: Extensible) {
this.cms = extensible as GoogleSheetsCMS;
extensible.middleware('retrieve')!.use(this.retrieve.bind(this));
if (this.cms.config.caching === false || this.config.caching === false) {
this.cms.baseApp.middleware('request').use(this.retrieve.bind(this));
}
}
async retrieve(handleRequest: HandleRequest) {
if (!this.cms) {
return Promise.reject('No cms initialized.');
}
const spreadsheetId = this.config.spreadsheetId || this.cms.config.spreadsheetId;
if (!spreadsheetId) {
return Promise.reject(
new JovoError(
'spreadsheetId has to be set.',
ErrorCode.ERR_PLUGIN,
'jovo-cms-googlesheets',
'the spreadsheetId has to be defined in your config.js file',
undefined,
'https://www.jovo.tech/docs/cms/google-sheets#configuration',
),
);
}
if (!this.config.name) {
return Promise.reject(
new JovoError(
'sheet name has to be set.',
ErrorCode.ERR_PLUGIN,
'jovo-cms-googlesheets',
'the sheet name has to be defined in your config.js file',
undefined,
'https://www.jovo.tech/docs/cms/google-sheets#configuration',
),
);
}
if (!this.config.range) {
return Promise.reject(
new JovoError(
'range has to be set.',
ErrorCode.ERR_PLUGIN,
'jovo-cms-googlesheets',
'the range has to be defined in your config.js file',
undefined,
'https://www.jovo.tech/docs/cms/google-sheets#configuration',
),
);
}
let values: any[] = []; // tslint:disable-line
const access = this.config.access || this.cms.config.access || 'private';
if (access === 'private') {
Log.verbose('Retrieving private spreadsheet');
Log.verbose('Spreadsheet ID: ' + spreadsheetId);
Log.verbose('Sheet name: ' + this.config.name);
Log.verbose('Sheet range: ' + this.config.range);
values = await this.cms.loadPrivateSpreadsheetData(
spreadsheetId,
this.config.name,
this.config.range,
); // tslint:disable-line
} else if (access === 'public') {
Log.verbose('Retrieving public spreadsheet');
Log.verbose('Spreadsheet ID: ' + spreadsheetId);
Log.verbose('Sheet position: ' + this.config.position);
const publicValues = await this.cms.loadPublicSpreadsheetData(
spreadsheetId,
this.config.position,
); // tslint:disable-line
values = this.parsePublicToPrivate(publicValues);
}
if (values) {
this.parse(handleRequest, values);
}
}
parse(handleRequest: HandleRequest, values: any[]) {
// tslint:disable-line
if (!this.config.entity) {
throw new JovoError(
'entity has to be set.',
ErrorCode.ERR_PLUGIN,
'jovo-cms-googlesheets',
`The sheet's name has to be defined in your config.js file.`,
undefined,
'https://www.jovo.tech/docs/cms/google-sheets#configuration',
);
}
handleRequest.app.$cms[this.config.entity] = values;
}
/**
* Parses public spreadsheet json to a private spreadsheet format
* @param values
* @returns {any[]}
*/
private parsePublicToPrivate(values: any) {
// tslint:disable-line
const newValues: any[] = []; // tslint:disable-line
const entries = values.feed.entry;
const headers: string[] = [];
if (!entries) {
throw new JovoError(
'No spreadsheet values found.',
ErrorCode.ERR_PLUGIN,
'jovo-cms-googlesheets',
'It seems like your spreadsheet is empty or without values.',
);
}
entries.forEach((entry: any, index: number) => {
// tslint:disable-line
const row: string[] = [];
// get headers
if (index === 0) {
Object.keys(entry).forEach((key: string) => {
if (key.startsWith('gsx$')) {
headers.push(key.substr(4));
}
});
newValues.push(headers);
}
// get values
Object.keys(entry).forEach((key: string) => {
if (key.startsWith('gsx$')) {
const cell = entry[key];
row.push(cell['$t']); // tslint:disable-line:no-string-literal
}
});
newValues.push(row);
});
return newValues;
}
}