@terreno/api 0.0.1
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/LICENSE +202 -0
- package/README.md +170 -0
- package/biome.jsonc +22 -0
- package/bunfig.toml +4 -0
- package/dist/api.d.ts +227 -0
- package/dist/api.js +1024 -0
- package/dist/api.test.d.ts +1 -0
- package/dist/api.test.js +2143 -0
- package/dist/auth.d.ts +50 -0
- package/dist/auth.js +512 -0
- package/dist/auth.test.d.ts +1 -0
- package/dist/auth.test.js +778 -0
- package/dist/errors.d.ts +75 -0
- package/dist/errors.js +216 -0
- package/dist/example.d.ts +1 -0
- package/dist/example.js +118 -0
- package/dist/expressServer.d.ts +35 -0
- package/dist/expressServer.js +436 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/logger.d.ts +23 -0
- package/dist/logger.js +249 -0
- package/dist/middleware.d.ts +10 -0
- package/dist/middleware.js +52 -0
- package/dist/notifiers/googleChatNotifier.d.ts +5 -0
- package/dist/notifiers/googleChatNotifier.js +130 -0
- package/dist/notifiers/googleChatNotifier.test.d.ts +1 -0
- package/dist/notifiers/googleChatNotifier.test.js +260 -0
- package/dist/notifiers/index.d.ts +3 -0
- package/dist/notifiers/index.js +19 -0
- package/dist/notifiers/slackNotifier.d.ts +5 -0
- package/dist/notifiers/slackNotifier.js +130 -0
- package/dist/notifiers/slackNotifier.test.d.ts +1 -0
- package/dist/notifiers/slackNotifier.test.js +259 -0
- package/dist/notifiers/zoomNotifier.d.ts +34 -0
- package/dist/notifiers/zoomNotifier.js +181 -0
- package/dist/notifiers/zoomNotifier.test.d.ts +1 -0
- package/dist/notifiers/zoomNotifier.test.js +370 -0
- package/dist/openApi.d.ts +60 -0
- package/dist/openApi.js +441 -0
- package/dist/openApi.test.d.ts +1 -0
- package/dist/openApi.test.js +445 -0
- package/dist/openApiBuilder.d.ts +419 -0
- package/dist/openApiBuilder.js +424 -0
- package/dist/openApiBuilder.test.d.ts +1 -0
- package/dist/openApiBuilder.test.js +509 -0
- package/dist/openApiEtag.d.ts +7 -0
- package/dist/openApiEtag.js +38 -0
- package/dist/permissions.d.ts +26 -0
- package/dist/permissions.js +331 -0
- package/dist/permissions.test.d.ts +1 -0
- package/dist/permissions.test.js +413 -0
- package/dist/plugins.d.ts +67 -0
- package/dist/plugins.js +315 -0
- package/dist/plugins.test.d.ts +1 -0
- package/dist/plugins.test.js +639 -0
- package/dist/populate.d.ts +14 -0
- package/dist/populate.js +315 -0
- package/dist/populate.test.d.ts +1 -0
- package/dist/populate.test.js +133 -0
- package/dist/response.d.ts +0 -0
- package/dist/response.js +1 -0
- package/dist/tests/bunSetup.d.ts +1 -0
- package/dist/tests/bunSetup.js +297 -0
- package/dist/tests/index.d.ts +1 -0
- package/dist/tests/index.js +17 -0
- package/dist/tests.d.ts +99 -0
- package/dist/tests.js +273 -0
- package/dist/transformers.d.ts +25 -0
- package/dist/transformers.js +217 -0
- package/dist/transformers.test.d.ts +1 -0
- package/dist/transformers.test.js +370 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +143 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +14 -0
- package/index.ts +1 -0
- package/package.json +88 -0
- package/src/__snapshots__/openApi.test.ts.snap +4814 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +1485 -0
- package/src/api.test.ts +1661 -0
- package/src/api.ts +1036 -0
- package/src/auth.test.ts +550 -0
- package/src/auth.ts +408 -0
- package/src/errors.ts +225 -0
- package/src/example.ts +99 -0
- package/src/express.d.ts +5 -0
- package/src/expressServer.ts +387 -0
- package/src/index.ts +14 -0
- package/src/logger.ts +190 -0
- package/src/middleware.ts +18 -0
- package/src/notifiers/googleChatNotifier.test.ts +114 -0
- package/src/notifiers/googleChatNotifier.ts +47 -0
- package/src/notifiers/index.ts +3 -0
- package/src/notifiers/slackNotifier.test.ts +113 -0
- package/src/notifiers/slackNotifier.ts +55 -0
- package/src/notifiers/zoomNotifier.test.ts +207 -0
- package/src/notifiers/zoomNotifier.ts +111 -0
- package/src/openApi.test.ts +331 -0
- package/src/openApi.ts +494 -0
- package/src/openApiBuilder.test.ts +442 -0
- package/src/openApiBuilder.ts +636 -0
- package/src/openApiEtag.ts +40 -0
- package/src/permissions.test.ts +219 -0
- package/src/permissions.ts +228 -0
- package/src/plugins.test.ts +390 -0
- package/src/plugins.ts +289 -0
- package/src/populate.test.ts +65 -0
- package/src/populate.ts +258 -0
- package/src/response.ts +0 -0
- package/src/tests/bunSetup.ts +234 -0
- package/src/tests/index.ts +1 -0
- package/src/tests.ts +218 -0
- package/src/transformers.test.ts +202 -0
- package/src/transformers.ts +170 -0
- package/src/utils.test.ts +14 -0
- package/src/utils.ts +47 -0
- package/tsconfig.json +60 -0
- package/types.d.ts +17 -0
package/dist/populate.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.getOpenApiSpecForModel = getOpenApiSpecForModel;
|
|
54
|
+
exports.unpopulate = unpopulate;
|
|
55
|
+
var isArray_1 = __importDefault(require("lodash/isArray"));
|
|
56
|
+
var mongoose_to_swagger_1 = __importDefault(require("mongoose-to-swagger"));
|
|
57
|
+
var errors_1 = require("./errors");
|
|
58
|
+
var m2sOptions = {
|
|
59
|
+
props: ["readOnly", "required", "enum", "default"],
|
|
60
|
+
};
|
|
61
|
+
// This function filters an object to only include specified keys.
|
|
62
|
+
// It supports nested keys using dot notation (e.g., 'user.name').
|
|
63
|
+
// If no keys are provided, it returns the original object.
|
|
64
|
+
// The function recursively traverses the object structure to handle nested properties.
|
|
65
|
+
var filterKeys = function (obj, keysToKeep) {
|
|
66
|
+
if (!keysToKeep) {
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
var result = {};
|
|
70
|
+
var filterNestedKeys = function (currentObj, currentResult, remainingKeys) {
|
|
71
|
+
var currentKey = remainingKeys[0];
|
|
72
|
+
var nestedKeys = currentKey.split(".");
|
|
73
|
+
if (nestedKeys.length > 1) {
|
|
74
|
+
var _a = __read(nestedKeys), firstKey = _a[0], rest = _a.slice(1);
|
|
75
|
+
if (firstKey === "__proto__" || firstKey === "constructor" || firstKey === "prototype") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (!currentResult[firstKey]) {
|
|
79
|
+
currentResult[firstKey] = {};
|
|
80
|
+
}
|
|
81
|
+
filterNestedKeys(currentObj[firstKey], currentResult[firstKey], __spreadArray([
|
|
82
|
+
rest.join(".")
|
|
83
|
+
], __read(remainingKeys.slice(1)), false));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: we need to use the prototype to check if the object has the property
|
|
87
|
+
if (Object.prototype.hasOwnProperty.call(currentObj, currentKey)) {
|
|
88
|
+
currentResult[currentKey] = currentObj[currentKey];
|
|
89
|
+
}
|
|
90
|
+
if (remainingKeys.length > 1) {
|
|
91
|
+
filterNestedKeys(currentObj, currentResult, remainingKeys.slice(1));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
filterNestedKeys(obj, result, keysToKeep);
|
|
96
|
+
return result;
|
|
97
|
+
};
|
|
98
|
+
// Helper function to get the path in the OpenAPI schema, so we can swap out the type for the
|
|
99
|
+
// populated model component or generated type.
|
|
100
|
+
function getPathInSchema(schema, path) {
|
|
101
|
+
var _a;
|
|
102
|
+
var keys = path.split(".");
|
|
103
|
+
var currentSchema = schema;
|
|
104
|
+
var fullPath = "";
|
|
105
|
+
for (var i = 0; i < keys.length; i++) {
|
|
106
|
+
var key = keys[i];
|
|
107
|
+
if ((_a = currentSchema.properties) === null || _a === void 0 ? void 0 : _a[key]) {
|
|
108
|
+
fullPath += fullPath ? ".".concat(key) : key;
|
|
109
|
+
currentSchema = currentSchema.properties[key];
|
|
110
|
+
// If it's an array, add 'items' to the path
|
|
111
|
+
if (currentSchema.type === "array" && currentSchema.items) {
|
|
112
|
+
fullPath += ".items";
|
|
113
|
+
currentSchema = currentSchema.items;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (i === keys.length - 1 && currentSchema.type === "array") {
|
|
117
|
+
// If we're at the last key and it's an array, we don't need to add anything
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new Error("Path ".concat(path, " not found in schema at key ").concat(key));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return fullPath;
|
|
125
|
+
}
|
|
126
|
+
// Replaces populated properties with the populated schema.
|
|
127
|
+
function getOpenApiSpecForModel(model, _a) {
|
|
128
|
+
var e_1, _b, _c, e_2, _d, e_3, _e, e_4, _f;
|
|
129
|
+
var _g, _h, _j, _k;
|
|
130
|
+
var _l = _a === void 0 ? {} : _a, populatePaths = _l.populatePaths, extraModelProperties = _l.extraModelProperties;
|
|
131
|
+
var modelSwagger = (0, mongoose_to_swagger_1.default)(model, {
|
|
132
|
+
props: ["required", "enum"],
|
|
133
|
+
});
|
|
134
|
+
if (populatePaths && (0, isArray_1.default)(populatePaths)) {
|
|
135
|
+
try {
|
|
136
|
+
for (var populatePaths_1 = __values(populatePaths), populatePaths_1_1 = populatePaths_1.next(); !populatePaths_1_1.done; populatePaths_1_1 = populatePaths_1.next()) {
|
|
137
|
+
var populatePath = populatePaths_1_1.value;
|
|
138
|
+
// Get the referenced populate model from the model schema
|
|
139
|
+
var populateModel = (_h = (_g = model.schema.path(populatePath.path)) === null || _g === void 0 ? void 0 : _g.options) === null || _h === void 0 ? void 0 : _h.ref;
|
|
140
|
+
var populatePathIsArray = Array.isArray(model.schema.path(populatePath.path).options.type);
|
|
141
|
+
if (populatePathIsArray) {
|
|
142
|
+
populateModel = model.schema.path(populatePath.path).options.type[0].ref;
|
|
143
|
+
}
|
|
144
|
+
if (!populateModel) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
// Get the properties of the referenced model
|
|
148
|
+
var properties = filterKeys((0, mongoose_to_swagger_1.default)(model.db.model(populateModel), m2sOptions).properties, populatePath.fields);
|
|
149
|
+
// Get the OpenAPI path for the current populate path
|
|
150
|
+
var openApiPath = getPathInSchema(modelSwagger, populatePath.path);
|
|
151
|
+
// Determine the schema to set
|
|
152
|
+
var schemaToSet = void 0;
|
|
153
|
+
if (populatePath.openApiComponent) {
|
|
154
|
+
schemaToSet = {
|
|
155
|
+
$ref: "#/components/schemas/".concat(populatePath.openApiComponent),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
schemaToSet = {
|
|
160
|
+
properties: properties,
|
|
161
|
+
type: "object",
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
// Navigate through the nested structure and set the schema
|
|
165
|
+
var pathParts = openApiPath.split(".");
|
|
166
|
+
var currentSchema = modelSwagger.properties;
|
|
167
|
+
for (var i = 0; i < pathParts.length; i++) {
|
|
168
|
+
var part = pathParts[i];
|
|
169
|
+
if (i === pathParts.length - 1) {
|
|
170
|
+
// We're at the last part, merge the schema
|
|
171
|
+
if ((_j = currentSchema[part]) === null || _j === void 0 ? void 0 : _j.properties) {
|
|
172
|
+
currentSchema[part].properties = __assign(__assign({}, currentSchema[part].properties), (schemaToSet.properties || (_c = {}, _c[part] = schemaToSet, _c)));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
currentSchema[part] = schemaToSet;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// We're still navigating, ensure the path exists
|
|
180
|
+
if (!currentSchema[part]) {
|
|
181
|
+
currentSchema[part] = {};
|
|
182
|
+
}
|
|
183
|
+
if (part === "items" && i < pathParts.length - 1) {
|
|
184
|
+
// If we're at 'items' and it's not the last part, it should be an object
|
|
185
|
+
if (!currentSchema[part].properties) {
|
|
186
|
+
currentSchema[part] = { properties: {}, type: "object" };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
currentSchema = currentSchema[part].properties || currentSchema[part];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
195
|
+
finally {
|
|
196
|
+
try {
|
|
197
|
+
if (populatePaths_1_1 && !populatePaths_1_1.done && (_b = populatePaths_1.return)) _b.call(populatePaths_1);
|
|
198
|
+
}
|
|
199
|
+
finally { if (e_1) throw e_1.error; }
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
// Add virtuals to the modelSwagger property
|
|
204
|
+
for (var _m = __values(Object.keys(model.schema.virtuals)), _o = _m.next(); !_o.done; _o = _m.next()) {
|
|
205
|
+
var virtual = _o.value;
|
|
206
|
+
// Skip Mongoose internals
|
|
207
|
+
if (virtual === "id" || virtual === "__v") {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
modelSwagger.properties[virtual] = {
|
|
211
|
+
type: "any",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
216
|
+
finally {
|
|
217
|
+
try {
|
|
218
|
+
if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
|
|
219
|
+
}
|
|
220
|
+
finally { if (e_2) throw e_2.error; }
|
|
221
|
+
}
|
|
222
|
+
// Check subschemas for virtuals (one level deep)
|
|
223
|
+
if (model.schema.childSchemas.length > 0) {
|
|
224
|
+
try {
|
|
225
|
+
for (var _p = __values(model.schema.childSchemas), _q = _p.next(); !_q.done; _q = _p.next()) {
|
|
226
|
+
var childSchema = _q.value;
|
|
227
|
+
try {
|
|
228
|
+
for (var _r = (e_4 = void 0, __values(Object.keys(childSchema.schema.virtuals))), _s = _r.next(); !_s.done; _s = _r.next()) {
|
|
229
|
+
var virtual = _s.value;
|
|
230
|
+
if (virtual === "id" || virtual === "__v") {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
modelSwagger.properties[childSchema.model.path].properties[virtual] = {
|
|
234
|
+
type: "any",
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
239
|
+
finally {
|
|
240
|
+
try {
|
|
241
|
+
if (_s && !_s.done && (_f = _r.return)) _f.call(_r);
|
|
242
|
+
}
|
|
243
|
+
finally { if (e_4) throw e_4.error; }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
248
|
+
finally {
|
|
249
|
+
try {
|
|
250
|
+
if (_q && !_q.done && (_e = _p.return)) _e.call(_p);
|
|
251
|
+
}
|
|
252
|
+
finally { if (e_3) throw e_3.error; }
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
properties: __assign(__assign({}, modelSwagger.properties), extraModelProperties),
|
|
257
|
+
required: (_k = modelSwagger.required) !== null && _k !== void 0 ? _k : [],
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
// Helper function to unpopulate a document that has been populated.
|
|
261
|
+
// This is helpful for supporting backwards compatibility. E.g. you use populatePaths
|
|
262
|
+
// to populate a document but if the version header for the request is below the version
|
|
263
|
+
// that the populatePath was added, we remove the population and just return the _id.
|
|
264
|
+
function unpopulate(doc, path) {
|
|
265
|
+
if (!path) {
|
|
266
|
+
throw new errors_1.APIError({ status: 500, title: "path is required for unpopulate" });
|
|
267
|
+
}
|
|
268
|
+
var pathParts = path.split(".");
|
|
269
|
+
// Recursive because we need to support nested paths.
|
|
270
|
+
var recursiveUnpopulate = function (current, parts) {
|
|
271
|
+
var e_5, _a;
|
|
272
|
+
var _b;
|
|
273
|
+
var part = parts[0];
|
|
274
|
+
// If the path doesn't exist, return the original doc
|
|
275
|
+
if (!current[part]) {
|
|
276
|
+
return doc;
|
|
277
|
+
}
|
|
278
|
+
if (parts.length === 1) {
|
|
279
|
+
// Base case: we've reached the last part of the path
|
|
280
|
+
if (Array.isArray(current[part])) {
|
|
281
|
+
// If the field is an array, recursively unpopulate each element
|
|
282
|
+
current[part] = current[part].map(function (item) {
|
|
283
|
+
return (item === null || item === void 0 ? void 0 : item._id) ? item._id : item;
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else if ((_b = current[part]) === null || _b === void 0 ? void 0 : _b._id) {
|
|
287
|
+
// If the field is a populated document, revert to _id
|
|
288
|
+
current[part] = current[part]._id;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
// Recursive case: continue down the path
|
|
293
|
+
if (Array.isArray(current[part])) {
|
|
294
|
+
try {
|
|
295
|
+
for (var _c = __values(current[part]), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
296
|
+
var item = _d.value;
|
|
297
|
+
recursiveUnpopulate(item, parts.slice(1)); // Recursively handle each item in the array
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
301
|
+
finally {
|
|
302
|
+
try {
|
|
303
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
304
|
+
}
|
|
305
|
+
finally { if (e_5) throw e_5.error; }
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
recursiveUnpopulate(current[part], parts.slice(1)); // Recursively handle the next part
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return current;
|
|
313
|
+
};
|
|
314
|
+
return recursiveUnpopulate(doc, pathParts);
|
|
315
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
39
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
40
|
+
if (!m) return o;
|
|
41
|
+
var i = m.call(o), r, ar = [], e;
|
|
42
|
+
try {
|
|
43
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
44
|
+
}
|
|
45
|
+
catch (error) { e = { error: error }; }
|
|
46
|
+
finally {
|
|
47
|
+
try {
|
|
48
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
49
|
+
}
|
|
50
|
+
finally { if (e) throw e.error; }
|
|
51
|
+
}
|
|
52
|
+
return ar;
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
|
+
var bun_test_1 = require("bun:test");
|
|
56
|
+
var populate_1 = require("./populate");
|
|
57
|
+
var tests_1 = require("./tests");
|
|
58
|
+
(0, bun_test_1.describe)("populate functions", function () {
|
|
59
|
+
var admin;
|
|
60
|
+
var notAdmin;
|
|
61
|
+
var spinach;
|
|
62
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
63
|
+
var _a, _b;
|
|
64
|
+
return __generator(this, function (_c) {
|
|
65
|
+
switch (_c.label) {
|
|
66
|
+
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
67
|
+
case 1:
|
|
68
|
+
_a = __read.apply(void 0, [_c.sent(), 2]), admin = _a[0], notAdmin = _a[1];
|
|
69
|
+
return [4 /*yield*/, Promise.all([
|
|
70
|
+
tests_1.FoodModel.create({
|
|
71
|
+
calories: 1,
|
|
72
|
+
created: new Date("2021-12-03T00:00:20.000Z"),
|
|
73
|
+
eatenBy: [admin._id],
|
|
74
|
+
hidden: false,
|
|
75
|
+
likesIds: [
|
|
76
|
+
{ likes: true, userId: admin._id },
|
|
77
|
+
{ likes: false, userId: notAdmin._id },
|
|
78
|
+
],
|
|
79
|
+
name: "Spinach",
|
|
80
|
+
ownerId: admin._id,
|
|
81
|
+
source: {
|
|
82
|
+
name: "Brand",
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
])];
|
|
86
|
+
case 2:
|
|
87
|
+
_b = __read.apply(void 0, [_c.sent(), 1]), spinach = _b[0];
|
|
88
|
+
return [2 /*return*/];
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}); });
|
|
92
|
+
(0, bun_test_1.it)("unpopulate", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
93
|
+
var populated, unpopulated;
|
|
94
|
+
var _a, _b;
|
|
95
|
+
return __generator(this, function (_c) {
|
|
96
|
+
switch (_c.label) {
|
|
97
|
+
case 0: return [4 /*yield*/, spinach.populate("ownerId")];
|
|
98
|
+
case 1:
|
|
99
|
+
populated = _c.sent();
|
|
100
|
+
return [4 /*yield*/, populated.populate("eatenBy")];
|
|
101
|
+
case 2:
|
|
102
|
+
populated = _c.sent();
|
|
103
|
+
return [4 /*yield*/, populated.populate("likesIds.userId")];
|
|
104
|
+
case 3:
|
|
105
|
+
populated = _c.sent();
|
|
106
|
+
(0, bun_test_1.expect)(populated.ownerId.name).toBe("Admin");
|
|
107
|
+
(0, bun_test_1.expect)(populated.eatenBy[0].id).toBe(admin.id);
|
|
108
|
+
(0, bun_test_1.expect)(populated.eatenBy[0].name).toBe("Admin");
|
|
109
|
+
(0, bun_test_1.expect)(populated.likesIds[0].userId.id).toBe(admin.id);
|
|
110
|
+
(0, bun_test_1.expect)(populated.likesIds[0].userId.name).toBe("Admin");
|
|
111
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.id).toBe(notAdmin.id);
|
|
112
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.name).toBe("Not Admin");
|
|
113
|
+
unpopulated = (0, populate_1.unpopulate)(populated, "ownerId");
|
|
114
|
+
(0, bun_test_1.expect)(spinach.ownerId.name).toBeUndefined();
|
|
115
|
+
(0, bun_test_1.expect)(unpopulated.ownerId.toString()).toBe(admin.id);
|
|
116
|
+
// Ensure nothing else was touched.
|
|
117
|
+
(0, bun_test_1.expect)(populated.likesIds[0].userId.id).toBe(admin.id);
|
|
118
|
+
(0, bun_test_1.expect)(populated.likesIds[0].userId.name).toBe("Admin");
|
|
119
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.id).toBe(notAdmin.id);
|
|
120
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.name).toBe("Not Admin");
|
|
121
|
+
unpopulated = (0, populate_1.unpopulate)(populated, "eatenBy");
|
|
122
|
+
(0, bun_test_1.expect)(populated.eatenBy.toString()).toBe(admin.id);
|
|
123
|
+
(0, bun_test_1.expect)((_a = populated.eatenBy[0]) === null || _a === void 0 ? void 0 : _a.name).toBeUndefined();
|
|
124
|
+
unpopulated = (0, populate_1.unpopulate)(populated, "likesIds.userId");
|
|
125
|
+
(0, bun_test_1.expect)(populated.likesIds[0].userId.toString()).toBe(admin.id);
|
|
126
|
+
(0, bun_test_1.expect)((_b = populated.likesIds[0].userId) === null || _b === void 0 ? void 0 : _b.name).toBeUndefined();
|
|
127
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.toString()).toBe(notAdmin.id);
|
|
128
|
+
(0, bun_test_1.expect)(populated.likesIds[1].userId.name).toBeUndefined();
|
|
129
|
+
return [2 /*return*/];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); });
|
|
133
|
+
});
|
|
File without changes
|
package/dist/response.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|