@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.
Files changed (119) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +170 -0
  3. package/biome.jsonc +22 -0
  4. package/bunfig.toml +4 -0
  5. package/dist/api.d.ts +227 -0
  6. package/dist/api.js +1024 -0
  7. package/dist/api.test.d.ts +1 -0
  8. package/dist/api.test.js +2143 -0
  9. package/dist/auth.d.ts +50 -0
  10. package/dist/auth.js +512 -0
  11. package/dist/auth.test.d.ts +1 -0
  12. package/dist/auth.test.js +778 -0
  13. package/dist/errors.d.ts +75 -0
  14. package/dist/errors.js +216 -0
  15. package/dist/example.d.ts +1 -0
  16. package/dist/example.js +118 -0
  17. package/dist/expressServer.d.ts +35 -0
  18. package/dist/expressServer.js +436 -0
  19. package/dist/index.d.ts +14 -0
  20. package/dist/index.js +30 -0
  21. package/dist/logger.d.ts +23 -0
  22. package/dist/logger.js +249 -0
  23. package/dist/middleware.d.ts +10 -0
  24. package/dist/middleware.js +52 -0
  25. package/dist/notifiers/googleChatNotifier.d.ts +5 -0
  26. package/dist/notifiers/googleChatNotifier.js +130 -0
  27. package/dist/notifiers/googleChatNotifier.test.d.ts +1 -0
  28. package/dist/notifiers/googleChatNotifier.test.js +260 -0
  29. package/dist/notifiers/index.d.ts +3 -0
  30. package/dist/notifiers/index.js +19 -0
  31. package/dist/notifiers/slackNotifier.d.ts +5 -0
  32. package/dist/notifiers/slackNotifier.js +130 -0
  33. package/dist/notifiers/slackNotifier.test.d.ts +1 -0
  34. package/dist/notifiers/slackNotifier.test.js +259 -0
  35. package/dist/notifiers/zoomNotifier.d.ts +34 -0
  36. package/dist/notifiers/zoomNotifier.js +181 -0
  37. package/dist/notifiers/zoomNotifier.test.d.ts +1 -0
  38. package/dist/notifiers/zoomNotifier.test.js +370 -0
  39. package/dist/openApi.d.ts +60 -0
  40. package/dist/openApi.js +441 -0
  41. package/dist/openApi.test.d.ts +1 -0
  42. package/dist/openApi.test.js +445 -0
  43. package/dist/openApiBuilder.d.ts +419 -0
  44. package/dist/openApiBuilder.js +424 -0
  45. package/dist/openApiBuilder.test.d.ts +1 -0
  46. package/dist/openApiBuilder.test.js +509 -0
  47. package/dist/openApiEtag.d.ts +7 -0
  48. package/dist/openApiEtag.js +38 -0
  49. package/dist/permissions.d.ts +26 -0
  50. package/dist/permissions.js +331 -0
  51. package/dist/permissions.test.d.ts +1 -0
  52. package/dist/permissions.test.js +413 -0
  53. package/dist/plugins.d.ts +67 -0
  54. package/dist/plugins.js +315 -0
  55. package/dist/plugins.test.d.ts +1 -0
  56. package/dist/plugins.test.js +639 -0
  57. package/dist/populate.d.ts +14 -0
  58. package/dist/populate.js +315 -0
  59. package/dist/populate.test.d.ts +1 -0
  60. package/dist/populate.test.js +133 -0
  61. package/dist/response.d.ts +0 -0
  62. package/dist/response.js +1 -0
  63. package/dist/tests/bunSetup.d.ts +1 -0
  64. package/dist/tests/bunSetup.js +297 -0
  65. package/dist/tests/index.d.ts +1 -0
  66. package/dist/tests/index.js +17 -0
  67. package/dist/tests.d.ts +99 -0
  68. package/dist/tests.js +273 -0
  69. package/dist/transformers.d.ts +25 -0
  70. package/dist/transformers.js +217 -0
  71. package/dist/transformers.test.d.ts +1 -0
  72. package/dist/transformers.test.js +370 -0
  73. package/dist/utils.d.ts +11 -0
  74. package/dist/utils.js +143 -0
  75. package/dist/utils.test.d.ts +1 -0
  76. package/dist/utils.test.js +14 -0
  77. package/index.ts +1 -0
  78. package/package.json +88 -0
  79. package/src/__snapshots__/openApi.test.ts.snap +4814 -0
  80. package/src/__snapshots__/openApiBuilder.test.ts.snap +1485 -0
  81. package/src/api.test.ts +1661 -0
  82. package/src/api.ts +1036 -0
  83. package/src/auth.test.ts +550 -0
  84. package/src/auth.ts +408 -0
  85. package/src/errors.ts +225 -0
  86. package/src/example.ts +99 -0
  87. package/src/express.d.ts +5 -0
  88. package/src/expressServer.ts +387 -0
  89. package/src/index.ts +14 -0
  90. package/src/logger.ts +190 -0
  91. package/src/middleware.ts +18 -0
  92. package/src/notifiers/googleChatNotifier.test.ts +114 -0
  93. package/src/notifiers/googleChatNotifier.ts +47 -0
  94. package/src/notifiers/index.ts +3 -0
  95. package/src/notifiers/slackNotifier.test.ts +113 -0
  96. package/src/notifiers/slackNotifier.ts +55 -0
  97. package/src/notifiers/zoomNotifier.test.ts +207 -0
  98. package/src/notifiers/zoomNotifier.ts +111 -0
  99. package/src/openApi.test.ts +331 -0
  100. package/src/openApi.ts +494 -0
  101. package/src/openApiBuilder.test.ts +442 -0
  102. package/src/openApiBuilder.ts +636 -0
  103. package/src/openApiEtag.ts +40 -0
  104. package/src/permissions.test.ts +219 -0
  105. package/src/permissions.ts +228 -0
  106. package/src/plugins.test.ts +390 -0
  107. package/src/plugins.ts +289 -0
  108. package/src/populate.test.ts +65 -0
  109. package/src/populate.ts +258 -0
  110. package/src/response.ts +0 -0
  111. package/src/tests/bunSetup.ts +234 -0
  112. package/src/tests/index.ts +1 -0
  113. package/src/tests.ts +218 -0
  114. package/src/transformers.test.ts +202 -0
  115. package/src/transformers.ts +170 -0
  116. package/src/utils.test.ts +14 -0
  117. package/src/utils.ts +47 -0
  118. package/tsconfig.json +60 -0
  119. package/types.d.ts +17 -0
@@ -0,0 +1,315 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ var desc = Object.getOwnPropertyDescriptor(m, k);
31
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
32
+ desc = { enumerable: true, get: function() { return m[k]; } };
33
+ }
34
+ Object.defineProperty(o, k2, desc);
35
+ }) : (function(o, m, k, k2) {
36
+ if (k2 === undefined) k2 = k;
37
+ o[k2] = m[k];
38
+ }));
39
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
41
+ }) : function(o, v) {
42
+ o["default"] = v;
43
+ });
44
+ var __importStar = (this && this.__importStar) || (function () {
45
+ var ownKeys = function(o) {
46
+ ownKeys = Object.getOwnPropertyNames || function (o) {
47
+ var ar = [];
48
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
49
+ return ar;
50
+ };
51
+ return ownKeys(o);
52
+ };
53
+ return function (mod) {
54
+ if (mod && mod.__esModule) return mod;
55
+ var result = {};
56
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
57
+ __setModuleDefault(result, mod);
58
+ return result;
59
+ };
60
+ })();
61
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
62
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
63
+ return new (P || (P = Promise))(function (resolve, reject) {
64
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
65
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
66
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
67
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
68
+ });
69
+ };
70
+ var __generator = (this && this.__generator) || function (thisArg, body) {
71
+ 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);
72
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
73
+ function verb(n) { return function (v) { return step([n, v]); }; }
74
+ function step(op) {
75
+ if (f) throw new TypeError("Generator is already executing.");
76
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
77
+ 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;
78
+ if (y = 0, t) op = [op[0] & 2, t.value];
79
+ switch (op[0]) {
80
+ case 0: case 1: t = op; break;
81
+ case 4: _.label++; return { value: op[1], done: false };
82
+ case 5: _.label++; y = op[1]; op = [0]; continue;
83
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
84
+ default:
85
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
86
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
87
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
88
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
89
+ if (t[2]) _.ops.pop();
90
+ _.trys.pop(); continue;
91
+ }
92
+ op = body.call(thisArg, _);
93
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
94
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
95
+ }
96
+ };
97
+ Object.defineProperty(exports, "__esModule", { value: true });
98
+ exports.DateOnly = void 0;
99
+ exports.baseUserPlugin = baseUserPlugin;
100
+ exports.isDeletedPlugin = isDeletedPlugin;
101
+ exports.isDisabledPlugin = isDisabledPlugin;
102
+ exports.createdUpdatedPlugin = createdUpdatedPlugin;
103
+ exports.firebaseJWTPlugin = firebaseJWTPlugin;
104
+ exports.findOneOrNone = findOneOrNone;
105
+ exports.findExactlyOne = findExactlyOne;
106
+ exports.upsertPlugin = upsertPlugin;
107
+ var luxon_1 = require("luxon");
108
+ var mongoose_1 = __importStar(require("mongoose"));
109
+ var errors_1 = require("./errors");
110
+ function baseUserPlugin(schema) {
111
+ schema.add({ admin: { default: false, type: Boolean } });
112
+ schema.add({ email: { index: true, type: String } });
113
+ }
114
+ function isDeletedPlugin(schema, defaultValue) {
115
+ if (defaultValue === void 0) { defaultValue = false; }
116
+ schema.add({
117
+ deleted: {
118
+ default: defaultValue,
119
+ description: "Deleted objects are not returned in any find() or findOne() by default. " +
120
+ "Add {deleted: true} to find them.",
121
+ index: true,
122
+ type: Boolean,
123
+ },
124
+ });
125
+ function applyDeleteFilter(q) {
126
+ var query = q.getQuery();
127
+ if (query && query.deleted === undefined) {
128
+ void q.where({ deleted: { $ne: true } });
129
+ }
130
+ }
131
+ schema.pre("find", function () {
132
+ applyDeleteFilter(this);
133
+ });
134
+ schema.pre("findOne", function () {
135
+ applyDeleteFilter(this);
136
+ });
137
+ }
138
+ function isDisabledPlugin(schema, defaultValue) {
139
+ if (defaultValue === void 0) { defaultValue = false; }
140
+ schema.add({
141
+ disabled: {
142
+ default: defaultValue,
143
+ description: "When a user is set to disable, all requests will return a 401",
144
+ index: true,
145
+ type: Boolean,
146
+ },
147
+ });
148
+ }
149
+ function createdUpdatedPlugin(schema) {
150
+ schema.add({ updated: { index: true, type: Date } });
151
+ schema.add({ created: { index: true, type: Date } });
152
+ schema.pre("save", function () {
153
+ if (this.disableCreatedUpdatedPlugin === true) {
154
+ return;
155
+ }
156
+ // If we aren't specifying created, use now.
157
+ if (!this.created) {
158
+ this.created = new Date();
159
+ }
160
+ // All writes change the updated time.
161
+ this.updated = new Date();
162
+ });
163
+ schema.pre(/save|updateOne|insertMany/, function () {
164
+ void this.updateOne({}, { $set: { updated: new Date() } });
165
+ });
166
+ }
167
+ function firebaseJWTPlugin(schema) {
168
+ schema.add({ firebaseId: { index: true, type: String } });
169
+ }
170
+ /**
171
+ * This adds a static method `Model.findOneOrNone` to the schema. This should replace `Model.findOne` in most instances.
172
+ * `Model.findOne` should only be used with a unique index, but that's not apparent from the docs. Otherwise you can wind
173
+ * up with a random document that matches the query. The returns either null if no document matches, the actual
174
+ * document, or throws an exception if multiple are found.
175
+ * @param schema Mongoose Schema
176
+ */
177
+ function findOneOrNone(schema) {
178
+ schema.statics.findOneOrNone = function (query, errorArgs) {
179
+ return __awaiter(this, void 0, void 0, function () {
180
+ var results;
181
+ return __generator(this, function (_a) {
182
+ switch (_a.label) {
183
+ case 0: return [4 /*yield*/, this.find(query)];
184
+ case 1:
185
+ results = _a.sent();
186
+ if (results.length === 0) {
187
+ return [2 /*return*/, null];
188
+ }
189
+ if (results.length > 1) {
190
+ throw new errors_1.APIError(__assign({ detail: "query: ".concat(JSON.stringify(query)), status: 500, title: "".concat(this.modelName, ".findOne query returned multiple documents") }, errorArgs));
191
+ }
192
+ return [2 /*return*/, results[0]];
193
+ }
194
+ });
195
+ });
196
+ };
197
+ }
198
+ /**
199
+ * This adds a static method `Model.findExactlyOne` to the schema. This or findOneOrNone should replace `Model.findOne`
200
+ * in most instances.
201
+ * `Model.findOne` should only be used with a unique index, but that's not apparent from the docs. Otherwise you can wind
202
+ * up with a random document that matches the query. The returns the one matching document, or throws an exception if
203
+ * multiple or none are found.
204
+ * @param schema Mongoose Schema
205
+ */
206
+ function findExactlyOne(schema) {
207
+ schema.statics.findExactlyOne = function (query, errorArgs) {
208
+ return __awaiter(this, void 0, void 0, function () {
209
+ var results;
210
+ return __generator(this, function (_a) {
211
+ switch (_a.label) {
212
+ case 0: return [4 /*yield*/, this.find(query)];
213
+ case 1:
214
+ results = _a.sent();
215
+ if (results.length === 0) {
216
+ throw new errors_1.APIError(__assign({ detail: "query: ".concat(JSON.stringify(query)), status: 404, title: "".concat(this.modelName, ".findExactlyOne query returned no documents") }, errorArgs));
217
+ }
218
+ if (results.length > 1) {
219
+ throw new errors_1.APIError(__assign({ detail: "query: ".concat(JSON.stringify(query)), status: 500, title: "".concat(this.modelName, ".findExactlyOne query returned multiple documents") }, errorArgs));
220
+ }
221
+ return [2 /*return*/, results[0]];
222
+ }
223
+ });
224
+ });
225
+ };
226
+ }
227
+ /**
228
+ * This adds a static method `Model.upsert` to the schema. This method will either update an existing document
229
+ * that matches the conditions or create a new document if none exists. It throws an error if multiple documents
230
+ * match the conditions to prevent ambiguous updates.
231
+ * @param schema Mongoose Schema
232
+ */
233
+ function upsertPlugin(schema) {
234
+ schema.statics.upsert = function (conditions, update) {
235
+ return __awaiter(this, void 0, void 0, function () {
236
+ var docs, doc, combinedData, newDoc;
237
+ return __generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0: return [4 /*yield*/, this.find(conditions)];
240
+ case 1:
241
+ docs = _a.sent();
242
+ if (docs.length > 1) {
243
+ throw new errors_1.APIError({
244
+ detail: "query: ".concat(JSON.stringify(conditions)),
245
+ status: 500,
246
+ title: "".concat(this.modelName, ".upsert find query returned multiple documents"),
247
+ });
248
+ }
249
+ doc = docs[0];
250
+ if (doc) {
251
+ // If the document exists, update it with the provided update values.
252
+ Object.assign(doc, update);
253
+ return [2 /*return*/, doc.save()];
254
+ }
255
+ combinedData = __assign(__assign({}, conditions), update);
256
+ newDoc = new this(combinedData);
257
+ return [2 /*return*/, newDoc.save()];
258
+ }
259
+ });
260
+ });
261
+ };
262
+ }
263
+ var DateOnly = /** @class */ (function (_super) {
264
+ __extends(DateOnly, _super);
265
+ function DateOnly(key, options) {
266
+ var _this = _super.call(this, key, options, "DateOnly") || this;
267
+ _this.$conditionalHandlers = __assign(__assign({}, mongoose_1.SchemaType.prototype.$conditionalHandlers), { $gt: _this.handleSingle, $gte: _this.handleSingle, $lt: _this.handleSingle, $lte: _this.handleSingle });
268
+ return _this;
269
+ }
270
+ DateOnly.prototype.handleSingle = function (val) {
271
+ return this.cast(val);
272
+ };
273
+ // Based on castForQuery in mongoose/lib/schema/date.js
274
+ // When using $gt, $gte, $lt, $lte, etc, we need to cast the value to a Date
275
+ DateOnly.prototype.castForQuery = function ($conditional, val, context) {
276
+ if ($conditional == null) {
277
+ return this.applySetters(val, context);
278
+ }
279
+ var handler = this.$conditionalHandlers[$conditional];
280
+ if (!handler) {
281
+ throw new Error("Can't use ".concat($conditional, " with DateOnly."));
282
+ }
283
+ return handler.call(this, val);
284
+ };
285
+ // When either setting a value to a DateOnly or fetching from the DB,
286
+ // we want to strip off the time portion.
287
+ DateOnly.prototype.cast = function (val) {
288
+ if (val instanceof Date) {
289
+ var date = luxon_1.DateTime.fromJSDate(val).toUTC().startOf("day");
290
+ if (!date.isValid) {
291
+ throw new mongoose_1.Error.CastError("DateOnly", val, this.path, new Error("Value is not a valid date"));
292
+ }
293
+ return date.toJSDate();
294
+ }
295
+ if (typeof val === "string" || typeof val === "number") {
296
+ var date = luxon_1.DateTime.fromJSDate(new Date(val)).toUTC().startOf("day");
297
+ if (!date.isValid) {
298
+ throw new mongoose_1.Error.CastError("DateOnly", val, this.path, new Error("Value is not a valid date"));
299
+ }
300
+ return date.toJSDate();
301
+ }
302
+ // Handle $gte, $lte, etc
303
+ if (typeof val === "object") {
304
+ return val;
305
+ }
306
+ throw new mongoose_1.Error.CastError("DateOnly", val, this.path, new Error("Value is not a valid date"));
307
+ };
308
+ DateOnly.prototype.get = function (val) {
309
+ return (val instanceof Date ? luxon_1.DateTime.fromJSDate(val).startOf("day").toJSDate() : val);
310
+ };
311
+ return DateOnly;
312
+ }(mongoose_1.SchemaType));
313
+ exports.DateOnly = DateOnly;
314
+ // Register DateOnly with Mongoose's Schema.Types
315
+ mongoose_1.default.Schema.Types.DateOnly = DateOnly;
@@ -0,0 +1 @@
1
+ export {};