@it-ybm-agency/lead-notify 0.0.1 → 0.1.0-rc.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/airtable.d.ts +6 -0
- package/dist/blacklistLoader.d.ts +10 -0
- package/dist/deadletter.d.ts +1 -0
- package/dist/index.cjs +173 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +140 -0
- package/dist/notifyError.d.ts +31 -0
- package/package.json +42 -5
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type BlacklistResult = {
|
|
2
|
+
emails: string[];
|
|
3
|
+
error?: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type BlacklistLoaderOptions = {
|
|
6
|
+
fetchEmails: () => Promise<string[]>;
|
|
7
|
+
ttlMs?: number;
|
|
8
|
+
backoffMs?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function createBlacklistLoader({ fetchEmails, ttlMs, backoffMs }: BlacklistLoaderOptions): () => Promise<BlacklistResult>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function saveToDeadLetter(requestId: string, lead: unknown, storeName?: string): Promise<boolean>;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AirtableApiError: () => AirtableApiError,
|
|
24
|
+
createBlacklistLoader: () => createBlacklistLoader,
|
|
25
|
+
createNotifier: () => createNotifier,
|
|
26
|
+
describeError: () => describeError,
|
|
27
|
+
maskPii: () => maskPii,
|
|
28
|
+
saveToDeadLetter: () => saveToDeadLetter,
|
|
29
|
+
throwIfAirtableError: () => throwIfAirtableError
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// src/notifyError.ts
|
|
34
|
+
var esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
35
|
+
var cut = (s, n) => s.length > n ? s.slice(0, n) + "\u2026" : s;
|
|
36
|
+
function describeError(err) {
|
|
37
|
+
const e = err;
|
|
38
|
+
return {
|
|
39
|
+
name: e?.name,
|
|
40
|
+
message: e?.message ?? String(err),
|
|
41
|
+
code: e?.code,
|
|
42
|
+
responseCode: e?.responseCode,
|
|
43
|
+
response: typeof e?.response === "string" ? e.response.slice(0, 300) : void 0,
|
|
44
|
+
status: e?.status ?? e?.statusCode,
|
|
45
|
+
type: e?.type ?? (typeof e?.error === "string" ? e.error : void 0),
|
|
46
|
+
stack: typeof e?.stack === "string" ? e.stack : void 0
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function maskPii(d, keep = []) {
|
|
50
|
+
const email = d.email ?? "";
|
|
51
|
+
const phone = d.phone ?? "";
|
|
52
|
+
const [user = "", domain = ""] = email.split("@");
|
|
53
|
+
const out = {
|
|
54
|
+
email: email ? `${user[0] ?? ""}***@${domain}` : void 0,
|
|
55
|
+
phone: phone ? `***${phone.slice(-3)}` : void 0
|
|
56
|
+
};
|
|
57
|
+
for (const key of keep) out[key] = d[key];
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
function createNotifier(config) {
|
|
61
|
+
const { botToken, chatId, threadId, siteName, timeoutMs = 3e3 } = config;
|
|
62
|
+
async function notifyError(failures, ctx) {
|
|
63
|
+
const described = failures.map((f) => ({ stage: f.stage, ...describeError(f.error) }));
|
|
64
|
+
console.error(JSON.stringify({ site: siteName, ...ctx, failures: described }));
|
|
65
|
+
if (!botToken || !chatId) return;
|
|
66
|
+
const icon = ctx.severity === "critical" ? "\u{1F534}" : "\u{1F7E1}";
|
|
67
|
+
const blocks = described.map((d) => {
|
|
68
|
+
const meta = [d.code, d.responseCode, d.status, d.type].filter(Boolean).join(" / ");
|
|
69
|
+
return [
|
|
70
|
+
`<b>${esc(d.stage)}</b>: <code>${esc(cut(String(d.message), 400))}</code>`,
|
|
71
|
+
meta && `<i>${esc(meta)}</i>`,
|
|
72
|
+
d.stage === "unexpected" && d.stack && `<pre>${esc(cut(d.stack.split("\n").slice(0, 5).join("\n"), 500))}</pre>`,
|
|
73
|
+
d.response && `<pre>${esc(cut(d.response, 300))}</pre>`
|
|
74
|
+
].filter(Boolean).join("\n");
|
|
75
|
+
});
|
|
76
|
+
const text = [
|
|
77
|
+
`${icon} <b>${esc(siteName ?? "site")}</b> ${esc(ctx.route)}`,
|
|
78
|
+
...blocks,
|
|
79
|
+
ctx.requestId && `<b>ID:</b> <code>${esc(ctx.requestId)}</code>`,
|
|
80
|
+
ctx.extra && `<pre>${esc(cut(JSON.stringify(ctx.extra, null, 2), 600))}</pre>`
|
|
81
|
+
].filter(Boolean).join("\n");
|
|
82
|
+
try {
|
|
83
|
+
const res = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { "Content-Type": "application/json" },
|
|
86
|
+
body: JSON.stringify({
|
|
87
|
+
chat_id: chatId,
|
|
88
|
+
message_thread_id: threadId ? Number(threadId) : void 0,
|
|
89
|
+
text,
|
|
90
|
+
parse_mode: "HTML",
|
|
91
|
+
disable_web_page_preview: true
|
|
92
|
+
}),
|
|
93
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
94
|
+
});
|
|
95
|
+
if (!res.ok) {
|
|
96
|
+
console.error("Telegram notify rejected", res.status, await res.text());
|
|
97
|
+
}
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.error("Telegram notify failed", e);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { notifyError };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/deadletter.ts
|
|
106
|
+
var import_blobs = require("@netlify/blobs");
|
|
107
|
+
async function saveToDeadLetter(requestId, lead, storeName = "failed-leads") {
|
|
108
|
+
try {
|
|
109
|
+
await (0, import_blobs.getStore)(storeName).setJSON(requestId, { at: (/* @__PURE__ */ new Date()).toISOString(), lead });
|
|
110
|
+
return true;
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.error("Dead-letter save failed", e);
|
|
113
|
+
console.error("LEAD LOST", JSON.stringify({ requestId, lead }));
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/airtable.ts
|
|
119
|
+
var AirtableApiError = class extends Error {
|
|
120
|
+
constructor(status, type, message) {
|
|
121
|
+
super(message);
|
|
122
|
+
this.status = status;
|
|
123
|
+
this.type = type;
|
|
124
|
+
this.name = "AirtableApiError";
|
|
125
|
+
}
|
|
126
|
+
status;
|
|
127
|
+
type;
|
|
128
|
+
};
|
|
129
|
+
async function throwIfAirtableError(response) {
|
|
130
|
+
if (response.ok) return;
|
|
131
|
+
const body = await response.json().catch(() => null);
|
|
132
|
+
const type = typeof body?.error === "string" ? body.error : body?.error?.type;
|
|
133
|
+
const message = body?.error?.message ?? response.statusText;
|
|
134
|
+
throw new AirtableApiError(response.status, type, `Airtable ${response.status}: ${message}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/blacklistLoader.ts
|
|
138
|
+
function createBlacklistLoader({
|
|
139
|
+
fetchEmails,
|
|
140
|
+
ttlMs = 5 * 6e4,
|
|
141
|
+
backoffMs = 6e4
|
|
142
|
+
}) {
|
|
143
|
+
let cache = null;
|
|
144
|
+
let retryAfter = 0;
|
|
145
|
+
let inflight = null;
|
|
146
|
+
return async function getBlacklistEmail() {
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
if (cache && now - cache.at < ttlMs) return { emails: cache.emails };
|
|
149
|
+
if (now < retryAfter) return { emails: cache?.emails ?? [] };
|
|
150
|
+
try {
|
|
151
|
+
inflight ??= fetchEmails().finally(() => {
|
|
152
|
+
inflight = null;
|
|
153
|
+
});
|
|
154
|
+
const emails = await inflight;
|
|
155
|
+
cache = { emails, at: Date.now() };
|
|
156
|
+
return { emails };
|
|
157
|
+
} catch (error) {
|
|
158
|
+
retryAfter = Date.now() + backoffMs;
|
|
159
|
+
console.error("Error fetching blacklist email:", error);
|
|
160
|
+
return { emails: cache?.emails ?? [], error };
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
165
|
+
0 && (module.exports = {
|
|
166
|
+
AirtableApiError,
|
|
167
|
+
createBlacklistLoader,
|
|
168
|
+
createNotifier,
|
|
169
|
+
describeError,
|
|
170
|
+
maskPii,
|
|
171
|
+
saveToDeadLetter,
|
|
172
|
+
throwIfAirtableError
|
|
173
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// src/notifyError.ts
|
|
2
|
+
var esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
3
|
+
var cut = (s, n) => s.length > n ? s.slice(0, n) + "\u2026" : s;
|
|
4
|
+
function describeError(err) {
|
|
5
|
+
const e = err;
|
|
6
|
+
return {
|
|
7
|
+
name: e?.name,
|
|
8
|
+
message: e?.message ?? String(err),
|
|
9
|
+
code: e?.code,
|
|
10
|
+
responseCode: e?.responseCode,
|
|
11
|
+
response: typeof e?.response === "string" ? e.response.slice(0, 300) : void 0,
|
|
12
|
+
status: e?.status ?? e?.statusCode,
|
|
13
|
+
type: e?.type ?? (typeof e?.error === "string" ? e.error : void 0),
|
|
14
|
+
stack: typeof e?.stack === "string" ? e.stack : void 0
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function maskPii(d, keep = []) {
|
|
18
|
+
const email = d.email ?? "";
|
|
19
|
+
const phone = d.phone ?? "";
|
|
20
|
+
const [user = "", domain = ""] = email.split("@");
|
|
21
|
+
const out = {
|
|
22
|
+
email: email ? `${user[0] ?? ""}***@${domain}` : void 0,
|
|
23
|
+
phone: phone ? `***${phone.slice(-3)}` : void 0
|
|
24
|
+
};
|
|
25
|
+
for (const key of keep) out[key] = d[key];
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function createNotifier(config) {
|
|
29
|
+
const { botToken, chatId, threadId, siteName, timeoutMs = 3e3 } = config;
|
|
30
|
+
async function notifyError(failures, ctx) {
|
|
31
|
+
const described = failures.map((f) => ({ stage: f.stage, ...describeError(f.error) }));
|
|
32
|
+
console.error(JSON.stringify({ site: siteName, ...ctx, failures: described }));
|
|
33
|
+
if (!botToken || !chatId) return;
|
|
34
|
+
const icon = ctx.severity === "critical" ? "\u{1F534}" : "\u{1F7E1}";
|
|
35
|
+
const blocks = described.map((d) => {
|
|
36
|
+
const meta = [d.code, d.responseCode, d.status, d.type].filter(Boolean).join(" / ");
|
|
37
|
+
return [
|
|
38
|
+
`<b>${esc(d.stage)}</b>: <code>${esc(cut(String(d.message), 400))}</code>`,
|
|
39
|
+
meta && `<i>${esc(meta)}</i>`,
|
|
40
|
+
d.stage === "unexpected" && d.stack && `<pre>${esc(cut(d.stack.split("\n").slice(0, 5).join("\n"), 500))}</pre>`,
|
|
41
|
+
d.response && `<pre>${esc(cut(d.response, 300))}</pre>`
|
|
42
|
+
].filter(Boolean).join("\n");
|
|
43
|
+
});
|
|
44
|
+
const text = [
|
|
45
|
+
`${icon} <b>${esc(siteName ?? "site")}</b> ${esc(ctx.route)}`,
|
|
46
|
+
...blocks,
|
|
47
|
+
ctx.requestId && `<b>ID:</b> <code>${esc(ctx.requestId)}</code>`,
|
|
48
|
+
ctx.extra && `<pre>${esc(cut(JSON.stringify(ctx.extra, null, 2), 600))}</pre>`
|
|
49
|
+
].filter(Boolean).join("\n");
|
|
50
|
+
try {
|
|
51
|
+
const res = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/json" },
|
|
54
|
+
body: JSON.stringify({
|
|
55
|
+
chat_id: chatId,
|
|
56
|
+
message_thread_id: threadId ? Number(threadId) : void 0,
|
|
57
|
+
text,
|
|
58
|
+
parse_mode: "HTML",
|
|
59
|
+
disable_web_page_preview: true
|
|
60
|
+
}),
|
|
61
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
62
|
+
});
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
console.error("Telegram notify rejected", res.status, await res.text());
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.error("Telegram notify failed", e);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { notifyError };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/deadletter.ts
|
|
74
|
+
import { getStore } from "@netlify/blobs";
|
|
75
|
+
async function saveToDeadLetter(requestId, lead, storeName = "failed-leads") {
|
|
76
|
+
try {
|
|
77
|
+
await getStore(storeName).setJSON(requestId, { at: (/* @__PURE__ */ new Date()).toISOString(), lead });
|
|
78
|
+
return true;
|
|
79
|
+
} catch (e) {
|
|
80
|
+
console.error("Dead-letter save failed", e);
|
|
81
|
+
console.error("LEAD LOST", JSON.stringify({ requestId, lead }));
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/airtable.ts
|
|
87
|
+
var AirtableApiError = class extends Error {
|
|
88
|
+
constructor(status, type, message) {
|
|
89
|
+
super(message);
|
|
90
|
+
this.status = status;
|
|
91
|
+
this.type = type;
|
|
92
|
+
this.name = "AirtableApiError";
|
|
93
|
+
}
|
|
94
|
+
status;
|
|
95
|
+
type;
|
|
96
|
+
};
|
|
97
|
+
async function throwIfAirtableError(response) {
|
|
98
|
+
if (response.ok) return;
|
|
99
|
+
const body = await response.json().catch(() => null);
|
|
100
|
+
const type = typeof body?.error === "string" ? body.error : body?.error?.type;
|
|
101
|
+
const message = body?.error?.message ?? response.statusText;
|
|
102
|
+
throw new AirtableApiError(response.status, type, `Airtable ${response.status}: ${message}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/blacklistLoader.ts
|
|
106
|
+
function createBlacklistLoader({
|
|
107
|
+
fetchEmails,
|
|
108
|
+
ttlMs = 5 * 6e4,
|
|
109
|
+
backoffMs = 6e4
|
|
110
|
+
}) {
|
|
111
|
+
let cache = null;
|
|
112
|
+
let retryAfter = 0;
|
|
113
|
+
let inflight = null;
|
|
114
|
+
return async function getBlacklistEmail() {
|
|
115
|
+
const now = Date.now();
|
|
116
|
+
if (cache && now - cache.at < ttlMs) return { emails: cache.emails };
|
|
117
|
+
if (now < retryAfter) return { emails: cache?.emails ?? [] };
|
|
118
|
+
try {
|
|
119
|
+
inflight ??= fetchEmails().finally(() => {
|
|
120
|
+
inflight = null;
|
|
121
|
+
});
|
|
122
|
+
const emails = await inflight;
|
|
123
|
+
cache = { emails, at: Date.now() };
|
|
124
|
+
return { emails };
|
|
125
|
+
} catch (error) {
|
|
126
|
+
retryAfter = Date.now() + backoffMs;
|
|
127
|
+
console.error("Error fetching blacklist email:", error);
|
|
128
|
+
return { emails: cache?.emails ?? [], error };
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export {
|
|
133
|
+
AirtableApiError,
|
|
134
|
+
createBlacklistLoader,
|
|
135
|
+
createNotifier,
|
|
136
|
+
describeError,
|
|
137
|
+
maskPii,
|
|
138
|
+
saveToDeadLetter,
|
|
139
|
+
throwIfAirtableError
|
|
140
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type Failure = {
|
|
2
|
+
stage: string;
|
|
3
|
+
error: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type NotifyContext = {
|
|
6
|
+
route: string;
|
|
7
|
+
requestId?: string;
|
|
8
|
+
severity: 'partial' | 'critical';
|
|
9
|
+
extra?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
export type NotifierConfig = {
|
|
12
|
+
botToken?: string;
|
|
13
|
+
chatId?: string;
|
|
14
|
+
threadId?: string | number;
|
|
15
|
+
siteName?: string;
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
};
|
|
18
|
+
export declare function describeError(err: unknown): {
|
|
19
|
+
name: any;
|
|
20
|
+
message: any;
|
|
21
|
+
code: any;
|
|
22
|
+
responseCode: any;
|
|
23
|
+
response: any;
|
|
24
|
+
status: any;
|
|
25
|
+
type: any;
|
|
26
|
+
stack: any;
|
|
27
|
+
};
|
|
28
|
+
export declare function maskPii(d: Record<string, string | undefined>, keep?: string[]): Record<string, string | undefined>;
|
|
29
|
+
export declare function createNotifier(config: NotifierConfig): {
|
|
30
|
+
notifyError: (failures: Failure[], ctx: NotifyContext) => Promise<void>;
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@it-ybm-agency/lead-notify",
|
|
3
|
-
"version": "0.0.1",
|
|
4
|
-
"
|
|
5
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@it-ybm-agency/lead-notify",
|
|
3
|
+
"version": "0.1.0-rc.1",
|
|
4
|
+
"description": "Telegram alerts, dead-letter storage and helpers for lead form API routes",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup && tsc -p tsconfig.build.json",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@netlify/blobs": ">=8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://gitlab.com/ybm-agency/notify-error.git"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@netlify/blobs": "^11.1.11",
|
|
38
|
+
"@types/node": "^22.20.5",
|
|
39
|
+
"tsup": "^8.5.1",
|
|
40
|
+
"typescript": "^7.0.2"
|
|
41
|
+
}
|
|
42
|
+
}
|