-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
125 lines (95 loc) · 3.9 KB
/
fetch.js
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _assign = require('babel-runtime/core-js/object/assign');
var _assign2 = _interopRequireDefault(_assign);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var pagedGet = function () {
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(client) {
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var page = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
var pageSize = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 100;
var aggregatedResponse = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : null;
var mergedOptions, response;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
mergedOptions = (0, _assign2.default)({
lang: '*'
}, options);
_context2.next = 3;
return client.query(query, (0, _extends3.default)({}, mergedOptions, {
page: page,
pageSize: pageSize
}));
case 3:
response = _context2.sent;
if (!aggregatedResponse) {
aggregatedResponse = response.results;
} else {
aggregatedResponse = aggregatedResponse.concat(response.results);
}
if (!(page * pageSize < response.total_results_size)) {
_context2.next = 7;
break;
}
return _context2.abrupt('return', pagedGet(client, query, options, page + 1, pageSize, aggregatedResponse));
case 7:
return _context2.abrupt('return', aggregatedResponse);
case 8:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
return function pagedGet(_x2) {
return _ref3.apply(this, arguments);
};
}();
var _prismicJavascript = require('prismic-javascript');
var _prismicJavascript2 = _interopRequireDefault(_prismicJavascript);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_ref2) {
var repositoryName = _ref2.repositoryName,
accessToken = _ref2.accessToken;
var apiEndpoint, client, documents;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
console.time(' Fetch Prismic data');
console.log('Starting to fetch all data from Prismic');
apiEndpoint = 'https://' + repositoryName + '.prismic.io/api/v2';
_context.next = 5;
return _prismicJavascript2.default.getApi(apiEndpoint, { accessToken: accessToken });
case 5:
client = _context.sent;
_context.next = 8;
return pagedGet(client);
case 8:
documents = _context.sent;
console.timeEnd(' Fetch Prismic data');
return _context.abrupt('return', {
documents: documents
});
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}();