@donotdev/functions 0.1.44 → 0.2.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.
- package/README.md +25 -14
- package/lib/chunks/chunk-2JL3GDTO.js +151 -0
- package/lib/chunks/chunk-2JL3GDTO.js.map +7 -0
- package/lib/chunks/chunk-4TVLH2W2.js +22 -0
- package/lib/chunks/chunk-4TVLH2W2.js.map +7 -0
- package/lib/chunks/chunk-4U7QQWYG.js +22 -0
- package/lib/chunks/chunk-4U7QQWYG.js.map +7 -0
- package/lib/chunks/chunk-5DTPEM64.js +22 -0
- package/lib/chunks/chunk-5DTPEM64.js.map +7 -0
- package/lib/chunks/chunk-6CQXHNVL.js +22 -0
- package/lib/chunks/chunk-6CQXHNVL.js.map +7 -0
- package/lib/chunks/chunk-6DSCPZC6.js +22 -0
- package/lib/chunks/chunk-6DSCPZC6.js.map +7 -0
- package/lib/chunks/chunk-7E6TGWHU.js +22 -0
- package/lib/chunks/chunk-7E6TGWHU.js.map +7 -0
- package/lib/chunks/chunk-7LT32OWE.js +22 -0
- package/lib/chunks/chunk-7LT32OWE.js.map +7 -0
- package/lib/chunks/chunk-7VH2GJXT.js +151 -0
- package/lib/chunks/chunk-7VH2GJXT.js.map +7 -0
- package/lib/chunks/chunk-API5A4BV.js +22 -0
- package/lib/chunks/chunk-API5A4BV.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-BUP7SXPV.js +23 -0
- package/lib/chunks/chunk-BUP7SXPV.js.map +7 -0
- package/lib/chunks/chunk-BXKRI3JJ.js +22 -0
- package/lib/chunks/chunk-BXKRI3JJ.js.map +7 -0
- package/lib/chunks/chunk-FSLGZD7A.js +21 -0
- package/lib/chunks/chunk-FSLGZD7A.js.map +7 -0
- package/lib/chunks/chunk-GVLSPWDD.js +151 -0
- package/lib/chunks/chunk-GVLSPWDD.js.map +7 -0
- package/lib/chunks/chunk-GXQVWEFT.js +22 -0
- package/lib/chunks/chunk-GXQVWEFT.js.map +7 -0
- package/lib/chunks/chunk-HB7EGXVI.js +22 -0
- package/lib/chunks/chunk-HB7EGXVI.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-HM7XCMWW.js +22 -0
- package/lib/chunks/chunk-HM7XCMWW.js.map +7 -0
- package/lib/chunks/chunk-I7IOIL6U.js +151 -0
- package/lib/chunks/chunk-I7IOIL6U.js.map +7 -0
- package/lib/chunks/chunk-IFOYVSPU.js +22 -0
- package/lib/chunks/chunk-IFOYVSPU.js.map +7 -0
- package/lib/chunks/chunk-JTAGPPPL.js +22 -0
- package/lib/chunks/chunk-JTAGPPPL.js.map +7 -0
- package/lib/chunks/chunk-MZ232FUE.js +22 -0
- package/lib/chunks/chunk-MZ232FUE.js.map +7 -0
- package/lib/chunks/chunk-NE5ZTUUZ.js +22 -0
- package/lib/chunks/chunk-NE5ZTUUZ.js.map +7 -0
- package/lib/chunks/chunk-NP2AN2TV.js +22 -0
- package/lib/chunks/chunk-NP2AN2TV.js.map +7 -0
- package/lib/chunks/chunk-QJAU2MWV.js +22 -0
- package/lib/chunks/chunk-QJAU2MWV.js.map +7 -0
- package/lib/chunks/chunk-RM7DV6IQ.js +23 -0
- package/lib/chunks/chunk-RM7DV6IQ.js.map +7 -0
- package/lib/chunks/chunk-RQZGIZHD.js +151 -0
- package/lib/chunks/chunk-RQZGIZHD.js.map +7 -0
- package/lib/chunks/chunk-TBEDQLOB.js +23 -0
- package/lib/chunks/chunk-TBEDQLOB.js.map +7 -0
- package/lib/chunks/chunk-TMHBP3HB.js +22 -0
- package/lib/chunks/chunk-TMHBP3HB.js.map +7 -0
- package/lib/chunks/chunk-UH4L3EF3.js +151 -0
- package/lib/chunks/chunk-UH4L3EF3.js.map +7 -0
- package/lib/chunks/chunk-UJBIU6TL.js +22 -0
- package/lib/chunks/chunk-UJBIU6TL.js.map +7 -0
- package/lib/chunks/chunk-VKEYW3NT.js +22 -0
- package/lib/chunks/chunk-VKEYW3NT.js.map +7 -0
- package/lib/chunks/chunk-XGXBMXJ6.js +22 -0
- package/lib/chunks/chunk-XGXBMXJ6.js.map +7 -0
- package/lib/chunks/chunk-Y44NEQQG.js +22 -0
- package/lib/chunks/chunk-Y44NEQQG.js.map +7 -0
- package/lib/chunks/chunk-ZVU3KPXI.js +22 -0
- package/lib/chunks/chunk-ZVU3KPXI.js.map +7 -0
- package/lib/chunks/monitoring-AGCSU3FI.js +22 -0
- package/lib/chunks/monitoring-AGCSU3FI.js.map +7 -0
- package/lib/chunks/monitoring-PVQMFL34.js +22 -0
- package/lib/chunks/monitoring-PVQMFL34.js.map +7 -0
- package/lib/chunks/monitoring-V7FZXTTW.js +22 -0
- package/lib/chunks/monitoring-V7FZXTTW.js.map +7 -0
- package/lib/chunks/rateLimiter-6KO7AIZP.js +22 -0
- package/lib/chunks/rateLimiter-6KO7AIZP.js.map +7 -0
- package/lib/chunks/rateLimiter-OXVBFW5C.js +22 -0
- package/lib/chunks/rateLimiter-OXVBFW5C.js.map +7 -0
- package/lib/chunks/rateLimiter-U45OMTWR.js +22 -0
- package/lib/chunks/rateLimiter-U45OMTWR.js.map +7 -0
- package/lib/chunks/server-32A5RH4C.js +22 -0
- package/lib/chunks/server-32A5RH4C.js.map +7 -0
- package/lib/chunks/server-7HDAP5DL.js +22 -0
- package/lib/chunks/server-7HDAP5DL.js.map +7 -0
- package/lib/chunks/server-S2IMFOY7.js +22 -0
- package/lib/chunks/server-S2IMFOY7.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/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/createCheckoutSession.d.ts +2 -2
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- 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/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/list.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -9
- package/lib/firebase/index.js +1 -5
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/githubAccess.d.ts +2 -2
- package/lib/firebase/registerCrudFunctions.d.ts +9 -1
- 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/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 +32 -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/supabaseIdempotency.d.ts +32 -0
- package/lib/shared/billing/tierWebhookEvents.d.ts +54 -0
- package/lib/shared/billing/webhookHandler.d.ts +28 -20
- package/lib/shared/index.js +1 -6
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/utils/functionWrapper.d.ts +1 -1
- package/lib/shared/utils/internal/auth.d.ts +38 -1
- package/lib/shared/utils/internal/rateLimiter.d.ts +8 -0
- package/lib/shared/utils/schemaValidation.d.ts +1 -1
- package/lib/shared/utils/vercelHelpers.d.ts +8 -0
- package/lib/shared/utils.d.ts +4 -17
- package/lib/supabase/baseFunction.d.ts +1 -1
- package/lib/supabase/billing/stripeWebhook.d.ts +34 -0
- package/lib/supabase/crud/bulk.d.ts +1 -1
- package/lib/supabase/crud/entityStore.d.ts +71 -0
- package/lib/supabase/crud/index.d.ts +2 -0
- package/lib/supabase/helpers/authProvider.d.ts +5 -5
- package/lib/supabase/index.d.ts +10 -2
- package/lib/supabase/index.js +1 -3
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +0 -9
- 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/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/firebase-admin.d.ts +8 -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/list.d.ts +8 -0
- package/lib/vercel/api/crud/update.d.ts +8 -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/revoke-github-access.d.ts +1 -1
- 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 +1 -2
- package/src/firebase/billing/changePlan.ts +2 -3
- package/src/firebase/billing/createCheckoutSession.ts +30 -56
- package/src/firebase/billing/createCustomerPortal.ts +4 -5
- package/src/firebase/billing/healthCheck.ts +0 -0
- package/src/firebase/billing/index.ts +8 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +2 -3
- package/src/firebase/billing/webhookHandler.ts +20 -4
- 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 -3
- 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 +2 -1
- 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 +407 -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/helpers/index.ts +0 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +7 -9
- package/src/shared/billing/helpers/updateUserSubscription.ts +3 -36
- package/src/shared/billing/idempotency.ts +253 -150
- 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/supabaseIdempotency.ts +228 -0
- package/src/shared/billing/tierWebhookEvents.ts +432 -0
- package/src/shared/billing/webhookHandler.ts +188 -385
- 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/schemaValidation.ts +1 -2
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +14 -31
- 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/createCheckoutSession.ts +10 -36
- package/src/supabase/billing/refreshSubscriptionStatus.ts +1 -2
- 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/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 +3 -4
- package/src/vercel/api/billing/create-checkout-session.ts +42 -68
- package/src/vercel/api/billing/customer-portal.ts +2 -4
- package/src/vercel/api/billing/index.ts +4 -2
- package/src/vercel/api/billing/refresh-subscription-status.ts +21 -6
- package/src/vercel/api/billing/webhook-handler.ts +31 -25
- 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
|
@@ -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
20
|
import { stripe, validateStripeEnvironment } from '../../../shared/utils.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,21 +2,26 @@
|
|
|
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';
|
|
24
|
+
import type { StripeSessionProvider } from '../../../shared/billing/createCheckout.js';
|
|
20
25
|
import {
|
|
21
26
|
initStripe,
|
|
22
27
|
stripe,
|
|
@@ -24,78 +29,37 @@ import {
|
|
|
24
29
|
} from '../../../shared/utils.js';
|
|
25
30
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
26
31
|
|
|
27
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
28
|
-
import type * as v from 'valibot';
|
|
29
|
-
|
|
30
32
|
/**
|
|
31
33
|
* Business logic for creating checkout sessions
|
|
32
34
|
* 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
35
|
*/
|
|
38
36
|
async function createCheckoutSessionLogic(
|
|
39
|
-
req: NextApiRequest,
|
|
40
37
|
res: NextApiResponse,
|
|
41
38
|
data: CreateCheckoutSessionRequest,
|
|
42
39
|
context: { uid: string },
|
|
43
|
-
billingConfig: StripeBackConfig
|
|
40
|
+
billingConfig: StripeBackConfig,
|
|
41
|
+
options: VercelBillingOptions
|
|
44
42
|
) {
|
|
45
|
-
// Initialize Stripe with env var
|
|
46
43
|
initStripe(process.env.STRIPE_SECRET_KEY || '');
|
|
47
44
|
validateStripeEnvironment();
|
|
48
45
|
|
|
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
|
-
});
|
|
46
|
+
const stripeProvider: StripeSessionProvider = {
|
|
47
|
+
async createStripeSession(params) {
|
|
48
|
+
const session = await stripe.checkout.sessions.create(params);
|
|
84
49
|
return { id: session.id, url: session.url };
|
|
85
50
|
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const authProvider = {
|
|
90
|
-
async getUser(userId: string) {
|
|
91
|
-
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
92
|
-
return { customClaims: user.customClaims };
|
|
51
|
+
async retrieveTaxRate(id) {
|
|
52
|
+
const rate = await stripe.taxRates.retrieve(id);
|
|
53
|
+
return { id: rate.id, inclusive: rate.inclusive };
|
|
93
54
|
},
|
|
94
55
|
};
|
|
95
56
|
|
|
96
|
-
|
|
57
|
+
const { authProvider } = await resolveBillingBackend(options.backend);
|
|
58
|
+
|
|
59
|
+
// userId comes from the verified token, never from the client
|
|
97
60
|
const result = await createCheckoutAlgorithm(
|
|
98
61
|
data,
|
|
62
|
+
context.uid,
|
|
99
63
|
stripeProvider,
|
|
100
64
|
authProvider,
|
|
101
65
|
billingConfig
|
|
@@ -106,24 +70,34 @@ async function createCheckoutSessionLogic(
|
|
|
106
70
|
|
|
107
71
|
/**
|
|
108
72
|
* Vercel API handler for creating checkout sessions
|
|
109
|
-
* Base function handles all common concerns automatically
|
|
110
73
|
*
|
|
111
|
-
* @
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // api/billing/create-checkout-session.ts
|
|
77
|
+
* import { createCheckoutSession } from '@donotdev/functions/vercel';
|
|
78
|
+
* import { billingConfig } from '../../config/billing';
|
|
79
|
+
*
|
|
80
|
+
* export default createCheckoutSession(billingConfig, { backend: 'supabase' });
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @param billingConfig - App back config
|
|
84
|
+
* @param options - Backend of the app (token verification, rate limiting, user lookup)
|
|
85
|
+
*
|
|
86
|
+
* @version 0.2.0
|
|
112
87
|
* @since 0.0.1
|
|
113
88
|
* @author AMBROISE PARK Consulting
|
|
114
89
|
*/
|
|
115
|
-
|
|
90
|
+
export function createCheckoutSession(
|
|
116
91
|
billingConfig: StripeBackConfig,
|
|
117
|
-
|
|
118
|
-
)
|
|
119
|
-
const schema = customSchema || CreateCheckoutSessionRequestSchema;
|
|
92
|
+
options: VercelBillingOptions
|
|
93
|
+
) {
|
|
120
94
|
return createVercelBaseFunction(
|
|
121
95
|
'POST',
|
|
122
|
-
|
|
96
|
+
CreateCheckoutSessionRequestSchema,
|
|
123
97
|
'create_checkout_session',
|
|
124
|
-
(
|
|
125
|
-
createCheckoutSessionLogic(
|
|
98
|
+
(_req: NextApiRequest, res, data, context) =>
|
|
99
|
+
createCheckoutSessionLogic(res, data, context, billingConfig, options),
|
|
100
|
+
undefined,
|
|
101
|
+
options.backend
|
|
126
102
|
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export default createCheckoutSession;
|
|
103
|
+
}
|
|
@@ -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
16
|
import { stripe, validateStripeEnvironment } from '../../../shared/utils.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
18
|
import { stripe, validateStripeEnvironment } from '../../../shared/utils.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>>
|
|
@@ -4,30 +4,45 @@
|
|
|
4
4
|
* @fileoverview Stripe webhook handler API
|
|
5
5
|
* @description Vercel API route for handling Stripe webhook events
|
|
6
6
|
*
|
|
7
|
-
* @version 0.
|
|
7
|
+
* @version 0.2.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
13
|
|
|
14
14
|
import type { StripeBackConfig } from '@donotdev/core/server';
|
|
15
|
-
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
16
15
|
|
|
16
|
+
import { resolveBillingBackend } from './backend.js';
|
|
17
|
+
import type { VercelBillingOptions } from './backend.js';
|
|
17
18
|
import { updateUserSubscription } from '../../../shared/billing/helpers/updateUserSubscription.js';
|
|
18
19
|
import { processWebhook } from '../../../shared/billing/webhookHandler.js';
|
|
20
|
+
import { logger } from '../../../shared/logger.js';
|
|
19
21
|
import { stripe } from '../../../shared/utils.js';
|
|
20
22
|
|
|
21
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
22
|
-
|
|
23
23
|
/**
|
|
24
|
-
* Create Vercel webhook handler from billing config
|
|
24
|
+
* Create Vercel webhook handler from billing config.
|
|
25
|
+
* Idempotency storage and user claims follow `options.backend`.
|
|
26
|
+
* Any failure answers non-2xx so Stripe retries the event.
|
|
25
27
|
*
|
|
26
|
-
* @
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // api/billing/stripe-webhook.ts
|
|
31
|
+
* import { createStripeWebhook } from '@donotdev/functions/vercel';
|
|
32
|
+
* import { billingConfig } from '../../config/billing';
|
|
33
|
+
*
|
|
34
|
+
* export const config = { api: { bodyParser: false } };
|
|
35
|
+
* export default createStripeWebhook(billingConfig, { backend: 'supabase' });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @version 0.2.0
|
|
27
39
|
* @since 0.0.1
|
|
28
40
|
* @author AMBROISE PARK Consulting
|
|
29
41
|
*/
|
|
30
|
-
export function createStripeWebhook(
|
|
42
|
+
export function createStripeWebhook(
|
|
43
|
+
billingConfig: StripeBackConfig,
|
|
44
|
+
options: VercelBillingOptions
|
|
45
|
+
) {
|
|
31
46
|
return async (req: NextApiRequest, res: NextApiResponse) => {
|
|
32
47
|
if (req.method !== 'POST') {
|
|
33
48
|
res.status(405).json({ error: 'Method not allowed' });
|
|
@@ -55,20 +70,9 @@ export function createStripeWebhook(billingConfig: StripeBackConfig) {
|
|
|
55
70
|
// Get raw body
|
|
56
71
|
const rawBody = await getRawBody(req);
|
|
57
72
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const authProvider = {
|
|
62
|
-
async getUser(userId: string) {
|
|
63
|
-
return getFirebaseAdminAuth().getUser(userId);
|
|
64
|
-
},
|
|
65
|
-
async setCustomUserClaims(
|
|
66
|
-
userId: string,
|
|
67
|
-
claims: Record<string, unknown>
|
|
68
|
-
) {
|
|
69
|
-
await getFirebaseAdminAuth().setCustomUserClaims(userId, claims);
|
|
70
|
-
},
|
|
71
|
-
};
|
|
73
|
+
const { authProvider, idempotencyStore } = await resolveBillingBackend(
|
|
74
|
+
options.backend
|
|
75
|
+
);
|
|
72
76
|
|
|
73
77
|
// Call shared algorithm
|
|
74
78
|
await processWebhook(
|
|
@@ -78,13 +82,15 @@ export function createStripeWebhook(billingConfig: StripeBackConfig) {
|
|
|
78
82
|
stripe,
|
|
79
83
|
billingConfig,
|
|
80
84
|
updateUserSubscription,
|
|
81
|
-
authProvider
|
|
85
|
+
authProvider,
|
|
86
|
+
idempotencyStore
|
|
82
87
|
);
|
|
83
88
|
|
|
84
89
|
res.status(200).json({ received: true });
|
|
85
90
|
} catch (error) {
|
|
86
|
-
logger.error('Vercel
|
|
87
|
-
error: error instanceof Error ? error
|
|
91
|
+
logger.error('[Vercel Webhook] Error', {
|
|
92
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
93
|
+
operation: 'webhook_processing',
|
|
88
94
|
});
|
|
89
95
|
res.status(400).json({
|
|
90
96
|
error: 'Webhook processing failed',
|
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { getFirebaseAdminApp } from '@donotdev/firebase/server';
|
|
13
|
-
|
|
14
12
|
import type { App } from 'firebase-admin/app';
|
|
15
13
|
|
|
16
14
|
/**
|
|
@@ -22,6 +20,7 @@ import type { App } from 'firebase-admin/app';
|
|
|
22
20
|
* @author AMBROISE PARK Consulting
|
|
23
21
|
*/
|
|
24
22
|
export async function initializeFirebaseAdmin(): Promise<App> {
|
|
23
|
+
const { getFirebaseAdminApp } = await import('@donotdev/firebase/server');
|
|
25
24
|
return await getFirebaseAdminApp();
|
|
26
25
|
}
|
|
27
26
|
|
|
@@ -34,5 +33,6 @@ export async function initializeFirebaseAdmin(): Promise<App> {
|
|
|
34
33
|
* @author AMBROISE PARK Consulting
|
|
35
34
|
*/
|
|
36
35
|
export async function getFirebaseAdmin(): Promise<App> {
|
|
36
|
+
const { getFirebaseAdminApp } = await import('@donotdev/firebase/server');
|
|
37
37
|
return await getFirebaseAdminApp();
|
|
38
38
|
}
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* @author AMBROISE PARK Consulting
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
21
22
|
import * as v from 'valibot';
|
|
22
23
|
|
|
23
24
|
import {
|
|
@@ -26,20 +27,14 @@ import {
|
|
|
26
27
|
BulkRequestSchema,
|
|
27
28
|
} from '@donotdev/core/server';
|
|
28
29
|
import type { BulkRequest, BulkResponse } from '@donotdev/core/server';
|
|
29
|
-
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
30
30
|
|
|
31
|
+
import { executeBulk } from '../../../shared/crud/bulkCore.js';
|
|
31
32
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
executeBulk,
|
|
35
|
-
prepareForFirestore,
|
|
36
|
-
updateMetadata,
|
|
37
|
-
} from '../../../shared/index.js';
|
|
33
|
+
import { prepareForFirestore } from '../../../shared/firebase.js';
|
|
34
|
+
import { createMetadata, updateMetadata } from '../../../shared/metadata.js';
|
|
38
35
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
39
36
|
import { validateCollectionName } from '../../../shared/utils.js';
|
|
40
37
|
|
|
41
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
42
|
-
|
|
43
38
|
/**
|
|
44
39
|
* Pass-through row schema — the bulk endpoint is URL-routed and does not
|
|
45
40
|
* carry entity schemas on the wire, so per-row validation reduces to the
|
|
@@ -114,6 +109,8 @@ export default async function handler(
|
|
|
114
109
|
}
|
|
115
110
|
const ops: BulkRequest = parseResult.output;
|
|
116
111
|
|
|
112
|
+
const { getFirebaseAdminFirestore } =
|
|
113
|
+
await import('@donotdev/firebase/server');
|
|
117
114
|
const response: BulkResponse = await executeBulk({
|
|
118
115
|
collection: collection as string,
|
|
119
116
|
ops,
|
|
@@ -9,23 +9,22 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
|
+
|
|
12
14
|
import type { CreateEntityData } from '@donotdev/core/server';
|
|
13
|
-
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
14
15
|
|
|
15
16
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
16
17
|
import {
|
|
17
18
|
prepareForFirestore,
|
|
18
19
|
transformFirestoreData,
|
|
19
|
-
} from '../../../shared/
|
|
20
|
-
import { createMetadata } from '../../../shared/
|
|
20
|
+
} from '../../../shared/firebase.js';
|
|
21
|
+
import { createMetadata } from '../../../shared/metadata.js';
|
|
21
22
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
22
23
|
import {
|
|
23
24
|
validateCollectionName,
|
|
24
25
|
validateDocument,
|
|
25
26
|
} from '../../../shared/utils.js';
|
|
26
27
|
|
|
27
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
28
|
-
|
|
29
28
|
export default async function handler(
|
|
30
29
|
req: NextApiRequest,
|
|
31
30
|
res: NextApiResponse
|
|
@@ -68,6 +67,8 @@ export default async function handler(
|
|
|
68
67
|
};
|
|
69
68
|
|
|
70
69
|
// Save the document to Firestore
|
|
70
|
+
const { getFirebaseAdminFirestore } =
|
|
71
|
+
await import('@donotdev/firebase/server');
|
|
71
72
|
const db = getFirebaseAdminFirestore();
|
|
72
73
|
const docRef = await db
|
|
73
74
|
.collection(schema.metadata.collection)
|
|
@@ -9,15 +9,14 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
|
+
|
|
12
14
|
import type { GetEntityData } from '@donotdev/core/server';
|
|
13
|
-
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
14
15
|
|
|
15
16
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
16
17
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
17
18
|
import { validateCollectionName } from '../../../shared/utils.js';
|
|
18
19
|
|
|
19
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
20
|
-
|
|
21
20
|
export default async function handler(
|
|
22
21
|
req: NextApiRequest,
|
|
23
22
|
res: NextApiResponse
|
|
@@ -40,6 +39,8 @@ export default async function handler(
|
|
|
40
39
|
validateCollectionName(schema.metadata.collection);
|
|
41
40
|
|
|
42
41
|
// Check if document exists
|
|
42
|
+
const { getFirebaseAdminFirestore } =
|
|
43
|
+
await import('@donotdev/firebase/server');
|
|
43
44
|
const db = getFirebaseAdminFirestore();
|
|
44
45
|
const doc = await db.collection(schema.metadata.collection).doc(id).get();
|
|
45
46
|
|
|
@@ -9,18 +9,17 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
13
|
+
|
|
12
14
|
import { HIDDEN_STATUSES } from '@donotdev/core/server';
|
|
13
15
|
import type { GetEntityData } from '@donotdev/core/server';
|
|
14
|
-
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
15
16
|
|
|
16
17
|
import { handleError } from '../../../shared/errorHandling.js';
|
|
17
|
-
import { transformFirestoreData } from '../../../shared/
|
|
18
|
-
import { filterVisibleFields } from '../../../shared/
|
|
18
|
+
import { transformFirestoreData } from '../../../shared/firebase.js';
|
|
19
|
+
import { filterVisibleFields } from '../../../shared/schema.js';
|
|
19
20
|
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
20
21
|
import { validateCollectionName } from '../../../shared/utils.js';
|
|
21
22
|
|
|
22
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
23
|
-
|
|
24
23
|
export default async function handler(
|
|
25
24
|
req: NextApiRequest,
|
|
26
25
|
res: NextApiResponse
|
|
@@ -48,6 +47,8 @@ export default async function handler(
|
|
|
48
47
|
validateCollectionName(schema.metadata.collection);
|
|
49
48
|
|
|
50
49
|
// Get the document from Firestore
|
|
50
|
+
const { getFirebaseAdminFirestore } =
|
|
51
|
+
await import('@donotdev/firebase/server');
|
|
51
52
|
const db = getFirebaseAdminFirestore();
|
|
52
53
|
const doc = await db
|
|
53
54
|
.collection(schema.metadata.collection)
|