-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathapi.js
More file actions
153 lines (127 loc) · 4.65 KB
/
api.js
File metadata and controls
153 lines (127 loc) · 4.65 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
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
import { toInt } from '../utils/misc';
import { each, replace } from 'lodash';
const requestOptions = (verb, data, headers) => {
const _headers = Object.assign({
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
}, headers);
if (_headers['Content-Type'] === null)
delete _headers['Content-Type'];
var options = {
method: verb,
credentials: 'same-origin',
headers: _headers
}
if (verb !== 'GET')
options.body = data;
return options;
}
const post = (url, data, headers) => {
return fetch(url, requestOptions('POST', data, headers));
}
const put = (url, data, headers) => {
return fetch(url, requestOptions('PUT', data, headers));
}
const jsonPut = (url, data, headers) => {
return put(url, JSON.stringify(data), headers)
.then((response) => { return response.json() })
.then((data) => {
if (data.errors) throw(data.errors);
return data;
})
}
const get = (url, query, headers) => {
var url = new URL(url, window.location.origin);
Object.keys(query || {}).forEach(key => url.searchParams.append(key, query[key]));
return fetch(url.href, requestOptions('GET', null, headers));
}
const jsonGet = (url, query, headers) => {
return get(url, query, headers)
.then((response) => {
const headers = response.headers;
return response.json().then(json => { return { headers, json } });
});
}
// CONTENT
export function saveContent(url, site, page, locale) {
return jsonPut(url, {
content_locale: locale,
site: { sections_content: JSON.stringify(site.sectionsContent) },
page: {
title: page.title,
slug: page.slug,
listed: page.listed,
published: page.published,
seo_title: page.seo_title,
meta_keywords: page.meta_keywords,
meta_description: page.meta_description,
meta_robots: page.meta_robots,
sections_content: JSON.stringify(page.sectionsContent),
sections_dropzone_content: JSON.stringify(page.sectionsDropzoneContent)
}
});
}
export function loadContent(url, pageId, contentEntryId, locale) {
const _url = replace(url, /\/pages\/[0-9a-z]+\//, `/pages/${pageId}\/`) + '.json';
return jsonGet(_url, { content_locale: locale, content_entry_id: contentEntryId })
.then(response => ({ data: response.json.data, urls: response.json.urls }))
}
// SECTION
export function loadSectionHTML(url, section, content) {
return put(url,
JSON.stringify({ section_content: content }),
{ 'Locomotive-Section-Type': section.type }
).then(response => { return response.text(); })
}
// CONTENT ASSETS
export function uploadAssets(url, assets) {
var form = new FormData();
each(assets, asset => {
if (typeof(asset.name) == 'string')
form.append('content_assets[][source]', asset)
else
form.append('content_assets[][source]', asset.blob, asset.filename)
})
return post(url, form, {
'Content-Type': null
}).then(response => response.json())
}
export function loadAssets(url, options) {
return jsonGet(url, {
query: options.query || '',
page: options.pagination.page || 1,
per_page: options.pagination.perPage || 10
})
.then(response => {
return {
list: response.json,
pagination: {
page: toInt(response.headers.get('x-current-page')),
perPage: toInt(response.headers.get('x-per-page')),
totalPages: toInt(response.headers.get('x-total-pages')),
totalEntries: toInt(response.headers.get('x-total-entries'))
}
}
});
}
export function getThumbnail(url, imageUrl, format) {
var _imageUrl = new URL(imageUrl, window.location.origin);
return get(url, { image: _imageUrl, format }).
then(response => response.text())
}
// RESOURCES
export function searchForResources(url, locale, type, q, scope) {
return jsonGet(url, { content_locale: locale, q, type, scope })
.then(response => ({ list: response.json }));
}
export default function ApiFactory(urls, locale) {
return {
loadContent: (pageId, contentEntryId, locale) => loadContent(urls.load, pageId, contentEntryId, locale),
saveContent: (site, page, locale) => saveContent(urls.save, site, page, locale),
loadAssets: (options) => loadAssets(urls.assets, options),
uploadAssets: (assets) => uploadAssets(urls.bulkAssetUpload, assets),
getThumbnail: (imageUrl, format) => getThumbnail(urls.thumbnail, imageUrl, format),
searchForResources: (type, query, scope) => searchForResources(urls.resources, locale, type, query, scope),
loadSectionHTML: (sectionType, content) => loadSectionHTML(urls.preview , sectionType, content)
};
}