@mountainpass/waycharter 2.0.27 → 2.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ This library is compatible with Node.js 10.x, 12.x and 14.x
|
|
|
13
13
|
|
|
14
14
|
<!-- [](https://www.codacy.com/gh/mountain-pass/waycharter/dashboard?utm_source=github.com&utm_medium=referral&utm_content=mountain-pass/waycharter&utm_campaign=Badge_Grade) [](https://www.codacy.com/gh/mountain-pass/waycharter/dashboard?utm_source=github.com&utm_medium=referral&utm_content=mountain-pass/waycharter&utm_campaign=Badge_Coverage) -->
|
|
15
15
|
|
|
16
|
-
[](https://snyk.io/test/github/mountain-pass/waycharter) [](https://snyk.io/test/github/mountain-pass/waycharter) [](https://snyk.io/test/npm/@mountainpass/waycharter/2.0.28)
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
[](https://conventionalcommits.org) [](https://github.com/prettier/prettier)
|
|
@@ -57,6 +57,17 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
57
57
|
}
|
|
58
58
|
return t;
|
|
59
59
|
};
|
|
60
|
+
var __values = (this && this.__values) || function(o) {
|
|
61
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
62
|
+
if (m) return m.call(o);
|
|
63
|
+
if (o && typeof o.length === "number") return {
|
|
64
|
+
next: function () {
|
|
65
|
+
if (o && i >= o.length) o = void 0;
|
|
66
|
+
return { value: o && o[i++], done: !o };
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
70
|
+
};
|
|
60
71
|
var __read = (this && this.__read) || function (o, n) {
|
|
61
72
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
73
|
if (!m) return o;
|
|
@@ -121,9 +132,10 @@ var WayCharter = /** @class */ (function () {
|
|
|
121
132
|
WayCharter.prototype.registerCollection = function (_a) {
|
|
122
133
|
var _this = this;
|
|
123
134
|
var itemPath = _a.itemPath, itemLoader = _a.itemLoader, collectionPath = _a.collectionPath, collectionLoader = _a.collectionLoader, _b = _a.filters, filters = _b === void 0 ? [] : _b;
|
|
124
|
-
|
|
135
|
+
// Register item endpoint if provided
|
|
136
|
+
var itemPathTemplate;
|
|
125
137
|
if (itemPath !== undefined && itemLoader !== undefined) {
|
|
126
|
-
itemEndpoint = waycharter_1.EndPoint.create({
|
|
138
|
+
var itemEndpoint = waycharter_1.EndPoint.create({
|
|
127
139
|
router: this.router,
|
|
128
140
|
path: "".concat(collectionPath).concat(itemPath),
|
|
129
141
|
handler: function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
@@ -140,36 +152,82 @@ var WayCharter = /** @class */ (function () {
|
|
|
140
152
|
});
|
|
141
153
|
}); }
|
|
142
154
|
});
|
|
155
|
+
itemPathTemplate = itemEndpoint.pathTemplate;
|
|
143
156
|
}
|
|
144
157
|
var additionalPaths = filters.map(function (f) { return ({
|
|
145
158
|
rel: f.rel,
|
|
146
159
|
uri: "".concat(collectionPath, "{?").concat(f.parameters.join(','), "}")
|
|
147
160
|
}); });
|
|
148
|
-
|
|
161
|
+
// Use EndPoint.create (not createCollection) so we can generate
|
|
162
|
+
// per-item links like v1 did, instead of v2's template links
|
|
163
|
+
var collectionEndpoint = waycharter_1.EndPoint.create({
|
|
149
164
|
router: this.router,
|
|
150
165
|
path: collectionPath,
|
|
151
|
-
filters: filters,
|
|
152
|
-
itemEndpoint: itemEndpoint,
|
|
153
166
|
handler: function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
154
|
-
var pageInt, result, hasMore, loaderLinks, rest,
|
|
155
|
-
var
|
|
156
|
-
|
|
157
|
-
|
|
167
|
+
var page, pageInt, filteredQuery, allowedParameters, _c, _d, _e, key, value, result, hasMore, loaderLinks, rest, body, itemLinks, canonicalLinks, index, queryString, paginationLinks, nextParameters, previousParameters;
|
|
168
|
+
var e_1, _f;
|
|
169
|
+
var queryParameters = _b.queryParameters, response = _b.response;
|
|
170
|
+
return __generator(this, function (_g) {
|
|
171
|
+
switch (_g.label) {
|
|
158
172
|
case 0:
|
|
173
|
+
page = typeof queryParameters.page === 'string' ? queryParameters.page : undefined;
|
|
159
174
|
pageInt = Number.parseInt(page || '0');
|
|
160
|
-
|
|
175
|
+
filteredQuery = {};
|
|
176
|
+
allowedParameters = new Set(filters.flatMap(function (f) { return f.parameters; }));
|
|
177
|
+
try {
|
|
178
|
+
for (_c = __values(Object.entries(queryParameters)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
179
|
+
_e = __read(_d.value, 2), key = _e[0], value = _e[1];
|
|
180
|
+
if (allowedParameters.has(key) && typeof value === 'string') {
|
|
181
|
+
filteredQuery[key] = value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
186
|
+
finally {
|
|
187
|
+
try {
|
|
188
|
+
if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
|
|
189
|
+
}
|
|
190
|
+
finally { if (e_1) throw e_1.error; }
|
|
191
|
+
}
|
|
192
|
+
return [4 /*yield*/, collectionLoader(__assign({ page: pageInt }, filteredQuery))];
|
|
161
193
|
case 1:
|
|
162
|
-
result =
|
|
194
|
+
result = _g.sent();
|
|
163
195
|
hasMore = result.hasMore, loaderLinks = result.links, rest = __rest(result, ["hasMore", "links"]);
|
|
164
|
-
|
|
196
|
+
body = rest.body;
|
|
197
|
+
itemLinks = [];
|
|
198
|
+
canonicalLinks = [];
|
|
199
|
+
if (Array.isArray(body)) {
|
|
200
|
+
for (index = 0; index < body.length; index++) {
|
|
201
|
+
itemLinks.push({ rel: 'item', uri: "#/".concat(index) });
|
|
202
|
+
if (itemPathTemplate) {
|
|
203
|
+
canonicalLinks.push({
|
|
204
|
+
rel: 'canonical',
|
|
205
|
+
uri: itemPathTemplate,
|
|
206
|
+
anchor: "#/".concat(index)
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
queryString = new URLSearchParams(filteredQuery).toString();
|
|
212
|
+
paginationLinks = [{
|
|
213
|
+
rel: 'first',
|
|
214
|
+
uri: queryString ? "".concat(collectionPath, "?").concat(queryString) : collectionPath
|
|
215
|
+
}];
|
|
216
|
+
if (hasMore) {
|
|
217
|
+
nextParameters = new URLSearchParams(__assign({ page: String(pageInt + 1) }, filteredQuery)).toString();
|
|
218
|
+
paginationLinks.push({ rel: 'next', uri: "".concat(collectionPath, "?").concat(nextParameters) });
|
|
219
|
+
}
|
|
165
220
|
if (pageInt === 1) {
|
|
166
|
-
|
|
167
|
-
previousLinks.push({
|
|
221
|
+
paginationLinks.push({
|
|
168
222
|
rel: 'prev',
|
|
169
223
|
uri: queryString ? "".concat(collectionPath, "?").concat(queryString) : collectionPath
|
|
170
224
|
});
|
|
171
225
|
}
|
|
172
|
-
|
|
226
|
+
else if (pageInt > 1) {
|
|
227
|
+
previousParameters = new URLSearchParams(__assign({ page: String(pageInt - 1) }, filteredQuery)).toString();
|
|
228
|
+
paginationLinks.push({ rel: 'prev', uri: "".concat(collectionPath, "?").concat(previousParameters) });
|
|
229
|
+
}
|
|
230
|
+
response.chart(__assign(__assign({}, rest), { links: __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(itemLinks), false), __read(canonicalLinks), false), __read(paginationLinks), false), __read((loaderLinks || [])), false) }));
|
|
173
231
|
return [2 /*return*/];
|
|
174
232
|
}
|
|
175
233
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"waycharter-convenience.js","sourceRoot":"","sources":["../src/waycharter-convenience.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"waycharter-convenience.js","sourceRoot":"","sources":["../src/waycharter-convenience.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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