@terreno/api 0.27.0 → 0.29.0

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 (50) hide show
  1. package/dist/actions.test.js +2 -1
  2. package/dist/api.coverage.test.js +4 -2
  3. package/dist/api.errorPaths.test.js +5 -3
  4. package/dist/api.errors.test.js +6 -7
  5. package/dist/api.hooks.test.js +65 -8
  6. package/dist/api.js +93 -47
  7. package/dist/api.test.js +10 -4
  8. package/dist/auth.test.js +1 -1
  9. package/dist/config.js +7 -4
  10. package/dist/config.test.js +28 -3
  11. package/dist/docLoader.js +15 -51
  12. package/dist/docLoader.test.js +2 -1
  13. package/dist/errors.d.ts +72 -5
  14. package/dist/errors.js +265 -67
  15. package/dist/errors.test.js +163 -13
  16. package/dist/httpClient.d.ts +2 -3
  17. package/dist/httpClient.js +4 -4
  18. package/dist/httpClient.test.js +3 -2
  19. package/dist/notifiers/googleChatNotifier.test.js +2 -1
  20. package/dist/notifiers/slackNotifier.test.js +1 -1
  21. package/dist/notifiers/zoomNotifier.test.js +2 -1
  22. package/dist/permissions.js +0 -2
  23. package/dist/permissions.middleware.test.js +7 -42
  24. package/dist/plugins.test.js +4 -2
  25. package/dist/transformers.js +4 -2
  26. package/package.json +2 -2
  27. package/src/actions.test.ts +2 -1
  28. package/src/api.coverage.test.ts +4 -2
  29. package/src/api.errorPaths.test.ts +5 -3
  30. package/src/api.errors.test.ts +6 -7
  31. package/src/api.hooks.test.ts +24 -1
  32. package/src/api.test.ts +10 -4
  33. package/src/api.ts +93 -48
  34. package/src/auth.test.ts +1 -1
  35. package/src/config.test.ts +25 -3
  36. package/src/config.ts +7 -4
  37. package/src/docLoader.test.ts +2 -1
  38. package/src/docLoader.ts +16 -18
  39. package/src/errors.test.ts +178 -13
  40. package/src/errors.ts +278 -79
  41. package/src/httpClient.test.ts +3 -2
  42. package/src/httpClient.ts +4 -4
  43. package/src/notifiers/googleChatNotifier.test.ts +2 -2
  44. package/src/notifiers/slackNotifier.test.ts +2 -2
  45. package/src/notifiers/zoomNotifier.test.ts +2 -2
  46. package/src/permissions.middleware.test.ts +7 -11
  47. package/src/permissions.ts +0 -2
  48. package/src/plugins.test.ts +4 -2
  49. package/src/transformers.ts +4 -2
  50. package/tsconfig.json +1 -1
package/dist/auth.test.js CHANGED
@@ -711,7 +711,7 @@ var decodeTokenPayload = function (token) {
711
711
  case 0: return [4 /*yield*/, agent.post("/auth/signup").send({ email: "new@example.com" }).expect(400)];
712
712
  case 1:
713
713
  res = _a.sent();
714
- (0, bun_test_1.expect)(res.body).toEqual({ meta: {}, status: 400, title: "Missing credentials" });
714
+ (0, bun_test_1.expect)(res.body).toEqual({ status: 400, title: "Missing credentials" });
715
715
  return [2 /*return*/];
716
716
  }
717
717
  });
package/dist/config.js CHANGED
@@ -126,9 +126,10 @@ var getNumber = function (key) {
126
126
  var parsed = Number(raw);
127
127
  if (!Number.isFinite(parsed)) {
128
128
  throw new errors_1.APIError({
129
- error: new Error("Config key \"".concat(key, "\" is not a valid number: ").concat(JSON.stringify(raw))),
129
+ code: "invalid-config-value",
130
+ detail: "Config key \"".concat(key, "\" is not a valid number: ").concat(JSON.stringify(raw)),
130
131
  status: 500,
131
- title: "Config key \"".concat(key, "\" is not a valid number"),
132
+ title: "Invalid config value",
132
133
  });
133
134
  }
134
135
  return parsed;
@@ -156,9 +157,11 @@ var getJSON = function (key) {
156
157
  }
157
158
  catch (error) {
158
159
  throw new errors_1.APIError({
159
- error: new Error("Config key \"".concat(key, "\" is not valid JSON: ").concat(error.message)),
160
+ cause: error,
161
+ code: "invalid-config-value",
162
+ detail: "Config key \"".concat(key, "\" is not valid JSON: ").concat(error.message),
160
163
  status: 500,
161
- title: "Config key \"".concat(key, "\" is not valid JSON"),
164
+ title: "Invalid config value",
162
165
  });
163
166
  }
164
167
  };
@@ -74,6 +74,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
74
74
  Object.defineProperty(exports, "__esModule", { value: true });
75
75
  var bun_test_1 = require("bun:test");
76
76
  var config_1 = require("./config");
77
+ var errors_1 = require("./errors");
77
78
  var KEYS = [
78
79
  "TERRENO_CFG_STRING",
79
80
  "TERRENO_CFG_DEFAULTED",
@@ -174,11 +175,27 @@ var resetEnv = function () {
174
175
  });
175
176
  (0, bun_test_1.it)("throws on non-numeric values", function () {
176
177
  process.env.TERRENO_CFG_NUM = "not-a-number";
177
- (0, bun_test_1.expect)(function () { return config_1.Config.getNumber("TERRENO_CFG_NUM"); }).toThrow(/not a valid number/);
178
+ try {
179
+ config_1.Config.getNumber("TERRENO_CFG_NUM");
180
+ bun_test_1.expect.unreachable("expected throw");
181
+ }
182
+ catch (error) {
183
+ (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
184
+ (0, bun_test_1.expect)(error.title).toBe("Invalid config value");
185
+ (0, bun_test_1.expect)(error.detail).toContain("not a valid number");
186
+ }
178
187
  });
179
188
  (0, bun_test_1.it)("throws on partially-numeric strings like '5000ms'", function () {
180
189
  process.env.TERRENO_CFG_NUM = "5000ms";
181
- (0, bun_test_1.expect)(function () { return config_1.Config.getNumber("TERRENO_CFG_NUM"); }).toThrow(/not a valid number/);
190
+ try {
191
+ config_1.Config.getNumber("TERRENO_CFG_NUM");
192
+ bun_test_1.expect.unreachable("expected throw");
193
+ }
194
+ catch (error) {
195
+ (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
196
+ (0, bun_test_1.expect)(error.title).toBe("Invalid config value");
197
+ (0, bun_test_1.expect)(error.detail).toContain("not a valid number");
198
+ }
182
199
  });
183
200
  (0, bun_test_1.it)("supports floats", function () {
184
201
  process.env.TERRENO_CFG_NUM = "3.14";
@@ -221,7 +238,15 @@ var resetEnv = function () {
221
238
  });
222
239
  (0, bun_test_1.it)("throws on malformed JSON instead of silently returning undefined", function () {
223
240
  process.env.TERRENO_CFG_JSON = "{not json";
224
- (0, bun_test_1.expect)(function () { return config_1.Config.getJSON("TERRENO_CFG_JSON"); }).toThrow(/not valid JSON/);
241
+ try {
242
+ config_1.Config.getJSON("TERRENO_CFG_JSON");
243
+ bun_test_1.expect.unreachable("expected throw");
244
+ }
245
+ catch (error) {
246
+ (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
247
+ (0, bun_test_1.expect)(error.title).toBe("Invalid config value");
248
+ (0, bun_test_1.expect)(error.detail).toContain("not valid JSON");
249
+ }
225
250
  });
226
251
  });
227
252
  (0, bun_test_1.describe)("registry", function () {
package/dist/docLoader.js CHANGED
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -73,7 +40,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
73
40
  };
74
41
  Object.defineProperty(exports, "__esModule", { value: true });
75
42
  exports.loadDocOr404 = void 0;
76
- var Sentry = __importStar(require("@sentry/bun"));
77
43
  var mongoose_1 = __importDefault(require("mongoose"));
78
44
  var api_1 = require("./api");
79
45
  var errors_1 = require("./errors");
@@ -82,7 +48,7 @@ var errors_1 = require("./errors");
82
48
  * Matches permission middleware behavior including soft-delete metadata.
83
49
  */
84
50
  var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0, void 0, void 0, function () {
85
- var builtQuery, populatedQuery, data, error_1, hiddenDoc, error, reason, error;
51
+ var builtQuery, populatedQuery, data, error_1, hiddenDoc, notFoundDetail, reason;
86
52
  return __generator(this, function (_a) {
87
53
  switch (_a.label) {
88
54
  case 0:
@@ -101,9 +67,11 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
101
67
  throw error_1;
102
68
  }
103
69
  throw new errors_1.APIError({
104
- error: error_1,
70
+ cause: error_1,
71
+ code: "get-error",
72
+ detail: "GET failed on ".concat(id),
105
73
  status: 500,
106
- title: "GET failed on ".concat(id),
74
+ title: "GET error",
107
75
  });
108
76
  case 4:
109
77
  if (!!data) return [3 /*break*/, 6];
@@ -112,14 +80,12 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
112
80
  })];
113
81
  case 5:
114
82
  hiddenDoc = _a.sent();
83
+ notFoundDetail = "Document ".concat(id, " not found for model ").concat(model.modelName);
115
84
  if (!hiddenDoc) {
116
- Sentry.captureMessage("Document ".concat(id, " not found for model ").concat(model.modelName));
117
- error = new errors_1.APIError({
118
- status: 404,
119
- title: "Document ".concat(id, " not found for model ").concat(model.modelName),
85
+ throw new errors_1.NotFoundError({
86
+ detail: notFoundDetail,
87
+ title: "Document not found",
120
88
  });
121
- error.meta = undefined;
122
- throw error;
123
89
  }
124
90
  reason = null;
125
91
  if (hiddenDoc.deleted) {
@@ -132,18 +98,16 @@ var loadDocOr404 = function (model, id, populatePaths) { return __awaiter(void 0
132
98
  reason = { archived: "true" };
133
99
  }
134
100
  if (!reason) {
135
- error = new errors_1.APIError({
136
- status: 404,
137
- title: "Document ".concat(id, " not found for model ").concat(model.modelName),
101
+ throw new errors_1.NotFoundError({
102
+ detail: notFoundDetail,
103
+ title: "Document not found",
138
104
  });
139
- error.meta = undefined;
140
- throw error;
141
105
  }
142
- throw new errors_1.APIError({
106
+ throw new errors_1.NotFoundError({
107
+ detail: notFoundDetail,
143
108
  disableExternalErrorTracking: true,
144
109
  meta: reason,
145
- status: 404,
146
- title: "Document ".concat(id, " not found for model ").concat(model.modelName),
110
+ title: "Document not found",
147
111
  });
148
112
  case 6: return [2 /*return*/, data];
149
113
  }
@@ -127,7 +127,8 @@ var errors_1 = require("./errors");
127
127
  (0, bun_test_1.expect)(error_2).toBeInstanceOf(errors_1.APIError);
128
128
  apiError = error_2;
129
129
  (0, bun_test_1.expect)(apiError.status).toBe(404);
130
- (0, bun_test_1.expect)(apiError.meta).toBeUndefined();
130
+ (0, bun_test_1.expect)(apiError.title).toBe("Document not found");
131
+ (0, bun_test_1.expect)(apiError.meta).toEqual({});
131
132
  return [3 /*break*/, 4];
132
133
  case 4: return [2 /*return*/];
133
134
  }
package/dist/errors.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { NextFunction, Request, Response } from "express";
2
2
  import { Schema } from "mongoose";
3
- export interface APIErrorConstructor {
3
+ export interface APIErrorOptions {
4
4
  title: string;
5
5
  fields?: {
6
6
  [id: string]: string;
@@ -19,16 +19,46 @@ export interface APIErrorConstructor {
19
19
  header?: string;
20
20
  };
21
21
  meta?: {
22
- [id: string]: string;
22
+ [id: string]: unknown;
23
23
  };
24
+ cause?: unknown;
25
+ /** @deprecated Use `cause` instead. Kept as an alias for backwards compatibility. */
24
26
  error?: unknown;
25
27
  disableExternalErrorTracking?: boolean;
26
28
  }
29
+ /** @deprecated Use {@link APIErrorOptions} instead. */
30
+ export type APIErrorConstructor = APIErrorOptions;
31
+ export interface APIErrorBody {
32
+ status: number;
33
+ title: string;
34
+ id?: string;
35
+ links?: {
36
+ about?: string;
37
+ type?: string;
38
+ };
39
+ code?: string;
40
+ detail?: string;
41
+ source?: {
42
+ pointer?: string;
43
+ parameter?: string;
44
+ header?: string;
45
+ };
46
+ meta?: {
47
+ [id: string]: unknown;
48
+ };
49
+ disableExternalErrorTracking?: boolean;
50
+ }
27
51
  /**
28
52
  * APIError is a simple way to throw an error in an API route and control what is shown and the
29
53
  * HTTP code displayed. It follows the JSONAPI spec to standardize the fields,
30
54
  * allowing the UI to show more consistent, better error messages.
31
55
  *
56
+ * It uses the standard `Error` fields the way external tools (e.g. Sentry) expect:
57
+ * - `message` is exactly `title` — a stable, human-readable summary of the problem type.
58
+ * - `name` is the error type, derived from the subclass name, `code`, or `status`
59
+ * (e.g. a 404 becomes "NotFoundError").
60
+ * - `cause` holds the wrapped original error, so linked exceptions keep their own stack.
61
+ *
32
62
  * ```ts
33
63
  * throw new APIError({
34
64
  * title: "Only an admin can update that!",
@@ -39,7 +69,7 @@ export interface APIErrorConstructor {
39
69
  * ```
40
70
  */
41
71
  export declare class APIError extends Error {
42
- title: string;
72
+ readonly isTerrenoAPIError = true;
43
73
  id: string | undefined;
44
74
  links: {
45
75
  about?: string;
@@ -56,9 +86,42 @@ export declare class APIError extends Error {
56
86
  meta: {
57
87
  [id: string]: unknown;
58
88
  } | undefined;
59
- error?: unknown;
60
89
  disableExternalErrorTracking?: boolean;
61
- constructor(data: APIErrorConstructor);
90
+ constructor(options: APIErrorOptions);
91
+ /** @deprecated Use the standard `cause` field instead. */
92
+ get error(): unknown;
93
+ get title(): string;
94
+ toJSON(): APIErrorBody;
95
+ }
96
+ /** Options accepted by the status-specific APIError subclasses: a title string or full options. */
97
+ export type APIErrorSubclassOptions = Omit<APIErrorOptions, "status"> | string;
98
+ /** 400 Bad Request. */
99
+ export declare class BadRequestError extends APIError {
100
+ constructor(options: APIErrorSubclassOptions);
101
+ }
102
+ /** 401 Unauthorized. */
103
+ export declare class UnauthorizedError extends APIError {
104
+ constructor(options: APIErrorSubclassOptions);
105
+ }
106
+ /** 403 Forbidden. */
107
+ export declare class ForbiddenError extends APIError {
108
+ constructor(options: APIErrorSubclassOptions);
109
+ }
110
+ /** 404 Not Found. */
111
+ export declare class NotFoundError extends APIError {
112
+ constructor(options: APIErrorSubclassOptions);
113
+ }
114
+ /** 409 Conflict. */
115
+ export declare class ConflictError extends APIError {
116
+ constructor(options: APIErrorSubclassOptions);
117
+ }
118
+ /** 400 Bad Request with a "ValidationError" type, for request/schema validation failures. */
119
+ export declare class ValidationError extends APIError {
120
+ constructor(options: APIErrorSubclassOptions);
121
+ }
122
+ /** 500 Internal Server Error. */
123
+ export declare class InternalServerError extends APIError {
124
+ constructor(options: APIErrorSubclassOptions);
62
125
  }
63
126
  export declare const errorsPlugin: (schema: Schema) => void;
64
127
  export declare const isAPIError: (error: unknown) => error is APIError;
@@ -72,6 +135,10 @@ export declare const errorStack: (error: unknown) => string;
72
135
  * this property attached.
73
136
  */
74
137
  export declare const getDisableExternalErrorTracking: (error: unknown) => boolean | undefined;
138
+ /**
139
+ * Creates an APIError body to send to clients as JSON.
140
+ * @deprecated Use `error.toJSON()` instead.
141
+ */
75
142
  export declare const getAPIErrorBody: (error: APIError) => Record<string, unknown>;
76
143
  export declare const apiUnauthorizedMiddleware: (err: Error, _req: Request, res: Response, next: NextFunction) => void;
77
144
  /**