@donotdev/functions 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/chunks/chunk-6NXIK35R.js +23 -0
- package/lib/chunks/chunk-6NXIK35R.js.map +7 -0
- package/lib/chunks/chunk-EZKRLLSQ.js +22 -0
- package/lib/chunks/{chunk-OZX5ZG44.js.map → chunk-EZKRLLSQ.js.map} +1 -1
- package/lib/chunks/chunk-F3PHE627.js +22 -0
- package/lib/chunks/chunk-F3PHE627.js.map +7 -0
- package/lib/chunks/chunk-F3WBFWPO.js +151 -0
- package/lib/chunks/chunk-F3WBFWPO.js.map +7 -0
- package/lib/chunks/chunk-G5DRV3DH.js +22 -0
- package/lib/chunks/chunk-G5DRV3DH.js.map +7 -0
- package/lib/chunks/{chunk-2A4FPYNB.js → chunk-GDIUOJMD.js} +2 -2
- package/lib/chunks/chunk-GDIUOJMD.js.map +7 -0
- package/lib/chunks/chunk-Q2WLRFOE.js +22 -0
- package/lib/chunks/chunk-Q2WLRFOE.js.map +7 -0
- package/lib/chunks/chunk-QXIOO2RL.js +22 -0
- package/lib/chunks/chunk-QXIOO2RL.js.map +7 -0
- package/lib/chunks/{chunk-PJCJ5H2O.js → chunk-UH445BGK.js} +2 -2
- package/lib/chunks/chunk-W6W4L44F.js +22 -0
- package/lib/chunks/chunk-W6W4L44F.js.map +7 -0
- package/lib/chunks/chunk-W74UL7AY.js +22 -0
- package/lib/chunks/chunk-W74UL7AY.js.map +7 -0
- package/lib/chunks/{monitoring-UIDJS2KA.js → monitoring-LXNVBD3G.js} +2 -2
- package/lib/chunks/{rateLimiter-WNWHI3B4.js → rateLimiter-GG5LPWXJ.js} +2 -2
- package/lib/chunks/{server-RYGEBS2B.js → server-NODQAE3F.js} +2 -2
- package/lib/firebase/crud/aggregate.d.ts +1 -1
- package/lib/firebase/index.js +1 -1
- package/lib/firebase/index.js.map +3 -3
- package/lib/shared/billing/checkoutValidation.d.ts +1 -1
- package/lib/shared/billing/createCheckout.d.ts +1 -1
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +2 -2
- package/lib/shared/billing/orderWebhook.d.ts +29 -2
- package/lib/shared/billing/stripePinned.d.ts +43 -0
- package/lib/shared/denoEnv.d.ts +25 -0
- package/lib/shared/github.d.ts +0 -8
- package/lib/shared/index.js +1 -1
- package/lib/shared/logger.d.ts +14 -14
- package/lib/shared/utils/external/subscription.d.ts +0 -8
- package/lib/shared/utils.d.ts +15 -3
- package/lib/supabase/auth/deleteAccount.d.ts +34 -2
- package/lib/supabase/crud/create.d.ts +0 -8
- package/lib/supabase/crud/entityStore.d.ts +13 -0
- package/lib/supabase/crud/update.d.ts +0 -8
- package/lib/supabase/index.d.ts +1 -0
- package/lib/supabase/index.js +1 -1
- package/lib/supabase/index.js.map +4 -4
- package/lib/vercel/api/index.js +1 -1
- package/lib/vercel/api/index.js.map +3 -3
- package/package.json +9 -9
- package/src/firebase/auth/getCustomClaims.ts +2 -0
- package/src/firebase/auth/getUserAuthStatus.ts +2 -0
- package/src/firebase/auth/removeCustomClaims.ts +2 -0
- package/src/firebase/auth/setCustomClaims.ts +3 -0
- package/src/firebase/baseFunction.ts +5 -2
- package/src/firebase/billing/changePlan.ts +6 -5
- package/src/firebase/billing/createCustomerPortal.ts +6 -4
- package/src/firebase/billing/refreshSubscriptionStatus.ts +7 -5
- package/src/firebase/billing/webhookHandler.ts +4 -4
- package/src/firebase/crud/aggregate.ts +41 -12
- package/src/firebase/crud/bulk.ts +7 -0
- package/src/firebase/crud/create.ts +21 -8
- package/src/firebase/crud/delete.ts +6 -1
- package/src/firebase/crud/get.ts +9 -2
- package/src/firebase/crud/list.ts +18 -8
- package/src/firebase/crud/update.ts +15 -7
- package/src/firebase/helpers/githubAccessHelper.ts +3 -0
- package/src/firebase/helpers/githubTeamAccessHelper.ts +1 -0
- package/src/firebase/oauth/exchangeToken.ts +4 -0
- package/src/firebase/oauth/getConnections.ts +1 -1
- package/src/firebase/oauth/githubAccess.ts +3 -0
- package/src/firebase/oauth/refreshToken.ts +1 -0
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +4 -1
- package/src/shared/ai/classifyAIError.ts +18 -10
- package/src/shared/ai/createAIRoute.ts +2 -1
- package/src/shared/ai/resolveModelTier.ts +2 -1
- package/src/shared/billing/__tests__/createCheckout.test.ts +94 -0
- package/src/shared/billing/__tests__/firestoreIdempotency.emulator.test.ts +1 -1
- package/src/shared/billing/__tests__/webhookHandler.test.ts +108 -0
- package/src/shared/billing/checkoutValidation.ts +19 -2
- package/src/shared/billing/createCheckout.ts +56 -11
- package/src/shared/billing/helpers/subscriptionManagement.ts +14 -10
- package/src/shared/billing/orderWebhook.ts +69 -1
- package/src/shared/billing/stripeClient.ts +2 -1
- package/src/shared/billing/stripePinned.ts +71 -0
- package/src/shared/billing/tierWebhookEvents.ts +3 -2
- package/src/shared/billing/webhookHandler.ts +5 -1
- package/src/shared/crud/bulkCore.ts +3 -2
- package/src/shared/denoEnv.ts +29 -0
- package/src/shared/email/createEmailRoute.ts +2 -2
- package/src/shared/errorHandling.ts +8 -8
- package/src/shared/firebase.ts +26 -9
- package/src/shared/github.ts +15 -5
- package/src/shared/logger.ts +37 -38
- package/src/shared/schema.ts +6 -10
- package/src/shared/utils/external/date.ts +4 -4
- package/src/shared/utils/external/subscription.ts +2 -89
- package/src/shared/utils/firebaseHelpers.ts +2 -0
- package/src/shared/utils/internal/idempotency.ts +3 -0
- package/src/shared/utils/internal/monitoring.ts +6 -5
- package/src/shared/utils/internal/validation.ts +2 -0
- package/src/shared/utils/schemaValidation.ts +1 -0
- package/src/shared/utils/validation.ts +10 -20
- package/src/shared/utils.ts +12 -7
- package/src/supabase/__tests__/aggregate.test.ts +132 -0
- package/src/supabase/auth/__tests__/deleteAccount.test.ts +102 -0
- package/src/supabase/auth/deleteAccount.ts +72 -5
- package/src/supabase/billing/refreshSubscriptionStatus.ts +6 -5
- package/src/supabase/crud/aggregate.ts +85 -72
- package/src/supabase/crud/bulk.ts +3 -0
- package/src/supabase/crud/create.ts +13 -9
- package/src/supabase/crud/delete.ts +2 -1
- package/src/supabase/crud/entityStore.ts +1 -1
- package/src/supabase/crud/get.ts +2 -1
- package/src/supabase/crud/list.ts +23 -17
- package/src/supabase/crud/update.ts +9 -6
- package/src/supabase/edgeFunction.ts +2 -1
- package/src/supabase/email/createEmailFunction.ts +2 -1
- package/src/supabase/index.ts +1 -0
- package/src/supabase/oauth/exchangeToken.ts +1 -0
- package/src/supabase/oauth/getConnections.ts +1 -1
- package/src/supabase/oauth/refreshToken.ts +1 -0
- package/src/supabase/utils/idempotency.ts +1 -1
- package/src/supabase/utils/monitoring.ts +1 -0
- package/src/vercel/api/billing/change-plan.ts +6 -5
- package/src/vercel/api/billing/refresh-subscription-status.ts +3 -1
- package/src/vercel/api/crud/bulk.ts +7 -2
- package/src/vercel/api/crud/create.ts +6 -3
- package/src/vercel/api/crud/delete.ts +5 -2
- package/src/vercel/api/crud/get.ts +5 -2
- package/src/vercel/api/crud/list.ts +6 -3
- package/src/vercel/api/crud/update.ts +6 -3
- package/src/vercel/api/oauth/check-github-access.ts +1 -0
- package/src/vercel/api/oauth/exchange-token.ts +5 -2
- package/src/vercel/api/oauth/grant-github-access.ts +1 -0
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -0
- package/src/vercel/baseFunction.ts +5 -1
- package/lib/chunks/chunk-2A4FPYNB.js.map +0 -7
- package/lib/chunks/chunk-2IFFIUFV.js +0 -23
- package/lib/chunks/chunk-2IFFIUFV.js.map +0 -7
- package/lib/chunks/chunk-FQSQGOBN.js +0 -22
- package/lib/chunks/chunk-FQSQGOBN.js.map +0 -7
- package/lib/chunks/chunk-G4FO76EA.js +0 -22
- package/lib/chunks/chunk-G4FO76EA.js.map +0 -7
- package/lib/chunks/chunk-LSM5Y2E5.js +0 -22
- package/lib/chunks/chunk-LSM5Y2E5.js.map +0 -7
- package/lib/chunks/chunk-M4JV2JCF.js +0 -22
- package/lib/chunks/chunk-M4JV2JCF.js.map +0 -7
- package/lib/chunks/chunk-OZX5ZG44.js +0 -22
- package/lib/chunks/chunk-PWAGW4R6.js +0 -22
- package/lib/chunks/chunk-PWAGW4R6.js.map +0 -7
- package/lib/chunks/chunk-UYI56E6Q.js +0 -151
- package/lib/chunks/chunk-UYI56E6Q.js.map +0 -7
- package/lib/chunks/chunk-ZVHC2CAV.js +0 -22
- package/lib/chunks/chunk-ZVHC2CAV.js.map +0 -7
- package/lib/shared/utils/functionWrapper.d.ts +0 -36
- package/src/shared/utils/__tests__/functionWrapper.test.ts +0 -122
- package/src/shared/utils/functionWrapper.ts +0 -73
- /package/lib/chunks/{chunk-PJCJ5H2O.js.map → chunk-UH445BGK.js.map} +0 -0
- /package/lib/chunks/{monitoring-UIDJS2KA.js.map → monitoring-LXNVBD3G.js.map} +0 -0
- /package/lib/chunks/{rateLimiter-WNWHI3B4.js.map → rateLimiter-GG5LPWXJ.js.map} +0 -0
- /package/lib/chunks/{server-RYGEBS2B.js.map → server-NODQAE3F.js.map} +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// packages/functions/src/shared/billing/stripePinned.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Typed access to Stripe fields of the pinned API version
|
|
5
|
+
* @description The Stripe SDK types follow the latest API version only, while
|
|
6
|
+
* requests run at `STRIPE_API_VERSION_PIN` (pre-basil). Fields that basil
|
|
7
|
+
* moved (subscription period, invoice subscription) are read here, through one
|
|
8
|
+
* documented cast, instead of `as any` at every call site. Internal: not
|
|
9
|
+
* re-exported from any package entry.
|
|
10
|
+
*
|
|
11
|
+
* @version 0.1.0
|
|
12
|
+
* @since 0.2.2
|
|
13
|
+
* @author AMBROISE PARK Consulting
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type Stripe from 'stripe';
|
|
17
|
+
|
|
18
|
+
/** Subscription fields present at `STRIPE_API_VERSION_PIN`, dropped from the latest SDK types. */
|
|
19
|
+
interface PinnedSubscriptionFields {
|
|
20
|
+
current_period_start: number;
|
|
21
|
+
current_period_end: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Invoice fields present at `STRIPE_API_VERSION_PIN`, dropped from the latest SDK types. */
|
|
25
|
+
interface PinnedInvoiceFields {
|
|
26
|
+
/** Not expanded in webhook payloads: the subscription id, or null for one-off invoices. */
|
|
27
|
+
subscription: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Period fields of a subscription at the pinned API version.
|
|
32
|
+
*
|
|
33
|
+
* @version 0.1.0
|
|
34
|
+
* @since 0.2.2
|
|
35
|
+
* @author AMBROISE PARK Consulting
|
|
36
|
+
*/
|
|
37
|
+
export function pinnedSubscriptionPeriod(
|
|
38
|
+
subscription: Stripe.Subscription
|
|
39
|
+
): PinnedSubscriptionFields {
|
|
40
|
+
// why: SDK v20 types track the latest API (period moved to items); runtime is pinned pre-basil
|
|
41
|
+
return subscription as unknown as PinnedSubscriptionFields;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Subscription id of an invoice at the pinned API version.
|
|
46
|
+
*
|
|
47
|
+
* @version 0.1.0
|
|
48
|
+
* @since 0.2.2
|
|
49
|
+
* @author AMBROISE PARK Consulting
|
|
50
|
+
*/
|
|
51
|
+
export function pinnedInvoiceSubscriptionId(
|
|
52
|
+
invoice: Stripe.Invoice
|
|
53
|
+
): string | null {
|
|
54
|
+
// why: SDK v20 types track the latest API (moved to invoice.parent); runtime is pinned pre-basil
|
|
55
|
+
return (invoice as unknown as PinnedInvoiceFields).subscription;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Narrows a value to one of a readonly list of literals.
|
|
60
|
+
*
|
|
61
|
+
* @version 0.1.0
|
|
62
|
+
* @since 0.2.2
|
|
63
|
+
* @author AMBROISE PARK Consulting
|
|
64
|
+
*/
|
|
65
|
+
export function isOneOf<T extends string>(
|
|
66
|
+
values: readonly T[],
|
|
67
|
+
value: string
|
|
68
|
+
): value is T {
|
|
69
|
+
const list: readonly string[] = values;
|
|
70
|
+
return list.includes(value);
|
|
71
|
+
}
|
|
@@ -23,6 +23,7 @@ import type { BillingAuthProvider, StripeBackConfig, UpdateSubscriptionFn } from
|
|
|
23
23
|
import { handleError } from '../errorHandling.js';
|
|
24
24
|
import { logger } from '../logger.js';
|
|
25
25
|
import type { OrderSessionEventType } from './orderWebhook.js';
|
|
26
|
+
import { pinnedInvoiceSubscriptionId, pinnedSubscriptionPeriod } from './stripePinned.js';
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* Checkout Session event of a StripePayment / StripeSubscription item.
|
|
@@ -224,7 +225,7 @@ export async function handleInvoicePaymentSucceeded(
|
|
|
224
225
|
stripe: Stripe,
|
|
225
226
|
authProvider: BillingAuthProvider | null
|
|
226
227
|
): Promise<void> {
|
|
227
|
-
const subscriptionId = (invoice
|
|
228
|
+
const subscriptionId = pinnedInvoiceSubscriptionId(invoice);
|
|
228
229
|
if (!subscriptionId) return;
|
|
229
230
|
|
|
230
231
|
// ✅ FIX: Fetch subscription to get metadata
|
|
@@ -322,7 +323,7 @@ export async function handleSubscriptionDeleted(
|
|
|
322
323
|
tier: SUBSCRIPTION_TIERS.FREE,
|
|
323
324
|
status: SUBSCRIPTION_STATUS.CANCELED,
|
|
324
325
|
subscriptionEnd: new Date(
|
|
325
|
-
(subscription
|
|
326
|
+
pinnedSubscriptionPeriod(subscription).current_period_end * 1000
|
|
326
327
|
).toISOString(),
|
|
327
328
|
subscriptionId: subscription.id,
|
|
328
329
|
customerId: subscription.customer as string,
|
|
@@ -20,7 +20,7 @@ import { handleError } from '../errorHandling.js';
|
|
|
20
20
|
import { logger } from '../logger.js';
|
|
21
21
|
import { ReservationLostError } from './idempotencyContract.js';
|
|
22
22
|
import type { IdempotencyStore } from './idempotencyContract.js';
|
|
23
|
-
import { handleOrderSessionEvent } from './orderWebhook.js';
|
|
23
|
+
import { handleOrderSessionEvent, handleTaxIdUpdated } from './orderWebhook.js';
|
|
24
24
|
import type { OrderSessionEventType } from './orderWebhook.js';
|
|
25
25
|
import {
|
|
26
26
|
handleInvoicePaymentSucceeded,
|
|
@@ -275,6 +275,10 @@ async function routeEvent(
|
|
|
275
275
|
await handlePaymentFailed(event.data.object as Stripe.Invoice, config);
|
|
276
276
|
break;
|
|
277
277
|
|
|
278
|
+
case 'customer.tax_id.updated':
|
|
279
|
+
await handleTaxIdUpdated(event.id, event.data.object, config);
|
|
280
|
+
break;
|
|
281
|
+
|
|
278
282
|
default:
|
|
279
283
|
// W23: Use structured logger instead of console.log in production
|
|
280
284
|
logger.debug('[Webhook] Unhandled event type', { type: event.type });
|
|
@@ -46,6 +46,7 @@ import type {
|
|
|
46
46
|
* the parsed output) so callers can pass entity-specific schemas generated by
|
|
47
47
|
* `createSchemas()` without fighting variance.
|
|
48
48
|
*/
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: accepts schemas of any output type; v.GenericSchema would make InferOutput unknown for callers (breaking)
|
|
49
50
|
export type BulkRowSchema = v.BaseSchema<unknown, any, v.BaseIssue<unknown>>;
|
|
50
51
|
|
|
51
52
|
/**
|
|
@@ -281,7 +282,7 @@ export async function executeBulk<TRow = unknown>(
|
|
|
281
282
|
const messages = parsed.issues
|
|
282
283
|
.map(
|
|
283
284
|
(issue) =>
|
|
284
|
-
`${issue.path?.map((p
|
|
285
|
+
`${issue.path?.map((p) => p.key).join('.') || 'root'}: ${issue.message}`
|
|
285
286
|
)
|
|
286
287
|
.join('; ');
|
|
287
288
|
throw new DoNotDevError(
|
|
@@ -472,7 +473,7 @@ function validateRow(
|
|
|
472
473
|
const messages = parsed.issues
|
|
473
474
|
.map(
|
|
474
475
|
(issue) =>
|
|
475
|
-
`${issue.path?.map((p
|
|
476
|
+
`${issue.path?.map((p) => p.key).join('.') || 'root'}: ${issue.message}`
|
|
476
477
|
)
|
|
477
478
|
.join('; ');
|
|
478
479
|
throw new DoNotDevError(
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// packages/functions/src/shared/denoEnv.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Typed access to the Deno env from cross-runtime code
|
|
5
|
+
* @description Supabase Edge Functions run on Deno, Firebase/Vercel on Node.
|
|
6
|
+
* This package compiles against the Node lib types, which have no `Deno`
|
|
7
|
+
* global. Internal: not re-exported from any package entry.
|
|
8
|
+
*
|
|
9
|
+
* @version 0.1.0
|
|
10
|
+
* @since 0.2.2
|
|
11
|
+
* @author AMBROISE PARK Consulting
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The part of the Deno namespace read by this package. */
|
|
15
|
+
interface DenoEnvNamespace {
|
|
16
|
+
env?: { get(key: string): string | undefined };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Deno env accessor, when running on Deno.
|
|
21
|
+
*
|
|
22
|
+
* @version 0.1.0
|
|
23
|
+
* @since 0.2.2
|
|
24
|
+
* @author AMBROISE PARK Consulting
|
|
25
|
+
*/
|
|
26
|
+
export function getDenoEnv(): DenoEnvNamespace['env'] {
|
|
27
|
+
// why: the Deno global is absent from the Node lib types this package compiles against
|
|
28
|
+
return (globalThis as { Deno?: DenoEnvNamespace }).Deno?.env;
|
|
29
|
+
}
|
|
@@ -62,8 +62,8 @@ export async function handleEmailRequest(
|
|
|
62
62
|
|
|
63
63
|
// Render template with locale
|
|
64
64
|
const locale = request.locale || 'fr';
|
|
65
|
-
const subject = template.subject(request.data
|
|
66
|
-
const html = template.html(request.data
|
|
65
|
+
const subject = template.subject(request.data, locale);
|
|
66
|
+
const html = template.html(request.data, locale);
|
|
67
67
|
|
|
68
68
|
// Send via provider
|
|
69
69
|
const result = await provider.send({
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @author AMBROISE PARK Consulting
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import type { EntityHookError } from '@donotdev/core/server';
|
|
13
|
+
import type { EntityHookError, ErrorCode } from '@donotdev/core/server';
|
|
14
14
|
import { DoNotDevError } from '@donotdev/core/server';
|
|
15
15
|
|
|
16
16
|
// Re-export DoNotDevError so existing imports from this module continue to work
|
|
@@ -40,12 +40,12 @@ export function handleError(error: unknown): never {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// Error classification logic (same for all platforms)
|
|
43
|
-
let code:
|
|
43
|
+
let code: ErrorCode;
|
|
44
44
|
let message: string;
|
|
45
|
-
let details:
|
|
45
|
+
let details: unknown;
|
|
46
46
|
|
|
47
47
|
if (error instanceof DoNotDevError) {
|
|
48
|
-
code = error.code
|
|
48
|
+
code = error.code;
|
|
49
49
|
message = error.message;
|
|
50
50
|
details = error.details;
|
|
51
51
|
} else if (
|
|
@@ -53,17 +53,17 @@ export function handleError(error: unknown): never {
|
|
|
53
53
|
typeof error === 'object' &&
|
|
54
54
|
'issues' in error &&
|
|
55
55
|
((error instanceof Error && error.name === 'ValiError') ||
|
|
56
|
-
(error
|
|
56
|
+
('name' in error && error.name === 'ValiError'))
|
|
57
57
|
) {
|
|
58
58
|
code = 'invalid-argument';
|
|
59
59
|
message = 'Validation failed';
|
|
60
|
-
details = { validationErrors:
|
|
60
|
+
details = { validationErrors: error.issues };
|
|
61
61
|
} else if (
|
|
62
62
|
error &&
|
|
63
63
|
typeof error === 'object' &&
|
|
64
64
|
'type' in error &&
|
|
65
65
|
((error instanceof Error && error.name === 'EntityHookError') ||
|
|
66
|
-
(error
|
|
66
|
+
('name' in error && error.name === 'EntityHookError'))
|
|
67
67
|
) {
|
|
68
68
|
const entityError = error as EntityHookError;
|
|
69
69
|
switch (entityError.type) {
|
|
@@ -104,7 +104,7 @@ export function handleError(error: unknown): never {
|
|
|
104
104
|
|
|
105
105
|
throw new DoNotDevError(
|
|
106
106
|
message,
|
|
107
|
-
code
|
|
107
|
+
code,
|
|
108
108
|
details ? { details } : undefined
|
|
109
109
|
);
|
|
110
110
|
}
|
package/src/shared/firebase.ts
CHANGED
|
@@ -89,6 +89,7 @@ export function toISOString(timestamp: FirestoreTimestamp): string {
|
|
|
89
89
|
* @since 0.0.1
|
|
90
90
|
* @author AMBROISE PARK Consulting
|
|
91
91
|
*/
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
|
|
92
93
|
export function isTimestamp(value: any): value is FirestoreTimestamp {
|
|
93
94
|
return (
|
|
94
95
|
value &&
|
|
@@ -100,6 +101,23 @@ export function isTimestamp(value: any): value is FirestoreTimestamp {
|
|
|
100
101
|
);
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Whether a value looks like a Firestore DocumentSnapshot (id, ref, data()).
|
|
106
|
+
*/
|
|
107
|
+
function isDocumentSnapshotLike(value: object): value is {
|
|
108
|
+
id: unknown;
|
|
109
|
+
ref: unknown;
|
|
110
|
+
// why: Firestore DocumentSnapshot.data() contract; the return is not checked at runtime
|
|
111
|
+
data: () => Record<string, unknown> | undefined;
|
|
112
|
+
} {
|
|
113
|
+
return (
|
|
114
|
+
'id' in value &&
|
|
115
|
+
'ref' in value &&
|
|
116
|
+
'data' in value &&
|
|
117
|
+
typeof value.data === 'function'
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
103
121
|
/**
|
|
104
122
|
* Recursively transforms Firestore data to application format by converting Timestamps to ISO strings.
|
|
105
123
|
* @param data - The data to transform
|
|
@@ -110,6 +128,7 @@ export function isTimestamp(value: any): value is FirestoreTimestamp {
|
|
|
110
128
|
* @since 0.0.1
|
|
111
129
|
* @author AMBROISE PARK Consulting
|
|
112
130
|
*/
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
|
|
113
132
|
export function transformFirestoreData<T = any>(
|
|
114
133
|
data: T,
|
|
115
134
|
includeDocumentIds: boolean = false
|
|
@@ -121,12 +140,9 @@ export function transformFirestoreData<T = any>(
|
|
|
121
140
|
includeDocumentIds &&
|
|
122
141
|
typeof data === 'object' &&
|
|
123
142
|
data !== null &&
|
|
124
|
-
|
|
125
|
-
'ref' in data &&
|
|
126
|
-
'data' in data &&
|
|
127
|
-
typeof (data as any).data === 'function'
|
|
143
|
+
isDocumentSnapshotLike(data)
|
|
128
144
|
) {
|
|
129
|
-
const doc = data
|
|
145
|
+
const doc = data;
|
|
130
146
|
const documentData = doc.data();
|
|
131
147
|
return {
|
|
132
148
|
id: doc.id,
|
|
@@ -146,8 +162,8 @@ export function transformFirestoreData<T = any>(
|
|
|
146
162
|
|
|
147
163
|
// Handle objects recursively
|
|
148
164
|
if (typeof data === 'object' && data !== null) {
|
|
149
|
-
const transformed: Record<string,
|
|
150
|
-
for (const [key, value] of Object.entries(data
|
|
165
|
+
const transformed: Record<string, unknown> = {};
|
|
166
|
+
for (const [key, value] of Object.entries(data)) {
|
|
151
167
|
transformed[key] = transformFirestoreData(value);
|
|
152
168
|
}
|
|
153
169
|
return transformed as T;
|
|
@@ -168,6 +184,7 @@ export function transformFirestoreData<T = any>(
|
|
|
168
184
|
* @since 0.0.1
|
|
169
185
|
* @author AMBROISE PARK Consulting
|
|
170
186
|
*/
|
|
187
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
|
|
171
188
|
export function prepareForFirestore<T = any>(
|
|
172
189
|
data: T,
|
|
173
190
|
removeFields: string[] = []
|
|
@@ -183,8 +200,8 @@ export function prepareForFirestore<T = any>(
|
|
|
183
200
|
|
|
184
201
|
// Handle objects recursively
|
|
185
202
|
if (typeof data === 'object' && data !== null) {
|
|
186
|
-
const prepared: Record<string,
|
|
187
|
-
for (const [key, value] of Object.entries(data
|
|
203
|
+
const prepared: Record<string, unknown> = {};
|
|
204
|
+
for (const [key, value] of Object.entries(data)) {
|
|
188
205
|
// Skip fields that should be removed
|
|
189
206
|
if (removeFields.includes(key)) {
|
|
190
207
|
continue;
|
package/src/shared/github.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import * as v from 'valibot';
|
|
13
|
+
|
|
12
14
|
/** Configuration for authenticating with the GitHub API. */
|
|
13
15
|
export interface GitHubApiConfig {
|
|
14
16
|
accessToken: string;
|
|
@@ -51,6 +53,12 @@ export interface GitHubApiError {
|
|
|
51
53
|
status?: number;
|
|
52
54
|
}
|
|
53
55
|
|
|
56
|
+
/** Error body shape returned by the GitHub API on non-2xx responses (untrusted). */
|
|
57
|
+
const GitHubErrorBodySchema = v.object({
|
|
58
|
+
message: v.optional(v.string()),
|
|
59
|
+
documentation_url: v.optional(v.string()),
|
|
60
|
+
});
|
|
61
|
+
|
|
54
62
|
/** Pattern for valid GitHub usernames, org names, repo names, and team slugs */
|
|
55
63
|
const SAFE_GITHUB_NAME = /^[a-zA-Z0-9_.-]+$/;
|
|
56
64
|
|
|
@@ -158,8 +166,8 @@ export class GitHubApiService {
|
|
|
158
166
|
method: 'GET',
|
|
159
167
|
});
|
|
160
168
|
return true;
|
|
161
|
-
} catch (error
|
|
162
|
-
if (error.message
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (error instanceof Error && error.message.includes('404')) {
|
|
163
171
|
return false;
|
|
164
172
|
}
|
|
165
173
|
console.error('Failed to check collaborator status:', error);
|
|
@@ -276,8 +284,8 @@ export class GitHubApiService {
|
|
|
276
284
|
}
|
|
277
285
|
);
|
|
278
286
|
return true;
|
|
279
|
-
} catch (error
|
|
280
|
-
if (error.message
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (error instanceof Error && error.message.includes('404')) {
|
|
281
289
|
return false;
|
|
282
290
|
}
|
|
283
291
|
console.error('Failed to check team membership:', error);
|
|
@@ -310,7 +318,9 @@ export class GitHubApiService {
|
|
|
310
318
|
});
|
|
311
319
|
|
|
312
320
|
if (!response.ok) {
|
|
313
|
-
const
|
|
321
|
+
const errorBody: unknown = await response.json().catch(() => ({}));
|
|
322
|
+
const parsed = v.safeParse(GitHubErrorBodySchema, errorBody);
|
|
323
|
+
const errorData = parsed.success ? parsed.output : {};
|
|
314
324
|
const error: GitHubApiError = {
|
|
315
325
|
message:
|
|
316
326
|
errorData.message ||
|
package/src/shared/logger.ts
CHANGED
|
@@ -23,14 +23,15 @@
|
|
|
23
23
|
* @author AMBROISE PARK Consulting
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
//
|
|
27
|
-
// (Supabase Edge
|
|
28
|
-
//
|
|
29
|
-
// defines `window`, which made such a check throw on every edge function).
|
|
30
|
-
const _isNodeEnv = typeof process !== 'undefined' && !!process.versions?.node;
|
|
26
|
+
// Server-only by package: @donotdev/functions is never imported by client code,
|
|
27
|
+
// so no runtime browser check (Supabase Edge defines `window`, which made such a
|
|
28
|
+
// check throw on every edge function).
|
|
31
29
|
|
|
32
30
|
let sentryEnabled = false;
|
|
33
|
-
let sentryClient:
|
|
31
|
+
let sentryClient: Pick<
|
|
32
|
+
typeof import('@sentry/node'),
|
|
33
|
+
'captureException' | 'captureMessage'
|
|
34
|
+
> | null = null;
|
|
34
35
|
|
|
35
36
|
// Initialize Sentry lazily to avoid Firebase Functions issues
|
|
36
37
|
let sentryInitialized = false;
|
|
@@ -88,9 +89,9 @@ export interface LogEntry {
|
|
|
88
89
|
level: LogLevel;
|
|
89
90
|
message: string;
|
|
90
91
|
timestamp: string;
|
|
91
|
-
context?: Record<string,
|
|
92
|
+
context?: Record<string, unknown>;
|
|
92
93
|
error?: Error;
|
|
93
|
-
metadata?: Record<string,
|
|
94
|
+
metadata?: Record<string, unknown>;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
/**
|
|
@@ -130,7 +131,7 @@ export const logger = {
|
|
|
130
131
|
* @since 0.0.1
|
|
131
132
|
* @author AMBROISE PARK Consulting
|
|
132
133
|
*/
|
|
133
|
-
debug(message: string, context?: Record<string,
|
|
134
|
+
debug(message: string, context?: Record<string, unknown>): void {
|
|
134
135
|
this.log(LogLevel.DEBUG, message, context);
|
|
135
136
|
},
|
|
136
137
|
|
|
@@ -143,7 +144,7 @@ export const logger = {
|
|
|
143
144
|
* @since 0.0.1
|
|
144
145
|
* @author AMBROISE PARK Consulting
|
|
145
146
|
*/
|
|
146
|
-
info(message: string, context?: Record<string,
|
|
147
|
+
info(message: string, context?: Record<string, unknown>): void {
|
|
147
148
|
this.log(LogLevel.INFO, message, context);
|
|
148
149
|
},
|
|
149
150
|
|
|
@@ -156,7 +157,7 @@ export const logger = {
|
|
|
156
157
|
* @since 0.0.1
|
|
157
158
|
* @author AMBROISE PARK Consulting
|
|
158
159
|
*/
|
|
159
|
-
warn(message: string, context?: Record<string,
|
|
160
|
+
warn(message: string, context?: Record<string, unknown>): void {
|
|
160
161
|
this.log(LogLevel.WARN, message, context);
|
|
161
162
|
},
|
|
162
163
|
|
|
@@ -169,7 +170,10 @@ export const logger = {
|
|
|
169
170
|
* @since 0.0.1
|
|
170
171
|
* @author AMBROISE PARK Consulting
|
|
171
172
|
*/
|
|
172
|
-
async error(
|
|
173
|
+
async error(
|
|
174
|
+
message: string,
|
|
175
|
+
context?: Record<string, unknown>
|
|
176
|
+
): Promise<void> {
|
|
173
177
|
this.log(LogLevel.ERROR, message, context);
|
|
174
178
|
|
|
175
179
|
// Initialize Sentry lazily if needed
|
|
@@ -201,7 +205,10 @@ export const logger = {
|
|
|
201
205
|
* @since 0.0.1
|
|
202
206
|
* @author AMBROISE PARK Consulting
|
|
203
207
|
*/
|
|
204
|
-
async fatal(
|
|
208
|
+
async fatal(
|
|
209
|
+
message: string,
|
|
210
|
+
context?: Record<string, unknown>
|
|
211
|
+
): Promise<void> {
|
|
205
212
|
this.log(LogLevel.FATAL, message, context);
|
|
206
213
|
|
|
207
214
|
// Initialize Sentry lazily if needed
|
|
@@ -236,26 +243,12 @@ export const logger = {
|
|
|
236
243
|
* @since 0.0.1
|
|
237
244
|
* @author AMBROISE PARK Consulting
|
|
238
245
|
*/
|
|
239
|
-
log(
|
|
246
|
+
log(
|
|
247
|
+
level: LogLevel,
|
|
248
|
+
message: string,
|
|
249
|
+
context?: Record<string, unknown>
|
|
250
|
+
): void {
|
|
240
251
|
const timestamp = new Date().toISOString();
|
|
241
|
-
const logEntry: LogEntry = {
|
|
242
|
-
level,
|
|
243
|
-
message,
|
|
244
|
-
timestamp,
|
|
245
|
-
context,
|
|
246
|
-
error: context?.error,
|
|
247
|
-
metadata: {
|
|
248
|
-
...(_isNodeEnv
|
|
249
|
-
? {
|
|
250
|
-
pid: process.pid,
|
|
251
|
-
nodeVersion: process.version,
|
|
252
|
-
platform: process.platform,
|
|
253
|
-
}
|
|
254
|
-
: {}),
|
|
255
|
-
...context?.metadata,
|
|
256
|
-
},
|
|
257
|
-
};
|
|
258
|
-
|
|
259
252
|
// Format log entry for console output
|
|
260
253
|
const levelName = LogLevel[level];
|
|
261
254
|
const contextStr = context ? ` ${JSON.stringify(context)}` : '';
|
|
@@ -302,21 +295,27 @@ export const logger = {
|
|
|
302
295
|
* @param context - Additional context to include in all logs
|
|
303
296
|
* @returns Child logger instance
|
|
304
297
|
*/
|
|
305
|
-
export function createChildLogger(context: Record<string,
|
|
298
|
+
export function createChildLogger(context: Record<string, unknown>) {
|
|
306
299
|
return {
|
|
307
|
-
debug: (message: string, additionalContext?: Record<string,
|
|
300
|
+
debug: (message: string, additionalContext?: Record<string, unknown>) => {
|
|
308
301
|
logger.debug(message, { ...context, ...additionalContext });
|
|
309
302
|
},
|
|
310
|
-
info: (message: string, additionalContext?: Record<string,
|
|
303
|
+
info: (message: string, additionalContext?: Record<string, unknown>) => {
|
|
311
304
|
logger.info(message, { ...context, ...additionalContext });
|
|
312
305
|
},
|
|
313
|
-
warn: (message: string, additionalContext?: Record<string,
|
|
306
|
+
warn: (message: string, additionalContext?: Record<string, unknown>) => {
|
|
314
307
|
logger.warn(message, { ...context, ...additionalContext });
|
|
315
308
|
},
|
|
316
|
-
error: async (
|
|
309
|
+
error: async (
|
|
310
|
+
message: string,
|
|
311
|
+
additionalContext?: Record<string, unknown>
|
|
312
|
+
) => {
|
|
317
313
|
await logger.error(message, { ...context, ...additionalContext });
|
|
318
314
|
},
|
|
319
|
-
fatal: async (
|
|
315
|
+
fatal: async (
|
|
316
|
+
message: string,
|
|
317
|
+
additionalContext?: Record<string, unknown>
|
|
318
|
+
) => {
|
|
320
319
|
await logger.fatal(message, { ...context, ...additionalContext });
|
|
321
320
|
},
|
|
322
321
|
};
|
package/src/shared/schema.ts
CHANGED
|
@@ -15,11 +15,7 @@ import type * as v from 'valibot';
|
|
|
15
15
|
type Visibility = 'guest' | 'user' | 'admin' | 'technical' | 'hidden' | 'owner';
|
|
16
16
|
|
|
17
17
|
// Define a type for the custom visibility property we might add to Valibot schemas
|
|
18
|
-
interface ValibotSchemaWithVisibility extends v.
|
|
19
|
-
unknown,
|
|
20
|
-
any,
|
|
21
|
-
v.BaseIssue<unknown>
|
|
22
|
-
> {
|
|
18
|
+
interface ValibotSchemaWithVisibility extends v.GenericSchema {
|
|
23
19
|
visibility?: Visibility;
|
|
24
20
|
}
|
|
25
21
|
|
|
@@ -34,7 +30,7 @@ interface ValibotSchemaWithVisibility extends v.BaseSchema<
|
|
|
34
30
|
* @since 0.0.1
|
|
35
31
|
* @author AMBROISE PARK Consulting
|
|
36
32
|
*/
|
|
37
|
-
function getFieldVisibility(field:
|
|
33
|
+
function getFieldVisibility(field: unknown): Visibility | undefined {
|
|
38
34
|
// Check if the field has visibility property
|
|
39
35
|
if (field && typeof field === 'object' && 'visibility' in field) {
|
|
40
36
|
return (field as ValibotSchemaWithVisibility).visibility;
|
|
@@ -119,6 +115,7 @@ export function isFieldVisible(
|
|
|
119
115
|
* @author AMBROISE PARK Consulting
|
|
120
116
|
*/
|
|
121
117
|
export function getVisibleFields(
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
|
|
122
119
|
schema: any, // Accept any Valibot type, but process only objects
|
|
123
120
|
isAdmin: boolean
|
|
124
121
|
): string[] {
|
|
@@ -132,10 +129,7 @@ export function getVisibleFields(
|
|
|
132
129
|
}
|
|
133
130
|
|
|
134
131
|
// Extract the entries (field definitions) from the schema
|
|
135
|
-
const entries = schema.entries
|
|
136
|
-
string,
|
|
137
|
-
v.BaseSchema<unknown, any, v.BaseIssue<unknown>>
|
|
138
|
-
>;
|
|
132
|
+
const entries: Record<string, unknown> = schema.entries;
|
|
139
133
|
const visibleFieldNames: string[] = [];
|
|
140
134
|
|
|
141
135
|
// Iterate over each field defined in the schema's entries
|
|
@@ -164,8 +158,10 @@ export function getVisibleFields(
|
|
|
164
158
|
* @returns {Partial<T>} A new object containing only the fields visible to the user,
|
|
165
159
|
* or an empty object if input data is null/undefined.
|
|
166
160
|
*/
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: interface-typed records have no implicit index signature; Record<string, unknown> would reject them
|
|
167
162
|
export function filterVisibleFields<T extends Record<string, any>>(
|
|
168
163
|
data: T | null | undefined,
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- why: exported signature; narrowing it is a breaking change for importers (deferred to a major, any-audit 2026-11-11)
|
|
169
165
|
schema: any, // Use any for broader compatibility
|
|
170
166
|
isAdmin: boolean
|
|
171
167
|
): Partial<T> {
|
|
@@ -42,9 +42,8 @@ export function toISOString(date: DateValue): string {
|
|
|
42
42
|
return date.toISOString();
|
|
43
43
|
}
|
|
44
44
|
if (typeof date === 'object' && date !== null && 'toDate' in date) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return timestamp.toDate().toISOString();
|
|
45
|
+
if (typeof date.toDate === 'function') {
|
|
46
|
+
return date.toDate().toISOString();
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
49
|
|
|
@@ -58,7 +57,8 @@ export function toISOString(date: DateValue): string {
|
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
// Fallback for any other type
|
|
61
|
-
|
|
60
|
+
// why: unreachable for typed callers; keeps the runtime path for untyped values (numbers, plain objects)
|
|
61
|
+
const parsed = new Date(date as unknown as string);
|
|
62
62
|
if (isNaN(parsed.getTime())) {
|
|
63
63
|
throw new Error('Invalid date format');
|
|
64
64
|
}
|