@progressive-development/pd-provider-firebase-functions 1.3.0 → 1.3.1
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/auth-trash.js +118 -107
- package/dist/auth.js +30 -23
- package/dist/changes.js +53 -60
- package/dist/counter.js +19 -17
- package/dist/errors.js +41 -40
- package/dist/firestore.js +17 -11
- package/dist/history.js +74 -45
- package/dist/import-templates.js +102 -106
- package/dist/index.js +16 -15
- package/dist/logger.js +11 -8
- package/dist/mail.js +56 -35
- package/dist/response.js +35 -26
- package/dist/script-helpers.js +55 -44
- package/dist/trash.js +105 -72
- package/dist/types.js +10 -9
- package/dist/visits.js +124 -82
- package/package.json +11 -8
package/dist/history.js
CHANGED
|
@@ -1,53 +1,82 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { ValidationError } from "./errors.js";
|
|
2
|
+
import { logger } from "./logger.js";
|
|
3
|
+
import { getDb } from "./firestore.js";
|
|
4
|
+
import { Timestamp } from "firebase-admin/firestore";
|
|
5
|
+
//#region src/history.ts
|
|
6
|
+
var HISTORY_COLLECTION = "_history";
|
|
7
|
+
/**
|
|
8
|
+
* Core event categories — fixed in the helper. App-specific semantics go
|
|
9
|
+
* into the free-form `action` field, NOT in here.
|
|
10
|
+
*/
|
|
11
|
+
var HistoryKind = {
|
|
12
|
+
CREATED: "created",
|
|
13
|
+
UPDATED: "updated",
|
|
14
|
+
DELETED: "deleted",
|
|
15
|
+
RESTORED: "restored",
|
|
16
|
+
PROCESS: "process"
|
|
13
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Write a history entry.
|
|
20
|
+
*
|
|
21
|
+
* Throws on failure — use recordHistory() at app write sites where a
|
|
22
|
+
* history error must never fail the main operation.
|
|
23
|
+
*
|
|
24
|
+
* @param entry - The event data (at is set server-side)
|
|
25
|
+
* @param databaseId - Optional named database
|
|
26
|
+
* @returns The created history entry
|
|
27
|
+
* @throws ValidationError if the object reference is incomplete
|
|
28
|
+
*/
|
|
14
29
|
async function addHistoryEntry(entry, databaseId) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const docRef = await db.collection(HISTORY_COLLECTION).add(entryData);
|
|
26
|
-
return {
|
|
27
|
-
id: docRef.id,
|
|
28
|
-
...entryData
|
|
29
|
-
};
|
|
30
|
+
if (!entry.objectCollection || !entry.objectId || !entry.objectType) throw new ValidationError("History entry requires objectCollection, objectId and objectType");
|
|
31
|
+
const db = getDb(databaseId);
|
|
32
|
+
const entryData = {
|
|
33
|
+
...entry,
|
|
34
|
+
at: Timestamp.now()
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
id: (await db.collection(HISTORY_COLLECTION).add(entryData)).id,
|
|
38
|
+
...entryData
|
|
39
|
+
};
|
|
30
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Best-effort variant of addHistoryEntry for app write sites: catches ANY
|
|
43
|
+
* error, logs it as a warning and returns null. The app's main operation
|
|
44
|
+
* can never fail because of history recording.
|
|
45
|
+
*
|
|
46
|
+
* @param entry - The event data
|
|
47
|
+
* @param databaseId - Optional named database
|
|
48
|
+
* @returns The created history entry, or null if recording failed
|
|
49
|
+
*/
|
|
31
50
|
async function recordHistory(entry, databaseId) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
try {
|
|
52
|
+
return await addHistoryEntry(entry, databaseId);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
logger.warn("recordHistory failed", {
|
|
55
|
+
error: error instanceof Error ? error.message : String(error),
|
|
56
|
+
objectCollection: entry.objectCollection,
|
|
57
|
+
objectId: entry.objectId,
|
|
58
|
+
kind: entry.kind
|
|
59
|
+
});
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
43
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* List the history of an object, newest first.
|
|
65
|
+
*
|
|
66
|
+
* Requires a composite index in the consuming project:
|
|
67
|
+
* objectCollection ASC, objectId ASC, at DESC on collection "_history".
|
|
68
|
+
*
|
|
69
|
+
* @param objectCollection - Collection of the object (e.g., "locations")
|
|
70
|
+
* @param objectId - Document ID of the object
|
|
71
|
+
* @param limit - Maximum entries to return (default: 100)
|
|
72
|
+
* @param databaseId - Optional named database
|
|
73
|
+
* @returns Array of history entries, newest first
|
|
74
|
+
*/
|
|
44
75
|
async function listHistory(objectCollection, objectId, limit = 100, databaseId) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
...doc.data()
|
|
50
|
-
}));
|
|
76
|
+
return (await getDb(databaseId).collection(HISTORY_COLLECTION).where("objectCollection", "==", objectCollection).where("objectId", "==", objectId).orderBy("at", "desc").limit(limit).get()).docs.map((doc) => ({
|
|
77
|
+
id: doc.id,
|
|
78
|
+
...doc.data()
|
|
79
|
+
}));
|
|
51
80
|
}
|
|
52
|
-
|
|
81
|
+
//#endregion
|
|
53
82
|
export { HistoryKind, addHistoryEntry, listHistory, recordHistory };
|
package/dist/import-templates.js
CHANGED
|
@@ -1,113 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { createScriptContext, scriptLog } from "./script-helpers.js";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
3
|
+
//#region src/import-templates.ts
|
|
4
|
+
/**
|
|
5
|
+
* Reusable Mail Templates Import
|
|
6
|
+
*
|
|
7
|
+
* Imports mail templates from a templates directory into Firestore.
|
|
8
|
+
* Each template consists of three files:
|
|
9
|
+
* - {name}.json — metadata (name, subject)
|
|
10
|
+
* - {name}.html.hbs — HTML template
|
|
11
|
+
* - {name}.txt.hbs — Text template
|
|
12
|
+
*/
|
|
4
13
|
function findTemplates(templatesDir) {
|
|
5
|
-
|
|
6
|
-
const jsonFiles = files.filter(
|
|
7
|
-
(f) => f.endsWith(".json") && f !== "serviceAccountKey.json"
|
|
8
|
-
);
|
|
9
|
-
return jsonFiles.map((f) => f.replace(".json", ""));
|
|
14
|
+
return readdirSync(templatesDir).filter((f) => f.endsWith(".json") && f !== "serviceAccountKey.json").map((f) => f.replace(".json", ""));
|
|
10
15
|
}
|
|
11
16
|
function loadTemplate(templatesDir, name) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
const jsonPath = `${templatesDir}/${name}.json`;
|
|
18
|
+
const htmlPath = `${templatesDir}/${name}.html.hbs`;
|
|
19
|
+
const textPath = `${templatesDir}/${name}.txt.hbs`;
|
|
20
|
+
if (!existsSync(jsonPath)) {
|
|
21
|
+
scriptLog(`Metadata not found: ${jsonPath}`, "error");
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (!existsSync(htmlPath)) {
|
|
25
|
+
scriptLog(`HTML template not found: ${htmlPath}`, "error");
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (!existsSync(textPath)) {
|
|
29
|
+
scriptLog(`Text template not found: ${textPath}`, "error");
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const metadata = JSON.parse(readFileSync(jsonPath, "utf-8"));
|
|
33
|
+
const html = readFileSync(htmlPath, "utf-8");
|
|
34
|
+
const text = readFileSync(textPath, "utf-8");
|
|
35
|
+
return {
|
|
36
|
+
...metadata,
|
|
37
|
+
html,
|
|
38
|
+
text
|
|
39
|
+
};
|
|
33
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Import mail templates into Firestore.
|
|
43
|
+
*/
|
|
34
44
|
async function runImportTemplates(config) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
successCount++;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
console.log("\n" + "-".repeat(50));
|
|
101
|
-
console.log(`
|
|
102
|
-
Summary:`);
|
|
103
|
-
console.log(` Success: ${successCount}`);
|
|
104
|
-
console.log(` Errors: ${errorCount}`);
|
|
105
|
-
if (dryRun) {
|
|
106
|
-
console.log(`
|
|
107
|
-
Run without --dry-run to apply changes.
|
|
108
|
-
`);
|
|
109
|
-
}
|
|
110
|
-
process.exit(errorCount > 0 ? 1 : 0);
|
|
45
|
+
const { templatesDir, serviceAccountPath, databaseId, collection = "emailTemplates", templateFilter, dryRun = false } = config;
|
|
46
|
+
if (!existsSync(templatesDir)) {
|
|
47
|
+
scriptLog(`Templates directory not found: ${templatesDir}`, "error");
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
const { db } = createScriptContext({
|
|
51
|
+
title: "Mail Templates Import",
|
|
52
|
+
serviceAccountPath,
|
|
53
|
+
databaseId,
|
|
54
|
+
dryRun
|
|
55
|
+
});
|
|
56
|
+
scriptLog(`Templates dir: ${templatesDir}`);
|
|
57
|
+
scriptLog(`Target collection: ${collection}`);
|
|
58
|
+
let templates = findTemplates(templatesDir);
|
|
59
|
+
if (templateFilter) {
|
|
60
|
+
templates = templates.filter((t) => t === templateFilter);
|
|
61
|
+
if (templates.length === 0) {
|
|
62
|
+
scriptLog(`Template not found: ${templateFilter}`, "error");
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
scriptLog(`Found ${templates.length} template(s): ${templates.join(", ")}`);
|
|
67
|
+
console.log("");
|
|
68
|
+
let successCount = 0;
|
|
69
|
+
let errorCount = 0;
|
|
70
|
+
for (const templateName of templates) {
|
|
71
|
+
const template = loadTemplate(templatesDir, templateName);
|
|
72
|
+
if (!template) {
|
|
73
|
+
errorCount++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const docId = `${templateName}Id`;
|
|
77
|
+
console.log(`\n ${templateName}`);
|
|
78
|
+
console.log(` Document ID: ${docId}`);
|
|
79
|
+
console.log(` Subject: ${template.subject}`);
|
|
80
|
+
console.log(` HTML: ${template.html.length} chars`);
|
|
81
|
+
console.log(` Text: ${template.text.length} chars`);
|
|
82
|
+
if (!dryRun) try {
|
|
83
|
+
await db.collection(collection).doc(docId).set({
|
|
84
|
+
name: template.name,
|
|
85
|
+
subject: template.subject,
|
|
86
|
+
html: template.html,
|
|
87
|
+
text: template.text,
|
|
88
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
89
|
+
});
|
|
90
|
+
scriptLog(`Imported: ${templateName} -> ${docId}`, "success");
|
|
91
|
+
successCount++;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
scriptLog(`Failed to import ${templateName}: ${error}`, "error");
|
|
94
|
+
errorCount++;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
scriptLog(`Would import: ${templateName} -> ${docId}`, "info");
|
|
98
|
+
successCount++;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
console.log("\n" + "-".repeat(50));
|
|
102
|
+
console.log(`\nSummary:`);
|
|
103
|
+
console.log(` Success: ${successCount}`);
|
|
104
|
+
console.log(` Errors: ${errorCount}`);
|
|
105
|
+
if (dryRun) console.log(`\n Run without --dry-run to apply changes.\n`);
|
|
106
|
+
process.exit(errorCount > 0 ? 1 : 0);
|
|
111
107
|
}
|
|
112
|
-
|
|
108
|
+
//#endregion
|
|
113
109
|
export { runImportTemplates };
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { StatusCode } from "./types.js";
|
|
2
|
+
import { AppError, ConflictError, EmailAlreadyUsedError, ForbiddenError, NotFoundError, SC_EMAIL_ALREADY_USED, UnauthorizedError, ValidationError } from "./errors.js";
|
|
3
|
+
import { logger } from "./logger.js";
|
|
4
|
+
import { requireAuth, requireClaims, requireOwnership } from "./auth.js";
|
|
5
|
+
import { created, handleError, success } from "./response.js";
|
|
6
|
+
import { getDb } from "./firestore.js";
|
|
7
|
+
import { getNextCounter } from "./counter.js";
|
|
8
|
+
import { countVisit } from "./visits.js";
|
|
9
|
+
import { getTrashItem, listTrash, moveToTrash, permanentDelete, restoreFromTrash } from "./trash.js";
|
|
10
|
+
import { moveToTrashWithAuth, permanentDeleteWithAuth, restoreFromTrashWithAuth } from "./auth-trash.js";
|
|
11
|
+
import { computeChanges } from "./changes.js";
|
|
12
|
+
import { HistoryKind, addHistoryEntry, listHistory, recordHistory } from "./history.js";
|
|
13
|
+
import { addMailForSend, getMailById, getMailsByField } from "./mail.js";
|
|
14
|
+
import { createScriptContext, initFirebaseAdmin, scriptLog } from "./script-helpers.js";
|
|
15
|
+
import { runImportTemplates } from "./import-templates.js";
|
|
16
|
+
export { AppError, ConflictError, EmailAlreadyUsedError, ForbiddenError, HistoryKind, NotFoundError, SC_EMAIL_ALREADY_USED, StatusCode, UnauthorizedError, ValidationError, addHistoryEntry, addMailForSend, computeChanges, countVisit, createScriptContext, created, getDb, getMailById, getMailsByField, getNextCounter, getTrashItem, handleError, initFirebaseAdmin, listHistory, listTrash, logger, moveToTrash, moveToTrashWithAuth, permanentDelete, permanentDeleteWithAuth, recordHistory, requireAuth, requireClaims, requireOwnership, restoreFromTrash, restoreFromTrashWithAuth, runImportTemplates, scriptLog, success };
|
package/dist/logger.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import * as functions from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import * as functions from "firebase-functions/logger";
|
|
2
|
+
//#region src/logger.ts
|
|
3
|
+
var logger = {
|
|
4
|
+
debug: (msg, data) => functions.debug(msg, data),
|
|
5
|
+
info: (msg, data) => functions.info(msg, data),
|
|
6
|
+
warn: (msg, data) => functions.warn(msg, data),
|
|
7
|
+
error: (msg, error, data) => functions.error(msg, {
|
|
8
|
+
error,
|
|
9
|
+
...data
|
|
10
|
+
})
|
|
8
11
|
};
|
|
9
|
-
|
|
12
|
+
//#endregion
|
|
10
13
|
export { logger };
|
package/dist/mail.js
CHANGED
|
@@ -1,42 +1,63 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getDb } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { ValidationError } from "./errors.js";
|
|
2
|
+
import { getDb } from "./firestore.js";
|
|
3
|
+
import { Timestamp } from "firebase-admin/firestore";
|
|
4
|
+
//#region src/mail.ts
|
|
5
|
+
var DEFAULT_MAIL_COLLECTION = "mail";
|
|
6
|
+
/**
|
|
7
|
+
* Write a mail document for the Firebase mail extension to send.
|
|
8
|
+
*
|
|
9
|
+
* Throws on failure — the calling app decides whether a mail error aborts
|
|
10
|
+
* its flow or is just logged.
|
|
11
|
+
*
|
|
12
|
+
* @param mail - The mail data; app-specific extra fields are stored as-is
|
|
13
|
+
* @param collection - Mail collection name (default: "mail")
|
|
14
|
+
* @param databaseId - Optional named database
|
|
15
|
+
* @returns The created mail document ID
|
|
16
|
+
* @throws ValidationError if `to` is missing or neither template nor message is set
|
|
17
|
+
*/
|
|
6
18
|
async function addMailForSend(mail, collection = DEFAULT_MAIL_COLLECTION, databaseId) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const db = getDb(databaseId);
|
|
14
|
-
const docRef = await db.collection(collection).add({
|
|
15
|
-
...mail,
|
|
16
|
-
creation: Timestamp.now()
|
|
17
|
-
});
|
|
18
|
-
return docRef.id;
|
|
19
|
+
if (!mail.to) throw new ValidationError("Mail requires a recipient (to)");
|
|
20
|
+
if (!mail.template && !mail.message) throw new ValidationError("Mail requires a template or a message");
|
|
21
|
+
return (await getDb(databaseId).collection(collection).add({
|
|
22
|
+
...mail,
|
|
23
|
+
creation: Timestamp.now()
|
|
24
|
+
})).id;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Get a single mail document by ID (e.g. to inspect delivery state).
|
|
28
|
+
*
|
|
29
|
+
* @param mailId - The mail document ID
|
|
30
|
+
* @param collection - Mail collection name (default: "mail")
|
|
31
|
+
* @param databaseId - Optional named database
|
|
32
|
+
* @returns The stored mail or null if not found
|
|
33
|
+
*/
|
|
20
34
|
async function getMailById(mailId, collection = DEFAULT_MAIL_COLLECTION, databaseId) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
id: doc.id,
|
|
28
|
-
...doc.data()
|
|
29
|
-
};
|
|
35
|
+
const doc = await getDb(databaseId).collection(collection).doc(mailId).get();
|
|
36
|
+
if (!doc.exists) return null;
|
|
37
|
+
return {
|
|
38
|
+
id: doc.id,
|
|
39
|
+
...doc.data()
|
|
40
|
+
};
|
|
30
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* List mail documents by an app-specific correlation field
|
|
44
|
+
* (e.g. field "eventId").
|
|
45
|
+
*
|
|
46
|
+
* Deliberately without orderBy — no composite index requirement per queried
|
|
47
|
+
* field; sorting is up to the caller.
|
|
48
|
+
*
|
|
49
|
+
* @param field - Field name to filter on
|
|
50
|
+
* @param value - Value the field must equal
|
|
51
|
+
* @param collection - Mail collection name (default: "mail")
|
|
52
|
+
* @param limit - Maximum documents to return (default: 100)
|
|
53
|
+
* @param databaseId - Optional named database
|
|
54
|
+
* @returns Array of stored mails
|
|
55
|
+
*/
|
|
31
56
|
async function getMailsByField(field, value, collection = DEFAULT_MAIL_COLLECTION, limit = 100, databaseId) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
id: doc.id,
|
|
37
|
-
...doc.data()
|
|
38
|
-
})
|
|
39
|
-
);
|
|
57
|
+
return (await getDb(databaseId).collection(collection).where(field, "==", value).limit(limit).get()).docs.map((doc) => ({
|
|
58
|
+
id: doc.id,
|
|
59
|
+
...doc.data()
|
|
60
|
+
}));
|
|
40
61
|
}
|
|
41
|
-
|
|
62
|
+
//#endregion
|
|
42
63
|
export { addMailForSend, getMailById, getMailsByField };
|
package/dist/response.js
CHANGED
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
import { StatusCode } from
|
|
2
|
-
import { AppError } from
|
|
3
|
-
import { logger } from
|
|
4
|
-
|
|
1
|
+
import { StatusCode } from "./types.js";
|
|
2
|
+
import { AppError } from "./errors.js";
|
|
3
|
+
import { logger } from "./logger.js";
|
|
4
|
+
//#region src/response.ts
|
|
5
5
|
function success(data) {
|
|
6
|
-
|
|
6
|
+
return {
|
|
7
|
+
statusCode: StatusCode.OK,
|
|
8
|
+
resultData: data
|
|
9
|
+
};
|
|
7
10
|
}
|
|
8
11
|
function created(data) {
|
|
9
|
-
|
|
12
|
+
return {
|
|
13
|
+
statusCode: StatusCode.CREATED,
|
|
14
|
+
resultData: data
|
|
15
|
+
};
|
|
10
16
|
}
|
|
11
17
|
function handleError(error) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
if (error instanceof AppError) {
|
|
19
|
+
if (error.statusCode >= 500) logger.error("App error", error);
|
|
20
|
+
else logger.warn("Request error", {
|
|
21
|
+
code: error.code,
|
|
22
|
+
message: error.message
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
statusCode: error.statusCode,
|
|
26
|
+
resultData: {
|
|
27
|
+
error: error.message,
|
|
28
|
+
code: error.code
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
logger.error("Unexpected error", error);
|
|
33
|
+
return {
|
|
34
|
+
statusCode: StatusCode.INTERNAL_ERROR,
|
|
35
|
+
resultData: {
|
|
36
|
+
error: "Internal server error",
|
|
37
|
+
code: "INTERNAL_ERROR"
|
|
38
|
+
}
|
|
39
|
+
};
|
|
31
40
|
}
|
|
32
|
-
|
|
41
|
+
//#endregion
|
|
33
42
|
export { created, handleError, success };
|