@sfxcode/nuxt-mongocamp-server 2.4.0 → 2.6.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 (61) hide show
  1. package/dist/module.d.mts +10 -4
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +36 -20
  4. package/dist/runtime/api/.openapi-generator/FILES +2 -0
  5. package/dist/runtime/api/apis/AdminApi.d.ts +28 -20
  6. package/dist/runtime/api/apis/AdminApi.js +49 -47
  7. package/dist/runtime/api/apis/ApplicationApi.d.ts +20 -0
  8. package/dist/runtime/api/apis/ApplicationApi.js +51 -0
  9. package/dist/runtime/api/apis/FileApi.d.ts +53 -0
  10. package/dist/runtime/api/apis/FileApi.js +81 -0
  11. package/dist/runtime/api/docs/AdminApi.md +33 -33
  12. package/dist/runtime/api/docs/ApplicationApi.md +79 -0
  13. package/dist/runtime/api/docs/CapabilityLimits.md +8 -0
  14. package/dist/runtime/api/docs/CollectionDeclaration.md +2 -0
  15. package/dist/runtime/api/docs/CollectionDeclarationIndexesInner.md +10 -0
  16. package/dist/runtime/api/docs/CollectionDeclarationReferencedByInner.md +40 -0
  17. package/dist/runtime/api/docs/FileApi.md +97 -0
  18. package/dist/runtime/api/models/CapabilityLimits.d.ts +16 -0
  19. package/dist/runtime/api/models/CapabilityLimits.js +12 -0
  20. package/dist/runtime/api/models/CollectionDeclaration.d.ts +5 -0
  21. package/dist/runtime/api/models/CollectionDeclaration.js +7 -0
  22. package/dist/runtime/api/models/CollectionDeclarationIndexesInner.d.ts +37 -0
  23. package/dist/runtime/api/models/CollectionDeclarationIndexesInner.js +21 -2
  24. package/dist/runtime/api/models/CollectionDeclarationReferencedByInner.d.ts +42 -0
  25. package/dist/runtime/api/models/CollectionDeclarationReferencedByInner.js +35 -0
  26. package/dist/runtime/api/models/CollectionDeclarationRefsInner.d.ts +2 -2
  27. package/dist/runtime/api/models/CollectionDeclarationRefsInner.js +2 -1
  28. package/dist/runtime/api/models/index.d.ts +1 -0
  29. package/dist/runtime/api/models/index.js +1 -0
  30. package/dist/runtime/client.d.ts +8 -0
  31. package/dist/runtime/client.js +3 -0
  32. package/dist/runtime/composables/mongocampApi.d.ts +4 -0
  33. package/dist/runtime/composables/mongocampApi.js +10 -2
  34. package/dist/runtime/composables/mongocampApiAdmin.js +1 -1
  35. package/dist/runtime/composables/mongocampAuth.js +29 -25
  36. package/dist/runtime/composables/mongocampCollection.d.ts +2 -1
  37. package/dist/runtime/composables/mongocampCollection.js +36 -13
  38. package/dist/runtime/composables/mongocampLive.js +25 -3
  39. package/dist/runtime/composables/mongocampSearch.js +12 -3
  40. package/dist/runtime/plugins/bson-payload.d.ts +8 -0
  41. package/dist/runtime/plugins/bson-payload.js +6 -0
  42. package/dist/runtime/server/composables/mongocampApi.d.ts +5 -1
  43. package/dist/runtime/server/composables/mongocampApi.js +1 -9
  44. package/dist/runtime/server/composables/mongocampServerApi.d.ts +4 -0
  45. package/dist/runtime/server/composables/mongocampServerApi.js +5 -0
  46. package/dist/runtime/server/mongocamp/core.d.ts +1 -1
  47. package/dist/runtime/server/mongocamp/core.js +1 -1
  48. package/dist/runtime/server/mongocamp/embedded-api.d.ts +18 -0
  49. package/dist/runtime/server/mongocamp/embedded-api.js +10 -0
  50. package/dist/runtime/utils/appState.d.ts +21 -0
  51. package/dist/runtime/utils/appState.js +11 -0
  52. package/dist/runtime/utils/bsonPayload.d.ts +8 -0
  53. package/dist/runtime/utils/bsonPayload.js +10 -0
  54. package/dist/runtime/utils/buildFieldFilterExpression.d.ts +4 -0
  55. package/dist/runtime/utils/buildFieldFilterExpression.js +5 -3
  56. package/dist/runtime/utils/createMongocampApis.d.ts +9 -1
  57. package/dist/runtime/utils/createMongocampApis.js +9 -2
  58. package/dist/runtime/utils/keepLongs.d.ts +14 -0
  59. package/dist/runtime/utils/keepLongs.js +45 -0
  60. package/dist/runtime/utils/sse.js +7 -1
  61. package/package.json +21 -9
@@ -22,6 +22,10 @@ import {
22
22
  CollectionDeclarationIndexesInnerFromJSON,
23
23
  CollectionDeclarationIndexesInnerToJSON
24
24
  } from "./CollectionDeclarationIndexesInner.js";
25
+ import {
26
+ CollectionDeclarationReferencedByInnerFromJSON,
27
+ CollectionDeclarationReferencedByInnerToJSON
28
+ } from "./CollectionDeclarationReferencedByInner.js";
25
29
  import {
26
30
  CollectionDeclarationSoftDeleteFromJSON,
27
31
  CollectionDeclarationSoftDeleteToJSON
@@ -39,6 +43,7 @@ export function instanceOfCollectionDeclaration(value) {
39
43
  if (!("protectedFields" in value) || value["protectedFields"] === void 0) return false;
40
44
  if (!("indexes" in value) || value["indexes"] === void 0) return false;
41
45
  if (!("refs" in value) || value["refs"] === void 0) return false;
46
+ if (!("referencedBy" in value) || value["referencedBy"] === void 0) return false;
42
47
  return true;
43
48
  }
44
49
  export function CollectionDeclarationFromJSON(json) {
@@ -56,6 +61,7 @@ export function CollectionDeclarationFromJSONTyped(json, ignoreDiscriminator) {
56
61
  "protectedFields": json["protectedFields"],
57
62
  "indexes": json["indexes"].map(CollectionDeclarationIndexesInnerFromJSON),
58
63
  "refs": json["refs"].map(CollectionDeclarationRefsInnerFromJSON),
64
+ "referencedBy": json["referencedBy"].map(CollectionDeclarationReferencedByInnerFromJSON),
59
65
  "timestamps": json["timestamps"] == null ? void 0 : CollectionDeclarationTimestampsFromJSON(json["timestamps"]),
60
66
  "owner": json["owner"] == null ? void 0 : CollectionDeclarationOwnerFromJSON(json["owner"]),
61
67
  "softDelete": json["softDelete"] == null ? void 0 : CollectionDeclarationSoftDeleteFromJSON(json["softDelete"]),
@@ -78,6 +84,7 @@ export function CollectionDeclarationToJSONTyped(value, ignoreDiscriminator = fa
78
84
  "protectedFields": value["protectedFields"],
79
85
  "indexes": value["indexes"].map(CollectionDeclarationIndexesInnerToJSON),
80
86
  "refs": value["refs"].map(CollectionDeclarationRefsInnerToJSON),
87
+ "referencedBy": value["referencedBy"].map(CollectionDeclarationReferencedByInnerToJSON),
81
88
  "timestamps": CollectionDeclarationTimestampsToJSON(value["timestamps"]),
82
89
  "owner": CollectionDeclarationOwnerToJSON(value["owner"]),
83
90
  "softDelete": CollectionDeclarationSoftDeleteToJSON(value["softDelete"]),
@@ -29,7 +29,44 @@ export interface CollectionDeclarationIndexesInner {
29
29
  *
30
30
  */
31
31
  unique: boolean;
32
+ /**
33
+ * 2.6.0
34
+ */
35
+ sparse: boolean;
36
+ /**
37
+ * Declared, or added for a behaviour (owner = the index on the owner field), 2.6.0.
38
+ */
39
+ origin: CollectionDeclarationIndexesInnerOriginEnum;
40
+ /**
41
+ * The partial filter as relaxed Extended JSON (2.6.0), as index details show it.
42
+ */
43
+ partialFilterExpression?: {
44
+ [key: string]: any;
45
+ };
46
+ /**
47
+ * softDelete when the soft delete behaviour added the partial filter to a unique index (2.6.0).
48
+ */
49
+ partialFilterFrom?: CollectionDeclarationIndexesInnerPartialFilterFromEnum;
50
+ /**
51
+ * 2.6.0
52
+ */
53
+ expireAfterSeconds?: number;
32
54
  }
55
+ /**
56
+ * @export
57
+ */
58
+ export declare const CollectionDeclarationIndexesInnerOriginEnum: {
59
+ readonly Declaration: "declaration";
60
+ readonly Owner: "owner";
61
+ };
62
+ export type CollectionDeclarationIndexesInnerOriginEnum = typeof CollectionDeclarationIndexesInnerOriginEnum[keyof typeof CollectionDeclarationIndexesInnerOriginEnum];
63
+ /**
64
+ * @export
65
+ */
66
+ export declare const CollectionDeclarationIndexesInnerPartialFilterFromEnum: {
67
+ readonly SoftDelete: "softDelete";
68
+ };
69
+ export type CollectionDeclarationIndexesInnerPartialFilterFromEnum = typeof CollectionDeclarationIndexesInnerPartialFilterFromEnum[keyof typeof CollectionDeclarationIndexesInnerPartialFilterFromEnum];
33
70
  /**
34
71
  * Check if a given object implements the CollectionDeclarationIndexesInner interface.
35
72
  */
@@ -1,7 +1,16 @@
1
+ export const CollectionDeclarationIndexesInnerOriginEnum = {
2
+ Declaration: "declaration",
3
+ Owner: "owner"
4
+ };
5
+ export const CollectionDeclarationIndexesInnerPartialFilterFromEnum = {
6
+ SoftDelete: "softDelete"
7
+ };
1
8
  export function instanceOfCollectionDeclarationIndexesInner(value) {
2
9
  if (!("name" in value) || value["name"] === void 0) return false;
3
10
  if (!("key" in value) || value["key"] === void 0) return false;
4
11
  if (!("unique" in value) || value["unique"] === void 0) return false;
12
+ if (!("sparse" in value) || value["sparse"] === void 0) return false;
13
+ if (!("origin" in value) || value["origin"] === void 0) return false;
5
14
  return true;
6
15
  }
7
16
  export function CollectionDeclarationIndexesInnerFromJSON(json) {
@@ -14,7 +23,12 @@ export function CollectionDeclarationIndexesInnerFromJSONTyped(json, ignoreDiscr
14
23
  return {
15
24
  "name": json["name"],
16
25
  "key": json["key"],
17
- "unique": json["unique"]
26
+ "unique": json["unique"],
27
+ "sparse": json["sparse"],
28
+ "origin": json["origin"],
29
+ "partialFilterExpression": json["partialFilterExpression"] == null ? void 0 : json["partialFilterExpression"],
30
+ "partialFilterFrom": json["partialFilterFrom"] == null ? void 0 : json["partialFilterFrom"],
31
+ "expireAfterSeconds": json["expireAfterSeconds"] == null ? void 0 : json["expireAfterSeconds"]
18
32
  };
19
33
  }
20
34
  export function CollectionDeclarationIndexesInnerToJSON(json) {
@@ -27,6 +41,11 @@ export function CollectionDeclarationIndexesInnerToJSONTyped(value, ignoreDiscri
27
41
  return {
28
42
  "name": value["name"],
29
43
  "key": value["key"],
30
- "unique": value["unique"]
44
+ "unique": value["unique"],
45
+ "sparse": value["sparse"],
46
+ "origin": value["origin"],
47
+ "partialFilterExpression": value["partialFilterExpression"],
48
+ "partialFilterFrom": value["partialFilterFrom"],
49
+ "expireAfterSeconds": value["expireAfterSeconds"]
31
50
  };
32
51
  }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * mongocamp-library
3
+ * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
4
+ *
5
+ * The version of the OpenAPI document: 2.0.0
6
+ *
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ *
14
+ * @export
15
+ * @interface CollectionDeclarationReferencedByInner
16
+ */
17
+ export interface CollectionDeclarationReferencedByInner {
18
+ /**
19
+ * The referencing collection as the routes name it (name or database:name).
20
+ */
21
+ collection: string;
22
+ /**
23
+ *
24
+ */
25
+ database: string;
26
+ /**
27
+ * The field of the referencing collection that holds the id.
28
+ */
29
+ field: string;
30
+ /**
31
+ *
32
+ */
33
+ many: boolean;
34
+ }
35
+ /**
36
+ * Check if a given object implements the CollectionDeclarationReferencedByInner interface.
37
+ */
38
+ export declare function instanceOfCollectionDeclarationReferencedByInner(value: object): value is CollectionDeclarationReferencedByInner;
39
+ export declare function CollectionDeclarationReferencedByInnerFromJSON(json: any): CollectionDeclarationReferencedByInner;
40
+ export declare function CollectionDeclarationReferencedByInnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): CollectionDeclarationReferencedByInner;
41
+ export declare function CollectionDeclarationReferencedByInnerToJSON(json: any): CollectionDeclarationReferencedByInner;
42
+ export declare function CollectionDeclarationReferencedByInnerToJSONTyped(value?: CollectionDeclarationReferencedByInner | null, ignoreDiscriminator?: boolean): any;
@@ -0,0 +1,35 @@
1
+ export function instanceOfCollectionDeclarationReferencedByInner(value) {
2
+ if (!("collection" in value) || value["collection"] === void 0) return false;
3
+ if (!("database" in value) || value["database"] === void 0) return false;
4
+ if (!("field" in value) || value["field"] === void 0) return false;
5
+ if (!("many" in value) || value["many"] === void 0) return false;
6
+ return true;
7
+ }
8
+ export function CollectionDeclarationReferencedByInnerFromJSON(json) {
9
+ return CollectionDeclarationReferencedByInnerFromJSONTyped(json, false);
10
+ }
11
+ export function CollectionDeclarationReferencedByInnerFromJSONTyped(json, ignoreDiscriminator) {
12
+ if (json == null) {
13
+ return json;
14
+ }
15
+ return {
16
+ "collection": json["collection"],
17
+ "database": json["database"],
18
+ "field": json["field"],
19
+ "many": json["many"]
20
+ };
21
+ }
22
+ export function CollectionDeclarationReferencedByInnerToJSON(json) {
23
+ return CollectionDeclarationReferencedByInnerToJSONTyped(json, false);
24
+ }
25
+ export function CollectionDeclarationReferencedByInnerToJSONTyped(value, ignoreDiscriminator = false) {
26
+ if (value == null) {
27
+ return value;
28
+ }
29
+ return {
30
+ "collection": value["collection"],
31
+ "database": value["database"],
32
+ "field": value["field"],
33
+ "many": value["many"]
34
+ };
35
+ }
@@ -24,9 +24,9 @@ export interface CollectionDeclarationRefsInner {
24
24
  */
25
25
  collection: string;
26
26
  /**
27
- * Whether the field holds an array of ids (from the schema); missing when the schema does not tell.
27
+ * Whether the field holds an array of ids (from the schema or the declaration); false when neither tells (2.6.0, before it was missing then).
28
28
  */
29
- many?: boolean;
29
+ many: boolean;
30
30
  }
31
31
  /**
32
32
  * Check if a given object implements the CollectionDeclarationRefsInner interface.
@@ -1,6 +1,7 @@
1
1
  export function instanceOfCollectionDeclarationRefsInner(value) {
2
2
  if (!("field" in value) || value["field"] === void 0) return false;
3
3
  if (!("collection" in value) || value["collection"] === void 0) return false;
4
+ if (!("many" in value) || value["many"] === void 0) return false;
4
5
  return true;
5
6
  }
6
7
  export function CollectionDeclarationRefsInnerFromJSON(json) {
@@ -13,7 +14,7 @@ export function CollectionDeclarationRefsInnerFromJSONTyped(json, ignoreDiscrimi
13
14
  return {
14
15
  "field": json["field"],
15
16
  "collection": json["collection"],
16
- "many": json["many"] == null ? void 0 : json["many"]
17
+ "many": json["many"]
17
18
  };
18
19
  }
19
20
  export function CollectionDeclarationRefsInnerToJSON(json) {
@@ -9,6 +9,7 @@ export * from './CollectionDeclaration.js';
9
9
  export * from './CollectionDeclarationAudit.js';
10
10
  export * from './CollectionDeclarationIndexesInner.js';
11
11
  export * from './CollectionDeclarationOwner.js';
12
+ export * from './CollectionDeclarationReferencedByInner.js';
12
13
  export * from './CollectionDeclarationRefsInner.js';
13
14
  export * from './CollectionDeclarationSoftDelete.js';
14
15
  export * from './CollectionDeclarationTimestamps.js';
@@ -9,6 +9,7 @@ export * from "./CollectionDeclaration.js";
9
9
  export * from "./CollectionDeclarationAudit.js";
10
10
  export * from "./CollectionDeclarationIndexesInner.js";
11
11
  export * from "./CollectionDeclarationOwner.js";
12
+ export * from "./CollectionDeclarationReferencedByInner.js";
12
13
  export * from "./CollectionDeclarationRefsInner.js";
13
14
  export * from "./CollectionDeclarationSoftDelete.js";
14
15
  export * from "./CollectionDeclarationTimestamps.js";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The generated client with the helpers of the module (2.6.0, `@sfxcode/nuxt-mongocamp-server/client`): every API class and model of
3
+ * `/api`, `createMongocampApis` to build the 11 APIs on one configuration, and Long-safe JSON.
4
+ */
5
+ export * from './api/index.js';
6
+ export type { MongocampApisOptions } from './utils/createMongocampApis.js';
7
+ export { createMongocampApis } from './utils/createMongocampApis.js';
8
+ export { keepLongsInJson, keepLongsMiddleware, parseJsonKeepingLongs } from './utils/keepLongs.js';
@@ -0,0 +1,3 @@
1
+ export * from "./api/index.js";
2
+ export { createMongocampApis } from "./utils/createMongocampApis.js";
3
+ export { keepLongsInJson, keepLongsMiddleware, parseJsonKeepingLongs } from "./utils/keepLongs.js";
@@ -1,3 +1,7 @@
1
+ /**
2
+ * The generated clients for the browser and server side rendering, with the token of the session. Created once per Nuxt app and url
3
+ * (2.5.0; before, on every call): the token is read per request, so the clients stay valid after a login or a refresh.
4
+ */
1
5
  export declare function useMongocampApi(): {
2
6
  adminApi: import("../api/index.js").AdminApi;
3
7
  applicationApi: import("../api/index.js").ApplicationApi;
@@ -1,15 +1,21 @@
1
1
  import { useRequestFetch } from "#app";
2
2
  import { Configuration } from "../api/index.js";
3
+ import { mongocampAppState } from "../utils/appState.js";
3
4
  import { createMongocampApis } from "../utils/createMongocampApis.js";
4
5
  import { useMongocampUrl } from "./mongocampUrl.js";
5
6
  import { useMongocampStorage } from "./mongocampStorage.js";
6
7
  export function useMongocampApi() {
7
8
  const url = useMongocampUrl();
9
+ const { apis } = mongocampAppState();
10
+ const known = apis.get(url);
11
+ if (known !== void 0) {
12
+ return known;
13
+ }
8
14
  const state = useMongocampStorage();
9
15
  let fetchApi;
10
16
  if (import.meta.server && url?.startsWith("/")) {
11
17
  const requestFetch = useRequestFetch();
12
- fetchApi = ((input, init) => requestFetch.native(input, init));
18
+ fetchApi = (input, init) => requestFetch.native(input, init);
13
19
  }
14
20
  const configuration = new Configuration({
15
21
  basePath: url,
@@ -28,5 +34,7 @@ export function useMongocampApi() {
28
34
  }
29
35
  }]
30
36
  });
31
- return createMongocampApis(configuration);
37
+ const created = createMongocampApis(configuration);
38
+ apis.set(url, created);
39
+ return created;
32
40
  }
@@ -20,7 +20,7 @@ export function useMongocampApiAdmin() {
20
20
  return adminApi.updateRolesForUser({ userId, requestBody: roles });
21
21
  }
22
22
  function generateNewApiKeyForUser(userId) {
23
- return adminApi.gnerateNewApiKeyForUser({ userId });
23
+ return adminApi.generateNewApiKeyForUser({ userId });
24
24
  }
25
25
  function listRoles(filter, rowsPerPage, page) {
26
26
  return adminApi.listRoles({ filter, rowsPerPage, page });
@@ -1,24 +1,35 @@
1
- import { computed, watch } from "vue";
1
+ import { computed, effectScope, watch } from "vue";
2
2
  import { consola } from "consola";
3
+ import { mongocampAppState } from "../utils/appState.js";
3
4
  import { toMongocampError } from "../utils/toMongocampError.js";
4
5
  import { useMongocampApi } from "./mongocampApi.js";
5
6
  import { useMongocampStorage } from "./mongocampStorage.js";
6
- import { useRuntimeConfig, useState } from "#app";
7
+ import { useRuntimeConfig } from "#app";
7
8
  export function useMongocampAuth() {
8
9
  const config = useRuntimeConfig();
9
10
  const refreshToken = config.public.mongocamp?.refreshToken;
10
11
  const tokenRefreshInterval = config.public.mongocamp?.tokenRefreshInterval;
11
12
  const state = useMongocampStorage();
12
13
  const { authApi } = useMongocampApi();
13
- const updateTokenInterval = useState("mongocamp.tokenRefreshInterval", () => void 0);
14
+ const { auth } = mongocampAppState();
15
+ function updateUserState(result) {
16
+ state.value.token = result.authToken;
17
+ state.value.profile = result.userProfile;
18
+ }
19
+ const logout = () => {
20
+ if (state.value.token)
21
+ authApi.logout().catch((e) => consola.error(e));
22
+ state.value.token = "";
23
+ state.value.profile = { user: "", isAdmin: false };
24
+ };
14
25
  function startTokenRefresh() {
15
- if (import.meta.client && refreshToken && !updateTokenInterval.value)
16
- updateTokenInterval.value = window.setInterval(updateToken, tokenRefreshInterval);
26
+ if (import.meta.client && refreshToken && auth.timer === void 0)
27
+ auth.timer = setInterval(updateToken, tokenRefreshInterval);
17
28
  }
18
29
  function stopTokenRefresh() {
19
- if (import.meta.client && updateTokenInterval.value) {
20
- window.clearInterval(updateTokenInterval.value);
21
- updateTokenInterval.value = void 0;
30
+ if (auth.timer !== void 0) {
31
+ clearInterval(auth.timer);
32
+ auth.timer = void 0;
22
33
  }
23
34
  }
24
35
  async function isAuthenticated() {
@@ -35,10 +46,6 @@ export function useMongocampAuth() {
35
46
  logout();
36
47
  }
37
48
  }
38
- function updateUserState(result) {
39
- state.value.token = result.authToken;
40
- state.value.profile = result.userProfile;
41
- }
42
49
  const login = async (loginId, loginPassword) => {
43
50
  logout();
44
51
  const login2 = { userId: loginId, password: loginPassword };
@@ -46,20 +53,17 @@ export function useMongocampAuth() {
46
53
  updateUserState(result);
47
54
  return result.userProfile;
48
55
  };
49
- const logout = () => {
50
- if (state.value.token)
51
- authApi.logout().catch((e) => consola.error(e));
52
- state.value.token = "";
53
- state.value.profile = { user: "", isAdmin: false };
54
- };
55
56
  const isLoggedIn = computed(() => (state.value.token?.length ?? 0) > 0);
56
- if (import.meta.client) {
57
- watch(isLoggedIn, (loggedIn) => {
58
- if (loggedIn)
59
- startTokenRefresh();
60
- else
61
- stopTokenRefresh();
62
- }, { immediate: true });
57
+ if (import.meta.client && auth.scope === void 0) {
58
+ auth.scope = effectScope(true);
59
+ auth.scope.run(() => {
60
+ watch(isLoggedIn, (loggedIn) => {
61
+ if (loggedIn)
62
+ startTokenRefresh();
63
+ else
64
+ stopTokenRefresh();
65
+ }, { immediate: true });
66
+ });
63
67
  }
64
68
  const userRoles = computed(() => state.value.profile.roles ?? []);
65
69
  const userGrants = computed(() => state.value.profile.grants ?? []);
@@ -72,10 +72,11 @@ export interface AuditEntry<T> {
72
72
  after?: T;
73
73
  }
74
74
  /** The id of a document: the text of the path, an `ObjectId` or a document with `_id`. */
75
+ /** (2.5.0: `_id` optional, as in the type of a declared schema; a document without one is refused when it is used.) */
75
76
  export type DocumentIdentity = string | number | {
76
77
  toHexString: () => string;
77
78
  } | {
78
- _id: unknown;
79
+ _id?: unknown;
79
80
  };
80
81
  export interface TypedCollection<T, I = T, Refs = Record<string, string>> {
81
82
  readonly name: string;
@@ -1,5 +1,6 @@
1
1
  import { ref } from "vue";
2
2
  import { ResponseError } from "../api/index.js";
3
+ import { mongocampAppState } from "../utils/appState.js";
3
4
  import { documentId, fromExtendedJson, toExtendedJson } from "../utils/extendedJson.js";
4
5
  import { toMongocampError } from "../utils/toMongocampError.js";
5
6
  import { useMongocampApi } from "./mongocampApi.js";
@@ -21,7 +22,6 @@ async function requestError(error) {
21
22
  ...current !== void 0 ? { current: fromExtendedJson(current) } : {}
22
23
  });
23
24
  }
24
- const declarations = /* @__PURE__ */ new Map();
25
25
  function getPath(document, path) {
26
26
  let current = document;
27
27
  for (const segment of path.split(".")) {
@@ -65,6 +65,14 @@ function setPathOf(document, path, value) {
65
65
  function idOfReference(value) {
66
66
  return isDocument(value) ? value._id : value;
67
67
  }
68
+ function idOf(target) {
69
+ const plain = target !== null && typeof target === "object" && Object.getPrototypeOf(target) === Object.prototype;
70
+ const id = plain ? target._id : target;
71
+ if (id === void 0 || id === null) {
72
+ throw Object.assign(new Error("the document has no _id"), { status: 400, isConflict: false });
73
+ }
74
+ return documentId(target);
75
+ }
68
76
  function isDocument(target) {
69
77
  return target !== null && typeof target === "object" && "_id" in target && typeof target.toHexString !== "function";
70
78
  }
@@ -79,6 +87,7 @@ export function useMongocampCollection(name) {
79
87
  const session = useMongocampStorage();
80
88
  const defaultRows = () => config.public.mongocamp?.paginationSize;
81
89
  const declarationKey = `${useMongocampUrl()}|${name}`;
90
+ const { declarations } = mongocampAppState();
82
91
  const declaration = declarations.get(declarationKey) ?? ref(void 0);
83
92
  declarations.set(declarationKey, declaration);
84
93
  let loading;
@@ -86,9 +95,15 @@ export function useMongocampCollection(name) {
86
95
  if (declaration.value !== void 0) {
87
96
  return Promise.resolve(declaration.value);
88
97
  }
89
- loading ??= Promise.resolve().then(() => collectionApi.getCollectionDeclaration({ collectionName: name })).catch(() => null).then((loaded) => {
98
+ loading ??= Promise.resolve().then(() => collectionApi.getCollectionDeclaration({ collectionName: name })).then((loaded) => {
90
99
  declaration.value = loaded;
91
100
  return loaded;
101
+ }, (error) => {
102
+ if (error instanceof ResponseError && error.response.status === 404) {
103
+ declaration.value = null;
104
+ }
105
+ loading = void 0;
106
+ return null;
92
107
  });
93
108
  return loading;
94
109
  };
@@ -170,7 +185,7 @@ export function useMongocampCollection(name) {
170
185
  findById: async (id, options = {}) => {
171
186
  const expand = options.expand !== void 0 && options.expand.length > 0 ? { expand: [...options.expand] } : {};
172
187
  try {
173
- return markExpanded(fromExtendedJson(await documentApi.getDocument({ collectionName: name, documentId: documentId(id), ...expand })), options.expand);
188
+ return markExpanded(fromExtendedJson(await documentApi.getDocument({ collectionName: name, documentId: idOf(id), ...expand })), options.expand);
174
189
  } catch (error) {
175
190
  if (error instanceof ResponseError && error.response.status === 404) {
176
191
  return null;
@@ -182,16 +197,16 @@ export function useMongocampCollection(name) {
182
197
  insertMany: (documents) => call(async () => documentApi.insertMany({ collectionName: name, requestBody: await Promise.all(documents.map(async (document) => toExtendedJson(await writable(document)))) })),
183
198
  replace: (target, document, options) => withRetry(target, options, (current) => call(async () => (requireComplete(document), documentApi.update({
184
199
  collectionName: name,
185
- documentId: documentId(current),
200
+ documentId: idOf(current),
186
201
  requestBody: toExtendedJson({ ...await writable(document), ...await versionOf(current) })
187
202
  })))),
188
203
  update: (target, changes, options) => withRetry(target, options, (current) => call(async () => documentApi.updateDocumentPartial({
189
204
  collectionName: name,
190
- documentId: documentId(current),
205
+ documentId: idOf(current),
191
206
  requestBody: toExtendedJson({ ...await writable(changes), ...await versionOf(current) })
192
207
  }))),
193
- remove: (id) => call(() => documentApi._delete({ collectionName: name, documentId: documentId(id) })),
194
- restore: (id) => call(() => documentApi.restoreDocument({ collectionName: name, documentId: documentId(id) })),
208
+ remove: (id) => call(() => documentApi._delete({ collectionName: name, documentId: idOf(id) })),
209
+ restore: (id) => call(() => documentApi.restoreDocument({ collectionName: name, documentId: idOf(id) })),
195
210
  isMine: (document) => {
196
211
  if (declaration.value === void 0) {
197
212
  void loadDeclaration();
@@ -201,10 +216,11 @@ export function useMongocampCollection(name) {
201
216
  return field !== void 0 && document !== null && document !== void 0 && session.value.profile.user !== "" && getPath(document, field) === session.value.profile.user;
202
217
  },
203
218
  history: (id, options = {}) => call(async () => {
204
- const records = await documentApi.getDocumentHistory({ collectionName: name, documentId: documentId(id), page: options.page ?? 1, rowsPerPage: options.rowsPerPage ?? defaultRows() });
219
+ const records = await documentApi.getDocumentHistory({ collectionName: name, documentId: idOf(id), page: options.page ?? 1, rowsPerPage: options.rowsPerPage ?? defaultRows() });
205
220
  return records.map((record) => fromExtendedJson(record));
206
221
  }),
207
222
  paginated: (options = {}) => {
223
+ let latest = 0;
208
224
  const state = {
209
225
  documents: ref([]),
210
226
  page: ref(1),
@@ -214,17 +230,24 @@ export function useMongocampCollection(name) {
214
230
  pending: ref(false),
215
231
  error: ref(null),
216
232
  load: async () => {
233
+ const request = ++latest;
217
234
  state.pending.value = true;
218
235
  state.error.value = null;
219
236
  try {
220
237
  const result = await findPage({ ...options, page: state.page.value, rowsPerPage: state.rowsPerPage.value });
221
- state.documents.value = result.documents;
222
- state.countRows.value = result.countRows;
223
- state.countPages.value = result.countPages;
238
+ if (request === latest) {
239
+ state.documents.value = result.documents;
240
+ state.countRows.value = result.countRows;
241
+ state.countPages.value = result.countPages;
242
+ }
224
243
  } catch (error) {
225
- state.error.value = error;
244
+ if (request === latest) {
245
+ state.error.value = error;
246
+ }
226
247
  } finally {
227
- state.pending.value = false;
248
+ if (request === latest) {
249
+ state.pending.value = false;
250
+ }
228
251
  }
229
252
  },
230
253
  goToPage: (target) => {
@@ -4,6 +4,7 @@ import { createSseParser } from "../utils/sse.js";
4
4
  import { markExpanded, useMongocampCollection } from "./mongocampCollection.js";
5
5
  import { useMongocampStorage } from "./mongocampStorage.js";
6
6
  import { useMongocampUrl } from "./mongocampUrl.js";
7
+ import { useRuntimeConfig } from "#app";
7
8
  const MaxBackoff = 3e4;
8
9
  const RefetchDebounce = 200;
9
10
  let pollingExplained = false;
@@ -11,6 +12,7 @@ export function useMongocampLive(name, options = {}) {
11
12
  const collection = useMongocampCollection(name);
12
13
  const url = useMongocampUrl();
13
14
  const session = useMongocampStorage();
15
+ const rows = options.rowsPerPage ?? useRuntimeConfig().public.mongocamp?.paginationSize;
14
16
  const mode = options.mode ?? (options.sort !== void 0 || options.page !== void 0 ? "refetch" : "patch");
15
17
  const documents = ref([]);
16
18
  const status = ref("idle");
@@ -30,14 +32,17 @@ export function useMongocampLive(name, options = {}) {
30
32
  error.value = caught;
31
33
  }
32
34
  };
35
+ const refetch = () => {
36
+ clearTimeout(debounce);
37
+ debounce = setTimeout(() => void reload(), RefetchDebounce);
38
+ };
33
39
  const apply = (event, data) => {
34
40
  if (event === "reset") {
35
41
  void reload();
36
42
  return;
37
43
  }
38
44
  if (mode === "refetch") {
39
- clearTimeout(debounce);
40
- debounce = setTimeout(() => void reload(), RefetchDebounce);
45
+ refetch();
41
46
  return;
42
47
  }
43
48
  const id = documentId(fromExtendedJson({ _id: data.id }));
@@ -47,6 +52,10 @@ export function useMongocampLive(name, options = {}) {
47
52
  } else if (event === "upsert" && data.document !== void 0) {
48
53
  const document = markExpanded(fromExtendedJson(data.document), options.expand);
49
54
  const index = documents.value.findIndex((entry) => documentId(entry) === id);
55
+ if (index < 0 && typeof rows === "number" && rows > 0 && documents.value.length >= rows) {
56
+ refetch();
57
+ return;
58
+ }
50
59
  documents.value = index < 0 ? [...documents.value, document] : documents.value.map((entry, position) => position === index ? document : entry);
51
60
  }
52
61
  };
@@ -98,7 +107,14 @@ export function useMongocampLive(name, options = {}) {
98
107
  if (message.id !== void 0) {
99
108
  lastEventId = message.id;
100
109
  }
101
- apply(message.event, message.data === "" ? {} : JSON.parse(message.data));
110
+ let data;
111
+ try {
112
+ data = message.data === "" ? {} : JSON.parse(message.data);
113
+ } catch {
114
+ error.value = new Error(`live updates: the data of the event ${message.event} is no JSON`);
115
+ return;
116
+ }
117
+ apply(message.event, data);
102
118
  });
103
119
  const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
104
120
  for (; ; ) {
@@ -131,6 +147,12 @@ export function useMongocampLive(name, options = {}) {
131
147
  }
132
148
  stopped = false;
133
149
  await reload();
150
+ const failedStatus = error.value?.status;
151
+ if (failedStatus === 401 || failedStatus === 403) {
152
+ status.value = "error";
153
+ stopped = true;
154
+ return;
155
+ }
134
156
  void connect();
135
157
  };
136
158
  const stop = () => {
@@ -31,15 +31,24 @@ export function usePaginatedFind(collectionName, options = {}) {
31
31
  const documents = ref([]);
32
32
  const pending = ref(false);
33
33
  const error = ref(null);
34
+ let latest = 0;
34
35
  async function load() {
36
+ const request = ++latest;
35
37
  pending.value = true;
36
38
  error.value = null;
37
39
  try {
38
- documents.value = await find(collectionName, options.filter, page.value, options.sort, options.projection, rowsPerPage.value, options.filterCaseInsensitive);
40
+ const loaded = await find(collectionName, options.filter, page.value, options.sort, options.projection, rowsPerPage.value, options.filterCaseInsensitive);
41
+ if (request === latest) {
42
+ documents.value = loaded;
43
+ }
39
44
  } catch (e) {
40
- error.value = e;
45
+ if (request === latest) {
46
+ error.value = e;
47
+ }
41
48
  } finally {
42
- pending.value = false;
49
+ if (request === latest) {
50
+ pending.value = false;
51
+ }
43
52
  }
44
53
  }
45
54
  function goToPage(target) {
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The BSON values of decoded documents in the payload of server side rendering (2.5.0): `useAsyncData(() => orders.find())` puts
3
+ * `ObjectId`s, `Decimal128`s, ... into the payload, which devalue cannot write (before: a 500 of the page as soon as a document was found).
4
+ * They travel as canonical Extended JSON and are values again in the browser. A payload plugin, so the reviver exists before the payload
5
+ * is read.
6
+ */
7
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
8
+ export default _default;