@caprogramacion/mongo-call 1.0.0 → 1.1.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.
- package/README.md +7 -7
- package/dist/server/index.d.ts +2 -8
- package/dist/server/index.js +1 -53
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @caprogramacion/mongo-call
|
|
2
2
|
|
|
3
3
|
Firebase `onCall`-style RPC wrapper for MongoDB, replicating the `firebase-functions`
|
|
4
4
|
developer experience (`onCall` / `httpsCallable`) over Express + MongoDB with
|
|
@@ -20,7 +20,7 @@ production-grade security enabled by default.
|
|
|
20
20
|
## Install
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
pnpm add @
|
|
23
|
+
pnpm add @caprogramacion/mongo-call
|
|
24
24
|
# optional: argon2 for Argon2id hashing (falls back to node:crypto scrypt)
|
|
25
25
|
pnpm add argon2
|
|
26
26
|
```
|
|
@@ -59,7 +59,7 @@ import {
|
|
|
59
59
|
CallError,
|
|
60
60
|
toObjectId,
|
|
61
61
|
SignJWT
|
|
62
|
-
} from '@
|
|
62
|
+
} from '@caprogramacion/mongo-call'
|
|
63
63
|
|
|
64
64
|
const signIn = onCall<{ email: string; password: string }>(
|
|
65
65
|
{
|
|
@@ -134,14 +134,14 @@ When `AUTH_ENABLED` is not disabled, the server mounts:
|
|
|
134
134
|
- `POST {base}/auth/change-password` — rotates `tokenVersion` (revokes previous tokens)
|
|
135
135
|
- `POST {base}/auth/logout` — clears session cookies
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
`
|
|
137
|
+
The library connects to an existing MongoDB and assumes the `users` and `_roles`
|
|
138
|
+
collections already exist. Users store a `role` field; role metadata
|
|
139
|
+
(`isActive`, `readOnly`) lives in `_roles`.
|
|
140
140
|
|
|
141
141
|
## Client SDK
|
|
142
142
|
|
|
143
143
|
```ts
|
|
144
|
-
import { createCallClient } from '@
|
|
144
|
+
import { createCallClient } from '@caprogramacion/mongo-call/client'
|
|
145
145
|
|
|
146
146
|
const client = createCallClient({
|
|
147
147
|
baseUrl: 'http://localhost:4000',
|
package/dist/server/index.d.ts
CHANGED
|
@@ -132,11 +132,6 @@ interface AuthConfig {
|
|
|
132
132
|
loginRateLimit?: RateLimitConfig;
|
|
133
133
|
allowPasswordRotationGate?: boolean;
|
|
134
134
|
}
|
|
135
|
-
interface BootstrapConfig {
|
|
136
|
-
email?: string;
|
|
137
|
-
password?: string;
|
|
138
|
-
displayName?: string;
|
|
139
|
-
}
|
|
140
135
|
interface ServerConfig {
|
|
141
136
|
mongoUri?: string;
|
|
142
137
|
dbName?: string;
|
|
@@ -158,7 +153,6 @@ interface ServerConfig {
|
|
|
158
153
|
swagger?: SwaggerConfig;
|
|
159
154
|
minPasswordLength?: number;
|
|
160
155
|
passwordHashAlgo?: PasswordHashAlgo;
|
|
161
|
-
bootstrap?: BootstrapConfig;
|
|
162
156
|
apiBasePath?: string;
|
|
163
157
|
port?: number;
|
|
164
158
|
}
|
|
@@ -201,7 +195,7 @@ type CallHandler<TInput, TOutput> = (data: TInput, context: CallContext) => Prom
|
|
|
201
195
|
interface CallableDefinition<TInput = unknown, TOutput = unknown> {
|
|
202
196
|
__isMongoCallable: true;
|
|
203
197
|
options: OnCallOptions;
|
|
204
|
-
handler:
|
|
198
|
+
handler(data: TInput, context: CallContext): Promise<TOutput> | TOutput;
|
|
205
199
|
}
|
|
206
200
|
declare function onCall<TInput = unknown, TOutput = unknown>(handler: CallHandler<TInput, TOutput>): CallableDefinition<TInput, TOutput>;
|
|
207
201
|
declare function onCall<TInput = unknown, TOutput = unknown>(options: OnCallOptions, handler: CallHandler<TInput, TOutput>): CallableDefinition<TInput, TOutput>;
|
|
@@ -268,4 +262,4 @@ declare class CallError extends Error {
|
|
|
268
262
|
declare const isCallError: (err: unknown) => err is CallError;
|
|
269
263
|
|
|
270
264
|
export { CallError, InMemoryUserStore, MongoUserStore, assertAllowedRole, assertPasswordComplexity, closeDatabase, createAjvInstance, createCallApp, createLazyUserStore, ensureMongoIndexes, getDatabase, hashPassword, isCallError, loadEnv, loadServerConfig, onCall, pingDatabase, resolveCallerRole, sanitizeNoSqlPayload, signSessionToken, startCallServer, toObjectId, validatePayload, verifyPassword, verifySessionToken };
|
|
271
|
-
export type { AuthConfig, AuthContext,
|
|
265
|
+
export type { AuthConfig, AuthContext, CallContext, CallErrorCode, CallHandler, CallableDefinition, CallerAccess, CookieConfig, DbProvider, JwtVerifyConfig, NoSqlSanitizeMode, OnCallOptions, PasswordHashAlgo, RateLimitConfig, ServerConfig, SessionClaims, StoredDocument, SwaggerConfig, UserStore, ValidatorConfig };
|
package/dist/server/index.js
CHANGED
|
@@ -1168,41 +1168,6 @@ var createAuthRouter = ({
|
|
|
1168
1168
|
return router;
|
|
1169
1169
|
};
|
|
1170
1170
|
|
|
1171
|
-
// src/server/bootstrap.ts
|
|
1172
|
-
var EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
1173
|
-
var ensureBootstrapAdmin = async (store, options = {}) => {
|
|
1174
|
-
const email = (options.email ?? "").trim().toLowerCase();
|
|
1175
|
-
const password = options.password ?? "";
|
|
1176
|
-
if (!email || !password) {
|
|
1177
|
-
return { seeded: false };
|
|
1178
|
-
}
|
|
1179
|
-
const hasUsers = await store.hasAnyUser();
|
|
1180
|
-
if (hasUsers) {
|
|
1181
|
-
return { seeded: false };
|
|
1182
|
-
}
|
|
1183
|
-
if (!EMAIL_REGEX.test(email)) {
|
|
1184
|
-
throw new Error("Invalid SUPERADMIN_EMAIL for bootstrap seed.");
|
|
1185
|
-
}
|
|
1186
|
-
const now = /* @__PURE__ */ new Date();
|
|
1187
|
-
const passwordHash = await hashPassword(password, {
|
|
1188
|
-
minLength: options.minPasswordLength ?? 8
|
|
1189
|
-
});
|
|
1190
|
-
const created = await store.createUser({
|
|
1191
|
-
email,
|
|
1192
|
-
displayName: options.displayName ?? "Super Admin",
|
|
1193
|
-
role: "superadmin",
|
|
1194
|
-
isActive: true,
|
|
1195
|
-
authProvider: "classic",
|
|
1196
|
-
firebaseUid: null,
|
|
1197
|
-
passwordHash,
|
|
1198
|
-
mustChangePassword: true,
|
|
1199
|
-
tokenVersion: 1,
|
|
1200
|
-
createdAt: now,
|
|
1201
|
-
updatedAt: now
|
|
1202
|
-
});
|
|
1203
|
-
return { seeded: true, userId: created.id };
|
|
1204
|
-
};
|
|
1205
|
-
|
|
1206
1171
|
// src/server/config.ts
|
|
1207
1172
|
import { config as loadDotenv } from "dotenv";
|
|
1208
1173
|
var parseBool = (value) => {
|
|
@@ -1299,11 +1264,6 @@ var loadServerConfig = (env = process.env) => {
|
|
|
1299
1264
|
const raw = (env.PASSWORD_HASH_ALGO ?? "").trim().toLowerCase();
|
|
1300
1265
|
return raw === "argon2" || raw === "scrypt" ? raw : "auto";
|
|
1301
1266
|
})(),
|
|
1302
|
-
bootstrap: {
|
|
1303
|
-
email: (env.SUPERADMIN_EMAIL ?? "").trim().toLowerCase() || void 0,
|
|
1304
|
-
password: env.SUPERADMIN_INITIAL_PASSWORD || void 0,
|
|
1305
|
-
displayName: "Super Admin"
|
|
1306
|
-
},
|
|
1307
1267
|
apiBasePath: normalizeBasePath(env.API_BASE_PATH ?? ""),
|
|
1308
1268
|
port: parseIntOr(env.PORT, 4e3)
|
|
1309
1269
|
};
|
|
@@ -2074,24 +2034,12 @@ var startCallServer = async (functions, portOrConfig, config = {}) => {
|
|
|
2074
2034
|
};
|
|
2075
2035
|
const merged = { ...loadEnvConfig(), ...explicit };
|
|
2076
2036
|
const resolvedPort = isServerConfig(portOrConfig) ? portOrConfig.port ?? 4e3 : typeof portOrConfig === "number" ? portOrConfig : config.port ?? 4e3;
|
|
2077
|
-
const db = await getDatabase(merged.mongoUri, merged.dbName);
|
|
2078
|
-
await ensureMongoIndexes(db, { users: merged.usersCollection });
|
|
2079
2037
|
const dbProvider = () => getDatabase(merged.mongoUri, merged.dbName);
|
|
2080
2038
|
const store = merged.store ?? createLazyUserStore(dbProvider, {
|
|
2081
2039
|
users: merged.usersCollection,
|
|
2082
2040
|
roles: merged.rolesCollection
|
|
2083
2041
|
});
|
|
2084
|
-
|
|
2085
|
-
const seed = await ensureBootstrapAdmin(store, {
|
|
2086
|
-
email: merged.bootstrap.email,
|
|
2087
|
-
password: merged.bootstrap.password,
|
|
2088
|
-
displayName: merged.bootstrap.displayName,
|
|
2089
|
-
minPasswordLength: merged.minPasswordLength
|
|
2090
|
-
});
|
|
2091
|
-
if (seed.seeded) {
|
|
2092
|
-
console.info(`[mongo-call] Seeded initial superadmin (${merged.bootstrap.email}).`);
|
|
2093
|
-
}
|
|
2094
|
-
}
|
|
2042
|
+
await getDatabase(merged.mongoUri, merged.dbName);
|
|
2095
2043
|
const app = createCallApp(functions, { ...merged, dbProvider, store });
|
|
2096
2044
|
const server = app.listen(resolvedPort, () => {
|
|
2097
2045
|
console.log(`mongo-call server listening on http://localhost:${resolvedPort}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caprogramacion/mongo-call",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Firebase onCall-like wrapper for MongoDB with AJV, Swagger UI and hardened security",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"@caprogramacion/mongo-call": "^1.0.0",
|
|
28
29
|
"ajv": "^8.20.0",
|
|
29
30
|
"ajv-formats": "^3.0.1",
|
|
30
31
|
"cookie-parser": "^1.4.7",
|