@lastshotlabs/bunshot 0.0.18 → 0.0.20

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.
@@ -1,3 +1,5 @@
1
1
  export { connectMongo, connectAuthMongo, connectAppMongo, disconnectMongo, authConnection, appConnection, mongoose } from "../lib/mongo";
2
2
  export { mongoAuthAdapter } from "../adapters/mongoAuth";
3
3
  export { AuthUser } from "../models/AuthUser";
4
+ export { zodToMongoose } from "../lib/zodToMongoose";
5
+ export type { ZodToMongooseConfig, ZodToMongooseRefConfig } from "../lib/zodToMongoose";
@@ -1,3 +1,4 @@
1
1
  export { connectMongo, connectAuthMongo, connectAppMongo, disconnectMongo, authConnection, appConnection, mongoose } from "../lib/mongo";
2
2
  export { mongoAuthAdapter } from "../adapters/mongoAuth";
3
3
  export { AuthUser } from "../models/AuthUser";
4
+ export { zodToMongoose } from "../lib/zodToMongoose";
@@ -1,4 +1,4 @@
1
- import { Schema } from "mongoose";
1
+ import type { Schema as SchemaType } from "mongoose";
2
2
  type ZodSchema = any;
3
3
  export type ZodToMongooseRefConfig = {
4
4
  /** DB field name (e.g., "account") */
@@ -14,7 +14,7 @@ export type ZodToMongooseConfig = {
14
14
  /** Override Mongoose type for specific fields (e.g., { date: { type: Date, required: true } }) */
15
15
  typeOverrides?: Record<string, unknown>;
16
16
  /** Subdocument array fields: { items: mongooseSubSchema } */
17
- subdocSchemas?: Record<string, Schema>;
17
+ subdocSchemas?: Record<string, SchemaType>;
18
18
  };
19
19
  /**
20
20
  * Derive a Mongoose SchemaDefinition from a Zod object schema.
@@ -1,4 +1,4 @@
1
- import { Schema } from "mongoose";
1
+ import { mongoose } from "./mongo";
2
2
  /** Unwrap nullable, optional, and default wrappers to get the core Zod type */
3
3
  function unwrap(zodType) {
4
4
  let t = zodType;
@@ -22,6 +22,10 @@ function unwrap(zodType) {
22
22
  }
23
23
  return { core: t, required };
24
24
  }
25
+ /** Lazily access the Mongoose Schema class (avoids top-level require of mongoose) */
26
+ function getSchema() {
27
+ return mongoose.Schema;
28
+ }
25
29
  /** Convert a single Zod type to a Mongoose field definition */
26
30
  function toMongooseField(zodType) {
27
31
  const { core, required } = unwrap(zodType);
@@ -36,7 +40,7 @@ function toMongooseField(zodType) {
36
40
  return { type: Date, required };
37
41
  if (defType === "enum")
38
42
  return { type: String, enum: core.options, required };
39
- return { type: Schema.Types.Mixed, required };
43
+ return { type: getSchema().Types.Mixed, required };
40
44
  }
41
45
  /**
42
46
  * Derive a Mongoose SchemaDefinition from a Zod object schema.
@@ -64,7 +68,7 @@ export function zodToMongoose(zodSchema, config = {}) {
64
68
  continue;
65
69
  if (config.refs?.[apiField]) {
66
70
  const { dbField, ref } = config.refs[apiField];
67
- fields[dbField] = { type: Schema.Types.ObjectId, ref, required: true };
71
+ fields[dbField] = { type: getSchema().Types.ObjectId, ref, required: true };
68
72
  continue;
69
73
  }
70
74
  if (config.typeOverrides?.[apiField]) {
@@ -15,17 +15,32 @@ export const cors = async (req, next) => {
15
15
  };
16
16
  function corsHeaders(requestOrigin) {
17
17
  let allowOrigin;
18
+ let withCredentials = false;
18
19
  if (_allowedOrigins === "*") {
19
20
  allowOrigin = "*";
20
21
  }
21
22
  else {
22
23
  const origins = Array.isArray(_allowedOrigins) ? _allowedOrigins : [_allowedOrigins];
23
- allowOrigin = requestOrigin && origins.includes(requestOrigin) ? requestOrigin : origins[0];
24
+ // Filter out "*" wildcard is incompatible with credentials
25
+ const specific = origins.filter((o) => o !== "*");
26
+ if (requestOrigin && specific.includes(requestOrigin)) {
27
+ allowOrigin = requestOrigin;
28
+ withCredentials = true;
29
+ }
30
+ else if (origins.includes("*")) {
31
+ // Fallback: reflect the request origin so credentials still work
32
+ allowOrigin = requestOrigin ?? "*";
33
+ withCredentials = !!requestOrigin;
34
+ }
35
+ else {
36
+ allowOrigin = specific[0] ?? "*";
37
+ }
24
38
  }
25
39
  return {
26
40
  "Access-Control-Allow-Origin": allowOrigin,
27
41
  "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
28
- "Access-Control-Allow-Headers": "Content-Type, Authorization",
42
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, x-user-token, x-csrf-token, x-refresh-token",
43
+ ...(withCredentials ? { "Access-Control-Allow-Credentials": "true" } : {}),
29
44
  ...(allowOrigin !== "*" ? { Vary: "Origin" } : {}),
30
45
  };
31
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lastshotlabs/bunshot",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "Batteries-included Bun + Hono API framework — auth, sessions, rate limiting, WebSocket, queues, and OpenAPI docs out of the box",
5
5
  "repository": {
6
6
  "type": "git",
@@ -52,7 +52,8 @@
52
52
  "start": "bun src/index.ts",
53
53
  "readme": "bun docs/build-readme.ts",
54
54
  "readme:npm": "bun docs/build-readme.ts npm",
55
- "test": "bun test tests/unit tests/integration",
55
+ "test": "bun test tests/unit tests/integration && bun test tests/isolated",
56
+ "test:isolated": "bun test tests/isolated",
56
57
  "test:coverage": "bun test --coverage tests/unit tests/integration",
57
58
  "test:docker:up": "docker compose -f docker-compose.test.yml up -d --wait",
58
59
  "test:docker:down": "docker compose -f docker-compose.test.yml down",
@@ -61,6 +62,7 @@
61
62
  "test:coverage:full": "bun run test:docker:up && bun test --coverage --config bunfig.ci.toml tests/unit tests/integration tests/docker; bun run test:docker:down"
62
63
  },
63
64
  "dependencies": {
65
+ "@asteasolutions/zod-to-openapi": "^8.4.1",
64
66
  "@hono/zod-openapi": "1.2.2",
65
67
  "@scalar/hono-api-reference": "0.10.0",
66
68
  "arctic": "^3.7.0",