@donotdev/functions 0.1.44 → 0.2.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/README.md +25 -14
- package/lib/chunks/chunk-3F4BWKQD.js +22 -0
- package/lib/chunks/chunk-3F4BWKQD.js.map +7 -0
- package/lib/chunks/chunk-5IPULSUX.js +22 -0
- package/lib/chunks/chunk-5IPULSUX.js.map +7 -0
- package/lib/chunks/chunk-6USHTJED.js +22 -0
- package/lib/chunks/chunk-6USHTJED.js.map +7 -0
- package/lib/chunks/chunk-AYY7UXNI.js +22 -0
- package/lib/chunks/chunk-AYY7UXNI.js.map +7 -0
- package/lib/chunks/chunk-CXBSROEW.js +23 -0
- package/lib/chunks/chunk-CXBSROEW.js.map +7 -0
- package/lib/chunks/chunk-GYTCWMXV.js +22 -0
- package/lib/chunks/chunk-GYTCWMXV.js.map +7 -0
- package/lib/chunks/chunk-HLNYUAZH.js +22 -0
- package/lib/chunks/chunk-HLNYUAZH.js.map +7 -0
- package/lib/chunks/chunk-LCDLG4VG.js +151 -0
- package/lib/chunks/chunk-LCDLG4VG.js.map +7 -0
- package/lib/chunks/chunk-MQANUSY3.js +22 -0
- package/lib/chunks/chunk-MQANUSY3.js.map +7 -0
- package/lib/chunks/chunk-OFQVO3FD.js +22 -0
- package/lib/chunks/chunk-OFQVO3FD.js.map +7 -0
- package/lib/chunks/chunk-PWAGW4R6.js +22 -0
- package/lib/chunks/chunk-PWAGW4R6.js.map +7 -0
- package/lib/chunks/chunk-US2VOP4S.js +22 -0
- package/lib/chunks/chunk-US2VOP4S.js.map +7 -0
- package/lib/chunks/chunk-YTKDNZNW.js +22 -0
- package/lib/chunks/chunk-YTKDNZNW.js.map +7 -0
- package/lib/chunks/monitoring-E7JBYBHQ.js +22 -0
- package/lib/chunks/monitoring-E7JBYBHQ.js.map +7 -0
- package/lib/chunks/rateLimiter-SQISLOP5.js +22 -0
- package/lib/chunks/rateLimiter-SQISLOP5.js.map +7 -0
- package/lib/chunks/server-IQ5JNJOM.js +22 -0
- package/lib/chunks/server-IQ5JNJOM.js.map +7 -0
- package/lib/firebase/auth/getCustomClaims.d.ts +1 -1
- package/lib/firebase/auth/getUserAuthStatus.d.ts +1 -1
- package/lib/firebase/auth/index.d.ts +0 -0
- package/lib/firebase/auth/removeCustomClaims.d.ts +1 -1
- package/lib/firebase/auth/setCustomClaims.d.ts +1 -1
- package/lib/firebase/baseFunction.d.ts +1 -1
- package/lib/firebase/billing/cancelSubscription.d.ts +0 -0
- package/lib/firebase/billing/changePlan.d.ts +0 -0
- package/lib/firebase/billing/createCheckoutSession.d.ts +2 -2
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/firebase/billing/healthCheck.d.ts +0 -0
- package/lib/firebase/billing/index.d.ts +2 -1
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +0 -8
- package/lib/firebase/billing/webhookHandler.d.ts +17 -2
- package/lib/firebase/config/constants.d.ts +0 -0
- package/lib/firebase/config/index.d.ts +0 -0
- package/lib/firebase/config/secrets.d.ts +0 -0
- package/lib/firebase/crud/aggregate.d.ts +0 -9
- package/lib/firebase/crud/bulk.d.ts +2 -2
- package/lib/firebase/crud/create.d.ts +1 -1
- package/lib/firebase/crud/delete.d.ts +1 -1
- package/lib/firebase/crud/get.d.ts +1 -1
- package/lib/firebase/crud/index.d.ts +0 -0
- package/lib/firebase/crud/list.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -9
- package/lib/firebase/helpers/githubAccessHelper.d.ts +0 -0
- package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +0 -0
- package/lib/firebase/helpers/index.d.ts +0 -0
- package/lib/firebase/index.d.ts +0 -0
- package/lib/firebase/index.js +1 -5
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/disconnect.d.ts +0 -0
- package/lib/firebase/oauth/exchangeToken.d.ts +0 -0
- package/lib/firebase/oauth/getConnections.d.ts +0 -0
- package/lib/firebase/oauth/githubAccess.d.ts +2 -2
- package/lib/firebase/oauth/index.d.ts +0 -0
- package/lib/firebase/oauth/refreshToken.d.ts +0 -0
- package/lib/firebase/registerCrudFunctions.d.ts +9 -1
- package/lib/firebase/scheduled/checkExpiredSubscriptions.d.ts +0 -0
- package/lib/firebase/scheduled/index.d.ts +0 -0
- package/lib/shared/ai/classifyAIError.d.ts +0 -0
- package/lib/shared/ai/costTracker.d.ts +0 -0
- package/lib/shared/ai/createAIRoute.d.ts +0 -0
- package/lib/shared/ai/index.d.ts +0 -0
- package/lib/shared/ai/resolveModelTier.d.ts +0 -0
- package/lib/shared/billing/apiVersion.d.ts +22 -0
- package/lib/shared/billing/checkoutPrice.d.ts +27 -0
- package/lib/shared/billing/checkoutValidation.d.ts +89 -0
- package/lib/shared/billing/createCheckout.d.ts +73 -24
- package/lib/shared/billing/firestoreIdempotency.d.ts +13 -0
- package/lib/shared/billing/helpers/index.d.ts +0 -1
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +4 -4
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +2 -29
- package/lib/shared/billing/idempotency.d.ts +21 -10
- package/lib/shared/billing/idempotencyContract.d.ts +121 -0
- package/lib/shared/billing/index.d.ts +2 -0
- package/lib/shared/billing/orderWebhook.d.ts +75 -0
- package/lib/shared/billing/stripeClient.d.ts +45 -0
- package/lib/shared/billing/supabaseIdempotency.d.ts +32 -0
- package/lib/shared/billing/tierWebhookEvents.d.ts +54 -0
- package/lib/shared/billing/webhookHandler.d.ts +29 -20
- package/lib/shared/email/createEmailRoute.d.ts +0 -0
- package/lib/shared/email/index.d.ts +0 -0
- package/lib/shared/email/provider.d.ts +0 -0
- package/lib/shared/email/resendProvider.d.ts +0 -0
- package/lib/shared/errorHandling.d.ts +0 -0
- package/lib/shared/firebase.d.ts +0 -0
- package/lib/shared/github.d.ts +0 -0
- package/lib/shared/index.d.ts +0 -0
- package/lib/shared/index.js +1 -6
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +0 -0
- package/lib/shared/metadata.d.ts +0 -0
- package/lib/shared/oauth/exchangeToken.d.ts +0 -0
- package/lib/shared/oauth/grantAccess.d.ts +0 -0
- package/lib/shared/oauth/index.d.ts +0 -0
- package/lib/shared/schema.d.ts +0 -0
- package/lib/shared/utils/detectFirestore.d.ts +0 -0
- package/lib/shared/utils/external/date.d.ts +0 -0
- package/lib/shared/utils/external/index.d.ts +0 -0
- package/lib/shared/utils/external/references.d.ts +0 -0
- package/lib/shared/utils/external/subscription.d.ts +0 -0
- package/lib/shared/utils/firebaseHelpers.d.ts +0 -0
- package/lib/shared/utils/functionWrapper.d.ts +1 -1
- package/lib/shared/utils/index.d.ts +0 -0
- package/lib/shared/utils/internal/auth.d.ts +38 -1
- package/lib/shared/utils/internal/firebase.d.ts +0 -0
- package/lib/shared/utils/internal/idempotency.d.ts +0 -0
- package/lib/shared/utils/internal/index.d.ts +0 -0
- package/lib/shared/utils/internal/monitoring.d.ts +0 -0
- package/lib/shared/utils/internal/rateLimiter.d.ts +8 -0
- package/lib/shared/utils/internal/validation.d.ts +0 -8
- package/lib/shared/utils/schemaValidation.d.ts +1 -1
- package/lib/shared/utils/validation.d.ts +0 -0
- package/lib/shared/utils/vercelHelpers.d.ts +8 -0
- package/lib/shared/utils.d.ts +0 -56
- package/lib/shared/validation.d.ts +0 -0
- package/lib/supabase/ai/createAIFunction.d.ts +0 -0
- package/lib/supabase/auth/deleteAccount.d.ts +0 -0
- package/lib/supabase/auth/getCustomClaims.d.ts +0 -0
- package/lib/supabase/auth/getUserAuthStatus.d.ts +0 -0
- package/lib/supabase/auth/removeCustomClaims.d.ts +0 -0
- package/lib/supabase/auth/setCustomClaims.d.ts +0 -0
- package/lib/supabase/baseFunction.d.ts +1 -1
- package/lib/supabase/billing/cancelSubscription.d.ts +0 -0
- package/lib/supabase/billing/changePlan.d.ts +0 -0
- package/lib/supabase/billing/createCheckoutSession.d.ts +0 -0
- package/lib/supabase/billing/createCustomerPortal.d.ts +0 -0
- package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +0 -0
- package/lib/supabase/billing/stripeWebhook.d.ts +34 -0
- package/lib/supabase/crud/aggregate.d.ts +0 -0
- package/lib/supabase/crud/bulk.d.ts +1 -1
- package/lib/supabase/crud/create.d.ts +0 -0
- package/lib/supabase/crud/delete.d.ts +0 -0
- package/lib/supabase/crud/entityStore.d.ts +71 -0
- package/lib/supabase/crud/get.d.ts +0 -0
- package/lib/supabase/crud/index.d.ts +2 -0
- package/lib/supabase/crud/list.d.ts +0 -8
- package/lib/supabase/crud/update.d.ts +0 -0
- package/lib/supabase/edgeFunction.d.ts +0 -0
- package/lib/supabase/email/createEmailFunction.d.ts +0 -0
- package/lib/supabase/helpers/authProvider.d.ts +5 -5
- package/lib/supabase/index.d.ts +11 -2
- package/lib/supabase/index.js +1 -3
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/oauth/disconnect.d.ts +0 -0
- package/lib/supabase/oauth/exchangeToken.d.ts +0 -0
- package/lib/supabase/oauth/getConnections.d.ts +0 -0
- package/lib/supabase/oauth/index.d.ts +0 -0
- package/lib/supabase/oauth/refreshToken.d.ts +0 -0
- package/lib/supabase/registerCrudFunctions.d.ts +0 -0
- package/lib/supabase/utils/idempotency.d.ts +0 -0
- package/lib/supabase/utils/monitoring.d.ts +0 -0
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +0 -9
- package/lib/vercel/api/ai/index.d.ts +0 -0
- package/lib/vercel/api/auth/get-custom-claims.d.ts +8 -0
- package/lib/vercel/api/auth/get-user-auth-status.d.ts +8 -0
- package/lib/vercel/api/auth/index.d.ts +0 -0
- package/lib/vercel/api/auth/remove-custom-claims.d.ts +8 -0
- package/lib/vercel/api/auth/set-custom-claims.d.ts +8 -0
- package/lib/vercel/api/billing/backend.d.ts +39 -0
- package/lib/vercel/api/billing/cancel.d.ts +8 -0
- package/lib/vercel/api/billing/change-plan.d.ts +9 -1
- package/lib/vercel/api/billing/create-checkout-session.d.ts +25 -6
- package/lib/vercel/api/billing/customer-portal.d.ts +8 -0
- package/lib/vercel/api/billing/index.d.ts +4 -2
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +19 -3
- package/lib/vercel/api/billing/webhook-handler.d.ts +25 -4
- package/lib/vercel/api/config/constants.d.ts +0 -0
- package/lib/vercel/api/config/firebase-admin.d.ts +8 -0
- package/lib/vercel/api/config/index.d.ts +0 -0
- package/lib/vercel/api/crud/bulk.d.ts +17 -0
- package/lib/vercel/api/crud/create.d.ts +8 -0
- package/lib/vercel/api/crud/delete.d.ts +8 -0
- package/lib/vercel/api/crud/get.d.ts +8 -0
- package/lib/vercel/api/crud/index.d.ts +0 -0
- package/lib/vercel/api/crud/list.d.ts +8 -0
- package/lib/vercel/api/crud/update.d.ts +8 -0
- package/lib/vercel/api/index.d.ts +0 -0
- package/lib/vercel/api/index.js +1 -3
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +8 -0
- package/lib/vercel/api/oauth/exchange-token.d.ts +8 -0
- package/lib/vercel/api/oauth/grant-github-access.d.ts +8 -0
- package/lib/vercel/api/oauth/index.d.ts +0 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +1 -1
- package/lib/vercel/api/utils/cors.d.ts +0 -0
- package/lib/vercel/baseFunction.d.ts +4 -1
- package/lib/vercel/supabaseAdmin.d.ts +22 -0
- package/package.json +25 -26
- package/src/firebase/auth/getCustomClaims.ts +4 -5
- package/src/firebase/auth/getUserAuthStatus.ts +4 -5
- package/src/firebase/auth/removeCustomClaims.ts +4 -5
- package/src/firebase/auth/setCustomClaims.ts +4 -5
- package/src/firebase/baseFunction.ts +4 -5
- package/src/firebase/billing/cancelSubscription.ts +2 -3
- package/src/firebase/billing/changePlan.ts +3 -8
- package/src/firebase/billing/createCheckoutSession.ts +31 -61
- package/src/firebase/billing/createCustomerPortal.ts +5 -6
- package/src/firebase/billing/healthCheck.ts +1 -1
- package/src/firebase/billing/index.ts +8 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -4
- package/src/firebase/billing/webhookHandler.ts +21 -5
- package/src/firebase/crud/__tests__/bulk.test.ts +11 -1
- package/src/firebase/crud/aggregate.ts +1 -2
- package/src/firebase/crud/bulk.ts +5 -6
- package/src/firebase/crud/create.ts +5 -6
- package/src/firebase/crud/delete.ts +4 -5
- package/src/firebase/crud/get.ts +4 -5
- package/src/firebase/crud/list.ts +4 -5
- package/src/firebase/crud/update.ts +5 -6
- package/src/firebase/oauth/githubAccess.ts +2 -3
- package/src/firebase/registerCrudFunctions.ts +2 -2
- package/src/shared/__tests__/auth.test.ts +0 -3
- package/src/shared/__tests__/bulkCore.test.ts +3 -2
- package/src/shared/__tests__/errorHandling.test.ts +2 -2
- package/src/shared/__tests__/idempotency.test.ts +282 -55
- package/src/shared/__tests__/internalValidation.test.ts +3 -42
- package/src/shared/__tests__/logger.test.ts +31 -0
- package/src/shared/__tests__/rateLimiter.test.ts +2 -8
- package/src/shared/__tests__/schemaValidation.test.ts +1 -1
- package/src/shared/__tests__/utils.test.ts +3 -2
- package/src/shared/__tests__/validation-env.test.ts +0 -0
- package/src/shared/ai/createAIRoute.ts +53 -30
- package/src/shared/billing/__tests__/checkoutValidation.test.ts +219 -0
- package/src/shared/billing/__tests__/createCheckout.compat.test.ts +349 -0
- package/src/shared/billing/__tests__/createCheckout.test.ts +454 -411
- package/src/shared/billing/__tests__/firestoreIdempotency.emulator.test.ts +92 -0
- package/src/shared/billing/__tests__/webhookCorrectness.test.ts +387 -0
- package/src/shared/billing/__tests__/webhookHandler.test.ts +370 -23
- package/src/shared/billing/apiVersion.ts +25 -0
- package/src/shared/billing/checkoutPrice.ts +125 -0
- package/src/shared/billing/checkoutValidation.ts +284 -0
- package/src/shared/billing/createCheckout.ts +332 -196
- package/src/shared/billing/firestoreIdempotency.ts +189 -0
- package/src/shared/billing/helpers/index.ts +0 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +8 -10
- package/src/shared/billing/helpers/updateUserSubscription.ts +3 -36
- package/src/shared/billing/idempotency.ts +104 -168
- package/src/shared/billing/idempotencyContract.ts +135 -0
- package/src/shared/billing/index.ts +2 -0
- package/src/shared/billing/orderWebhook.ts +269 -0
- package/src/shared/billing/stripeClient.ts +91 -0
- package/src/shared/billing/supabaseIdempotency.ts +228 -0
- package/src/shared/billing/tierWebhookEvents.ts +432 -0
- package/src/shared/billing/webhookHandler.ts +189 -386
- package/src/shared/logger.ts +4 -6
- package/src/shared/utils/external/subscription.ts +0 -0
- package/src/shared/utils/firebaseHelpers.ts +2 -3
- package/src/shared/utils/functionWrapper.ts +1 -2
- package/src/shared/utils/internal/auth.ts +55 -7
- package/src/shared/utils/internal/monitoring.ts +17 -9
- package/src/shared/utils/internal/rateLimiter.ts +14 -5
- package/src/shared/utils/internal/validation.ts +0 -18
- package/src/shared/utils/schemaValidation.ts +1 -2
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +5 -99
- package/src/shared/validation.ts +1 -1
- package/src/supabase/__tests__/bulk.test.ts +3 -1
- package/src/supabase/__tests__/entityStore.test.ts +90 -0
- package/src/supabase/baseFunction.ts +1 -2
- package/src/supabase/billing/cancelSubscription.ts +1 -1
- package/src/supabase/billing/changePlan.ts +1 -1
- package/src/supabase/billing/createCheckoutSession.ts +11 -41
- package/src/supabase/billing/createCustomerPortal.ts +1 -1
- package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -7
- package/src/supabase/billing/stripeWebhook.ts +79 -0
- package/src/supabase/crud/aggregate.ts +1 -1
- package/src/supabase/crud/bulk.ts +3 -7
- package/src/supabase/crud/create.ts +1 -1
- package/src/supabase/crud/entityStore.ts +240 -0
- package/src/supabase/crud/index.ts +8 -0
- package/src/supabase/crud/list.ts +2 -0
- package/src/supabase/crud/update.ts +1 -1
- package/src/supabase/helpers/authProvider.ts +6 -5
- package/src/supabase/index.ts +26 -0
- package/src/supabase/utils/rateLimiter.ts +2 -2
- package/src/vercel/__tests__/bulk.test.ts +24 -21
- package/src/vercel/__tests__/firebaseOptional.test.ts +64 -0
- package/src/vercel/api/ai/chat.ts +2 -1
- package/src/vercel/api/auth/get-custom-claims.ts +2 -3
- package/src/vercel/api/auth/get-user-auth-status.ts +2 -3
- package/src/vercel/api/auth/remove-custom-claims.ts +2 -3
- package/src/vercel/api/auth/set-custom-claims.ts +3 -3
- package/src/vercel/api/billing/backend.ts +72 -0
- package/src/vercel/api/billing/cancel.ts +2 -4
- package/src/vercel/api/billing/change-plan.ts +4 -5
- package/src/vercel/api/billing/create-checkout-session.ts +43 -73
- package/src/vercel/api/billing/customer-portal.ts +3 -5
- package/src/vercel/api/billing/index.ts +4 -2
- package/src/vercel/api/billing/refresh-subscription-status.ts +22 -7
- package/src/vercel/api/billing/webhook-handler.ts +32 -26
- package/src/vercel/api/config/firebase-admin.ts +2 -2
- package/src/vercel/api/crud/bulk.ts +6 -9
- package/src/vercel/api/crud/create.ts +6 -5
- package/src/vercel/api/crud/delete.ts +4 -3
- package/src/vercel/api/crud/get.ts +6 -5
- package/src/vercel/api/crud/list.ts +6 -5
- package/src/vercel/api/crud/update.ts +6 -5
- package/src/vercel/api/oauth/check-github-access.ts +5 -5
- package/src/vercel/api/oauth/exchange-token.ts +3 -3
- package/src/vercel/api/oauth/grant-github-access.ts +5 -5
- package/src/vercel/api/oauth/revoke-github-access.ts +3 -4
- package/src/vercel/baseFunction.ts +23 -8
- package/src/vercel/supabaseAdmin.ts +45 -0
- package/lib/shared/ai/createAIHandler.d.ts +0 -39
- package/lib/shared/ai/providers/anthropic.d.ts +0 -12
- package/lib/shared/ai/providers/google.d.ts +0 -10
- package/lib/shared/ai/providers/index.d.ts +0 -23
- package/lib/shared/ai/providers/openai.d.ts +0 -10
- package/lib/shared/ai/utils/parseSSE.d.ts +0 -22
|
@@ -38,26 +38,28 @@ vi.mock('../../shared/utils.js', () => ({
|
|
|
38
38
|
// `executeBulk` is the real orchestrator so end-to-end behaviour (collision
|
|
39
39
|
// detection, empty short-circuit, transact invocation) exercises the shared
|
|
40
40
|
// core.
|
|
41
|
-
vi.mock('../../shared/
|
|
42
|
-
const actual = await vi.importActual<
|
|
43
|
-
'../../shared/
|
|
44
|
-
);
|
|
41
|
+
vi.mock('../../shared/firebase.js', async () => {
|
|
42
|
+
const actual = await vi.importActual<
|
|
43
|
+
typeof import('../../shared/firebase.js')
|
|
44
|
+
>('../../shared/firebase.js');
|
|
45
45
|
return {
|
|
46
46
|
...actual,
|
|
47
47
|
prepareForFirestore: (obj: Record<string, unknown>) => ({ ...obj }),
|
|
48
48
|
transformFirestoreData: (obj: Record<string, unknown>) => ({ ...obj }),
|
|
49
|
-
createMetadata: (uid: string) => ({
|
|
50
|
-
createdAt: 'now',
|
|
51
|
-
updatedAt: 'now',
|
|
52
|
-
createdById: uid,
|
|
53
|
-
updatedById: uid,
|
|
54
|
-
}),
|
|
55
|
-
updateMetadata: (uid: string) => ({
|
|
56
|
-
updatedAt: 'now',
|
|
57
|
-
updatedById: uid,
|
|
58
|
-
}),
|
|
59
49
|
};
|
|
60
50
|
});
|
|
51
|
+
vi.mock('../../shared/metadata.js', () => ({
|
|
52
|
+
createMetadata: (uid: string) => ({
|
|
53
|
+
createdAt: 'now',
|
|
54
|
+
updatedAt: 'now',
|
|
55
|
+
createdById: uid,
|
|
56
|
+
updatedById: uid,
|
|
57
|
+
}),
|
|
58
|
+
updateMetadata: (uid: string) => ({
|
|
59
|
+
updatedAt: 'now',
|
|
60
|
+
updatedById: uid,
|
|
61
|
+
}),
|
|
62
|
+
}));
|
|
61
63
|
|
|
62
64
|
// ---------------------------------------------------------------------------
|
|
63
65
|
// Firebase Admin SDK — shared spy surface so each test can assert calls.
|
|
@@ -70,7 +72,7 @@ type TxSpy = {
|
|
|
70
72
|
delete: ReturnType<typeof vi.fn>;
|
|
71
73
|
};
|
|
72
74
|
|
|
73
|
-
let txSpy: TxSpy;
|
|
75
|
+
let txSpy: TxSpy | undefined;
|
|
74
76
|
let existingDocIds: Set<string>;
|
|
75
77
|
let mintedIds: string[];
|
|
76
78
|
let mintedIdx: number;
|
|
@@ -170,6 +172,7 @@ function makeReq(opts: {
|
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
beforeEach(() => {
|
|
175
|
+
txSpy = undefined;
|
|
173
176
|
existingDocIds = new Set();
|
|
174
177
|
mintedIds = [];
|
|
175
178
|
mintedIdx = 0;
|
|
@@ -203,9 +206,9 @@ describe('vercel bulk handler — happy path', () => {
|
|
|
203
206
|
deletedIds: ['d1'],
|
|
204
207
|
});
|
|
205
208
|
// One transaction, N writes inside it.
|
|
206
|
-
expect(txSpy
|
|
207
|
-
expect(txSpy
|
|
208
|
-
expect(txSpy
|
|
209
|
+
expect(txSpy!.set).toHaveBeenCalledTimes(2);
|
|
210
|
+
expect(txSpy!.update).toHaveBeenCalledTimes(1);
|
|
211
|
+
expect(txSpy!.delete).toHaveBeenCalledTimes(1);
|
|
209
212
|
});
|
|
210
213
|
});
|
|
211
214
|
|
|
@@ -263,9 +266,9 @@ describe('vercel bulk handler — validation failure', () => {
|
|
|
263
266
|
|
|
264
267
|
expect(res._status).toBe(500);
|
|
265
268
|
// No writes issued — existence check runs before any tx.set/update/delete.
|
|
266
|
-
expect(txSpy
|
|
267
|
-
expect(txSpy
|
|
268
|
-
expect(txSpy
|
|
269
|
+
expect(txSpy!.set).not.toHaveBeenCalled();
|
|
270
|
+
expect(txSpy!.update).not.toHaveBeenCalled();
|
|
271
|
+
expect(txSpy!.delete).not.toHaveBeenCalled();
|
|
269
272
|
});
|
|
270
273
|
|
|
271
274
|
it('rejects structurally invalid body (ACL-equivalent schema check)', async () => {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// packages/functions/src/vercel/__tests__/firebaseOptional.test.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Regression test: the Vercel entry loads without Firebase
|
|
5
|
+
* @description `firebase-admin` and `firebase-functions` are optional peers. A
|
|
6
|
+
* Supabase-only Vercel app does not install them, so nothing reachable from
|
|
7
|
+
* `@donotdev/functions/vercel` may import them (or `@donotdev/firebase/server`)
|
|
8
|
+
* at module load. Every Firebase module is mocked to throw on load; the entry
|
|
9
|
+
* and the Supabase billing backend must still load and resolve.
|
|
10
|
+
*
|
|
11
|
+
* vitest isolates the module registry per test file, so the mocks below
|
|
12
|
+
* cannot leak into suites that load Firebase for real.
|
|
13
|
+
*
|
|
14
|
+
* @version 0.2.0
|
|
15
|
+
* @since 0.2.0
|
|
16
|
+
* @author AMBROISE PARK Consulting
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
20
|
+
|
|
21
|
+
function blocked(name: string): never {
|
|
22
|
+
throw new Error(`${name} must not be loaded on the Supabase path`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
vi.mock('firebase-admin', () => blocked('firebase-admin'));
|
|
26
|
+
vi.mock('firebase-admin/app', () => blocked('firebase-admin/app'));
|
|
27
|
+
vi.mock('firebase-admin/auth', () => blocked('firebase-admin/auth'));
|
|
28
|
+
vi.mock('firebase-admin/firestore', () => blocked('firebase-admin/firestore'));
|
|
29
|
+
vi.mock('firebase-admin/functions', () => blocked('firebase-admin/functions'));
|
|
30
|
+
vi.mock('firebase-functions', () => blocked('firebase-functions'));
|
|
31
|
+
vi.mock('firebase-functions/v2', () => blocked('firebase-functions/v2'));
|
|
32
|
+
vi.mock('firebase-functions/v2/https', () => blocked('firebase-functions/v2/https'));
|
|
33
|
+
vi.mock('firebase-functions/params', () => blocked('firebase-functions/params'));
|
|
34
|
+
vi.mock('@donotdev/firebase/server', () => blocked('@donotdev/firebase/server'));
|
|
35
|
+
|
|
36
|
+
vi.mock('@supabase/supabase-js', () => ({
|
|
37
|
+
createClient: () => ({
|
|
38
|
+
auth: { admin: { getUserById: async () => ({ data: {}, error: null }) } },
|
|
39
|
+
from: () => ({}),
|
|
40
|
+
}),
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
describe('vercel entry without firebase', () => {
|
|
44
|
+
it('loads the whole @donotdev/functions/vercel entry', async () => {
|
|
45
|
+
const entry = await import('../api/index.js');
|
|
46
|
+
expect(typeof entry.createStripeWebhook).toBe('function');
|
|
47
|
+
expect(typeof entry.createCheckoutSession).toBe('function');
|
|
48
|
+
expect(typeof entry.refreshSubscriptionStatus).toBe('function');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('builds Supabase billing handlers and resolves the supabase backend', async () => {
|
|
52
|
+
process.env.SUPABASE_URL = 'https://example.supabase.co';
|
|
53
|
+
process.env.SUPABASE_SECRET_KEY = 'sb_secret_test';
|
|
54
|
+
const entry = await import('../api/index.js');
|
|
55
|
+
const { resolveBillingBackend } = await import('../api/billing/backend.js');
|
|
56
|
+
|
|
57
|
+
expect(typeof entry.createCheckoutSession({}, { backend: 'supabase' })).toBe(
|
|
58
|
+
'function'
|
|
59
|
+
);
|
|
60
|
+
const resolved = await resolveBillingBackend('supabase');
|
|
61
|
+
expect(typeof resolved.authProvider.getUser).toBe('function');
|
|
62
|
+
expect(typeof resolved.idempotencyStore).toBe('object');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -10,9 +10,10 @@
|
|
|
10
10
|
* @author AMBROISE PARK Consulting
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import * as v from 'valibot';
|
|
14
|
+
|
|
13
15
|
import type { AIRouteConfig } from '@donotdev/core/server';
|
|
14
16
|
import { AIChatRequestSchema } from '@donotdev/core/server';
|
|
15
|
-
import * as v from 'valibot';
|
|
16
17
|
|
|
17
18
|
import { handleAIRequest } from '../../../shared/ai/index.js';
|
|
18
19
|
import { verifyToken } from '../../../shared/utils/internal/auth.js';
|
|
@@ -9,13 +9,11 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
13
|
|
|
14
14
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
15
15
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
16
16
|
|
|
17
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
18
|
-
|
|
19
17
|
export default async function handler(
|
|
20
18
|
req: NextApiRequest,
|
|
21
19
|
res: NextApiResponse
|
|
@@ -27,6 +25,7 @@ export default async function handler(
|
|
|
27
25
|
try {
|
|
28
26
|
// C1: verify JWT — previously only checked header presence.
|
|
29
27
|
const uid = await verifyAuthToken(req);
|
|
28
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
30
29
|
const user = await getFirebaseAdminAuth().getUser(uid);
|
|
31
30
|
return res.status(200).json({ customClaims: user.customClaims || {} });
|
|
32
31
|
} catch (error) {
|
|
@@ -9,13 +9,11 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
13
|
|
|
14
14
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
15
15
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
16
16
|
|
|
17
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
18
|
-
|
|
19
17
|
export default async function handler(
|
|
20
18
|
req: NextApiRequest,
|
|
21
19
|
res: NextApiResponse
|
|
@@ -27,6 +25,7 @@ export default async function handler(
|
|
|
27
25
|
try {
|
|
28
26
|
// C1: verify JWT — previously only checked header presence.
|
|
29
27
|
const uid = await verifyAuthToken(req);
|
|
28
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
30
29
|
const user = await getFirebaseAdminAuth().getUser(uid);
|
|
31
30
|
|
|
32
31
|
return res.status(200).json({
|
|
@@ -9,13 +9,11 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
13
|
|
|
14
14
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
15
15
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
16
16
|
|
|
17
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
18
|
-
|
|
19
17
|
export default async function handler(
|
|
20
18
|
req: NextApiRequest,
|
|
21
19
|
res: NextApiResponse
|
|
@@ -33,6 +31,7 @@ export default async function handler(
|
|
|
33
31
|
throw handleError(new Error('claimsToRemove must be an array'));
|
|
34
32
|
}
|
|
35
33
|
|
|
34
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
36
35
|
const user = await getFirebaseAdminAuth().getUser(uid);
|
|
37
36
|
const currentClaims = user.customClaims || {};
|
|
38
37
|
|
|
@@ -9,13 +9,11 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
13
|
|
|
14
14
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
15
15
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
16
16
|
|
|
17
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
18
|
-
|
|
19
17
|
/**
|
|
20
18
|
* Claim keys that must NEVER be self-assignable — privilege escalation vectors.
|
|
21
19
|
*/
|
|
@@ -78,6 +76,8 @@ export function createSetCustomClaimsHandler(
|
|
|
78
76
|
);
|
|
79
77
|
}
|
|
80
78
|
|
|
79
|
+
const { getFirebaseAdminAuth } =
|
|
80
|
+
await import('@donotdev/firebase/server');
|
|
81
81
|
const user = await getFirebaseAdminAuth().getUser(uid);
|
|
82
82
|
const currentClaims = user.customClaims || {};
|
|
83
83
|
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// packages/functions/src/vercel/api/billing/backend.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Backend wiring for Vercel billing handlers
|
|
5
|
+
* @description Vercel functions run against either Firebase or Supabase. The
|
|
6
|
+
* backend decides token verification, rate limiting, user claims and webhook
|
|
7
|
+
* idempotency storage. It is always explicit: there is no auto-detection.
|
|
8
|
+
* Firebase modules are imported on the Firebase path only: `firebase-admin`
|
|
9
|
+
* and `firebase-functions` are optional peers a Supabase app does not install.
|
|
10
|
+
*
|
|
11
|
+
* @version 0.2.0
|
|
12
|
+
* @since 0.2.0
|
|
13
|
+
* @author AMBROISE PARK Consulting
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { BillingAuthProvider } from '@donotdev/core/server';
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
createFirestoreIdempotencyStore,
|
|
20
|
+
createSupabaseIdempotencyStore,
|
|
21
|
+
} from '../../../shared/billing/idempotency.js';
|
|
22
|
+
import type { IdempotencyStore } from '../../../shared/billing/idempotency.js';
|
|
23
|
+
import { parseAuthBackend } from '../../../shared/utils/internal/auth.js';
|
|
24
|
+
import type { AuthProvider as AuthBackend } from '../../../shared/utils/internal/auth.js';
|
|
25
|
+
import { createSupabaseAuthProvider } from '../../../supabase/helpers/authProvider.js';
|
|
26
|
+
import { getSupabaseAdmin } from '../../supabaseAdmin.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Options of the Vercel billing handler factories.
|
|
30
|
+
*
|
|
31
|
+
* @version 0.2.0
|
|
32
|
+
* @since 0.2.0
|
|
33
|
+
* @author AMBROISE PARK Consulting
|
|
34
|
+
*/
|
|
35
|
+
export interface VercelBillingOptions {
|
|
36
|
+
/** Auth and data backend of the app (validated at runtime) */
|
|
37
|
+
backend: AuthBackend;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Resolve the user-claims provider and the webhook idempotency store of a backend.
|
|
42
|
+
*
|
|
43
|
+
* @throws DoNotDevError (`invalid-argument`) when `backend` is not `'firebase'` or `'supabase'`
|
|
44
|
+
*
|
|
45
|
+
* @version 0.2.0
|
|
46
|
+
* @since 0.2.0
|
|
47
|
+
* @author AMBROISE PARK Consulting
|
|
48
|
+
*/
|
|
49
|
+
export async function resolveBillingBackend(
|
|
50
|
+
backend: VercelBillingOptions['backend']
|
|
51
|
+
): Promise<{ authProvider: BillingAuthProvider; idempotencyStore: IdempotencyStore }> {
|
|
52
|
+
if (parseAuthBackend(backend) === 'supabase') {
|
|
53
|
+
const supabaseAdmin = await getSupabaseAdmin();
|
|
54
|
+
return {
|
|
55
|
+
authProvider: createSupabaseAuthProvider(supabaseAdmin),
|
|
56
|
+
idempotencyStore: createSupabaseIdempotencyStore(supabaseAdmin),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
60
|
+
return {
|
|
61
|
+
authProvider: {
|
|
62
|
+
async getUser(userId) {
|
|
63
|
+
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
64
|
+
return { customClaims: user.customClaims };
|
|
65
|
+
},
|
|
66
|
+
async setCustomUserClaims(userId, claims) {
|
|
67
|
+
await getFirebaseAdminAuth().setCustomUserClaims(userId, claims);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
idempotencyStore: createFirestoreIdempotencyStore(),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -9,15 +9,12 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
12
13
|
import * as v from 'valibot';
|
|
13
14
|
|
|
14
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
15
|
-
|
|
16
15
|
import { cancelUserSubscription } from '../../../shared/billing/helpers/subscriptionManagement.js';
|
|
17
16
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
18
17
|
|
|
19
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
20
|
-
|
|
21
18
|
const cancelSubscriptionSchema = v.object({
|
|
22
19
|
userId: v.string(),
|
|
23
20
|
});
|
|
@@ -39,6 +36,7 @@ async function cancelSubscriptionLogic(
|
|
|
39
36
|
// C7/W5: Use context.uid — ignore client-supplied userId to prevent IDOR.
|
|
40
37
|
const userId = context.uid;
|
|
41
38
|
|
|
39
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
42
40
|
const authProvider = {
|
|
43
41
|
async getUser(uid: string) {
|
|
44
42
|
return getFirebaseAdminAuth().getUser(uid);
|
|
@@ -9,19 +9,17 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
|
+
import type Stripe from 'stripe';
|
|
12
14
|
import * as v from 'valibot';
|
|
13
15
|
|
|
14
16
|
import type { StripeBackConfig } from '@donotdev/core/server';
|
|
15
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
16
17
|
|
|
17
18
|
import { updateUserSubscription } from '../../../shared/billing/helpers/updateUserSubscription.js';
|
|
18
19
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
19
|
-
import { stripe, validateStripeEnvironment } from '../../../shared/
|
|
20
|
+
import { stripe, validateStripeEnvironment } from '../../../shared/billing/stripeClient.js';
|
|
20
21
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
21
22
|
|
|
22
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
23
|
-
import type Stripe from 'stripe';
|
|
24
|
-
|
|
25
23
|
const changePlanSchema = v.object({
|
|
26
24
|
userId: v.string(),
|
|
27
25
|
newPriceId: v.string(),
|
|
@@ -60,6 +58,7 @@ async function changePlanLogic(
|
|
|
60
58
|
}
|
|
61
59
|
|
|
62
60
|
// Get current subscription
|
|
61
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
63
62
|
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
64
63
|
const subscriptionId = user.customClaims?.subscription?.subscriptionId;
|
|
65
64
|
|
|
@@ -2,100 +2,60 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @fileoverview Create checkout session API handler
|
|
5
|
-
* @description Vercel API route for creating Stripe checkout sessions
|
|
5
|
+
* @description Vercel API route for creating Stripe checkout sessions.
|
|
6
|
+
* Delegates to the shared createCheckoutAlgorithm (SSOT).
|
|
6
7
|
*
|
|
7
|
-
* @version 0.
|
|
8
|
+
* @version 0.2.0
|
|
8
9
|
* @since 0.0.1
|
|
9
10
|
* @author AMBROISE PARK Consulting
|
|
10
11
|
*/
|
|
11
12
|
|
|
13
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
14
|
+
|
|
12
15
|
import { CreateCheckoutSessionRequestSchema } from '@donotdev/core/server';
|
|
13
16
|
import type {
|
|
14
17
|
CreateCheckoutSessionRequest,
|
|
15
18
|
StripeBackConfig,
|
|
16
19
|
} from '@donotdev/core/server';
|
|
17
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
18
20
|
|
|
21
|
+
import { resolveBillingBackend } from './backend.js';
|
|
22
|
+
import type { VercelBillingOptions } from './backend.js';
|
|
19
23
|
import { createCheckoutAlgorithm } from '../../../shared/billing/createCheckout.js';
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
stripe,
|
|
23
|
-
validateStripeEnvironment,
|
|
24
|
-
} from '../../../shared/utils.js';
|
|
24
|
+
import type { StripeSessionProvider } from '../../../shared/billing/createCheckout.js';
|
|
25
|
+
import { initStripe, stripe, validateStripeEnvironment } from '../../../shared/billing/stripeClient.js';
|
|
25
26
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
26
27
|
|
|
27
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
28
|
-
import type * as v from 'valibot';
|
|
29
|
-
|
|
30
28
|
/**
|
|
31
29
|
* Business logic for creating checkout sessions
|
|
32
30
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
33
|
-
*
|
|
34
|
-
* @version 0.1.0
|
|
35
|
-
* @since 0.0.1
|
|
36
|
-
* @author AMBROISE PARK Consulting
|
|
37
31
|
*/
|
|
38
32
|
async function createCheckoutSessionLogic(
|
|
39
|
-
req: NextApiRequest,
|
|
40
33
|
res: NextApiResponse,
|
|
41
34
|
data: CreateCheckoutSessionRequest,
|
|
42
35
|
context: { uid: string },
|
|
43
|
-
billingConfig: StripeBackConfig
|
|
36
|
+
billingConfig: StripeBackConfig,
|
|
37
|
+
options: VercelBillingOptions
|
|
44
38
|
) {
|
|
45
|
-
// Initialize Stripe with env var
|
|
46
39
|
initStripe(process.env.STRIPE_SECRET_KEY || '');
|
|
47
40
|
validateStripeEnvironment();
|
|
48
41
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
priceId?: string;
|
|
53
|
-
unitAmount?: number;
|
|
54
|
-
productName?: string;
|
|
55
|
-
currency?: string;
|
|
56
|
-
customerEmail?: string;
|
|
57
|
-
metadata: Record<string, string>;
|
|
58
|
-
allowPromotionCodes: boolean;
|
|
59
|
-
successUrl: string;
|
|
60
|
-
cancelUrl: string;
|
|
61
|
-
mode?: 'payment' | 'subscription';
|
|
62
|
-
}) {
|
|
63
|
-
const session = await stripe.checkout.sessions.create({
|
|
64
|
-
payment_method_types: ['card'],
|
|
65
|
-
line_items: [
|
|
66
|
-
params.priceId
|
|
67
|
-
? { price: params.priceId, quantity: 1 }
|
|
68
|
-
: {
|
|
69
|
-
price_data: {
|
|
70
|
-
currency: params.currency || 'eur',
|
|
71
|
-
unit_amount: params.unitAmount!,
|
|
72
|
-
product_data: { name: params.productName! },
|
|
73
|
-
},
|
|
74
|
-
quantity: 1,
|
|
75
|
-
},
|
|
76
|
-
],
|
|
77
|
-
mode: params.mode || 'payment',
|
|
78
|
-
allow_promotion_codes: params.allowPromotionCodes,
|
|
79
|
-
success_url: params.successUrl,
|
|
80
|
-
cancel_url: params.cancelUrl,
|
|
81
|
-
customer_email: params.customerEmail,
|
|
82
|
-
metadata: params.metadata,
|
|
83
|
-
});
|
|
42
|
+
const stripeProvider: StripeSessionProvider = {
|
|
43
|
+
async createStripeSession(params) {
|
|
44
|
+
const session = await stripe.checkout.sessions.create(params);
|
|
84
45
|
return { id: session.id, url: session.url };
|
|
85
46
|
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const authProvider = {
|
|
90
|
-
async getUser(userId: string) {
|
|
91
|
-
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
92
|
-
return { customClaims: user.customClaims };
|
|
47
|
+
async retrieveTaxRate(id) {
|
|
48
|
+
const rate = await stripe.taxRates.retrieve(id);
|
|
49
|
+
return { id: rate.id, inclusive: rate.inclusive };
|
|
93
50
|
},
|
|
94
51
|
};
|
|
95
52
|
|
|
96
|
-
|
|
53
|
+
const { authProvider } = await resolveBillingBackend(options.backend);
|
|
54
|
+
|
|
55
|
+
// userId comes from the verified token, never from the client
|
|
97
56
|
const result = await createCheckoutAlgorithm(
|
|
98
57
|
data,
|
|
58
|
+
context.uid,
|
|
99
59
|
stripeProvider,
|
|
100
60
|
authProvider,
|
|
101
61
|
billingConfig
|
|
@@ -106,24 +66,34 @@ async function createCheckoutSessionLogic(
|
|
|
106
66
|
|
|
107
67
|
/**
|
|
108
68
|
* Vercel API handler for creating checkout sessions
|
|
109
|
-
* Base function handles all common concerns automatically
|
|
110
69
|
*
|
|
111
|
-
* @
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* // api/billing/create-checkout-session.ts
|
|
73
|
+
* import { createCheckoutSession } from '@donotdev/functions/vercel';
|
|
74
|
+
* import { billingConfig } from '../../config/billing';
|
|
75
|
+
*
|
|
76
|
+
* export default createCheckoutSession(billingConfig, { backend: 'supabase' });
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @param billingConfig - App back config
|
|
80
|
+
* @param options - Backend of the app (token verification, rate limiting, user lookup)
|
|
81
|
+
*
|
|
82
|
+
* @version 0.2.0
|
|
112
83
|
* @since 0.0.1
|
|
113
84
|
* @author AMBROISE PARK Consulting
|
|
114
85
|
*/
|
|
115
|
-
|
|
86
|
+
export function createCheckoutSession(
|
|
116
87
|
billingConfig: StripeBackConfig,
|
|
117
|
-
|
|
118
|
-
)
|
|
119
|
-
const schema = customSchema || CreateCheckoutSessionRequestSchema;
|
|
88
|
+
options: VercelBillingOptions
|
|
89
|
+
) {
|
|
120
90
|
return createVercelBaseFunction(
|
|
121
91
|
'POST',
|
|
122
|
-
|
|
92
|
+
CreateCheckoutSessionRequestSchema,
|
|
123
93
|
'create_checkout_session',
|
|
124
|
-
(
|
|
125
|
-
createCheckoutSessionLogic(
|
|
94
|
+
(_req: NextApiRequest, res, data, context) =>
|
|
95
|
+
createCheckoutSessionLogic(res, data, context, billingConfig, options),
|
|
96
|
+
undefined,
|
|
97
|
+
options.backend
|
|
126
98
|
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export default createCheckoutSession;
|
|
99
|
+
}
|
|
@@ -9,16 +9,13 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
12
13
|
import * as v from 'valibot';
|
|
13
14
|
|
|
14
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
15
|
-
|
|
16
15
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
17
|
-
import { stripe, validateStripeEnvironment } from '../../../shared/
|
|
16
|
+
import { stripe, validateStripeEnvironment } from '../../../shared/billing/stripeClient.js';
|
|
18
17
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
19
18
|
|
|
20
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
21
|
-
|
|
22
19
|
const customerPortalSchema = v.object({
|
|
23
20
|
userId: v.string(),
|
|
24
21
|
returnUrl: v.optional(v.pipe(v.string(), v.url())),
|
|
@@ -38,6 +35,7 @@ async function createCustomerPortalLogic(
|
|
|
38
35
|
const { returnUrl } = data;
|
|
39
36
|
|
|
40
37
|
// Get customer ID
|
|
38
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
41
39
|
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
42
40
|
const customerId = user.customClaims?.subscription?.customerId;
|
|
43
41
|
|
|
@@ -9,8 +9,10 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
export
|
|
13
|
-
export
|
|
12
|
+
export { createCheckoutSession } from './create-checkout-session.js';
|
|
13
|
+
export { createStripeWebhook } from './webhook-handler.js';
|
|
14
|
+
export type { VercelBillingOptions } from './backend.js';
|
|
15
|
+
export { default as refreshSubscriptionStatus } from './refresh-subscription-status.js';
|
|
14
16
|
|
|
15
17
|
// Vercel-specific implementations
|
|
16
18
|
// Note: updateUserSubscription is now handled via shared helpers
|
|
@@ -9,17 +9,15 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
|
+
import type Stripe from 'stripe';
|
|
12
14
|
import * as v from 'valibot';
|
|
13
15
|
|
|
14
16
|
import type { RefreshSubscriptionRequest } from '@donotdev/core/server';
|
|
15
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
16
17
|
|
|
17
|
-
import { stripe, validateStripeEnvironment } from '../../../shared/
|
|
18
|
+
import { stripe, validateStripeEnvironment } from '../../../shared/billing/stripeClient.js';
|
|
18
19
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
19
20
|
|
|
20
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
21
|
-
import type Stripe from 'stripe';
|
|
22
|
-
|
|
23
21
|
const refreshSubscriptionStatusSchema = v.object({
|
|
24
22
|
userId: v.pipe(v.string(), v.minLength(1, 'User ID is required')),
|
|
25
23
|
});
|
|
@@ -53,6 +51,7 @@ async function refreshSubscriptionStatusLogic(
|
|
|
53
51
|
const userId = context.uid;
|
|
54
52
|
|
|
55
53
|
// Get user from Firebase
|
|
54
|
+
const { getFirebaseAdminAuth } = await import('@donotdev/firebase/server');
|
|
56
55
|
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
57
56
|
const currentClaims = user.customClaims || {};
|
|
58
57
|
|
|
@@ -94,8 +93,24 @@ async function refreshSubscriptionStatusLogic(
|
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
/**
|
|
97
|
-
* Vercel API handler for refreshing subscription
|
|
98
|
-
*
|
|
96
|
+
* Vercel API handler factory for refreshing the caller's subscription claims
|
|
97
|
+
* from Stripe. Firebase backend only (reads and writes Firebase Auth custom
|
|
98
|
+
* claims); `firebase-admin` is loaded on the first request.
|
|
99
|
+
* Base function handles validation, auth, rate limiting and monitoring.
|
|
100
|
+
*
|
|
101
|
+
* @param customSchema - Optional request body schema (default: `{ userId: string }`)
|
|
102
|
+
* @returns Next.js API route handler
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* import { refreshSubscriptionStatus } from '@donotdev/functions/vercel';
|
|
107
|
+
*
|
|
108
|
+
* export default refreshSubscriptionStatus();
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @version 0.1.0
|
|
112
|
+
* @since 0.0.1
|
|
113
|
+
* @author AMBROISE PARK Consulting
|
|
99
114
|
*/
|
|
100
115
|
const refreshSubscriptionStatus = (
|
|
101
116
|
customSchema?: v.BaseSchema<unknown, any, v.BaseIssue<unknown>>
|