@cravery/core 0.0.5 → 0.0.6
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/dist/lib/ai/cost.d.ts.map +1 -1
- package/dist/lib/ai/cost.js +10 -1
- package/dist/lib/ai/cost.js.map +1 -1
- package/dist/lib/ai/embedding.d.ts.map +1 -1
- package/dist/lib/ai/embedding.js +6 -1
- package/dist/lib/ai/embedding.js.map +1 -1
- package/dist/lib/errors/index.d.ts +31 -0
- package/dist/lib/errors/index.d.ts.map +1 -0
- package/dist/lib/errors/index.js +70 -0
- package/dist/lib/errors/index.js.map +1 -0
- package/dist/lib/index.d.ts +2 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +2 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/repository/factory.d.ts +9 -0
- package/dist/lib/repository/factory.d.ts.map +1 -0
- package/dist/lib/repository/factory.js +16 -0
- package/dist/lib/repository/factory.js.map +1 -0
- package/dist/lib/repository/index.d.ts +1 -0
- package/dist/lib/repository/index.d.ts.map +1 -1
- package/dist/lib/repository/index.js +1 -0
- package/dist/lib/repository/index.js.map +1 -1
- package/dist/lib/storage.d.ts.map +1 -1
- package/dist/lib/storage.js +16 -0
- package/dist/lib/storage.js.map +1 -1
- package/dist/lib/utils/index.d.ts +2 -0
- package/dist/lib/utils/index.d.ts.map +1 -0
- package/dist/lib/utils/index.js +18 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/sanitize.d.ts +20 -0
- package/dist/lib/utils/sanitize.d.ts.map +1 -0
- package/dist/lib/utils/sanitize.js +40 -0
- package/dist/lib/utils/sanitize.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/response.d.ts +16 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +14 -0
- package/dist/types/response.js.map +1 -0
- package/package.json +1 -1
- package/src/lib/ai/cost.ts +10 -1
- package/src/lib/ai/embedding.ts +6 -1
- package/src/lib/errors/index.ts +79 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/repository/factory.ts +14 -0
- package/src/lib/repository/index.ts +1 -0
- package/src/lib/storage.ts +18 -0
- package/src/lib/utils/index.ts +1 -0
- package/src/lib/utils/sanitize.ts +38 -0
- package/src/types/index.ts +1 -0
- package/src/types/response.ts +30 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../../../src/lib/ai/cost.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,UAAU,SAAS;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,aAAa,CAAC;CACtB;AAED,UAAU,eAAgB,SAAQ,SAAS;IACzC,IAAI,EAAE,YAAY,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,UAAU,UAAW,SAAQ,SAAS;IACpC,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,cAAe,SAAQ,SAAS;IACxC,IAAI,EAAE,WAAW,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,OAAO,GAAG,eAAe,GAAG,UAAU,GAAG,cAAc,CAAC;AAEpE,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../../../src/lib/ai/cost.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,UAAU,SAAS;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,aAAa,CAAC;CACtB;AAED,UAAU,eAAgB,SAAQ,SAAS;IACzC,IAAI,EAAE,YAAY,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,UAAU,UAAW,SAAQ,SAAS;IACpC,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,cAAe,SAAQ,SAAS;IACxC,IAAI,EAAE,WAAW,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,OAAO,GAAG,eAAe,GAAG,UAAU,GAAG,cAAc,CAAC;AAEpE,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,MAsD3C,CAAC"}
|
package/dist/lib/ai/cost.js
CHANGED
|
@@ -38,7 +38,16 @@ const logAIUsage = (usage) => {
|
|
|
38
38
|
for (const [key, value] of Object.entries(metrics)) {
|
|
39
39
|
updates[`${base}/models/${model.id}/${key}`] = database_1.ServerValue.increment(value);
|
|
40
40
|
}
|
|
41
|
-
db.ref()
|
|
41
|
+
db.ref()
|
|
42
|
+
.update(updates)
|
|
43
|
+
.catch((error) => {
|
|
44
|
+
console.error("[AI Usage] Failed to log usage:", {
|
|
45
|
+
flowName,
|
|
46
|
+
model: model.id,
|
|
47
|
+
cost,
|
|
48
|
+
error: error instanceof Error ? error.message : String(error),
|
|
49
|
+
});
|
|
50
|
+
});
|
|
42
51
|
return cost;
|
|
43
52
|
};
|
|
44
53
|
exports.logAIUsage = logAIUsage;
|
package/dist/lib/ai/cost.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../../../src/lib/ai/cost.ts"],"names":[],"mappings":";;;AAAA,sDAAmE;AA0B5D,MAAM,UAAU,GAAG,CAAC,KAAc,EAAU,EAAE;IACnD,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IAElC,IAAI,IAAY,CAAC;IACjB,IAAI,OAA+B,CAAC;IAEpC,QAAQ,KAAK,CAAC,IAAI,EAAE,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CAAC;SACf,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACf,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE;YAC/C,QAAQ;YACR,KAAK,EAAE,KAAK,CAAC,EAAE;YACf,IAAI;YACJ,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAC9D,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEL,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAtDW,QAAA,UAAU,cAsDrB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../../src/lib/ai/embedding.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../../src/lib/ai/embedding.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,iBAAiB,GAAU,MAAM,MAAM,EAAE,UAAU,MAAM,sBAmBrE,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,YAAY,MAAM,EAClB,OAAO,MAAM,EACb,MAAM,MAAM,EACZ,WAAW,MAAM,EACjB,cAAa,MAAoB,kBAYlC,CAAC;AAEF,eAAO,MAAM,gBAAgB,kCAS3B,CAAC"}
|
package/dist/lib/ai/embedding.js
CHANGED
|
@@ -7,10 +7,15 @@ const firebase_2 = require("../firebase");
|
|
|
7
7
|
const genkit_1 = require("./genkit");
|
|
8
8
|
const cost_1 = require("./cost");
|
|
9
9
|
const ai_1 = require("../../config/ai");
|
|
10
|
+
const errors_1 = require("../errors");
|
|
10
11
|
const generateEmbedding = async (text, flowName) => {
|
|
11
12
|
var _a;
|
|
12
13
|
if (text.length < 1 || text.length > 7500) {
|
|
13
|
-
throw new
|
|
14
|
+
throw new errors_1.ValidationError("Text must be between 1 and 7500 characters", {
|
|
15
|
+
length: text.length,
|
|
16
|
+
minLength: 1,
|
|
17
|
+
maxLength: 7500,
|
|
18
|
+
});
|
|
14
19
|
}
|
|
15
20
|
const response = await genkit_1.ai.embed({
|
|
16
21
|
embedder: genkit_1.geminiEmbedding001,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedding.js","sourceRoot":"","sources":["../../../src/lib/ai/embedding.ts"],"names":[],"mappings":";;;AAAA,wDAAsD;AACtD,kDAA+D;AAC/D,0CAAwC;AACxC,qCAAkD;AAClD,iCAAoC;AACpC,wCAA4C;AAErC,MAAM,iBAAiB,GAAG,KAAK,EAAE,IAAY,EAAE,QAAgB,EAAE,EAAE;;IACxE,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;QAC1C,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"embedding.js","sourceRoot":"","sources":["../../../src/lib/ai/embedding.ts"],"names":[],"mappings":";;;AAAA,wDAAsD;AACtD,kDAA+D;AAC/D,0CAAwC;AACxC,qCAAkD;AAClD,iCAAoC;AACpC,wCAA4C;AAC5C,sCAA4C;AAErC,MAAM,iBAAiB,GAAG,KAAK,EAAE,IAAY,EAAE,QAAgB,EAAE,EAAE;;IACxE,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;QAC1C,MAAM,IAAI,wBAAe,CAAC,4CAA4C,EAAE;YACtE,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,SAAS,EAAE,CAAC;YACZ,SAAS,EAAE,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,WAAE,CAAC,KAAK,CAAC;QAC9B,QAAQ,EAAE,2BAAkB;QAC5B,OAAO,EAAE,IAAI;KACd,CAAC,CAAC;IACH,IAAA,iBAAU,EAAC;QACT,IAAI,EAAE,WAAW;QACjB,QAAQ;QACR,KAAK,EAAE,cAAS,CAAC,kBAAkB;QACnC,UAAU,EAAE,IAAI,CAAC,MAAM;KACxB,CAAC,CAAC;IACH,OAAO,MAAA,QAAQ,CAAC,CAAC,CAAC,0CAAE,SAAS,CAAC;AAChC,CAAC,CAAC;AAnBW,QAAA,iBAAiB,qBAmB5B;AAEK,MAAM,cAAc,GAAG,KAAK,EACjC,UAAkB,EAClB,KAAa,EACb,IAAY,EACZ,QAAiB,EACjB,cAAsB,WAAW,EACjC,EAAE;IACF,MAAM,SAAS,GAAG,MAAM,IAAA,yBAAiB,EACvC,IAAI,EACJ,QAAQ,IAAI,GAAG,UAAU,YAAY,CACtC,CAAC;IACF,MAAM,oBAAS;SACZ,UAAU,CAAC,UAAU,CAAC;SACtB,GAAG,CAAC,KAAK,CAAC;SACV,MAAM,CAAC;QACN,CAAC,WAAW,CAAC,EAAE,sBAAU,CAAC,MAAM,CAAC,SAAS,CAAC;KAC5C,CAAC,CAAC;AACP,CAAC,CAAC;AAjBW,QAAA,cAAc,kBAiBzB;AAEW,QAAA,gBAAgB,GAAG,IAAA,mCAAwB,EAAC,WAAE,EAAE;IAC3D,IAAI,EAAE,kBAAkB;IACxB,SAAS,EAAT,oBAAS;IACT,UAAU,EAAE,SAAS;IACrB,YAAY,EAAE,aAAa;IAC3B,WAAW,EAAE,gBAAgB;IAC7B,QAAQ,EAAE,2BAAkB;IAC5B,eAAe,EAAE,QAAQ;IACzB,mBAAmB,EAAE,UAAU;CAChC,CAAC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const APP_ERROR_CODE_VALUES: readonly ["BAD_REQUEST", "UNAUTHORIZED", "FORBIDDEN", "NOT_FOUND", "CONFLICT", "VALIDATION_ERROR", "INTERNAL_ERROR"];
|
|
2
|
+
export type AppErrorCode = (typeof APP_ERROR_CODE_VALUES)[number];
|
|
3
|
+
export declare class AppError extends Error {
|
|
4
|
+
readonly code: AppErrorCode;
|
|
5
|
+
readonly statusCode: number;
|
|
6
|
+
readonly context?: Record<string, unknown>;
|
|
7
|
+
constructor(code: AppErrorCode, message: string, statusCode?: number, context?: Record<string, unknown>);
|
|
8
|
+
toJSON(): {
|
|
9
|
+
name: string;
|
|
10
|
+
code: "BAD_REQUEST" | "UNAUTHORIZED" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT" | "VALIDATION_ERROR" | "INTERNAL_ERROR";
|
|
11
|
+
message: string;
|
|
12
|
+
statusCode: number;
|
|
13
|
+
context: Record<string, unknown> | undefined;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare class ValidationError extends AppError {
|
|
17
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
18
|
+
}
|
|
19
|
+
export declare class NotFoundError extends AppError {
|
|
20
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
21
|
+
}
|
|
22
|
+
export declare class UnauthorizedError extends AppError {
|
|
23
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
24
|
+
}
|
|
25
|
+
export declare class ForbiddenError extends AppError {
|
|
26
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
27
|
+
}
|
|
28
|
+
export declare class ConflictError extends AppError {
|
|
29
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/errors/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,sHAQxB,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AAElE,qBAAa,QAAS,SAAQ,KAAK;IACjC,SAAgB,IAAI,EAAE,YAAY,CAAC;IACnC,SAAgB,UAAU,EAAE,MAAM,CAAC;IACnC,SAAgB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBAGhD,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,MAAM,EACf,UAAU,GAAE,MAAY,EACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAanC,MAAM;;;;;;;CASP;AAED,qBAAa,eAAgB,SAAQ,QAAQ;gBAC/B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAI/D;AAED,qBAAa,aAAc,SAAQ,QAAQ;gBAC7B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAI/D;AAED,qBAAa,iBAAkB,SAAQ,QAAQ;gBACjC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAI/D;AAED,qBAAa,cAAe,SAAQ,QAAQ;gBAC9B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAI/D;AAED,qBAAa,aAAc,SAAQ,QAAQ;gBAC7B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAI/D"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConflictError = exports.ForbiddenError = exports.UnauthorizedError = exports.NotFoundError = exports.ValidationError = exports.AppError = exports.APP_ERROR_CODE_VALUES = void 0;
|
|
4
|
+
exports.APP_ERROR_CODE_VALUES = [
|
|
5
|
+
"BAD_REQUEST",
|
|
6
|
+
"UNAUTHORIZED",
|
|
7
|
+
"FORBIDDEN",
|
|
8
|
+
"NOT_FOUND",
|
|
9
|
+
"CONFLICT",
|
|
10
|
+
"VALIDATION_ERROR",
|
|
11
|
+
"INTERNAL_ERROR",
|
|
12
|
+
];
|
|
13
|
+
class AppError extends Error {
|
|
14
|
+
constructor(code, message, statusCode = 500, context) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "AppError";
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.statusCode = statusCode;
|
|
19
|
+
this.context = context;
|
|
20
|
+
if (Error.captureStackTrace) {
|
|
21
|
+
Error.captureStackTrace(this, AppError);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
toJSON() {
|
|
25
|
+
return {
|
|
26
|
+
name: this.name,
|
|
27
|
+
code: this.code,
|
|
28
|
+
message: this.message,
|
|
29
|
+
statusCode: this.statusCode,
|
|
30
|
+
context: this.context,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.AppError = AppError;
|
|
35
|
+
class ValidationError extends AppError {
|
|
36
|
+
constructor(message, context) {
|
|
37
|
+
super("VALIDATION_ERROR", message, 400, context);
|
|
38
|
+
this.name = "ValidationError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.ValidationError = ValidationError;
|
|
42
|
+
class NotFoundError extends AppError {
|
|
43
|
+
constructor(message, context) {
|
|
44
|
+
super("NOT_FOUND", message, 404, context);
|
|
45
|
+
this.name = "NotFoundError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.NotFoundError = NotFoundError;
|
|
49
|
+
class UnauthorizedError extends AppError {
|
|
50
|
+
constructor(message, context) {
|
|
51
|
+
super("UNAUTHORIZED", message, 401, context);
|
|
52
|
+
this.name = "UnauthorizedError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
56
|
+
class ForbiddenError extends AppError {
|
|
57
|
+
constructor(message, context) {
|
|
58
|
+
super("FORBIDDEN", message, 403, context);
|
|
59
|
+
this.name = "ForbiddenError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.ForbiddenError = ForbiddenError;
|
|
63
|
+
class ConflictError extends AppError {
|
|
64
|
+
constructor(message, context) {
|
|
65
|
+
super("CONFLICT", message, 409, context);
|
|
66
|
+
this.name = "ConflictError";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.ConflictError = ConflictError;
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/errors/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,qBAAqB,GAAG;IACnC,aAAa;IACb,cAAc;IACd,WAAW;IACX,WAAW;IACX,UAAU;IACV,kBAAkB;IAClB,gBAAgB;CACR,CAAC;AAIX,MAAa,QAAS,SAAQ,KAAK;IAKjC,YACE,IAAkB,EAClB,OAAe,EACf,aAAqB,GAAG,EACxB,OAAiC;QAEjC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC;IAED,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC;IACJ,CAAC;CACF;AA/BD,4BA+BC;AAED,MAAa,eAAgB,SAAQ,QAAQ;IAC3C,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,kBAAkB,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AALD,0CAKC;AAED,MAAa,aAAc,SAAQ,QAAQ;IACzC,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC;AAED,MAAa,iBAAkB,SAAQ,QAAQ;IAC7C,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,cAAc,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AALD,8CAKC;AAED,MAAa,cAAe,SAAQ,QAAQ;IAC1C,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF;AALD,wCAKC;AAED,MAAa,aAAc,SAAQ,QAAQ;IACzC,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC"}
|
package/dist/lib/index.d.ts
CHANGED
package/dist/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,CAAC;AACrB,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,CAAC;AACrB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
|
package/dist/lib/index.js
CHANGED
|
@@ -16,8 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./ai"), exports);
|
|
18
18
|
__exportStar(require("./api"), exports);
|
|
19
|
+
__exportStar(require("./errors"), exports);
|
|
19
20
|
__exportStar(require("./firebase"), exports);
|
|
20
21
|
__exportStar(require("./iam"), exports);
|
|
21
22
|
__exportStar(require("./repository"), exports);
|
|
22
23
|
__exportStar(require("./storage"), exports);
|
|
24
|
+
__exportStar(require("./utils"), exports);
|
|
23
25
|
//# sourceMappingURL=index.js.map
|
package/dist/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uCAAqB;AACrB,wCAAsB;AACtB,6CAA2B;AAC3B,wCAAsB;AACtB,+CAA6B;AAC7B,4CAA0B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uCAAqB;AACrB,wCAAsB;AACtB,2CAAyB;AACzB,6CAA2B;AAC3B,wCAAsB;AACtB,+CAA6B;AAC7B,4CAA0B;AAC1B,0CAAwB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { UserRepository } from "./user.repository";
|
|
2
|
+
import { ProfileRepository } from "./profile.repository";
|
|
3
|
+
import { SettingsRepository } from "./settings.repository";
|
|
4
|
+
export declare const repositories: {
|
|
5
|
+
user: () => UserRepository;
|
|
6
|
+
profile: () => ProfileRepository;
|
|
7
|
+
settings: () => SettingsRepository;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/lib/repository/factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAM3D,eAAO,MAAM,YAAY;;;;CAIxB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.repositories = void 0;
|
|
4
|
+
const firebase_1 = require("../firebase");
|
|
5
|
+
const user_repository_1 = require("./user.repository");
|
|
6
|
+
const profile_repository_1 = require("./profile.repository");
|
|
7
|
+
const settings_repository_1 = require("./settings.repository");
|
|
8
|
+
let userRepo = null;
|
|
9
|
+
let profileRepo = null;
|
|
10
|
+
let settingsRepo = null;
|
|
11
|
+
exports.repositories = {
|
|
12
|
+
user: () => (userRepo !== null && userRepo !== void 0 ? userRepo : (userRepo = new user_repository_1.UserRepository(firebase_1.firestore))),
|
|
13
|
+
profile: () => (profileRepo !== null && profileRepo !== void 0 ? profileRepo : (profileRepo = new profile_repository_1.ProfileRepository(firebase_1.firestore))),
|
|
14
|
+
settings: () => (settingsRepo !== null && settingsRepo !== void 0 ? settingsRepo : (settingsRepo = new settings_repository_1.SettingsRepository(firebase_1.firestore))),
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/lib/repository/factory.ts"],"names":[],"mappings":";;;AAAA,0CAAwC;AACxC,uDAAmD;AACnD,6DAAyD;AACzD,+DAA2D;AAE3D,IAAI,QAAQ,GAA0B,IAAI,CAAC;AAC3C,IAAI,WAAW,GAA6B,IAAI,CAAC;AACjD,IAAI,YAAY,GAA8B,IAAI,CAAC;AAEtC,QAAA,YAAY,GAAG;IAC1B,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,IAAR,QAAQ,GAAK,IAAI,gCAAc,CAAC,oBAAS,CAAC,EAAC;IACxD,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,WAAW,aAAX,WAAW,cAAX,WAAW,IAAX,WAAW,GAAK,IAAI,sCAAiB,CAAC,oBAAS,CAAC,EAAC;IACjE,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,YAAY,aAAZ,YAAY,cAAZ,YAAY,IAAZ,YAAY,GAAK,IAAI,wCAAkB,CAAC,oBAAS,CAAC,EAAC;CACrE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/repository/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/repository/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC"}
|
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./errors"), exports);
|
|
18
|
+
__exportStar(require("./factory"), exports);
|
|
18
19
|
__exportStar(require("./firestore.repository"), exports);
|
|
19
20
|
__exportStar(require("./rtdb.repository"), exports);
|
|
20
21
|
__exportStar(require("./user.repository"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/repository/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,yDAAuC;AACvC,oDAAkC;AAClC,oDAAkC;AAClC,uDAAqC;AACrC,wDAAsC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/repository/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,4CAA0B;AAC1B,yDAAuC;AACvC,oDAAkC;AAClC,oDAAkC;AAClC,uDAAqC;AACrC,wDAAsC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/lib/storage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/lib/storage.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd;AAiBD,eAAO,MAAM,oBAAoB,GAC/B,YAAY,MAAM,EAClB,MAAM,MAAM,EACZ,cAAa,MAAoB,KAChC,OAAO,CAAC,iBAAiB,CAgC3B,CAAC"}
|
package/dist/lib/storage.js
CHANGED
|
@@ -2,7 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.uploadImageToStorage = void 0;
|
|
4
4
|
const storage_1 = require("firebase-admin/storage");
|
|
5
|
+
const errors_1 = require("./errors");
|
|
6
|
+
function validateStoragePath(path) {
|
|
7
|
+
if (!path || path.length === 0) {
|
|
8
|
+
throw new errors_1.ValidationError("Storage path cannot be empty");
|
|
9
|
+
}
|
|
10
|
+
if (path.includes("..")) {
|
|
11
|
+
throw new errors_1.ValidationError("Storage path cannot contain '..'", { path });
|
|
12
|
+
}
|
|
13
|
+
if (path.startsWith("/")) {
|
|
14
|
+
throw new errors_1.ValidationError("Storage path cannot start with '/'", { path });
|
|
15
|
+
}
|
|
16
|
+
if (path.includes("//")) {
|
|
17
|
+
throw new errors_1.ValidationError("Storage path cannot contain '//'", { path });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
5
20
|
const uploadImageToStorage = async (base64Data, path, contentType = "image/png") => {
|
|
21
|
+
validateStoragePath(path);
|
|
6
22
|
try {
|
|
7
23
|
const bucket = (0, storage_1.getStorage)().bucket();
|
|
8
24
|
const buffer = Buffer.from(base64Data, "base64");
|
package/dist/lib/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/lib/storage.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/lib/storage.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AACpD,qCAA2C;AAO3C,SAAS,mBAAmB,CAAC,IAAY;IACvC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,wBAAe,CAAC,8BAA8B,CAAC,CAAC;IAC5D,CAAC;IACD,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,wBAAe,CAAC,kCAAkC,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,wBAAe,CAAC,oCAAoC,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5E,CAAC;IACD,IAA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sourceRoot":"","sources":["../../../src/lib/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./sanitize"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Removes HTML tags and script content from a string.
|
|
4
|
+
* Use for text fields that should not contain HTML (descriptions, bios, etc.)
|
|
5
|
+
*/
|
|
6
|
+
export declare function sanitizeText(text: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Zod string schema that sanitizes HTML on transformation.
|
|
9
|
+
* Use in schemas where text input needs to be cleaned.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const BioSchema = sanitizedString(z.string().max(280));
|
|
13
|
+
*/
|
|
14
|
+
export declare function sanitizedString<T extends z.ZodString>(schema: T): z.ZodPipe<T, z.ZodTransform<string, z.core.output<T>>>;
|
|
15
|
+
/**
|
|
16
|
+
* Escapes HTML special characters to prevent XSS.
|
|
17
|
+
* Use when HTML needs to be displayed as text.
|
|
18
|
+
*/
|
|
19
|
+
export declare function escapeHtml(text: string): string;
|
|
20
|
+
//# sourceMappingURL=sanitize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/sanitize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKjD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC,0DAE/D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAS/C"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitizeText = sanitizeText;
|
|
4
|
+
exports.sanitizedString = sanitizedString;
|
|
5
|
+
exports.escapeHtml = escapeHtml;
|
|
6
|
+
/**
|
|
7
|
+
* Removes HTML tags and script content from a string.
|
|
8
|
+
* Use for text fields that should not contain HTML (descriptions, bios, etc.)
|
|
9
|
+
*/
|
|
10
|
+
function sanitizeText(text) {
|
|
11
|
+
return text
|
|
12
|
+
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
|
|
13
|
+
.replace(/<[^>]*>/g, "")
|
|
14
|
+
.trim();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a Zod string schema that sanitizes HTML on transformation.
|
|
18
|
+
* Use in schemas where text input needs to be cleaned.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const BioSchema = sanitizedString(z.string().max(280));
|
|
22
|
+
*/
|
|
23
|
+
function sanitizedString(schema) {
|
|
24
|
+
return schema.transform(sanitizeText);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Escapes HTML special characters to prevent XSS.
|
|
28
|
+
* Use when HTML needs to be displayed as text.
|
|
29
|
+
*/
|
|
30
|
+
function escapeHtml(text) {
|
|
31
|
+
const htmlEscapes = {
|
|
32
|
+
"&": "&",
|
|
33
|
+
"<": "<",
|
|
34
|
+
">": ">",
|
|
35
|
+
'"': """,
|
|
36
|
+
"'": "'",
|
|
37
|
+
};
|
|
38
|
+
return text.replace(/[&<>"']/g, (char) => htmlEscapes[char]);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=sanitize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../../../src/lib/utils/sanitize.ts"],"names":[],"mappings":";;AAMA,oCAKC;AASD,0CAEC;AAMD,gCASC;AAnCD;;;GAGG;AACH,SAAgB,YAAY,CAAC,IAAY;IACvC,OAAO,IAAI;SACR,OAAO,CAAC,qDAAqD,EAAE,EAAE,CAAC;SAClE,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC;SACvB,IAAI,EAAE,CAAC;AACZ,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,eAAe,CAAwB,MAAS;IAC9D,OAAO,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC;AAED;;;GAGG;AACH,SAAgB,UAAU,CAAC,IAAY;IACrC,MAAM,WAAW,GAA2B;QAC1C,GAAG,EAAE,OAAO;QACZ,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,QAAQ;QACb,GAAG,EAAE,OAAO;KACb,CAAC;IACF,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,CAAC;AACrB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,QAAQ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,CAAC;AACrB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,QAAQ,CAAC"}
|
package/dist/types/index.js
CHANGED
|
@@ -23,6 +23,7 @@ __exportStar(require("./nutrition"), exports);
|
|
|
23
23
|
__exportStar(require("./profile"), exports);
|
|
24
24
|
__exportStar(require("./recipe"), exports);
|
|
25
25
|
__exportStar(require("./repository"), exports);
|
|
26
|
+
__exportStar(require("./response"), exports);
|
|
26
27
|
__exportStar(require("./settings"), exports);
|
|
27
28
|
__exportStar(require("./subscription"), exports);
|
|
28
29
|
__exportStar(require("./temperature"), exports);
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uCAAqB;AACrB,8CAA4B;AAC5B,+CAA6B;AAC7B,gDAA8B;AAC9B,+CAA6B;AAC7B,8CAA4B;AAC5B,4CAA0B;AAC1B,2CAAyB;AACzB,+CAA6B;AAC7B,6CAA2B;AAC3B,iDAA+B;AAC/B,gDAA8B;AAC9B,yCAAuB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uCAAqB;AACrB,8CAA4B;AAC5B,+CAA6B;AAC7B,gDAA8B;AAC9B,+CAA6B;AAC7B,8CAA4B;AAC5B,4CAA0B;AAC1B,2CAAyB;AACzB,+CAA6B;AAC7B,6CAA2B;AAC3B,6CAA2B;AAC3B,iDAA+B;AAC/B,gDAA8B;AAC9B,yCAAuB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type ApiSuccessResponse<T> = {
|
|
2
|
+
success: true;
|
|
3
|
+
data: T;
|
|
4
|
+
};
|
|
5
|
+
export type ApiErrorResponse = {
|
|
6
|
+
success: false;
|
|
7
|
+
error: {
|
|
8
|
+
code: string;
|
|
9
|
+
message: string;
|
|
10
|
+
details?: unknown;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
14
|
+
export declare function success<T>(data: T): ApiSuccessResponse<T>;
|
|
15
|
+
export declare function failure(code: string, message: string, details?: unknown): ApiErrorResponse;
|
|
16
|
+
//# sourceMappingURL=response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/types/response.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI;IAClC,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,CAAC,CAAC;CACT,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,KAAK,CAAC;IACf,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,kBAAkB,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC;AAEtE,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAEzD;AAED,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,OAAO,GAChB,gBAAgB,CAKlB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.success = success;
|
|
4
|
+
exports.failure = failure;
|
|
5
|
+
function success(data) {
|
|
6
|
+
return { success: true, data };
|
|
7
|
+
}
|
|
8
|
+
function failure(code, message, details) {
|
|
9
|
+
return {
|
|
10
|
+
success: false,
|
|
11
|
+
error: { code, message, details },
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../src/types/response.ts"],"names":[],"mappings":";;AAgBA,0BAEC;AAED,0BASC;AAbD,SAAgB,OAAO,CAAI,IAAO;IAChC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACjC,CAAC;AAED,SAAgB,OAAO,CACrB,IAAY,EACZ,OAAe,EACf,OAAiB;IAEjB,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE;KAClC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/src/lib/ai/cost.ts
CHANGED
|
@@ -66,7 +66,16 @@ export const logAIUsage = (usage: AIUsage): number => {
|
|
|
66
66
|
updates[`${base}/models/${model.id}/${key}`] = ServerValue.increment(value);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
db.ref()
|
|
69
|
+
db.ref()
|
|
70
|
+
.update(updates)
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
console.error("[AI Usage] Failed to log usage:", {
|
|
73
|
+
flowName,
|
|
74
|
+
model: model.id,
|
|
75
|
+
cost,
|
|
76
|
+
error: error instanceof Error ? error.message : String(error),
|
|
77
|
+
});
|
|
78
|
+
});
|
|
70
79
|
|
|
71
80
|
return cost;
|
|
72
81
|
};
|
package/src/lib/ai/embedding.ts
CHANGED
|
@@ -4,10 +4,15 @@ import { firestore } from "../firebase";
|
|
|
4
4
|
import { ai, geminiEmbedding001 } from "./genkit";
|
|
5
5
|
import { logAIUsage } from "./cost";
|
|
6
6
|
import { AI_MODELS } from "../../config/ai";
|
|
7
|
+
import { ValidationError } from "../errors";
|
|
7
8
|
|
|
8
9
|
export const generateEmbedding = async (text: string, flowName: string) => {
|
|
9
10
|
if (text.length < 1 || text.length > 7500) {
|
|
10
|
-
throw new
|
|
11
|
+
throw new ValidationError("Text must be between 1 and 7500 characters", {
|
|
12
|
+
length: text.length,
|
|
13
|
+
minLength: 1,
|
|
14
|
+
maxLength: 7500,
|
|
15
|
+
});
|
|
11
16
|
}
|
|
12
17
|
const response = await ai.embed({
|
|
13
18
|
embedder: geminiEmbedding001,
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export const APP_ERROR_CODE_VALUES = [
|
|
2
|
+
"BAD_REQUEST",
|
|
3
|
+
"UNAUTHORIZED",
|
|
4
|
+
"FORBIDDEN",
|
|
5
|
+
"NOT_FOUND",
|
|
6
|
+
"CONFLICT",
|
|
7
|
+
"VALIDATION_ERROR",
|
|
8
|
+
"INTERNAL_ERROR",
|
|
9
|
+
] as const;
|
|
10
|
+
|
|
11
|
+
export type AppErrorCode = (typeof APP_ERROR_CODE_VALUES)[number];
|
|
12
|
+
|
|
13
|
+
export class AppError extends Error {
|
|
14
|
+
public readonly code: AppErrorCode;
|
|
15
|
+
public readonly statusCode: number;
|
|
16
|
+
public readonly context?: Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
code: AppErrorCode,
|
|
20
|
+
message: string,
|
|
21
|
+
statusCode: number = 500,
|
|
22
|
+
context?: Record<string, unknown>,
|
|
23
|
+
) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = "AppError";
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.statusCode = statusCode;
|
|
28
|
+
this.context = context;
|
|
29
|
+
|
|
30
|
+
if (Error.captureStackTrace) {
|
|
31
|
+
Error.captureStackTrace(this, AppError);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
toJSON() {
|
|
36
|
+
return {
|
|
37
|
+
name: this.name,
|
|
38
|
+
code: this.code,
|
|
39
|
+
message: this.message,
|
|
40
|
+
statusCode: this.statusCode,
|
|
41
|
+
context: this.context,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class ValidationError extends AppError {
|
|
47
|
+
constructor(message: string, context?: Record<string, unknown>) {
|
|
48
|
+
super("VALIDATION_ERROR", message, 400, context);
|
|
49
|
+
this.name = "ValidationError";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class NotFoundError extends AppError {
|
|
54
|
+
constructor(message: string, context?: Record<string, unknown>) {
|
|
55
|
+
super("NOT_FOUND", message, 404, context);
|
|
56
|
+
this.name = "NotFoundError";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class UnauthorizedError extends AppError {
|
|
61
|
+
constructor(message: string, context?: Record<string, unknown>) {
|
|
62
|
+
super("UNAUTHORIZED", message, 401, context);
|
|
63
|
+
this.name = "UnauthorizedError";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class ForbiddenError extends AppError {
|
|
68
|
+
constructor(message: string, context?: Record<string, unknown>) {
|
|
69
|
+
super("FORBIDDEN", message, 403, context);
|
|
70
|
+
this.name = "ForbiddenError";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class ConflictError extends AppError {
|
|
75
|
+
constructor(message: string, context?: Record<string, unknown>) {
|
|
76
|
+
super("CONFLICT", message, 409, context);
|
|
77
|
+
this.name = "ConflictError";
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { firestore } from "../firebase";
|
|
2
|
+
import { UserRepository } from "./user.repository";
|
|
3
|
+
import { ProfileRepository } from "./profile.repository";
|
|
4
|
+
import { SettingsRepository } from "./settings.repository";
|
|
5
|
+
|
|
6
|
+
let userRepo: UserRepository | null = null;
|
|
7
|
+
let profileRepo: ProfileRepository | null = null;
|
|
8
|
+
let settingsRepo: SettingsRepository | null = null;
|
|
9
|
+
|
|
10
|
+
export const repositories = {
|
|
11
|
+
user: () => (userRepo ??= new UserRepository(firestore)),
|
|
12
|
+
profile: () => (profileRepo ??= new ProfileRepository(firestore)),
|
|
13
|
+
settings: () => (settingsRepo ??= new SettingsRepository(firestore)),
|
|
14
|
+
};
|
package/src/lib/storage.ts
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
import { getStorage } from "firebase-admin/storage";
|
|
2
|
+
import { ValidationError } from "./errors";
|
|
2
3
|
|
|
3
4
|
export interface UploadImageResult {
|
|
4
5
|
url: string;
|
|
5
6
|
path: string;
|
|
6
7
|
}
|
|
7
8
|
|
|
9
|
+
function validateStoragePath(path: string): void {
|
|
10
|
+
if (!path || path.length === 0) {
|
|
11
|
+
throw new ValidationError("Storage path cannot be empty");
|
|
12
|
+
}
|
|
13
|
+
if (path.includes("..")) {
|
|
14
|
+
throw new ValidationError("Storage path cannot contain '..'", { path });
|
|
15
|
+
}
|
|
16
|
+
if (path.startsWith("/")) {
|
|
17
|
+
throw new ValidationError("Storage path cannot start with '/'", { path });
|
|
18
|
+
}
|
|
19
|
+
if (path.includes("//")) {
|
|
20
|
+
throw new ValidationError("Storage path cannot contain '//'", { path });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
export const uploadImageToStorage = async (
|
|
9
25
|
base64Data: string,
|
|
10
26
|
path: string,
|
|
11
27
|
contentType: string = "image/png",
|
|
12
28
|
): Promise<UploadImageResult> => {
|
|
29
|
+
validateStoragePath(path);
|
|
30
|
+
|
|
13
31
|
try {
|
|
14
32
|
const bucket = getStorage().bucket();
|
|
15
33
|
const buffer = Buffer.from(base64Data, "base64");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./sanitize";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes HTML tags and script content from a string.
|
|
5
|
+
* Use for text fields that should not contain HTML (descriptions, bios, etc.)
|
|
6
|
+
*/
|
|
7
|
+
export function sanitizeText(text: string): string {
|
|
8
|
+
return text
|
|
9
|
+
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
|
|
10
|
+
.replace(/<[^>]*>/g, "")
|
|
11
|
+
.trim();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a Zod string schema that sanitizes HTML on transformation.
|
|
16
|
+
* Use in schemas where text input needs to be cleaned.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const BioSchema = sanitizedString(z.string().max(280));
|
|
20
|
+
*/
|
|
21
|
+
export function sanitizedString<T extends z.ZodString>(schema: T) {
|
|
22
|
+
return schema.transform(sanitizeText);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Escapes HTML special characters to prevent XSS.
|
|
27
|
+
* Use when HTML needs to be displayed as text.
|
|
28
|
+
*/
|
|
29
|
+
export function escapeHtml(text: string): string {
|
|
30
|
+
const htmlEscapes: Record<string, string> = {
|
|
31
|
+
"&": "&",
|
|
32
|
+
"<": "<",
|
|
33
|
+
">": ">",
|
|
34
|
+
'"': """,
|
|
35
|
+
"'": "'",
|
|
36
|
+
};
|
|
37
|
+
return text.replace(/[&<>"']/g, (char) => htmlEscapes[char]);
|
|
38
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type ApiSuccessResponse<T> = {
|
|
2
|
+
success: true;
|
|
3
|
+
data: T;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type ApiErrorResponse = {
|
|
7
|
+
success: false;
|
|
8
|
+
error: {
|
|
9
|
+
code: string;
|
|
10
|
+
message: string;
|
|
11
|
+
details?: unknown;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
16
|
+
|
|
17
|
+
export function success<T>(data: T): ApiSuccessResponse<T> {
|
|
18
|
+
return { success: true, data };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function failure(
|
|
22
|
+
code: string,
|
|
23
|
+
message: string,
|
|
24
|
+
details?: unknown,
|
|
25
|
+
): ApiErrorResponse {
|
|
26
|
+
return {
|
|
27
|
+
success: false,
|
|
28
|
+
error: { code, message, details },
|
|
29
|
+
};
|
|
30
|
+
}
|