@sfxcode/nuxt-mongocamp-server 2.2.0 → 2.4.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 (77) hide show
  1. package/README.md +74 -1
  2. package/dist/module.d.mts +10 -0
  3. package/dist/module.json +1 -1
  4. package/dist/module.mjs +95 -11
  5. package/dist/runtime/api/.openapi-generator/FILES +22 -0
  6. package/dist/runtime/api/apis/CollectionApi.d.ts +124 -0
  7. package/dist/runtime/api/apis/CollectionApi.js +205 -0
  8. package/dist/runtime/api/apis/DocumentApi.d.ts +162 -0
  9. package/dist/runtime/api/apis/DocumentApi.js +229 -0
  10. package/dist/runtime/api/docs/AuditEntry.md +52 -0
  11. package/dist/runtime/api/docs/CollectionApi.md +269 -2
  12. package/dist/runtime/api/docs/CollectionDeclaration.md +56 -0
  13. package/dist/runtime/api/docs/CollectionDeclarationAudit.md +34 -0
  14. package/dist/runtime/api/docs/CollectionDeclarationIndexesInner.md +38 -0
  15. package/dist/runtime/api/docs/CollectionDeclarationOwner.md +36 -0
  16. package/dist/runtime/api/docs/CollectionDeclarationRefsInner.md +38 -0
  17. package/dist/runtime/api/docs/CollectionDeclarationSoftDelete.md +36 -0
  18. package/dist/runtime/api/docs/CollectionDeclarationTimestamps.md +40 -0
  19. package/dist/runtime/api/docs/CollectionDeclarationVersioned.md +38 -0
  20. package/dist/runtime/api/docs/DocumentApi.md +303 -8
  21. package/dist/runtime/api/docs/ErrorDescription.md +4 -0
  22. package/dist/runtime/api/docs/ExportRequest.md +2 -0
  23. package/dist/runtime/api/docs/MongoFindRequest.md +2 -0
  24. package/dist/runtime/api/docs/SearchRequest.md +2 -0
  25. package/dist/runtime/api/docs/SuggestedValidator.md +40 -0
  26. package/dist/runtime/api/docs/ValidationIssue.md +36 -0
  27. package/dist/runtime/api/models/AuditEntry.d.ts +88 -0
  28. package/dist/runtime/api/models/AuditEntry.js +56 -0
  29. package/dist/runtime/api/models/CollectionDeclaration.d.ts +90 -0
  30. package/dist/runtime/api/models/CollectionDeclaration.js +87 -0
  31. package/dist/runtime/api/models/CollectionDeclarationAudit.d.ts +30 -0
  32. package/dist/runtime/api/models/CollectionDeclarationAudit.js +26 -0
  33. package/dist/runtime/api/models/CollectionDeclarationIndexesInner.d.ts +40 -0
  34. package/dist/runtime/api/models/CollectionDeclarationIndexesInner.js +32 -0
  35. package/dist/runtime/api/models/CollectionDeclarationOwner.d.ts +43 -0
  36. package/dist/runtime/api/models/CollectionDeclarationOwner.js +34 -0
  37. package/dist/runtime/api/models/CollectionDeclarationRefsInner.d.ts +38 -0
  38. package/dist/runtime/api/models/CollectionDeclarationRefsInner.js +31 -0
  39. package/dist/runtime/api/models/CollectionDeclarationSoftDelete.d.ts +34 -0
  40. package/dist/runtime/api/models/CollectionDeclarationSoftDelete.js +28 -0
  41. package/dist/runtime/api/models/CollectionDeclarationTimestamps.d.ts +42 -0
  42. package/dist/runtime/api/models/CollectionDeclarationTimestamps.js +33 -0
  43. package/dist/runtime/api/models/CollectionDeclarationVersioned.d.ts +46 -0
  44. package/dist/runtime/api/models/CollectionDeclarationVersioned.js +36 -0
  45. package/dist/runtime/api/models/ErrorDescription.d.ts +11 -0
  46. package/dist/runtime/api/models/ErrorDescription.js +10 -2
  47. package/dist/runtime/api/models/ExportRequest.d.ts +4 -0
  48. package/dist/runtime/api/models/ExportRequest.js +4 -2
  49. package/dist/runtime/api/models/MongoFindRequest.d.ts +4 -0
  50. package/dist/runtime/api/models/MongoFindRequest.js +4 -2
  51. package/dist/runtime/api/models/SearchRequest.d.ts +4 -0
  52. package/dist/runtime/api/models/SearchRequest.js +4 -2
  53. package/dist/runtime/api/models/SuggestedValidator.d.ts +44 -0
  54. package/dist/runtime/api/models/SuggestedValidator.js +35 -0
  55. package/dist/runtime/api/models/ValidationIssue.d.ts +34 -0
  56. package/dist/runtime/api/models/ValidationIssue.js +29 -0
  57. package/dist/runtime/api/models/index.d.ts +11 -0
  58. package/dist/runtime/api/models/index.js +11 -0
  59. package/dist/runtime/composables/mongocampApiCollection.d.ts +14 -15
  60. package/dist/runtime/composables/mongocampApiCollection.js +4 -1
  61. package/dist/runtime/composables/mongocampCollection.d.ts +144 -0
  62. package/dist/runtime/composables/mongocampCollection.js +241 -0
  63. package/dist/runtime/composables/mongocampLive.d.ts +36 -0
  64. package/dist/runtime/composables/mongocampLive.js +155 -0
  65. package/dist/runtime/composables/mongocampSearch.d.ts +12 -9
  66. package/dist/runtime/server/composables/mongocampServerCollection.d.ts +20 -0
  67. package/dist/runtime/server/composables/mongocampServerCollection.js +12 -0
  68. package/dist/runtime/server/mongocamp/plugin.d.ts +2 -2
  69. package/dist/runtime/server/mongocamp/plugin.js +10 -1
  70. package/dist/runtime/types/collections.d.ts +30 -0
  71. package/dist/runtime/types/collections.js +0 -0
  72. package/dist/runtime/utils/sse.d.ts +10 -0
  73. package/dist/runtime/utils/sse.js +33 -0
  74. package/dist/runtime/utils/toMongocampError.d.ts +7 -0
  75. package/dist/runtime/utils/toMongocampError.js +18 -1
  76. package/dist/types.d.mts +6 -0
  77. package/package.json +4 -3
@@ -1,4 +1,5 @@
1
- import { metrics, requestLogging } from "@sfxcode/mongocamp-server-core";
1
+ import { collections, metrics, requestLogging } from "@sfxcode/mongocamp-server-core";
2
+ import { declarations } from "#mongocamp/collections";
2
3
  import { definePlugin } from "nitro";
3
4
  import { useRuntimeConfig } from "nitro/runtime-config";
4
5
  import { closeMongocampCore } from "./core.js";
@@ -11,6 +12,14 @@ export default definePlugin((nitroApp) => {
11
12
  if (settings.metrics) {
12
13
  await server.use(metrics());
13
14
  }
15
+ if (declarations.length > 0) {
16
+ for (const { key, source, definition } of declarations) {
17
+ if (definition.name !== key) {
18
+ server.context.logger.warn(`[mongocamp] ${source} declares the collection ${definition.name}: the types of the client know it as ${key}, name the file ${definition.name}.ts`);
19
+ }
20
+ }
21
+ await server.use(collections(declarations.map(({ source, definition }) => ({ ...definition, source }))));
22
+ }
14
23
  });
15
24
  nitroApp.hooks?.hook("close", async () => {
16
25
  await closeMongocampCore();
@@ -0,0 +1,30 @@
1
+ import type { ExpandedDocument } from '@sfxcode/mongocamp-server-core';
2
+ /**
3
+ * The documents of the declared collections by name (2.3.0). Empty here; the module adds the declarations of `server/mongocamp/*.ts`
4
+ * (`.nuxt/types/mongocamp-collections.d.ts`), an application without the module's scan (external server) can add its own:
5
+ *
6
+ * declare global { interface MongocampCollections { orders: Order } }
7
+ */
8
+ declare global {
9
+ /** The documents as they are stored (the output of the schema). */
10
+ interface MongocampCollections {
11
+ }
12
+ /** The documents as they are written (the input of the schema: fields with a default are optional); falls back to the stored type. */
13
+ interface MongocampCollectionInputs {
14
+ }
15
+ /** The references of the declared collections (2.4.0): `{ orders: { customerId: 'customers' } }`. */
16
+ interface MongocampCollectionRefs {
17
+ }
18
+ }
19
+ /** The names of the declared collections. */
20
+ export type MongocampCollectionName = keyof MongocampCollections & string;
21
+ /** The stored document type of a declared collection. */
22
+ export type MongocampDocument<N extends MongocampCollectionName> = MongocampCollections[N];
23
+ /** The written document type of a declared collection. */
24
+ export type MongocampDocumentInput<N extends MongocampCollectionName> = N extends keyof MongocampCollectionInputs ? MongocampCollectionInputs[N] : MongocampCollections[N];
25
+ /** The references of a declared collection: field → target collection name (`{}` without refs). */
26
+ export type MongocampRefs<N extends MongocampCollectionName> = N extends keyof MongocampCollectionRefs ? MongocampCollectionRefs[N] : Record<never, never>;
27
+ /** The fields of a declared collection that a read can expand. */
28
+ export type MongocampRefName<N extends MongocampCollectionName> = keyof MongocampRefs<N> & string;
29
+ /** A document of a declared collection with the references `K` expanded (target document or `null`, arrays of documents). */
30
+ export type MongocampExpandedDocument<N extends MongocampCollectionName, K extends MongocampRefName<N>> = ExpandedDocument<MongocampDocument<N>, MongocampRefs<N>, K, MongocampCollections>;
File without changes
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A parser of Server-Sent Events for a stream read with `fetch` (2.3.0, live updates): `EventSource` cannot send the bearer token of the
3
+ * session. Feed it the decoded text as it arrives; it calls `onEvent` for every complete event (comments are skipped).
4
+ */
5
+ export interface SseEvent {
6
+ id?: string;
7
+ event: string;
8
+ data: string;
9
+ }
10
+ export declare function createSseParser(onEvent: (event: SseEvent) => void): (chunk: string) => void;
@@ -0,0 +1,33 @@
1
+ export function createSseParser(onEvent) {
2
+ let buffer = "";
3
+ return (chunk) => {
4
+ buffer += chunk.replace(/\r\n?/g, "\n");
5
+ let end = buffer.indexOf("\n\n");
6
+ while (end >= 0) {
7
+ const block = buffer.slice(0, end);
8
+ buffer = buffer.slice(end + 2);
9
+ let id;
10
+ let event = "message";
11
+ const data = [];
12
+ for (const line of block.split("\n")) {
13
+ if (line === "" || line.startsWith(":")) {
14
+ continue;
15
+ }
16
+ const colon = line.indexOf(":");
17
+ const field = colon < 0 ? line : line.slice(0, colon);
18
+ const value = colon < 0 ? "" : line.slice(colon + 1).replace(/^ /, "");
19
+ if (field === "id") {
20
+ id = value;
21
+ } else if (field === "event") {
22
+ event = value;
23
+ } else if (field === "data") {
24
+ data.push(value);
25
+ }
26
+ }
27
+ if (data.length > 0 || event !== "message") {
28
+ onEvent({ ...id !== void 0 ? { id } : {}, event, data: data.join("\n") });
29
+ }
30
+ end = buffer.indexOf("\n\n");
31
+ }
32
+ };
33
+ }
@@ -1,7 +1,14 @@
1
+ import type { ValidationIssue } from '../api/index.js';
1
2
  export interface MongocampError {
2
3
  status?: number;
3
4
  message: string;
4
5
  additionalInfo?: string;
6
+ /** The fields of a document that is not valid (400 of a schema, a before-hook or the validator of the collection). */
7
+ issues?: ValidationIssue[];
8
+ /** The error code of the server (`x-error-code`, e.g. 4092 for a version conflict). */
9
+ code?: number;
10
+ /** The stored document of a version conflict (409, code 4092), as the server sends it (Extended JSON). */
11
+ current?: Record<string, unknown>;
5
12
  cause: unknown;
6
13
  }
7
14
  export declare function toMongocampError(error: unknown): Promise<MongocampError>;
@@ -4,14 +4,31 @@ export async function toMongocampError(error) {
4
4
  const status = error.response.status;
5
5
  let message = `Request failed with status ${status}`;
6
6
  let additionalInfo;
7
+ let issues;
8
+ let code;
9
+ let current;
7
10
  try {
8
11
  const body = await error.response.clone().json();
9
12
  if (body?.msg)
10
13
  message = body.msg;
11
14
  additionalInfo = body?.additionalInfo;
15
+ if (Array.isArray(body?.issues))
16
+ issues = body.issues;
17
+ if (typeof body?.code === "number")
18
+ code = body.code;
19
+ if (body?.current && typeof body.current === "object")
20
+ current = body.current;
12
21
  } catch {
13
22
  }
14
- return { status, message, additionalInfo, cause: error };
23
+ return {
24
+ status,
25
+ message,
26
+ additionalInfo,
27
+ ...issues ? { issues } : {},
28
+ ...code !== void 0 ? { code } : {},
29
+ ...current ? { current } : {},
30
+ cause: error
31
+ };
15
32
  }
16
33
  if (error instanceof Error)
17
34
  return { message: error.message, cause: error };
package/dist/types.d.mts CHANGED
@@ -1,3 +1,9 @@
1
+ export { type AuditEntry, type CollectionFindOptions, type CollectionPage, type DocumentIdentity, type MongocampRequestError, type PaginatedCollection, type TypedCollection } from '../dist/runtime/composables/mongocampCollection.js'
2
+
3
+ export { type MongocampCollectionName, type MongocampDocument, type MongocampDocumentInput } from '../dist/runtime/types/collections.js'
4
+
5
+ export { type LiveOptions, type LiveState, type LiveStatus } from '../dist/runtime/composables/mongocampLive.js'
6
+
1
7
  export { default } from './module.mjs'
2
8
 
3
9
  export { type ModuleOptions, type ServerOptions } from './module.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfxcode/nuxt-mongocamp-server",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@nuxt/kit": "^4.7.0",
32
- "@sfxcode/mongocamp-server-core": "2.2.0",
32
+ "@sfxcode/mongocamp-server-core": "2.4.0",
33
33
  "@vueuse/nuxt": "^15.1.0",
34
34
  "bson": "^7.3.4",
35
35
  "consola": "^3.4.2",
@@ -53,7 +53,8 @@
53
53
  "nuxt": "^4.7.0",
54
54
  "srvx": "1.0.8",
55
55
  "typescript": "6.0.3",
56
- "vitest": "^5.1.2"
56
+ "vitest": "^5.1.2",
57
+ "zod": "^4.6.7"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "nitro": ">=3.0.261103-rc",