@neetru/sdk 1.1.1 → 2.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +284 -214
  2. package/README.md +194 -218
  3. package/dist/auth.cjs +4181 -346
  4. package/dist/auth.cjs.map +1 -1
  5. package/dist/auth.d.cts +5 -1
  6. package/dist/auth.d.ts +5 -1
  7. package/dist/auth.mjs +4181 -346
  8. package/dist/auth.mjs.map +1 -1
  9. package/dist/catalog.cjs +63 -24
  10. package/dist/catalog.cjs.map +1 -1
  11. package/dist/catalog.d.cts +6 -2
  12. package/dist/catalog.d.ts +6 -2
  13. package/dist/catalog.mjs +63 -24
  14. package/dist/catalog.mjs.map +1 -1
  15. package/dist/checkout.cjs +60 -18
  16. package/dist/checkout.cjs.map +1 -1
  17. package/dist/checkout.d.cts +5 -1
  18. package/dist/checkout.d.ts +5 -1
  19. package/dist/checkout.mjs +60 -18
  20. package/dist/checkout.mjs.map +1 -1
  21. package/dist/collection-ref-BBvTTXoG.d.cts +423 -0
  22. package/dist/collection-ref-BBvTTXoG.d.ts +423 -0
  23. package/dist/db-react.cjs +136 -0
  24. package/dist/db-react.cjs.map +1 -0
  25. package/dist/db-react.d.cts +99 -0
  26. package/dist/db-react.d.ts +99 -0
  27. package/dist/db-react.mjs +112 -0
  28. package/dist/db-react.mjs.map +1 -0
  29. package/dist/db.cjs +3652 -143
  30. package/dist/db.cjs.map +1 -1
  31. package/dist/db.d.cts +5 -8
  32. package/dist/db.d.ts +5 -8
  33. package/dist/db.mjs +3649 -143
  34. package/dist/db.mjs.map +1 -1
  35. package/dist/entitlements.cjs +101 -24
  36. package/dist/entitlements.cjs.map +1 -1
  37. package/dist/entitlements.d.cts +15 -5
  38. package/dist/entitlements.d.ts +15 -5
  39. package/dist/entitlements.mjs +101 -24
  40. package/dist/entitlements.mjs.map +1 -1
  41. package/dist/errors.cjs.map +1 -1
  42. package/dist/errors.mjs.map +1 -1
  43. package/dist/index.cjs +4341 -282
  44. package/dist/index.cjs.map +1 -1
  45. package/dist/index.d.cts +13 -6
  46. package/dist/index.d.ts +13 -6
  47. package/dist/index.mjs +4243 -189
  48. package/dist/index.mjs.map +1 -1
  49. package/dist/mocks.cjs +186 -9
  50. package/dist/mocks.cjs.map +1 -1
  51. package/dist/mocks.d.cts +21 -6
  52. package/dist/mocks.d.ts +21 -6
  53. package/dist/mocks.mjs +186 -9
  54. package/dist/mocks.mjs.map +1 -1
  55. package/dist/notifications.cjs +296 -0
  56. package/dist/notifications.cjs.map +1 -0
  57. package/dist/notifications.d.cts +5 -0
  58. package/dist/notifications.d.ts +5 -0
  59. package/dist/notifications.mjs +293 -0
  60. package/dist/notifications.mjs.map +1 -0
  61. package/dist/react.cjs +7 -3
  62. package/dist/react.cjs.map +1 -1
  63. package/dist/react.d.cts +5 -1
  64. package/dist/react.d.ts +5 -1
  65. package/dist/react.mjs +7 -3
  66. package/dist/react.mjs.map +1 -1
  67. package/dist/support.cjs +60 -18
  68. package/dist/support.cjs.map +1 -1
  69. package/dist/support.d.cts +5 -1
  70. package/dist/support.d.ts +5 -1
  71. package/dist/support.mjs +60 -18
  72. package/dist/support.mjs.map +1 -1
  73. package/dist/telemetry.cjs +130 -19
  74. package/dist/telemetry.cjs.map +1 -1
  75. package/dist/telemetry.d.cts +21 -1
  76. package/dist/telemetry.d.ts +21 -1
  77. package/dist/telemetry.mjs +130 -19
  78. package/dist/telemetry.mjs.map +1 -1
  79. package/dist/types-B1jylbMC.d.ts +1364 -0
  80. package/dist/types-Kmt4y1FQ.d.cts +1364 -0
  81. package/dist/usage.cjs +60 -18
  82. package/dist/usage.cjs.map +1 -1
  83. package/dist/usage.d.cts +5 -1
  84. package/dist/usage.d.ts +5 -1
  85. package/dist/usage.mjs +60 -18
  86. package/dist/usage.mjs.map +1 -1
  87. package/dist/webhooks.cjs +316 -0
  88. package/dist/webhooks.cjs.map +1 -0
  89. package/dist/webhooks.d.cts +5 -0
  90. package/dist/webhooks.d.ts +5 -0
  91. package/dist/webhooks.mjs +312 -0
  92. package/dist/webhooks.mjs.map +1 -0
  93. package/package.json +133 -101
  94. package/dist/types-BA53dd8S.d.cts +0 -490
  95. package/dist/types-BA53dd8S.d.ts +0 -490
@@ -0,0 +1,296 @@
1
+ 'use strict';
2
+
3
+ // src/errors.ts
4
+ var NeetruError = class _NeetruError extends Error {
5
+ code;
6
+ status;
7
+ requestId;
8
+ constructor(code, message, status, requestId) {
9
+ super(message);
10
+ this.name = "NeetruError";
11
+ this.code = code;
12
+ this.status = status;
13
+ this.requestId = requestId;
14
+ Object.setPrototypeOf(this, _NeetruError.prototype);
15
+ }
16
+ };
17
+
18
+ // src/http.ts
19
+ var DEFAULT_RETRIES = 2;
20
+ var RETRYABLE_CODES = /* @__PURE__ */ new Set([
21
+ "rate_limited",
22
+ "server_error",
23
+ "network_error"
24
+ ]);
25
+ function backoffMs(attempt) {
26
+ const base = 200 * Math.pow(4, attempt);
27
+ const jitter = base * 0.2 * (Math.random() * 2 - 1);
28
+ return Math.max(50, Math.round(base + jitter));
29
+ }
30
+ function parseRetryAfter(value) {
31
+ if (!value) return null;
32
+ const secs = Number(value);
33
+ if (Number.isFinite(secs) && secs >= 0) return Math.round(secs * 1e3);
34
+ const dateMs = Date.parse(value);
35
+ if (Number.isFinite(dateMs)) {
36
+ const delta = dateMs - Date.now();
37
+ if (delta > 0) return delta;
38
+ }
39
+ return null;
40
+ }
41
+ function sleep(ms) {
42
+ return new Promise((resolve) => setTimeout(resolve, ms));
43
+ }
44
+ function statusToCode(status) {
45
+ if (status === 401) return "unauthorized";
46
+ if (status === 403) return "forbidden";
47
+ if (status === 404) return "not_found";
48
+ if (status === 422 || status === 400) return "validation_failed";
49
+ if (status === 429) return "rate_limited";
50
+ if (status >= 500) return "server_error";
51
+ return "unknown";
52
+ }
53
+ function buildUrl(baseUrl, path, query) {
54
+ const base = baseUrl.replace(/\/+$/, "");
55
+ const p = path.startsWith("/") ? path : `/${path}`;
56
+ const url = new URL(`${base}${p}`);
57
+ if (query) {
58
+ for (const [k, v] of Object.entries(query)) {
59
+ if (v === void 0) continue;
60
+ url.searchParams.set(k, String(v));
61
+ }
62
+ }
63
+ return url.toString();
64
+ }
65
+ async function safeJson(res) {
66
+ const text = await res.text();
67
+ if (!text) return void 0;
68
+ try {
69
+ return JSON.parse(text);
70
+ } catch {
71
+ return void 0;
72
+ }
73
+ }
74
+ async function httpRequest(config, opts) {
75
+ const method = opts.method ?? "GET";
76
+ const url = buildUrl(config.baseUrl, opts.path, opts.query);
77
+ const maxRetries = opts.retries ?? DEFAULT_RETRIES;
78
+ const headers = {
79
+ accept: "application/json",
80
+ ...opts.headers
81
+ };
82
+ if (opts.requireAuth) {
83
+ if (!config.apiKey) {
84
+ throw new NeetruError(
85
+ "missing_api_key",
86
+ "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
87
+ );
88
+ }
89
+ headers.authorization = `Bearer ${config.apiKey}`;
90
+ }
91
+ const bodyString = opts.body !== void 0 && method !== "GET" && method !== "DELETE" ? JSON.stringify(opts.body) : void 0;
92
+ if (bodyString !== void 0) {
93
+ headers["content-type"] = "application/json";
94
+ }
95
+ let lastError = null;
96
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
97
+ const init = { method, headers };
98
+ if (bodyString !== void 0) init.body = bodyString;
99
+ init.signal = AbortSignal.timeout(3e4);
100
+ let res;
101
+ try {
102
+ res = await config.fetch(url, init);
103
+ } catch (err2) {
104
+ const message2 = err2 instanceof DOMException && err2.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${err2 instanceof Error ? err2.message : "fetch failed"}`;
105
+ lastError = new NeetruError("network_error", message2);
106
+ if (attempt < maxRetries) {
107
+ await sleep(backoffMs(attempt));
108
+ continue;
109
+ }
110
+ throw lastError;
111
+ }
112
+ const requestId = res.headers.get("x-request-id") ?? res.headers.get("x-correlation-id") ?? void 0;
113
+ if (res.ok) {
114
+ const parsed = await safeJson(res);
115
+ return parsed;
116
+ }
117
+ const body = await safeJson(res);
118
+ let code = statusToCode(res.status);
119
+ let message = `HTTP ${res.status}`;
120
+ if (body && typeof body === "object" && "error" in body) {
121
+ const errField = body.error;
122
+ if (typeof errField === "string") {
123
+ message = errField;
124
+ } else if (errField && typeof errField === "object") {
125
+ if (typeof errField.code === "string") code = errField.code;
126
+ if (typeof errField.message === "string") message = errField.message;
127
+ }
128
+ }
129
+ const err = new NeetruError(code, message, res.status, requestId);
130
+ lastError = err;
131
+ const isRetryable = RETRYABLE_CODES.has(code);
132
+ if (isRetryable && attempt < maxRetries) {
133
+ const retryAfter = parseRetryAfter(res.headers.get("retry-after"));
134
+ const delay = retryAfter ?? backoffMs(attempt);
135
+ await sleep(delay);
136
+ continue;
137
+ }
138
+ throw err;
139
+ }
140
+ throw lastError ?? new NeetruError("unknown", "unexpected httpRequest exit");
141
+ }
142
+
143
+ // src/notifications.ts
144
+ var VALID_SEVERITIES = [
145
+ "info",
146
+ "success",
147
+ "warning",
148
+ "error"
149
+ ];
150
+ function toNotification(raw) {
151
+ if (!raw || typeof raw !== "object") {
152
+ throw new NeetruError("invalid_response", "Notification response is not an object");
153
+ }
154
+ const r = raw;
155
+ if (typeof r.id !== "string") {
156
+ throw new NeetruError("invalid_response", "Notification missing id");
157
+ }
158
+ const sev = VALID_SEVERITIES.includes(r.severity) ? r.severity : "info";
159
+ return {
160
+ id: r.id,
161
+ userId: typeof r.userId === "string" ? r.userId : "",
162
+ kind: typeof r.kind === "string" ? r.kind : "unknown",
163
+ severity: sev,
164
+ title: typeof r.title === "string" ? r.title : "",
165
+ body: typeof r.body === "string" ? r.body : void 0,
166
+ link: typeof r.link === "string" ? r.link : void 0,
167
+ metadata: r.metadata && typeof r.metadata === "object" ? r.metadata : void 0,
168
+ createdAt: typeof r.createdAt === "string" ? r.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
169
+ readAt: typeof r.readAt === "string" ? r.readAt : void 0,
170
+ dismissedAt: typeof r.dismissedAt === "string" ? r.dismissedAt : void 0
171
+ };
172
+ }
173
+ function validateInput(input) {
174
+ if (!input.userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
175
+ if (!input.kind) throw new NeetruError("validation_failed", "kind obrigat\xF3rio");
176
+ if (!input.title) throw new NeetruError("validation_failed", "title obrigat\xF3rio");
177
+ if (input.severity && !VALID_SEVERITIES.includes(input.severity)) {
178
+ throw new NeetruError(
179
+ "validation_failed",
180
+ `severity inv\xE1lida: ${input.severity} (use ${VALID_SEVERITIES.join("|")})`
181
+ );
182
+ }
183
+ if (input.title.length > 200) {
184
+ throw new NeetruError("validation_failed", "title m\xE1x 200 chars");
185
+ }
186
+ if (input.body && input.body.length > 2e3) {
187
+ throw new NeetruError("validation_failed", "body m\xE1x 2000 chars");
188
+ }
189
+ }
190
+ function createNotificationsNamespace(config) {
191
+ return {
192
+ async send(input) {
193
+ validateInput(input);
194
+ const raw = await httpRequest(config, {
195
+ method: "POST",
196
+ path: "/api/sdk/v1/notifications",
197
+ body: input,
198
+ requireAuth: true
199
+ });
200
+ return toNotification(raw);
201
+ },
202
+ async list(userId, options) {
203
+ if (!userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
204
+ const params = new URLSearchParams();
205
+ if (options?.includeDismissed) params.set("includeDismissed", "true");
206
+ if (options?.onlyUnread) params.set("onlyUnread", "true");
207
+ if (options?.limit) {
208
+ params.set("limit", Math.min(Math.max(1, options.limit), 200).toString());
209
+ }
210
+ const qs = params.toString();
211
+ const raw = await httpRequest(config, {
212
+ method: "GET",
213
+ path: `/api/sdk/v1/notifications/user/${encodeURIComponent(userId)}${qs ? `?${qs}` : ""}`,
214
+ requireAuth: true
215
+ });
216
+ const list = Array.isArray(raw?.notifications) ? raw.notifications : [];
217
+ return list.map(toNotification);
218
+ },
219
+ async markRead(id) {
220
+ if (!id) throw new NeetruError("validation_failed", "id obrigat\xF3rio");
221
+ await httpRequest(config, {
222
+ method: "POST",
223
+ path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/read`,
224
+ requireAuth: true
225
+ });
226
+ return { ok: true };
227
+ },
228
+ async dismiss(id) {
229
+ if (!id) throw new NeetruError("validation_failed", "id obrigat\xF3rio");
230
+ await httpRequest(config, {
231
+ method: "POST",
232
+ path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/dismiss`,
233
+ requireAuth: true
234
+ });
235
+ return { ok: true };
236
+ }
237
+ };
238
+ }
239
+ var MockNotifications = class {
240
+ notifications = /* @__PURE__ */ new Map();
241
+ nextId = 1;
242
+ async send(input) {
243
+ validateInput(input);
244
+ if (input.fingerprint) {
245
+ const dayAgo = Date.now() - 864e5;
246
+ for (const existing of this.notifications.values()) {
247
+ const meta = existing.metadata ?? {};
248
+ if (meta.fingerprint === input.fingerprint && existing.userId === input.userId && new Date(existing.createdAt).getTime() > dayAgo) {
249
+ return existing;
250
+ }
251
+ }
252
+ }
253
+ const id = `mock_notif_${this.nextId++}`;
254
+ const notif = {
255
+ id,
256
+ userId: input.userId,
257
+ kind: input.kind,
258
+ severity: input.severity ?? "info",
259
+ title: input.title,
260
+ body: input.body,
261
+ link: input.link,
262
+ metadata: input.fingerprint ? { ...input.metadata ?? {}, fingerprint: input.fingerprint } : input.metadata,
263
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
264
+ };
265
+ this.notifications.set(id, notif);
266
+ return notif;
267
+ }
268
+ async list(userId, options) {
269
+ if (!userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
270
+ const limit = Math.min(Math.max(1, options?.limit ?? 50), 200);
271
+ return [...this.notifications.values()].filter((n) => n.userId === userId).filter((n) => options?.includeDismissed || !n.dismissedAt).filter((n) => !options?.onlyUnread || !n.readAt).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
272
+ }
273
+ async markRead(id) {
274
+ const n = this.notifications.get(id);
275
+ if (!n) throw new NeetruError("not_found", `Notification ${id} n\xE3o encontrada`);
276
+ if (!n.readAt) {
277
+ n.readAt = (/* @__PURE__ */ new Date()).toISOString();
278
+ this.notifications.set(id, n);
279
+ }
280
+ return { ok: true };
281
+ }
282
+ async dismiss(id) {
283
+ const n = this.notifications.get(id);
284
+ if (!n) throw new NeetruError("not_found", `Notification ${id} n\xE3o encontrada`);
285
+ if (!n.dismissedAt) {
286
+ n.dismissedAt = (/* @__PURE__ */ new Date()).toISOString();
287
+ this.notifications.set(id, n);
288
+ }
289
+ return { ok: true };
290
+ }
291
+ };
292
+
293
+ exports.MockNotifications = MockNotifications;
294
+ exports.createNotificationsNamespace = createNotificationsNamespace;
295
+ //# sourceMappingURL=notifications.cjs.map
296
+ //# sourceMappingURL=notifications.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/http.ts","../src/notifications.ts"],"names":["err","message"],"mappings":";;;AAgCO,IAAM,WAAA,GAAN,MAAM,YAAA,SAAoB,KAAA,CAAM;AAAA,EACrB,IAAA;AAAA,EACA,MAAA;AAAA,EACA,SAAA;AAAA,EAEhB,WAAA,CACE,IAAA,EACA,OAAA,EACA,MAAA,EACA,SAAA,EACA;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,aAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AAEjB,IAAA,MAAA,CAAO,cAAA,CAAe,IAAA,EAAM,YAAA,CAAY,SAAS,CAAA;AAAA,EACnD;AACF,CAAA;;;ACXA,IAAM,eAAA,GAAkB,CAAA;AACxB,IAAM,eAAA,uBAAsB,GAAA,CAAqB;AAAA,EAC/C,cAAA;AAAA,EACA,cAAA;AAAA,EACA;AACF,CAAC,CAAA;AAGD,SAAS,UAAU,OAAA,EAAyB;AAC1C,EAAA,MAAM,IAAA,GAAO,GAAA,GAAM,IAAA,CAAK,GAAA,CAAI,GAAG,O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AAA,kBAAA,CAAiB,CAAA;AAC9E,IAAA,IAAI,CAAC,EAAE,MAAA,EAAQ;AACb,MAAA,CAAA,CAAE,MAAA,GAAA,iBAAS,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AAClC,MAAA,IAAA,CAAK,aAAA,CAAc,GAAA,CAAI,EAAA,EAAI,CAAC,CAAA;AAAA,IAC9B;AACA,IAAA,OAAO,EAAE,IAAI,IAAA,EAAK;AAAA,EACpB;AAAA,EAEA,MAAM,QAAQ,EAAA,EAAmC;AAC/C,IAAA,MAAM,CAAA,GAAI,IAAA,CAAK,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA;AACnC,IAAA,IAAI,CAAC,GAAG,MAAM,IAAI,YAAY,WAAA,EAAa,CAAA,aAAA,EAAgB,EAAE,CAAA,kBAAA,CAAiB,CAAA;AAC9E,IAAA,IAAI,CAAC,EAAE,WAAA,EAAa;AAClB,MAAA,CAAA,CAAE,WAAA,GAAA,iBAAc,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AACvC,MAAA,IAAA,CAAK,aAAA,CAAc,GAAA,CAAI,EAAA,EAAI,CAAC,CAAA;AAAA,IAC9B;AACA,IAAA,OAAO,EAAE,IAAI,IAAA,EAAK;AAAA,EACpB;AACF","file":"notifications.cjs","sourcesContent":["/**\r\n * Erros tipados do SDK.\r\n *\r\n * Todo erro lançado pelo SDK estende `NeetruError` — caller pode discriminar\r\n * por `.code` (string estável) sem parsing de message.\r\n */\r\n\r\n/** Códigos de erro estáveis do SDK. Adicionar aqui requer minor bump. */\r\nexport type NeetruErrorCode =\r\n | 'invalid_config'\r\n | 'missing_api_key'\r\n | 'unauthorized'\r\n | 'forbidden'\r\n | 'not_found'\r\n | 'rate_limited'\r\n | 'validation_failed'\r\n | 'network_error'\r\n | 'invalid_response'\r\n | 'server_error'\r\n | 'unknown';\r\n\r\n/**\r\n * Erro tipado padrão do SDK. Sempre lançado em vez de Error genérico.\r\n *\r\n * @example\r\n * ```ts\r\n * try { await client.catalog.list(); }\r\n * catch (e) {\r\n * if (e instanceof NeetruError && e.code === 'rate_limited') retry();\r\n * }\r\n * ```\r\n */\r\nexport class NeetruError extends Error {\r\n public readonly code: NeetruErrorCode | string;\r\n public readonly status?: number;\r\n public readonly requestId?: string;\r\n\r\n constructor(\r\n code: NeetruErrorCode | string,\r\n message: string,\r\n status?: number,\r\n requestId?: string,\r\n ) {\r\n super(message);\r\n this.name = 'NeetruError';\r\n this.code = code;\r\n this.status = status;\r\n this.requestId = requestId;\r\n // Preserva o prototype chain ao herdar de Error (downlevel quirk de TS).\r\n Object.setPrototypeOf(this, NeetruError.prototype);\r\n }\r\n}\r\n","/**\r\n * HTTP transport interno do SDK.\r\n *\r\n * Responsabilidades:\r\n * - Construir URL absoluta a partir de `baseUrl` + path\r\n * - Injetar Bearer token quando `requireAuth=true`\r\n * - Mapear status HTTP → `NeetruError` com `code` estável\r\n * - Parse defensivo de JSON (não lança em body vazio em 204)\r\n * - Retry/backoff exponencial em `rate_limited` (429), `server_error` (5xx)\r\n * e `network_error` (timeout/falha de rede). Honra `Retry-After` quando\r\n * presente. Default 2 retries (3 tentativas no total). Caller opta-out\r\n * com `retries: 0`.\r\n */\r\nimport { NeetruError, type NeetruErrorCode } from './errors';\r\nimport type { ResolvedConfig } from './types';\r\n\r\n/** Opções da request HTTP. */\r\nexport interface HttpRequestOptions {\r\n method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';\r\n /** Path relativo (ex: `/api/v1/cli/catalog`). Concatenado a `baseUrl`. */\r\n path: string;\r\n /** Query string params (chave → valor primitivo). Valores `undefined` ignorados. */\r\n query?: Record<string, string | number | boolean | undefined>;\r\n /** Body JSON-serializável. Ignorado em GET/DELETE. */\r\n body?: unknown;\r\n /**\r\n * Se true, injeta `Authorization: Bearer <apiKey>`. Lança `missing_api_key`\r\n * se config.apiKey ausente. Default false.\r\n */\r\n requireAuth?: boolean;\r\n /** Cabeçalhos extras. */\r\n headers?: Record<string, string>;\r\n /**\r\n * Número de retries em códigos transientes (`rate_limited`, `server_error`,\r\n * `network_error`). Default 2 (= 3 tentativas total). Caller passa `0` pra\r\n * desativar (útil em operações não-idempotentes específicas).\r\n */\r\n retries?: number;\r\n}\r\n\r\nconst DEFAULT_RETRIES = 2;\r\nconst RETRYABLE_CODES = new Set<NeetruErrorCode>([\r\n 'rate_limited',\r\n 'server_error',\r\n 'network_error',\r\n]);\r\n\r\n/** Backoff exponencial com jitter ±20%. attempt: 0=primeira, 1=primeiro retry, ... */\r\nfunction backoffMs(attempt: number): number {\r\n const base = 200 * Math.pow(4, attempt); // 200ms, 800ms, 3.2s, ...\r\n const jitter = base * 0.2 * (Math.random() * 2 - 1);\r\n return Math.max(50, Math.round(base + jitter));\r\n}\r\n\r\n/**\r\n * Honra `Retry-After` header (segundos ou HTTP-date). Retorna ms ou null se\r\n * inválido. RFC 9110 §10.2.3.\r\n */\r\nfunction parseRetryAfter(value: string | null): number | null {\r\n if (!value) return null;\r\n const secs = Number(value);\r\n if (Number.isFinite(secs) && secs >= 0) return Math.round(secs * 1000);\r\n const dateMs = Date.parse(value);\r\n if (Number.isFinite(dateMs)) {\r\n const delta = dateMs - Date.now();\r\n if (delta > 0) return delta;\r\n }\r\n return null;\r\n}\r\n\r\nfunction sleep(ms: number): Promise<void> {\r\n return new Promise((resolve) => setTimeout(resolve, ms));\r\n}\r\n\r\n/** Mapeamento status → code estável do NeetruError. */\r\nfunction statusToCode(status: number): NeetruErrorCode {\r\n if (status === 401) return 'unauthorized';\r\n if (status === 403) return 'forbidden';\r\n if (status === 404) return 'not_found';\r\n if (status === 422 || status === 400) return 'validation_failed';\r\n if (status === 429) return 'rate_limited';\r\n if (status >= 500) return 'server_error';\r\n return 'unknown';\r\n}\r\n\r\nfunction buildUrl(baseUrl: string, path: string, query?: HttpRequestOptions['query']): string {\r\n // Trim trailing slash em base e leading em path pra evitar `//`.\r\n const base = baseUrl.replace(/\\/+$/, '');\r\n const p = path.startsWith('/') ? path : `/${path}`;\r\n const url = new URL(`${base}${p}`);\r\n if (query) {\r\n for (const [k, v] of Object.entries(query)) {\r\n if (v === undefined) continue;\r\n url.searchParams.set(k, String(v));\r\n }\r\n }\r\n return url.toString();\r\n}\r\n\r\n/** Parse JSON defensivo — retorna `undefined` em body vazio. */\r\nasync function safeJson(res: Response): Promise<unknown> {\r\n const text = await res.text();\r\n if (!text) return undefined;\r\n try {\r\n return JSON.parse(text);\r\n } catch {\r\n return undefined;\r\n }\r\n}\r\n\r\n/**\r\n * Executa request HTTP. Em sucesso retorna body parseado; em erro lança\r\n * `NeetruError` com `code` derivado do status. Aplica retry/backoff\r\n * automático em códigos transientes (rate_limited/server_error/network_error)\r\n * conforme `opts.retries` (default 2 = 3 tentativas).\r\n */\r\nexport async function httpRequest<T>(\r\n config: ResolvedConfig,\r\n opts: HttpRequestOptions,\r\n): Promise<T> {\r\n const method = opts.method ?? 'GET';\r\n const url = buildUrl(config.baseUrl, opts.path, opts.query);\r\n const maxRetries = opts.retries ?? DEFAULT_RETRIES;\r\n\r\n const headers: Record<string, string> = {\r\n accept: 'application/json',\r\n ...opts.headers,\r\n };\r\n\r\n if (opts.requireAuth) {\r\n if (!config.apiKey) {\r\n throw new NeetruError(\r\n 'missing_api_key',\r\n 'This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.',\r\n );\r\n }\r\n headers.authorization = `Bearer ${config.apiKey}`;\r\n }\r\n\r\n // Body só é serializado uma vez — reusado entre tentativas.\r\n const bodyString =\r\n opts.body !== undefined && method !== 'GET' && method !== 'DELETE'\r\n ? JSON.stringify(opts.body)\r\n : undefined;\r\n if (bodyString !== undefined) {\r\n headers['content-type'] = 'application/json';\r\n }\r\n\r\n let lastError: NeetruError | null = null;\r\n\r\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\r\n const init: RequestInit = { method, headers };\r\n if (bodyString !== undefined) init.body = bodyString;\r\n // BUG-020 fix (2026-05-13): timeout default 30s. AbortSignal.timeout\r\n // requer Node 18+ ou browsers recentes.\r\n init.signal = AbortSignal.timeout(30_000);\r\n\r\n let res: Response;\r\n try {\r\n res = await config.fetch(url, init);\r\n } catch (err) {\r\n const message =\r\n err instanceof DOMException && err.name === 'TimeoutError'\r\n ? 'Network error: timeout after 30s'\r\n : `Network error: ${err instanceof Error ? err.message : 'fetch failed'}`;\r\n lastError = new NeetruError('network_error', message);\r\n if (attempt < maxRetries) {\r\n await sleep(backoffMs(attempt));\r\n continue;\r\n }\r\n throw lastError;\r\n }\r\n\r\n const requestId =\r\n res.headers.get('x-request-id') ?? res.headers.get('x-correlation-id') ?? undefined;\r\n\r\n if (res.ok) {\r\n const parsed = await safeJson(res);\r\n return parsed as T;\r\n }\r\n\r\n const body = (await safeJson(res)) as\r\n | { error?: { code?: string; message?: string } | string }\r\n | undefined;\r\n let code: string = statusToCode(res.status);\r\n let message = `HTTP ${res.status}`;\r\n if (body && typeof body === 'object' && 'error' in body) {\r\n const errField = body.error;\r\n if (typeof errField === 'string') {\r\n message = errField;\r\n } else if (errField && typeof errField === 'object') {\r\n if (typeof errField.code === 'string') code = errField.code;\r\n if (typeof errField.message === 'string') message = errField.message;\r\n }\r\n }\r\n const err = new NeetruError(code, message, res.status, requestId);\r\n lastError = err;\r\n\r\n const isRetryable = RETRYABLE_CODES.has(code as NeetruErrorCode);\r\n if (isRetryable && attempt < maxRetries) {\r\n const retryAfter = parseRetryAfter(res.headers.get('retry-after'));\r\n const delay = retryAfter ?? backoffMs(attempt);\r\n await sleep(delay);\r\n continue;\r\n }\r\n throw err;\r\n }\r\n\r\n // Unreachable — loop sempre retorna ou lança. Safety net.\r\n throw lastError ?? new NeetruError('unknown', 'unexpected httpRequest exit');\r\n}\r\n","/**\r\n * Notifications namespace (v1.2) — produtos SaaS enviam notificações in-app\r\n * pros SEUS usuários (distinto das `notifications/` staff-only do Core).\r\n *\r\n * Caso típico: produto gestovendas notifica usuário \"Pedido #X recebido\" no\r\n * portal sem precisar manter a própria coleção Firestore.\r\n *\r\n * Schema isolado por produto: `product_user_notifications/{productId}/{notifId}`.\r\n * Owner Neetru não vê o conteúdo (privacidade do produto), mas o Core\r\n * armazena + serve a leitura. Produto pode também listar via SDK.\r\n *\r\n * Endpoints REST (em prod):\r\n * - `POST /api/sdk/v1/notifications` — produto envia\r\n * - `GET /api/sdk/v1/notifications/user/{userId}` — produto lista do user\r\n * - `POST /api/sdk/v1/notifications/{id}/read` — user marca lida (proxy)\r\n * - `POST /api/sdk/v1/notifications/{id}/dismiss` — user descarta (proxy)\r\n *\r\n * Severidade canônica: `info | success | warning | error`.\r\n *\r\n * Mock em `NEETRU_ENV=dev`: in-memory.\r\n */\r\nimport { NeetruError } from './errors';\r\nimport { httpRequest } from './http';\r\nimport type { ResolvedConfig } from './types';\r\n\r\nexport type ProductNotificationSeverity =\r\n | 'info'\r\n | 'success'\r\n | 'warning'\r\n | 'error';\r\n\r\nexport interface ProductNotification {\r\n id: string;\r\n /** UID do usuário final (do produto, não staff Neetru). */\r\n userId: string;\r\n /** Identificador semântico do evento (ex: `order.received`). */\r\n kind: string;\r\n severity: ProductNotificationSeverity;\r\n title: string;\r\n body?: string;\r\n /** Link opcional pra ação. */\r\n link?: string;\r\n /** Metadata livre (serializável). */\r\n metadata?: Record<string, string | number | boolean | null>;\r\n createdAt: string;\r\n /** ISO quando user marcou lida. */\r\n readAt?: string;\r\n /** ISO quando user descartou. */\r\n dismissedAt?: string;\r\n}\r\n\r\nexport interface SendNotificationInput {\r\n userId: string;\r\n kind: string;\r\n title: string;\r\n severity?: ProductNotificationSeverity;\r\n body?: string;\r\n link?: string;\r\n metadata?: Record<string, string | number | boolean | null>;\r\n /**\r\n * Dedup — se já existir notificação com mesmo fingerprint dentro de 24h,\r\n * NÃO cria duplicata. Útil pra evitar spam quando webhook reentrega.\r\n */\r\n fingerprint?: string;\r\n}\r\n\r\nexport interface ListNotificationsOptions {\r\n /** Inclui notificações dismissed (default false). */\r\n includeDismissed?: boolean;\r\n /** Filtro por unread. */\r\n onlyUnread?: boolean;\r\n /** Máx itens (default 50, máx 200). */\r\n limit?: number;\r\n}\r\n\r\nexport interface NotificationsNamespace {\r\n /** Produto envia uma notificação pra um usuário do produto. */\r\n send(input: SendNotificationInput): Promise<ProductNotification>;\r\n /** Lista notificações de um usuário. */\r\n list(\r\n userId: string,\r\n options?: ListNotificationsOptions,\r\n ): Promise<ProductNotification[]>;\r\n /** Marca como lida. */\r\n markRead(id: string): Promise<{ ok: true }>;\r\n /** Descarta. */\r\n dismiss(id: string): Promise<{ ok: true }>;\r\n}\r\n\r\n// ─── HTTP impl ──────────────────────────────────────────────────────────────\r\n\r\nconst VALID_SEVERITIES: readonly ProductNotificationSeverity[] = [\r\n 'info',\r\n 'success',\r\n 'warning',\r\n 'error',\r\n];\r\n\r\nfunction toNotification(raw: unknown): ProductNotification {\r\n if (!raw || typeof raw !== 'object') {\r\n throw new NeetruError('invalid_response', 'Notification response is not an object');\r\n }\r\n const r = raw as Record<string, unknown>;\r\n if (typeof r.id !== 'string') {\r\n throw new NeetruError('invalid_response', 'Notification missing id');\r\n }\r\n const sev = VALID_SEVERITIES.includes(r.severity as ProductNotificationSeverity)\r\n ? (r.severity as ProductNotificationSeverity)\r\n : 'info';\r\n return {\r\n id: r.id,\r\n userId: typeof r.userId === 'string' ? r.userId : '',\r\n kind: typeof r.kind === 'string' ? r.kind : 'unknown',\r\n severity: sev,\r\n title: typeof r.title === 'string' ? r.title : '',\r\n body: typeof r.body === 'string' ? r.body : undefined,\r\n link: typeof r.link === 'string' ? r.link : undefined,\r\n metadata:\r\n r.metadata && typeof r.metadata === 'object'\r\n ? (r.metadata as Record<string, string | number | boolean | null>)\r\n : undefined,\r\n createdAt: typeof r.createdAt === 'string' ? r.createdAt : new Date().toISOString(),\r\n readAt: typeof r.readAt === 'string' ? r.readAt : undefined,\r\n dismissedAt: typeof r.dismissedAt === 'string' ? r.dismissedAt : undefined,\r\n };\r\n}\r\n\r\nfunction validateInput(input: SendNotificationInput): void {\r\n if (!input.userId) throw new NeetruError('validation_failed', 'userId obrigatório');\r\n if (!input.kind) throw new NeetruError('validation_failed', 'kind obrigatório');\r\n if (!input.title) throw new NeetruError('validation_failed', 'title obrigatório');\r\n if (input.severity && !VALID_SEVERITIES.includes(input.severity)) {\r\n throw new NeetruError(\r\n 'validation_failed',\r\n `severity inválida: ${input.severity} (use ${VALID_SEVERITIES.join('|')})`,\r\n );\r\n }\r\n if (input.title.length > 200) {\r\n throw new NeetruError('validation_failed', 'title máx 200 chars');\r\n }\r\n if (input.body && input.body.length > 2000) {\r\n throw new NeetruError('validation_failed', 'body máx 2000 chars');\r\n }\r\n}\r\n\r\nexport function createNotificationsNamespace(\r\n config: ResolvedConfig,\r\n): NotificationsNamespace {\r\n return {\r\n async send(input) {\r\n validateInput(input);\r\n const raw = await httpRequest<unknown>(config, {\r\n method: 'POST',\r\n path: '/api/sdk/v1/notifications',\r\n body: input,\r\n requireAuth: true,\r\n });\r\n return toNotification(raw);\r\n },\r\n async list(userId, options) {\r\n if (!userId) throw new NeetruError('validation_failed', 'userId obrigatório');\r\n const params = new URLSearchParams();\r\n if (options?.includeDismissed) params.set('includeDismissed', 'true');\r\n if (options?.onlyUnread) params.set('onlyUnread', 'true');\r\n if (options?.limit) {\r\n params.set('limit', Math.min(Math.max(1, options.limit), 200).toString());\r\n }\r\n const qs = params.toString();\r\n const raw = await httpRequest<{ notifications?: unknown[] }>(config, {\r\n method: 'GET',\r\n path: `/api/sdk/v1/notifications/user/${encodeURIComponent(userId)}${qs ? `?${qs}` : ''}`,\r\n requireAuth: true,\r\n });\r\n const list = Array.isArray(raw?.notifications) ? raw.notifications : [];\r\n return list.map(toNotification);\r\n },\r\n async markRead(id) {\r\n if (!id) throw new NeetruError('validation_failed', 'id obrigatório');\r\n await httpRequest<unknown>(config, {\r\n method: 'POST',\r\n path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/read`,\r\n requireAuth: true,\r\n });\r\n return { ok: true };\r\n },\r\n async dismiss(id) {\r\n if (!id) throw new NeetruError('validation_failed', 'id obrigatório');\r\n await httpRequest<unknown>(config, {\r\n method: 'POST',\r\n path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/dismiss`,\r\n requireAuth: true,\r\n });\r\n return { ok: true };\r\n },\r\n };\r\n}\r\n\r\n// ─── Mock impl ──────────────────────────────────────────────────────────────\r\n\r\nexport class MockNotifications implements NotificationsNamespace {\r\n private notifications = new Map<string, ProductNotification>();\r\n private nextId = 1;\r\n\r\n async send(input: SendNotificationInput): Promise<ProductNotification> {\r\n validateInput(input);\r\n // Dedup por fingerprint: se já existe um com mesmo fingerprint < 24h, retorna ele.\r\n if (input.fingerprint) {\r\n const dayAgo = Date.now() - 86_400_000;\r\n for (const existing of this.notifications.values()) {\r\n const meta = existing.metadata ?? {};\r\n if (\r\n meta.fingerprint === input.fingerprint &&\r\n existing.userId === input.userId &&\r\n new Date(existing.createdAt).getTime() > dayAgo\r\n ) {\r\n return existing;\r\n }\r\n }\r\n }\r\n const id = `mock_notif_${this.nextId++}`;\r\n const notif: ProductNotification = {\r\n id,\r\n userId: input.userId,\r\n kind: input.kind,\r\n severity: input.severity ?? 'info',\r\n title: input.title,\r\n body: input.body,\r\n link: input.link,\r\n metadata: input.fingerprint\r\n ? { ...(input.metadata ?? {}), fingerprint: input.fingerprint }\r\n : input.metadata,\r\n createdAt: new Date().toISOString(),\r\n };\r\n this.notifications.set(id, notif);\r\n return notif;\r\n }\r\n\r\n async list(\r\n userId: string,\r\n options?: ListNotificationsOptions,\r\n ): Promise<ProductNotification[]> {\r\n if (!userId) throw new NeetruError('validation_failed', 'userId obrigatório');\r\n const limit = Math.min(Math.max(1, options?.limit ?? 50), 200);\r\n return [...this.notifications.values()]\r\n .filter((n) => n.userId === userId)\r\n .filter((n) => options?.includeDismissed || !n.dismissedAt)\r\n .filter((n) => !options?.onlyUnread || !n.readAt)\r\n .sort((a, b) => b.createdAt.localeCompare(a.createdAt))\r\n .slice(0, limit);\r\n }\r\n\r\n async markRead(id: string): Promise<{ ok: true }> {\r\n const n = this.notifications.get(id);\r\n if (!n) throw new NeetruError('not_found', `Notification ${id} não encontrada`);\r\n if (!n.readAt) {\r\n n.readAt = new Date().toISOString();\r\n this.notifications.set(id, n);\r\n }\r\n return { ok: true };\r\n }\r\n\r\n async dismiss(id: string): Promise<{ ok: true }> {\r\n const n = this.notifications.get(id);\r\n if (!n) throw new NeetruError('not_found', `Notification ${id} não encontrada`);\r\n if (!n.dismissedAt) {\r\n n.dismissedAt = new Date().toISOString();\r\n this.notifications.set(id, n);\r\n }\r\n return { ok: true };\r\n }\r\n}\r\n"]}
@@ -0,0 +1,5 @@
1
+ export { L as ListNotificationsOptions, m as MockNotifications, x as NotificationsNamespace, y as ProductNotification, z as ProductNotificationSeverity, S as SendNotificationInput, a4 as createNotificationsNamespace } from './types-Kmt4y1FQ.cjs';
2
+ import './collection-ref-BBvTTXoG.cjs';
3
+ import '@neetru/realtime-protocol';
4
+ import 'drizzle-orm/node-postgres';
5
+ import './errors.cjs';
@@ -0,0 +1,5 @@
1
+ export { L as ListNotificationsOptions, m as MockNotifications, x as NotificationsNamespace, y as ProductNotification, z as ProductNotificationSeverity, S as SendNotificationInput, a4 as createNotificationsNamespace } from './types-B1jylbMC.js';
2
+ import './collection-ref-BBvTTXoG.js';
3
+ import '@neetru/realtime-protocol';
4
+ import 'drizzle-orm/node-postgres';
5
+ import './errors.js';
@@ -0,0 +1,293 @@
1
+ // src/errors.ts
2
+ var NeetruError = class _NeetruError extends Error {
3
+ code;
4
+ status;
5
+ requestId;
6
+ constructor(code, message, status, requestId) {
7
+ super(message);
8
+ this.name = "NeetruError";
9
+ this.code = code;
10
+ this.status = status;
11
+ this.requestId = requestId;
12
+ Object.setPrototypeOf(this, _NeetruError.prototype);
13
+ }
14
+ };
15
+
16
+ // src/http.ts
17
+ var DEFAULT_RETRIES = 2;
18
+ var RETRYABLE_CODES = /* @__PURE__ */ new Set([
19
+ "rate_limited",
20
+ "server_error",
21
+ "network_error"
22
+ ]);
23
+ function backoffMs(attempt) {
24
+ const base = 200 * Math.pow(4, attempt);
25
+ const jitter = base * 0.2 * (Math.random() * 2 - 1);
26
+ return Math.max(50, Math.round(base + jitter));
27
+ }
28
+ function parseRetryAfter(value) {
29
+ if (!value) return null;
30
+ const secs = Number(value);
31
+ if (Number.isFinite(secs) && secs >= 0) return Math.round(secs * 1e3);
32
+ const dateMs = Date.parse(value);
33
+ if (Number.isFinite(dateMs)) {
34
+ const delta = dateMs - Date.now();
35
+ if (delta > 0) return delta;
36
+ }
37
+ return null;
38
+ }
39
+ function sleep(ms) {
40
+ return new Promise((resolve) => setTimeout(resolve, ms));
41
+ }
42
+ function statusToCode(status) {
43
+ if (status === 401) return "unauthorized";
44
+ if (status === 403) return "forbidden";
45
+ if (status === 404) return "not_found";
46
+ if (status === 422 || status === 400) return "validation_failed";
47
+ if (status === 429) return "rate_limited";
48
+ if (status >= 500) return "server_error";
49
+ return "unknown";
50
+ }
51
+ function buildUrl(baseUrl, path, query) {
52
+ const base = baseUrl.replace(/\/+$/, "");
53
+ const p = path.startsWith("/") ? path : `/${path}`;
54
+ const url = new URL(`${base}${p}`);
55
+ if (query) {
56
+ for (const [k, v] of Object.entries(query)) {
57
+ if (v === void 0) continue;
58
+ url.searchParams.set(k, String(v));
59
+ }
60
+ }
61
+ return url.toString();
62
+ }
63
+ async function safeJson(res) {
64
+ const text = await res.text();
65
+ if (!text) return void 0;
66
+ try {
67
+ return JSON.parse(text);
68
+ } catch {
69
+ return void 0;
70
+ }
71
+ }
72
+ async function httpRequest(config, opts) {
73
+ const method = opts.method ?? "GET";
74
+ const url = buildUrl(config.baseUrl, opts.path, opts.query);
75
+ const maxRetries = opts.retries ?? DEFAULT_RETRIES;
76
+ const headers = {
77
+ accept: "application/json",
78
+ ...opts.headers
79
+ };
80
+ if (opts.requireAuth) {
81
+ if (!config.apiKey) {
82
+ throw new NeetruError(
83
+ "missing_api_key",
84
+ "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
85
+ );
86
+ }
87
+ headers.authorization = `Bearer ${config.apiKey}`;
88
+ }
89
+ const bodyString = opts.body !== void 0 && method !== "GET" && method !== "DELETE" ? JSON.stringify(opts.body) : void 0;
90
+ if (bodyString !== void 0) {
91
+ headers["content-type"] = "application/json";
92
+ }
93
+ let lastError = null;
94
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
95
+ const init = { method, headers };
96
+ if (bodyString !== void 0) init.body = bodyString;
97
+ init.signal = AbortSignal.timeout(3e4);
98
+ let res;
99
+ try {
100
+ res = await config.fetch(url, init);
101
+ } catch (err2) {
102
+ const message2 = err2 instanceof DOMException && err2.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${err2 instanceof Error ? err2.message : "fetch failed"}`;
103
+ lastError = new NeetruError("network_error", message2);
104
+ if (attempt < maxRetries) {
105
+ await sleep(backoffMs(attempt));
106
+ continue;
107
+ }
108
+ throw lastError;
109
+ }
110
+ const requestId = res.headers.get("x-request-id") ?? res.headers.get("x-correlation-id") ?? void 0;
111
+ if (res.ok) {
112
+ const parsed = await safeJson(res);
113
+ return parsed;
114
+ }
115
+ const body = await safeJson(res);
116
+ let code = statusToCode(res.status);
117
+ let message = `HTTP ${res.status}`;
118
+ if (body && typeof body === "object" && "error" in body) {
119
+ const errField = body.error;
120
+ if (typeof errField === "string") {
121
+ message = errField;
122
+ } else if (errField && typeof errField === "object") {
123
+ if (typeof errField.code === "string") code = errField.code;
124
+ if (typeof errField.message === "string") message = errField.message;
125
+ }
126
+ }
127
+ const err = new NeetruError(code, message, res.status, requestId);
128
+ lastError = err;
129
+ const isRetryable = RETRYABLE_CODES.has(code);
130
+ if (isRetryable && attempt < maxRetries) {
131
+ const retryAfter = parseRetryAfter(res.headers.get("retry-after"));
132
+ const delay = retryAfter ?? backoffMs(attempt);
133
+ await sleep(delay);
134
+ continue;
135
+ }
136
+ throw err;
137
+ }
138
+ throw lastError ?? new NeetruError("unknown", "unexpected httpRequest exit");
139
+ }
140
+
141
+ // src/notifications.ts
142
+ var VALID_SEVERITIES = [
143
+ "info",
144
+ "success",
145
+ "warning",
146
+ "error"
147
+ ];
148
+ function toNotification(raw) {
149
+ if (!raw || typeof raw !== "object") {
150
+ throw new NeetruError("invalid_response", "Notification response is not an object");
151
+ }
152
+ const r = raw;
153
+ if (typeof r.id !== "string") {
154
+ throw new NeetruError("invalid_response", "Notification missing id");
155
+ }
156
+ const sev = VALID_SEVERITIES.includes(r.severity) ? r.severity : "info";
157
+ return {
158
+ id: r.id,
159
+ userId: typeof r.userId === "string" ? r.userId : "",
160
+ kind: typeof r.kind === "string" ? r.kind : "unknown",
161
+ severity: sev,
162
+ title: typeof r.title === "string" ? r.title : "",
163
+ body: typeof r.body === "string" ? r.body : void 0,
164
+ link: typeof r.link === "string" ? r.link : void 0,
165
+ metadata: r.metadata && typeof r.metadata === "object" ? r.metadata : void 0,
166
+ createdAt: typeof r.createdAt === "string" ? r.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
167
+ readAt: typeof r.readAt === "string" ? r.readAt : void 0,
168
+ dismissedAt: typeof r.dismissedAt === "string" ? r.dismissedAt : void 0
169
+ };
170
+ }
171
+ function validateInput(input) {
172
+ if (!input.userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
173
+ if (!input.kind) throw new NeetruError("validation_failed", "kind obrigat\xF3rio");
174
+ if (!input.title) throw new NeetruError("validation_failed", "title obrigat\xF3rio");
175
+ if (input.severity && !VALID_SEVERITIES.includes(input.severity)) {
176
+ throw new NeetruError(
177
+ "validation_failed",
178
+ `severity inv\xE1lida: ${input.severity} (use ${VALID_SEVERITIES.join("|")})`
179
+ );
180
+ }
181
+ if (input.title.length > 200) {
182
+ throw new NeetruError("validation_failed", "title m\xE1x 200 chars");
183
+ }
184
+ if (input.body && input.body.length > 2e3) {
185
+ throw new NeetruError("validation_failed", "body m\xE1x 2000 chars");
186
+ }
187
+ }
188
+ function createNotificationsNamespace(config) {
189
+ return {
190
+ async send(input) {
191
+ validateInput(input);
192
+ const raw = await httpRequest(config, {
193
+ method: "POST",
194
+ path: "/api/sdk/v1/notifications",
195
+ body: input,
196
+ requireAuth: true
197
+ });
198
+ return toNotification(raw);
199
+ },
200
+ async list(userId, options) {
201
+ if (!userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
202
+ const params = new URLSearchParams();
203
+ if (options?.includeDismissed) params.set("includeDismissed", "true");
204
+ if (options?.onlyUnread) params.set("onlyUnread", "true");
205
+ if (options?.limit) {
206
+ params.set("limit", Math.min(Math.max(1, options.limit), 200).toString());
207
+ }
208
+ const qs = params.toString();
209
+ const raw = await httpRequest(config, {
210
+ method: "GET",
211
+ path: `/api/sdk/v1/notifications/user/${encodeURIComponent(userId)}${qs ? `?${qs}` : ""}`,
212
+ requireAuth: true
213
+ });
214
+ const list = Array.isArray(raw?.notifications) ? raw.notifications : [];
215
+ return list.map(toNotification);
216
+ },
217
+ async markRead(id) {
218
+ if (!id) throw new NeetruError("validation_failed", "id obrigat\xF3rio");
219
+ await httpRequest(config, {
220
+ method: "POST",
221
+ path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/read`,
222
+ requireAuth: true
223
+ });
224
+ return { ok: true };
225
+ },
226
+ async dismiss(id) {
227
+ if (!id) throw new NeetruError("validation_failed", "id obrigat\xF3rio");
228
+ await httpRequest(config, {
229
+ method: "POST",
230
+ path: `/api/sdk/v1/notifications/${encodeURIComponent(id)}/dismiss`,
231
+ requireAuth: true
232
+ });
233
+ return { ok: true };
234
+ }
235
+ };
236
+ }
237
+ var MockNotifications = class {
238
+ notifications = /* @__PURE__ */ new Map();
239
+ nextId = 1;
240
+ async send(input) {
241
+ validateInput(input);
242
+ if (input.fingerprint) {
243
+ const dayAgo = Date.now() - 864e5;
244
+ for (const existing of this.notifications.values()) {
245
+ const meta = existing.metadata ?? {};
246
+ if (meta.fingerprint === input.fingerprint && existing.userId === input.userId && new Date(existing.createdAt).getTime() > dayAgo) {
247
+ return existing;
248
+ }
249
+ }
250
+ }
251
+ const id = `mock_notif_${this.nextId++}`;
252
+ const notif = {
253
+ id,
254
+ userId: input.userId,
255
+ kind: input.kind,
256
+ severity: input.severity ?? "info",
257
+ title: input.title,
258
+ body: input.body,
259
+ link: input.link,
260
+ metadata: input.fingerprint ? { ...input.metadata ?? {}, fingerprint: input.fingerprint } : input.metadata,
261
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
262
+ };
263
+ this.notifications.set(id, notif);
264
+ return notif;
265
+ }
266
+ async list(userId, options) {
267
+ if (!userId) throw new NeetruError("validation_failed", "userId obrigat\xF3rio");
268
+ const limit = Math.min(Math.max(1, options?.limit ?? 50), 200);
269
+ return [...this.notifications.values()].filter((n) => n.userId === userId).filter((n) => options?.includeDismissed || !n.dismissedAt).filter((n) => !options?.onlyUnread || !n.readAt).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
270
+ }
271
+ async markRead(id) {
272
+ const n = this.notifications.get(id);
273
+ if (!n) throw new NeetruError("not_found", `Notification ${id} n\xE3o encontrada`);
274
+ if (!n.readAt) {
275
+ n.readAt = (/* @__PURE__ */ new Date()).toISOString();
276
+ this.notifications.set(id, n);
277
+ }
278
+ return { ok: true };
279
+ }
280
+ async dismiss(id) {
281
+ const n = this.notifications.get(id);
282
+ if (!n) throw new NeetruError("not_found", `Notification ${id} n\xE3o encontrada`);
283
+ if (!n.dismissedAt) {
284
+ n.dismissedAt = (/* @__PURE__ */ new Date()).toISOString();
285
+ this.notifications.set(id, n);
286
+ }
287
+ return { ok: true };
288
+ }
289
+ };
290
+
291
+ export { MockNotifications, createNotificationsNamespace };
292
+ //# sourceMappingURL=notifications.mjs.map
293
+ //# sourceMappingURL=notifications.mjs.map