-
Notifications
You must be signed in to change notification settings - Fork 11
/
ContentDeliveryAPI.ts
370 lines (336 loc) · 11.7 KB
/
ContentDeliveryAPI.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import Axios, { AxiosRequestConfig, Method, AxiosResponse, AxiosAdapter, AxiosPromise } from 'axios';
import AppConfig from './AppConfig';
import IContent from './Models/IContent';
import ContentLink, { ContentReference, ContentLinkService } from './Models/ContentLink';
import ActionResponse, { ResponseType } from './Models/ActionResponse';
import WebsiteList from './Models/WebsiteList';
import Website from './Models/Website';
import PathProvider from './PathProvider';
import Property from './Property';
export type PathResponse<T = any, C extends IContent = IContent> = C | ActionResponse<T, C>;
export type NetworkErrorData<T = any> = IContent & {
error: Property<T>;
}
export function PathResponseIsIContent(iContent: PathResponse): iContent is IContent {
if ((iContent as ActionResponse<any>).actionName) {
return false;
}
return true;
}
export function PathResponseIsActionResponse<P extends any = any>(actionResponse: PathResponse): actionResponse is ActionResponse<P>
{
if ((actionResponse as ActionResponse<P>).actionName) {
return true;
}
return false;
}
export function getIContentFromPathResponse<IContentType extends IContent = IContent>(response: PathResponse<any, IContentType>) : IContentType | null
{
if (PathResponseIsActionResponse(response)) {
return response.currentContent;
}
if (PathResponseIsIContent(response)) {
return response;
}
return null;
}
/**
* ContentDelivery API Wrapper
*
* @deprecated
*/
export class ContentDeliveryAPI {
protected config: AppConfig;
protected componentService: string = '/api/episerver/v2.0/content/';
protected websiteService: string = '/api/episerver/v3/site/';
protected methodService: string = '/api/episerver/v3/action/';
protected debug: boolean = false;
protected pathProvider: PathProvider;
/**
* Marker to keep if we're in edit mode
*/
protected inEditMode: boolean = false;
/**
* Internal cache of the websites retrieved from the ContentDelivery API
*
* @private
*/
private websites!: WebsiteList;
/**
* Internal cache of the current website, as retrieved from the ContentDelivery API
*
* @private
*/
private website!: Website;
/**
* ContentDelivery API Wrapper
*
* @deprecated
*/
constructor(pathProvider: PathProvider, config: AppConfig) {
this.pathProvider = pathProvider;
this.config = config;
this.debug = this.config.enableDebug === true;
}
public get currentPathProvider() : PathProvider
{
return this.pathProvider;
}
public get currentConfig() : AppConfig
{
return this.config;
}
public isInEditMode(): boolean {
return this.inEditMode;
}
public setInEditMode(editMode: boolean): ContentDeliveryAPI {
this.inEditMode = editMode === true;
return this;
}
public isDisabled(): boolean {
return this.config.noAjax === true;
}
/**
* Invoke an ASP.Net MVC controller method using the generic content API. This is intended
* to be used only when attaching a SPA to an existing code-base.
*
* @param content The content for which the controller must be loaded
* @param method The (case sensitive) method name to invoke on the controller
* @param verb The HTTP verb to use when invoking the controller
* @param data The data (if any) to send to the controller for the method
*/
public async invokeControllerMethod(
content: ContentLink,
method: string,
verb?: Method,
data?: object,
): Promise<any> {
let options = this.getRequestSettings(verb);
options.data = data;
return await this.doRequest<any>(this.getMethodServiceUrl(content, method), options);
}
/**
* Strongly typed variant of invokeControllerMethod
*
* @see invokeControllerMethod()
* @param content The content for which the controller must be loaded
* @param method The (case sensitive) method name to invoke on the controller
* @param verb The HTTP verb to use when invoking the controller
* @param data The data (if any) to send to the controller for the method
*/
public async invokeTypedControllerMethod<TypeOut, TypeIn>(
content: ContentLink,
method: string,
verb?: Method,
data?: TypeIn,
): Promise<ActionResponse<TypeOut>> {
let options = this.getRequestSettings(verb);
options.data = data;
return await this.doRequest<ActionResponse<TypeOut>>(this.getMethodServiceUrl(content, method), options);
}
/**
* Retrieve a list of all websites registered within Episerver
*/
public async getWebsites(): Promise<WebsiteList> {
if (!this.websites) {
this.websites = await this.doRequest<WebsiteList>(this.config.epiBaseUrl + this.websiteService);
}
return this.websites;
}
/**
* Retrieve the first website registered within Episerver
*/
public async getWebsite(): Promise<Website> {
const list = await this.getWebsites();
return list[0];
}
public async getContent(content: ContentLink, forceGuid: boolean = false): Promise<IContent | null> {
if (!(content && (content.guidValue || content.url))) {
if (this.config.enableDebug) {
console.warn('Loading content for an empty reference ', content);
}
return null;
}
let useGuid = content.guidValue ? this.config.preferGuid || forceGuid : false;
let serviceUrl: URL;
if (useGuid) {
serviceUrl = new URL(this.config.epiBaseUrl + this.componentService + content.guidValue);
} else {
try {
serviceUrl = new URL(
this.config.epiBaseUrl +
(content.url ? content.url : this.componentService + ContentLinkService.createApiId(content)),
);
} catch (e) {
serviceUrl = new URL(this.config.epiBaseUrl + this.componentService + ContentLinkService.createApiId(content));
}
}
//serviceUrl.searchParams.append('currentPageUrl', this.pathProvider.getCurrentPath());
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
return this.doRequest<PathResponse>(serviceUrl.href).catch((r) => {
return this.buildNetworkError(r);
}).then(r => getIContentFromPathResponse(r));
}
public async getContentsByRefs(refs: Array<string>): Promise<Array<IContent>> {
if (!refs || refs.length == 0) {
return Promise.resolve<Array<IContent>>([]);
}
let serviceUrl: URL = new URL(this.config.epiBaseUrl + this.componentService);
serviceUrl.searchParams.append('references', refs.join(','));
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
return this.doRequest<Array<IContent>>(serviceUrl.href).catch((r) => {
return [];
});
}
public async getContentByRef(ref: string): Promise<IContent> {
let serviceUrl: URL = new URL(this.config.epiBaseUrl + this.componentService + ref);
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
return this.doRequest<IContent>(serviceUrl.href).catch((r) => {
return this.buildNetworkError(r);
});
}
public async getContentByPath(path: string): Promise<PathResponse> {
let serviceUrl: URL = new URL(this.config.epiBaseUrl + path);
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
//serviceUrl.searchParams.append('currentPageUrl', this.pathProvider.getCurrentPath());
return this.doRequest<PathResponse>(serviceUrl.href).catch((r) => {
return this.buildNetworkError(r, path);
});
}
public async getContentChildren<T extends IContent>(id: ContentReference): Promise<Array<T>> {
let itemId: string = ContentLinkService.createApiId(id);
let serviceUrl: URL = new URL(this.config.epiBaseUrl + this.componentService + itemId + '/children');
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
return this.doRequest<Array<T>>(serviceUrl.href).catch((r) => {
return [];
});
}
public async getContentAncestors(link: ContentReference): Promise<Array<IContent>> {
let itemId: string = ContentLinkService.createApiId(link);
let serviceUrl: URL = new URL(`${this.config.epiBaseUrl}${this.componentService}${itemId}/ancestors`);
if (this.config.autoExpandRequests) {
serviceUrl.searchParams.append('expand', '*');
}
return this.doRequest<Array<IContent>>(serviceUrl.href).catch((r) => {
return [];
});
}
/**
* Perform the actual request
*
* @param url The URL to request the data from
* @param options The Request options to use
*/
protected async doRequest<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
if (this.isDisabled()) {
return Promise.reject('The Content Delivery API has been disabled');
}
if (this.isInEditMode()) {
let urlObj = new URL(url);
urlObj.searchParams.append('epieditmode', 'True');
//Add channel...
//Add project...
urlObj.searchParams.append('preventCache', Math.round(Math.random() * 100000000).toString());
url = urlObj.href;
}
options = options ? options : this.getRequestSettings();
if (this.debug) console.debug('Requesting: ' + url);
options.url = url;
return Axios.request<any, AxiosResponse<T>>(options)
.then((response) => {
if (this.debug) console.debug(`Response from ${url}:`, response.data);
return response.data;
})
.catch((reason) => {
if (this.debug) console.error(`Response from ${url}: HTTP Fetch error `, reason);
throw reason;
});
}
protected getMethodServiceUrl(content: ContentLink, method: string): string {
let contentUrl: string = this.config.epiBaseUrl;
contentUrl = contentUrl + this.methodService;
contentUrl = contentUrl + content.guidValue + '/' + method;
return contentUrl;
}
/**
* Build the request parameters needed to perform the call to the Content Delivery API
*
* @param verb The verb for the generated configuration
*/
protected getRequestSettings(verb?: Method): AxiosRequestConfig {
let options: AxiosRequestConfig = {
method: verb ? verb : 'get',
baseURL: this.config.epiBaseUrl,
withCredentials: true,
headers: { ...this.getHeaders() },
transformRequest: [
(data: any, headers: any) => {
if (data) {
headers['Content-Type'] = 'application/json';
return JSON.stringify(data);
}
return data;
},
],
responseType: 'json',
};
if (this.config.networkAdapter) {
options.adapter = this.config.networkAdapter;
}
return options;
}
protected getHeaders(customHeaders?: object): object {
let defaultHeaders = {
Accept: 'application/json',
'Accept-Language': this.config.defaultLanguage, //@ToDo: Convert to context call, with default
};
if (!customHeaders) return defaultHeaders;
return {
...defaultHeaders,
...customHeaders,
};
}
public static IsActionResponse(response: PathResponse): response is ActionResponse<any> {
if (
response &&
(response as ActionResponse<any>).responseType &&
(response as ActionResponse<any>).responseType == ResponseType.ActionResult
) {
return true;
}
return false;
}
private counter: number = 0;
protected buildNetworkError(reason: any, path: string = ''): NetworkErrorData {
const errorId = ++this.counter;
return {
name: {
propertyDataType: 'String',
value: 'Error',
},
contentType: ['Errors', 'NetworkError'],
contentLink: {
guidValue: '',
id: errorId,
providerName: 'ContentDeliveryAPI_Errors',
url: path,
workId: 0,
},
error: {
propertyDataType: 'Unknown',
value: '', //reason,
},
};
}
}
export default ContentDeliveryAPI;