@terreno/api 0.28.0 → 0.30.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 (45) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/dist/actions.js +17 -8
  3. package/dist/actions.test.js +8 -3
  4. package/dist/api.arrayOperations.test.js +16 -8
  5. package/dist/api.coverage.test.js +2 -1
  6. package/dist/api.errorPaths.test.js +522 -1
  7. package/dist/api.errors.test.js +9 -0
  8. package/dist/api.js +137 -170
  9. package/dist/api.query.test.js +20 -8
  10. package/dist/api.test.js +12 -6
  11. package/dist/auth.coverage.test.d.ts +1 -0
  12. package/dist/auth.coverage.test.js +202 -0
  13. package/dist/auth.test.js +159 -0
  14. package/dist/docLoader.js +5 -1
  15. package/dist/envConfigurationPlugin.d.ts +1 -1
  16. package/dist/envConfigurationPlugin.js +0 -2
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.js +24 -2
  19. package/dist/errors.test.js +137 -0
  20. package/dist/httpClient.js +5 -1
  21. package/dist/plugins.d.ts +4 -4
  22. package/dist/plugins.js +51 -9
  23. package/dist/plugins.test.js +61 -17
  24. package/dist/realtime/registry.d.ts +3 -2
  25. package/package.json +2 -2
  26. package/src/actions.test.ts +8 -3
  27. package/src/actions.ts +19 -10
  28. package/src/api.arrayOperations.test.ts +16 -8
  29. package/src/api.coverage.test.ts +2 -1
  30. package/src/api.errorPaths.test.ts +313 -3
  31. package/src/api.errors.test.ts +12 -0
  32. package/src/api.query.test.ts +20 -8
  33. package/src/api.test.ts +12 -6
  34. package/src/api.ts +141 -161
  35. package/src/auth.coverage.test.ts +101 -0
  36. package/src/auth.test.ts +125 -2
  37. package/src/docLoader.ts +6 -2
  38. package/src/envConfigurationPlugin.ts +2 -6
  39. package/src/errors.test.ts +106 -1
  40. package/src/errors.ts +25 -1
  41. package/src/httpClient.ts +6 -2
  42. package/src/plugins.test.ts +45 -9
  43. package/src/plugins.ts +72 -35
  44. package/src/realtime/changeStreamWatcher.ts +8 -1
  45. package/src/realtime/registry.ts +3 -4
package/src/plugins.ts CHANGED
@@ -9,13 +9,29 @@ import mongoose, {
9
9
  type SchemaTypeOptions,
10
10
  } from "mongoose";
11
11
 
12
- import {APIError, type APIErrorConstructor} from "./errors";
12
+ import {APIError, type APIErrorOptions, InternalServerError, NotFoundError} from "./errors";
13
13
 
14
14
  export interface BaseUser {
15
15
  admin: boolean;
16
16
  email: string;
17
17
  }
18
18
 
19
+ /**
20
+ * Builds the error thrown by the query plugins. `errorArgs` is applied last so consumers can
21
+ * override every field, including `status`; the status-specific subclass is used when the resulting
22
+ * status still matches one of them, so Sentry sees a meaningful error type.
23
+ */
24
+ const pluginError = (defaults: APIErrorOptions, errorArgs?: Partial<APIErrorOptions>): APIError => {
25
+ const options: APIErrorOptions = {...defaults, ...errorArgs};
26
+ if (options.status === 404) {
27
+ return new NotFoundError(options);
28
+ }
29
+ if (options.status === 500) {
30
+ return new InternalServerError(options);
31
+ }
32
+ return new APIError(options);
33
+ };
34
+
19
35
  // noExplicitAny: Schema generics must be loose to accept arbitrary consumer schemas
20
36
  // biome-ignore lint/suspicious/noExplicitAny: Schema generics must be loose to accept arbitrary consumer schemas
21
37
  export const baseUserPlugin = (schema: Schema<any, any, any, any>): void => {
@@ -122,19 +138,23 @@ export const firebaseJWTPlugin = (schema: Schema): void => {
122
138
  export const findOneOrNone = <T>(schema: Schema<T>): void => {
123
139
  schema.statics.findOneOrNone = async function (
124
140
  query: Record<string, unknown>,
125
- errorArgs?: Partial<APIErrorConstructor>
141
+ errorArgs?: Partial<APIErrorOptions>
126
142
  ): Promise<(Document & T) | null> {
127
143
  const results = await this.find(query);
128
144
  if (results.length === 0) {
129
145
  return null;
130
146
  }
131
147
  if (results.length > 1) {
132
- throw new APIError({
133
- detail: `query: ${JSON.stringify(query)}`,
134
- status: 500,
135
- title: `${this.modelName}.findOne query returned multiple documents`,
136
- ...errorArgs,
137
- });
148
+ throw pluginError(
149
+ {
150
+ code: "find-one-multiple-documents",
151
+ detail: `${this.modelName}.findOne query returned multiple documents. query: ${JSON.stringify(query)}`,
152
+ meta: {model: this.modelName},
153
+ status: 500,
154
+ title: "findOne query returned multiple documents",
155
+ },
156
+ errorArgs
157
+ );
138
158
  }
139
159
  return results[0];
140
160
  };
@@ -153,7 +173,7 @@ export const findOneOrNone = <T>(schema: Schema<T>): void => {
153
173
  export const findOneOrNoneFor = async <T>(
154
174
  model: mongoose.Model<T>,
155
175
  query: FilterQuery<T>,
156
- errorArgs?: Partial<APIErrorConstructor>
176
+ errorArgs?: Partial<APIErrorOptions>
157
177
  ): Promise<(Document & T) | null> => {
158
178
  const withStatic = model as mongoose.Model<T> & Partial<FindOneOrNonePlugin<T>>;
159
179
  if (typeof withStatic.findOneOrNone === "function") {
@@ -164,12 +184,16 @@ export const findOneOrNoneFor = async <T>(
164
184
  return null;
165
185
  }
166
186
  if (results.length > 1) {
167
- throw new APIError({
168
- detail: `query: ${JSON.stringify(query)}`,
169
- status: 500,
170
- title: `${model.modelName}.findOne query returned multiple documents`,
171
- ...errorArgs,
172
- });
187
+ throw pluginError(
188
+ {
189
+ code: "find-one-multiple-documents",
190
+ detail: `${model.modelName}.findOne query returned multiple documents. query: ${JSON.stringify(query)}`,
191
+ meta: {model: model.modelName},
192
+ status: 500,
193
+ title: "findOne query returned multiple documents",
194
+ },
195
+ errorArgs
196
+ );
173
197
  }
174
198
  return results[0] as unknown as Document & T;
175
199
  };
@@ -185,24 +209,32 @@ export const findOneOrNoneFor = async <T>(
185
209
  export const findExactlyOne = <T>(schema: Schema<T>): void => {
186
210
  schema.statics.findExactlyOne = async function (
187
211
  query: Record<string, unknown>,
188
- errorArgs?: Partial<APIErrorConstructor>
212
+ errorArgs?: Partial<APIErrorOptions>
189
213
  ): Promise<Document & T> {
190
214
  const results = await this.find(query);
191
215
  if (results.length === 0) {
192
- throw new APIError({
193
- detail: `query: ${JSON.stringify(query)}`,
194
- status: 404,
195
- title: `${this.modelName}.findExactlyOne query returned no documents`,
196
- ...errorArgs,
197
- });
216
+ throw pluginError(
217
+ {
218
+ code: "find-exactly-one-no-documents",
219
+ detail: `${this.modelName}.findExactlyOne query returned no documents. query: ${JSON.stringify(query)}`,
220
+ meta: {model: this.modelName},
221
+ status: 404,
222
+ title: "findExactlyOne query returned no documents",
223
+ },
224
+ errorArgs
225
+ );
198
226
  }
199
227
  if (results.length > 1) {
200
- throw new APIError({
201
- detail: `query: ${JSON.stringify(query)}`,
202
- status: 500,
203
- title: `${this.modelName}.findExactlyOne query returned multiple documents`,
204
- ...errorArgs,
205
- });
228
+ throw pluginError(
229
+ {
230
+ code: "find-exactly-one-multiple-documents",
231
+ detail: `${this.modelName}.findExactlyOne query returned multiple documents. query: ${JSON.stringify(query)}`,
232
+ meta: {model: this.modelName},
233
+ status: 500,
234
+ title: "findExactlyOne query returned multiple documents",
235
+ },
236
+ errorArgs
237
+ );
206
238
  }
207
239
  return results[0];
208
240
  };
@@ -225,10 +257,11 @@ export const upsertPlugin = <T>(schema: Schema<any, any, any, any>): void => {
225
257
  // Try to find the document with the given conditions.
226
258
  const docs = await this.find(conditions);
227
259
  if (docs.length > 1) {
228
- throw new APIError({
229
- detail: `query: ${JSON.stringify(conditions)}`,
230
- status: 500,
231
- title: `${this.modelName}.upsert find query returned multiple documents`,
260
+ throw new InternalServerError({
261
+ code: "upsert-multiple-documents",
262
+ detail: `${this.modelName}.upsert find query returned multiple documents. query: ${JSON.stringify(conditions)}`,
263
+ meta: {model: this.modelName},
264
+ title: "upsert find query returned multiple documents",
232
265
  });
233
266
  }
234
267
  const doc = docs[0];
@@ -254,14 +287,14 @@ export interface HasUpsert<T> {
254
287
  export interface FindOneOrNonePlugin<T> {
255
288
  findOneOrNone(
256
289
  query: Record<string, unknown>,
257
- errorArgs?: Partial<APIErrorConstructor>
290
+ errorArgs?: Partial<APIErrorOptions>
258
291
  ): Promise<(Document & T) | null>;
259
292
  }
260
293
 
261
294
  export interface FindExactlyOnePlugin<T> {
262
295
  findExactlyOne(
263
296
  query: Record<string, unknown>,
264
- errorArgs?: Partial<APIErrorConstructor>
297
+ errorArgs?: Partial<APIErrorOptions>
265
298
  ): Promise<Document & T>;
266
299
  }
267
300
 
@@ -318,7 +351,11 @@ export class DateOnly extends SchemaType {
318
351
  return date.toJSDate();
319
352
  }
320
353
  if (typeof val === "string" || typeof val === "number") {
321
- const date = DateTime.fromJSDate(new Date(val)).toUTC().startOf("day");
354
+ const date = (
355
+ typeof val === "number" ? DateTime.fromMillis(val) : DateTime.fromISO(val, {zone: "utc"})
356
+ )
357
+ .toUTC()
358
+ .startOf("day");
322
359
  if (!date.isValid) {
323
360
  throw new MongooseError.CastError(
324
361
  "DateOnly",
@@ -196,7 +196,14 @@ export const serializeDoc = async (
196
196
  const restMethod = method === "delete" ? "read" : method;
197
197
  // Synthesize the minimal request shape responseHandlers commonly inspect.
198
198
  const syntheticReq = {params: {}, query: {}, user} as unknown as express.Request;
199
- return ensureApiId(await responseHandler(doc, restMethod, syntheticReq, entry.options));
199
+ return ensureApiId(
200
+ await responseHandler(
201
+ doc as unknown as mongoose.Document<unknown, unknown, unknown>,
202
+ restMethod,
203
+ syntheticReq,
204
+ entry.options
205
+ )
206
+ );
200
207
  } catch (error) {
201
208
  logger.error(
202
209
  `[realtime] modelRouter responseHandler threw during realtime serialization for ` +
@@ -1,5 +1,3 @@
1
- // noExplicitAny: model router options are generic across all models
2
- // biome-ignore-all lint/suspicious/noExplicitAny: model router options are generic across all models
3
1
  import type {ModelRouterOptions} from "../api";
4
2
  import type {RealtimeConfig} from "./types";
5
3
 
@@ -16,9 +14,10 @@ export interface RealtimeRegistryEntry {
16
14
  /** Real-time configuration from modelRouter options */
17
15
  config: RealtimeConfig;
18
16
  /**
19
- * Full modelRouter options (for responseHandler, permissions, etc.).
17
+ * Full modelRouter options (for responseHandler, permissions, etc.), erased to
18
+ * `unknown` since the registry holds entries for every model.
20
19
  */
21
- options: ModelRouterOptions<any>;
20
+ options: ModelRouterOptions<unknown>;
22
21
  }
23
22
 
24
23
  const realtimeRegistry: RealtimeRegistryEntry[] = [];