forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp-story-request-service.js
More file actions
126 lines (110 loc) · 3.77 KB
/
amp-story-request-service.js
File metadata and controls
126 lines (110 loc) · 3.77 KB
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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Services} from '../../../src/services';
import {childElementByTag} from '../../../src/dom';
import {getChildJsonConfig} from '../../../src/json';
import {isProtocolValid} from '../../../src/url';
import {once} from '../../../src/utils/function';
import {registerServiceBuilder} from '../../../src/service';
import {user, userAssert} from '../../../src/log';
/** @private @const {string} */
export const BOOKEND_CONFIG_ATTRIBUTE_NAME = 'src';
/** @private const {string} */
export const BOOKEND_CREDENTIALS_ATTRIBUTE_NAME = 'data-credentials';
/** @private @const {string} */
const TAG = 'amp-story-request-service';
/**
* Service to send XHRs.
*/
export class AmpStoryRequestService {
/**
* @param {!Window} win
* @param {!Element} storyElement
*/
constructor(win, storyElement) {
/** @private @const {!Element} */
this.storyElement_ = storyElement;
/** @private @const {!../../../src/service/xhr-impl.Xhr} */
this.xhr_ = Services.xhrFor(win);
/** @const @type {function():(!Promise<!JsonObject>|!Promise<null>)} */
this.loadBookendConfig = once(() => this.loadBookendConfigImpl_());
}
/**
* Retrieves the publisher bookend configuration, including the share
* providers.
* Has to be called through `loadBookendConfig`.
* @return {(!Promise<!JsonObject>|!Promise<null>)}
* @private
*/
loadBookendConfigImpl_() {
const bookendEl = childElementByTag(
this.storyElement_,
'amp-story-bookend'
);
if (!bookendEl) {
return Promise.resolve(null);
}
if (bookendEl.hasAttribute(BOOKEND_CONFIG_ATTRIBUTE_NAME)) {
const rawUrl = bookendEl.getAttribute(BOOKEND_CONFIG_ATTRIBUTE_NAME);
const credentials = bookendEl.getAttribute(
BOOKEND_CREDENTIALS_ATTRIBUTE_NAME
);
return this.executeRequest(rawUrl, credentials ? {credentials} : {});
}
// Fallback. Check for an inline json config.
let config = null;
try {
config = getChildJsonConfig(bookendEl);
} catch (err) {}
return Promise.resolve(config);
}
/**
* @param {string} rawUrl
* @param {Object=} opts
* @return {(!Promise<!JsonObject>|!Promise<null>)}
*/
executeRequest(rawUrl, opts = {}) {
if (!isProtocolValid(rawUrl)) {
user().error(TAG, 'Invalid config url.');
return Promise.resolve(null);
}
return Services.urlReplacementsForDoc(this.storyElement_)
.expandUrlAsync(user().assertString(rawUrl))
.then((url) => this.xhr_.fetchJson(url, opts))
.then((response) => {
userAssert(response.ok, 'Invalid HTTP response');
return response.json();
});
}
}
/**
* Util function to retrieve the request service. Ensures we can retrieve the
* service synchronously from the amp-story codebase without running into race
* conditions.
* @param {!Window} win
* @param {!Element} storyEl
* @return {!AmpStoryRequestService}
*/
export const getRequestService = (win, storyEl) => {
let service = Services.storyRequestService(win);
if (!service) {
service = new AmpStoryRequestService(win, storyEl);
registerServiceBuilder(win, 'story-request', function () {
return service;
});
}
return service;
};