@donotdev/functions 0.1.1 → 0.1.42
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/LICENSE.md +21 -242
- package/README.md +4 -4
- package/guidelines/FUNCTIONS.md +0 -0
- package/guidelines/VERCEL.md +27 -1
- package/lib/firebase/auth/getCustomClaims.d.ts +0 -0
- package/lib/firebase/auth/getUserAuthStatus.d.ts +0 -0
- package/lib/firebase/auth/index.d.ts +0 -0
- package/lib/firebase/auth/removeCustomClaims.d.ts +0 -0
- package/lib/firebase/auth/setCustomClaims.d.ts +0 -0
- package/lib/firebase/baseFunction.d.ts +0 -0
- 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 +0 -0
- package/lib/firebase/billing/createCustomerPortal.d.ts +0 -0
- package/lib/firebase/billing/healthCheck.d.ts +0 -0
- package/lib/firebase/billing/index.d.ts +0 -0
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +0 -0
- package/lib/firebase/billing/webhookHandler.d.ts +0 -0
- 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 -0
- package/lib/firebase/crud/bulk.d.ts +107 -0
- package/lib/firebase/crud/create.d.ts +1 -1
- package/lib/firebase/crud/delete.d.ts +0 -0
- package/lib/firebase/crud/get.d.ts +0 -0
- package/lib/firebase/crud/index.d.ts +1 -0
- package/lib/firebase/crud/list.d.ts +0 -0
- package/lib/firebase/crud/update.d.ts +0 -0
- 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 +3 -3
- 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 +0 -0
- package/lib/firebase/oauth/index.d.ts +0 -0
- package/lib/firebase/oauth/refreshToken.d.ts +0 -0
- package/lib/firebase/registerCrudFunctions.d.ts +0 -0
- 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/createAIHandler.d.ts +39 -0
- package/lib/shared/ai/createAIRoute.d.ts +0 -0
- package/lib/shared/ai/index.d.ts +0 -0
- package/lib/shared/ai/providers/anthropic.d.ts +12 -0
- package/lib/shared/ai/providers/google.d.ts +10 -0
- package/lib/shared/ai/providers/index.d.ts +23 -0
- package/lib/shared/ai/providers/openai.d.ts +10 -0
- package/lib/shared/ai/resolveModelTier.d.ts +0 -0
- package/lib/shared/ai/utils/parseSSE.d.ts +22 -0
- package/lib/shared/billing/createCheckout.d.ts +0 -0
- package/lib/shared/billing/helpers/index.d.ts +0 -0
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +0 -0
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +0 -0
- package/lib/shared/billing/idempotency.d.ts +0 -0
- package/lib/shared/billing/index.d.ts +0 -0
- package/lib/shared/billing/webhookHandler.d.ts +0 -0
- package/lib/shared/crud/bulkCore.d.ts +211 -0
- package/lib/shared/crud/index.d.ts +13 -0
- 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 +1 -0
- package/lib/shared/index.js +4 -4
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +0 -0
- package/lib/shared/metadata.d.ts +6 -6
- 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 +0 -0
- package/lib/shared/utils/index.d.ts +0 -0
- package/lib/shared/utils/internal/auth.d.ts +0 -0
- 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 +0 -0
- package/lib/shared/utils/internal/validation.d.ts +0 -0
- package/lib/shared/utils/schemaValidation.d.ts +0 -0
- package/lib/shared/utils/validation.d.ts +0 -0
- package/lib/shared/utils/vercelHelpers.d.ts +0 -0
- package/lib/shared/utils.d.ts +0 -0
- 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 +0 -0
- 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/crud/aggregate.d.ts +0 -0
- package/lib/supabase/crud/bulk.d.ts +63 -0
- package/lib/supabase/crud/create.d.ts +1 -1
- package/lib/supabase/crud/delete.d.ts +0 -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 -0
- 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 +0 -0
- package/lib/supabase/index.d.ts +3 -3
- package/lib/supabase/index.js +3 -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 +0 -0
- package/lib/vercel/api/ai/chat.d.ts +0 -0
- package/lib/vercel/api/ai/index.d.ts +0 -0
- package/lib/vercel/api/auth/get-custom-claims.d.ts +0 -0
- package/lib/vercel/api/auth/get-user-auth-status.d.ts +0 -0
- package/lib/vercel/api/auth/index.d.ts +0 -0
- package/lib/vercel/api/auth/remove-custom-claims.d.ts +0 -0
- package/lib/vercel/api/auth/set-custom-claims.d.ts +0 -0
- package/lib/vercel/api/billing/cancel.d.ts +0 -0
- package/lib/vercel/api/billing/change-plan.d.ts +0 -0
- package/lib/vercel/api/billing/create-checkout-session.d.ts +0 -0
- package/lib/vercel/api/billing/customer-portal.d.ts +0 -0
- package/lib/vercel/api/billing/index.d.ts +0 -0
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +0 -0
- package/lib/vercel/api/billing/webhook-handler.d.ts +0 -0
- package/lib/vercel/api/config/constants.d.ts +0 -0
- package/lib/vercel/api/config/firebase-admin.d.ts +0 -0
- package/lib/vercel/api/config/index.d.ts +0 -0
- package/lib/vercel/api/crud/bulk.d.ts +31 -0
- package/lib/vercel/api/crud/create.d.ts +0 -0
- package/lib/vercel/api/crud/delete.d.ts +0 -0
- package/lib/vercel/api/crud/get.d.ts +0 -0
- package/lib/vercel/api/crud/index.d.ts +1 -0
- package/lib/vercel/api/crud/list.d.ts +0 -0
- package/lib/vercel/api/crud/update.d.ts +0 -0
- package/lib/vercel/api/index.d.ts +0 -0
- package/lib/vercel/api/index.js +3 -3
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +0 -0
- package/lib/vercel/api/oauth/exchange-token.d.ts +0 -0
- package/lib/vercel/api/oauth/grant-github-access.d.ts +0 -0
- package/lib/vercel/api/oauth/index.d.ts +0 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +0 -0
- package/lib/vercel/api/utils/cors.d.ts +0 -0
- package/lib/vercel/baseFunction.d.ts +0 -0
- package/package.json +6 -6
- package/src/firebase/auth/getCustomClaims.ts +0 -0
- package/src/firebase/auth/getUserAuthStatus.ts +0 -0
- package/src/firebase/auth/index.ts +0 -0
- package/src/firebase/auth/removeCustomClaims.ts +0 -0
- package/src/firebase/auth/setCustomClaims.ts +0 -0
- package/src/firebase/baseFunction.ts +0 -0
- package/src/firebase/billing/cancelSubscription.ts +0 -0
- package/src/firebase/billing/changePlan.ts +0 -0
- package/src/firebase/billing/createCheckoutSession.ts +5 -1
- package/src/firebase/billing/createCustomerPortal.ts +0 -0
- package/src/firebase/billing/healthCheck.ts +0 -0
- package/src/firebase/billing/index.d.ts +0 -0
- package/src/firebase/billing/index.ts +0 -0
- package/src/firebase/billing/refreshSubscriptionStatus.ts +0 -0
- package/src/firebase/billing/webhookHandler.ts +0 -0
- package/src/firebase/config/constants.ts +0 -0
- package/src/firebase/config/index.ts +0 -0
- package/src/firebase/config/secrets.ts +0 -0
- package/src/firebase/crud/__tests__/bulk.test.ts +322 -0
- package/src/firebase/crud/aggregate.ts +0 -0
- package/src/firebase/crud/bulk.ts +272 -0
- package/src/firebase/crud/create.ts +20 -33
- package/src/firebase/crud/delete.ts +0 -0
- package/src/firebase/crud/get.ts +0 -0
- package/src/firebase/crud/index.ts +1 -0
- package/src/firebase/crud/list.ts +0 -0
- package/src/firebase/crud/update.ts +0 -0
- package/src/firebase/helpers/githubAccessHelper.ts +0 -0
- package/src/firebase/helpers/githubTeamAccessHelper.ts +0 -0
- package/src/firebase/helpers/index.ts +0 -0
- package/src/firebase/index.d.ts +0 -0
- package/src/firebase/index.ts +0 -0
- package/src/firebase/oauth/disconnect.ts +0 -0
- package/src/firebase/oauth/exchangeToken.ts +0 -0
- package/src/firebase/oauth/getConnections.ts +0 -0
- package/src/firebase/oauth/githubAccess.ts +0 -0
- package/src/firebase/oauth/index.ts +0 -0
- package/src/firebase/oauth/refreshToken.ts +0 -0
- package/src/firebase/registerCrudFunctions.ts +13 -0
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +0 -0
- package/src/firebase/scheduled/index.ts +0 -0
- package/src/shared/__tests__/auth.test.ts +0 -0
- package/src/shared/__tests__/bulkCore.test.ts +487 -0
- package/src/shared/__tests__/errorHandling.test.ts +0 -0
- package/src/shared/__tests__/firebase.test.ts +9 -3
- package/src/shared/__tests__/idempotency.test.ts +0 -0
- package/src/shared/__tests__/internalValidation.test.ts +9 -3
- package/src/shared/__tests__/metadata.test.ts +10 -10
- package/src/shared/__tests__/rateLimiter.test.ts +0 -0
- package/src/shared/__tests__/schema.test.ts +0 -0
- package/src/shared/__tests__/schemaValidation.test.ts +0 -0
- package/src/shared/__tests__/subscription.test.ts +0 -0
- package/src/shared/__tests__/utils.test.ts +10 -4
- package/src/shared/__tests__/validation-env.test.ts +9 -3
- package/src/shared/__tests__/validation.test.ts +0 -0
- package/src/shared/ai/__tests__/costTracker.test.ts +25 -13
- package/src/shared/ai/classifyAIError.ts +26 -11
- package/src/shared/ai/costTracker.ts +0 -0
- package/src/shared/ai/createAIRoute.ts +11 -8
- package/src/shared/ai/index.ts +0 -0
- package/src/shared/ai/resolveModelTier.ts +5 -2
- package/src/shared/billing/__tests__/createCheckout.test.ts +0 -0
- package/src/shared/billing/__tests__/webhookHandler.test.ts +0 -0
- package/src/shared/billing/createCheckout.ts +24 -9
- package/src/shared/billing/helpers/index.ts +0 -0
- package/src/shared/billing/helpers/subscriptionManagement.ts +0 -0
- package/src/shared/billing/helpers/updateUserSubscription.ts +0 -0
- package/src/shared/billing/idempotency.ts +0 -0
- package/src/shared/billing/index.ts +0 -0
- package/src/shared/billing/webhookHandler.ts +0 -0
- package/src/shared/crud/bulkCore.ts +483 -0
- package/src/shared/crud/index.ts +23 -0
- package/src/shared/email/createEmailRoute.ts +0 -0
- package/src/shared/email/index.ts +0 -0
- package/src/shared/email/provider.ts +0 -0
- package/src/shared/email/resendProvider.ts +1 -1
- package/src/shared/errorHandling.ts +0 -0
- package/src/shared/firebase.ts +0 -0
- package/src/shared/github.ts +0 -0
- package/src/shared/index.d.ts +9 -0
- package/src/shared/index.ts +1 -0
- package/src/shared/logger.ts +0 -0
- package/src/shared/metadata.ts +6 -6
- package/src/shared/oauth/__tests__/exchangeToken.test.ts +0 -0
- package/src/shared/oauth/__tests__/grantAccess.test.ts +0 -0
- package/src/shared/oauth/exchangeToken.ts +0 -0
- package/src/shared/oauth/grantAccess.ts +0 -0
- package/src/shared/oauth/index.ts +0 -0
- package/src/shared/schema.ts +0 -0
- package/src/shared/utils/__tests__/functionWrapper.test.ts +0 -0
- package/src/shared/utils/detectFirestore.ts +0 -0
- package/src/shared/utils/external/date.ts +0 -0
- package/src/shared/utils/external/index.ts +0 -0
- package/src/shared/utils/external/references.ts +0 -0
- package/src/shared/utils/external/subscription.ts +0 -0
- package/src/shared/utils/firebaseHelpers.ts +0 -0
- package/src/shared/utils/functionWrapper.ts +0 -0
- package/src/shared/utils/index.ts +0 -0
- package/src/shared/utils/internal/auth.ts +0 -0
- package/src/shared/utils/internal/firebase.ts +0 -0
- package/src/shared/utils/internal/idempotency.ts +0 -0
- package/src/shared/utils/internal/index.ts +0 -0
- package/src/shared/utils/internal/monitoring.ts +0 -0
- package/src/shared/utils/internal/rateLimiter.ts +0 -0
- package/src/shared/utils/internal/validation.ts +0 -0
- package/src/shared/utils/schemaValidation.ts +0 -0
- package/src/shared/utils/validation.ts +0 -0
- package/src/shared/utils/vercelHelpers.ts +0 -0
- package/src/shared/utils.ts +0 -0
- package/src/shared/validation.ts +0 -0
- package/src/supabase/__tests__/bulk.test.ts +330 -0
- package/src/supabase/ai/createAIFunction.ts +17 -16
- package/src/supabase/auth/deleteAccount.ts +0 -0
- package/src/supabase/auth/getCustomClaims.ts +0 -0
- package/src/supabase/auth/getUserAuthStatus.ts +0 -0
- package/src/supabase/auth/removeCustomClaims.ts +0 -0
- package/src/supabase/auth/setCustomClaims.ts +1 -3
- package/src/supabase/baseFunction.ts +0 -0
- package/src/supabase/billing/cancelSubscription.ts +0 -0
- package/src/supabase/billing/changePlan.ts +1 -1
- package/src/supabase/billing/createCheckoutSession.ts +0 -0
- package/src/supabase/billing/createCustomerPortal.ts +0 -0
- package/src/supabase/billing/refreshSubscriptionStatus.ts +0 -0
- package/src/supabase/crud/aggregate.ts +0 -0
- package/src/supabase/crud/bulk.ts +244 -0
- package/src/supabase/crud/create.ts +15 -14
- package/src/supabase/crud/delete.ts +0 -0
- package/src/supabase/crud/get.ts +0 -0
- package/src/supabase/crud/index.ts +2 -0
- package/src/supabase/crud/list.ts +0 -0
- package/src/supabase/crud/update.ts +0 -0
- package/src/supabase/edgeFunction.ts +4 -3
- package/src/supabase/email/createEmailFunction.ts +9 -12
- package/src/supabase/helpers/authProvider.ts +0 -0
- package/src/supabase/index.ts +12 -3
- package/src/supabase/oauth/disconnect.ts +1 -2
- package/src/supabase/oauth/exchangeToken.ts +1 -3
- package/src/supabase/oauth/getConnections.ts +1 -4
- package/src/supabase/oauth/index.ts +0 -0
- package/src/supabase/oauth/refreshToken.ts +2 -7
- package/src/supabase/registerCrudFunctions.ts +38 -26
- package/src/supabase/utils/idempotency.ts +0 -0
- package/src/supabase/utils/monitoring.ts +0 -0
- package/src/supabase/utils/rateLimiter.ts +0 -0
- package/src/vercel/__tests__/bulk.test.ts +301 -0
- package/src/vercel/api/ai/chat.ts +0 -0
- package/src/vercel/api/ai/index.ts +2 -0
- package/src/vercel/api/auth/get-custom-claims.ts +0 -0
- package/src/vercel/api/auth/get-user-auth-status.ts +0 -0
- package/src/vercel/api/auth/index.ts +0 -0
- package/src/vercel/api/auth/remove-custom-claims.ts +0 -0
- package/src/vercel/api/auth/set-custom-claims.ts +0 -0
- package/src/vercel/api/billing/cancel.ts +0 -0
- package/src/vercel/api/billing/change-plan.ts +0 -0
- package/src/vercel/api/billing/create-checkout-session.ts +5 -1
- package/src/vercel/api/billing/customer-portal.ts +0 -0
- package/src/vercel/api/billing/index.ts +0 -0
- package/src/vercel/api/billing/refresh-subscription-status.ts +0 -0
- package/src/vercel/api/billing/webhook-handler.ts +0 -0
- package/src/vercel/api/config/constants.ts +0 -0
- package/src/vercel/api/config/firebase-admin.ts +0 -0
- package/src/vercel/api/config/index.ts +0 -0
- package/src/vercel/api/crud/bulk.ts +216 -0
- package/src/vercel/api/crud/create.ts +10 -9
- package/src/vercel/api/crud/delete.ts +0 -0
- package/src/vercel/api/crud/get.ts +0 -0
- package/src/vercel/api/crud/index.ts +1 -0
- package/src/vercel/api/crud/list.ts +0 -0
- package/src/vercel/api/crud/update.ts +0 -0
- package/src/vercel/api/index.d.ts +0 -0
- package/src/vercel/api/index.ts +0 -0
- package/src/vercel/api/oauth/check-github-access.ts +0 -0
- package/src/vercel/api/oauth/exchange-token.ts +4 -1
- package/src/vercel/api/oauth/grant-github-access.ts +0 -0
- package/src/vercel/api/oauth/index.ts +0 -0
- package/src/vercel/api/oauth/revoke-github-access.ts +0 -0
- package/src/vercel/api/utils/cors.ts +3 -1
- package/src/vercel/baseFunction.ts +0 -0
|
File without changes
|
|
@@ -17,7 +17,11 @@ import type {
|
|
|
17
17
|
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
18
18
|
|
|
19
19
|
import { createCheckoutAlgorithm } from '../../../shared/billing/createCheckout.js';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
initStripe,
|
|
22
|
+
stripe,
|
|
23
|
+
validateStripeEnvironment,
|
|
24
|
+
} from '../../../shared/utils.js';
|
|
21
25
|
import { createVercelBaseFunction } from '../../baseFunction.js';
|
|
22
26
|
|
|
23
27
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// packages/functions/src/vercel/api/crud/bulk.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Vercel bulk / transactional CRUD API handler.
|
|
5
|
+
* @description Vercel Pages-Router API route for `POST /api/crud/:collection/bulk`.
|
|
6
|
+
* Thin HTTP adapter on top of the shared `executeBulk` orchestrator —
|
|
7
|
+
* assembles a firebase-admin `runTransaction` as the target `transact`
|
|
8
|
+
* callback.
|
|
9
|
+
*
|
|
10
|
+
* Unlike the Firebase Callable and Supabase Edge variants, this endpoint
|
|
11
|
+
* does not carry entity schemas on the wire (the REST-CRUD client hits
|
|
12
|
+
* `${baseUrl}/${collection}/bulk` with raw ops only). The wire contract
|
|
13
|
+
* is still frozen: structural parsing via `BulkRequestSchema`, collision
|
|
14
|
+
* rejection, empty no-op, id preservation in input order.
|
|
15
|
+
*
|
|
16
|
+
* @version 0.2.0
|
|
17
|
+
* @since 0.2.0
|
|
18
|
+
* @author AMBROISE PARK Consulting
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import * as v from 'valibot';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
BulkCollisionError,
|
|
25
|
+
BulkInsertSchema,
|
|
26
|
+
BulkRequestSchema,
|
|
27
|
+
} from '@donotdev/core/server';
|
|
28
|
+
import type { BulkRequest, BulkResponse } from '@donotdev/core/server';
|
|
29
|
+
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
30
|
+
|
|
31
|
+
import { handleError } from '../../../shared/errorHandling.js';
|
|
32
|
+
import {
|
|
33
|
+
createMetadata,
|
|
34
|
+
executeBulk,
|
|
35
|
+
prepareForFirestore,
|
|
36
|
+
updateMetadata,
|
|
37
|
+
} from '../../../shared/index.js';
|
|
38
|
+
import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
39
|
+
import { validateCollectionName } from '../../../shared/utils.js';
|
|
40
|
+
|
|
41
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Pass-through row schema — the bulk endpoint is URL-routed and does not
|
|
45
|
+
* carry entity schemas on the wire, so per-row validation reduces to the
|
|
46
|
+
* structural checks already enforced by {@link BulkRequestSchema}. Both
|
|
47
|
+
* inserts and update patches are opaque objects at this layer.
|
|
48
|
+
*/
|
|
49
|
+
const PASSTHROUGH_INSERT_SCHEMA = BulkInsertSchema;
|
|
50
|
+
const PASSTHROUGH_PATCH_SCHEMA = v.record(v.string(), v.unknown());
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* HTTP handler for `POST /api/crud/:collection/bulk`.
|
|
54
|
+
*
|
|
55
|
+
* Wire contract: the request body is parsed with `BulkRequestSchema` and
|
|
56
|
+
* the response body with `BulkResponseSchema`. On success, returns
|
|
57
|
+
* `{ insertedIds, updatedIds, deletedIds }` — each array preserves the
|
|
58
|
+
* input order of its bucket so clients can zip returned ids back onto
|
|
59
|
+
* optimistic cache entries.
|
|
60
|
+
*
|
|
61
|
+
* Semantics (non-negotiable — see `BULK_CRUD_TODO.md`):
|
|
62
|
+
* 1. Atomic — a single Firestore transaction wraps every write.
|
|
63
|
+
* 2. Collision rejection — same id in `updates` + `deletes`, or
|
|
64
|
+
* `inserts` + `updates`, throws `BulkCollisionError` before any
|
|
65
|
+
* read or write.
|
|
66
|
+
* 3. Empty bulk — `{}` short-circuits to a zeroed response; no DB work.
|
|
67
|
+
* 4. Order preserved — output arrays mirror the input order.
|
|
68
|
+
*
|
|
69
|
+
* The collection name is read from `req.query.collection` (Next.js
|
|
70
|
+
* dynamic route segment `[collection]`).
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // File: pages/api/crud/[collection]/bulk.ts (Next.js Pages Router)
|
|
75
|
+
* export { default } from '@donotdev/functions/vercel';
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @version 0.2.0
|
|
79
|
+
* @since 0.2.0
|
|
80
|
+
*/
|
|
81
|
+
export default async function handler(
|
|
82
|
+
req: NextApiRequest,
|
|
83
|
+
res: NextApiResponse
|
|
84
|
+
) {
|
|
85
|
+
if (req.method !== 'POST') {
|
|
86
|
+
return res.status(405).json({ error: 'Method not allowed' });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
// C1: verify JWT — rejects unauthenticated callers before any DB work.
|
|
91
|
+
const uid = await verifyAuthToken(req);
|
|
92
|
+
|
|
93
|
+
// W22: Validate collection name from the URL segment.
|
|
94
|
+
// `[collection]` route param is exposed via `req.query.collection`.
|
|
95
|
+
const rawCollection = req.query.collection;
|
|
96
|
+
const collection =
|
|
97
|
+
typeof rawCollection === 'string' ? rawCollection : rawCollection?.[0];
|
|
98
|
+
if (!collection) {
|
|
99
|
+
handleError(new Error('Missing collection'));
|
|
100
|
+
}
|
|
101
|
+
validateCollectionName(collection as string);
|
|
102
|
+
|
|
103
|
+
// Structurally parse the body up-front so a malformed payload produces a
|
|
104
|
+
// stable 400 (executeBulk also runs this parse, but it surfaces the error
|
|
105
|
+
// as a DoNotDevError — we intercept here to preserve the wire shape
|
|
106
|
+
// `{ error: string }` that existing clients expect for 400s).
|
|
107
|
+
const parseResult = v.safeParse(BulkRequestSchema, req.body);
|
|
108
|
+
if (!parseResult.success) {
|
|
109
|
+
return res.status(400).json({
|
|
110
|
+
error: `Validation failed: ${parseResult.issues
|
|
111
|
+
.map((e) => e.message)
|
|
112
|
+
.join(', ')}`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const ops: BulkRequest = parseResult.output;
|
|
116
|
+
|
|
117
|
+
const response: BulkResponse = await executeBulk({
|
|
118
|
+
collection: collection as string,
|
|
119
|
+
ops,
|
|
120
|
+
// The URL-routed wire contract does not carry entity schemas. We pass
|
|
121
|
+
// the structural row schemas so per-row validation is a no-op beyond
|
|
122
|
+
// what `BulkRequestSchema` already enforces.
|
|
123
|
+
createSchema: PASSTHROUGH_INSERT_SCHEMA,
|
|
124
|
+
updateSchema: PASSTHROUGH_PATCH_SCHEMA,
|
|
125
|
+
// URL-routed handler has no entity ACL config — the per-bucket ACL in
|
|
126
|
+
// `executeBulk` still enforces "populated bucket requires role", so we
|
|
127
|
+
// advertise 'user' across the three buckets (the surrounding auth layer
|
|
128
|
+
// has already verified the token).
|
|
129
|
+
access: { create: 'user', update: 'user', delete: 'user' },
|
|
130
|
+
uid,
|
|
131
|
+
userRole: 'user',
|
|
132
|
+
mintInsertId: () =>
|
|
133
|
+
getFirebaseAdminFirestore()
|
|
134
|
+
.collection(collection as string)
|
|
135
|
+
.doc().id,
|
|
136
|
+
stampInsertMetadata: (row, creatorUid) => ({
|
|
137
|
+
...prepareForFirestore(row),
|
|
138
|
+
...createMetadata(creatorUid),
|
|
139
|
+
}),
|
|
140
|
+
stampUpdateMetadata: (patch, editorUid) => ({
|
|
141
|
+
...prepareForFirestore(patch),
|
|
142
|
+
...updateMetadata(editorUid),
|
|
143
|
+
}),
|
|
144
|
+
transact: async ({ inserts, updates, deletes }) => {
|
|
145
|
+
const db = getFirebaseAdminFirestore();
|
|
146
|
+
const col = db.collection(collection as string);
|
|
147
|
+
|
|
148
|
+
return db.runTransaction(async (tx) => {
|
|
149
|
+
// Reads first (Firestore transactions require all reads before
|
|
150
|
+
// writes). For updates and deletes we verify document existence
|
|
151
|
+
// so a stale patch does not silently create a new row via tx.set
|
|
152
|
+
// and a stale delete surfaces a clear error.
|
|
153
|
+
const updateSnapshots = await Promise.all(
|
|
154
|
+
updates.map((u) => tx.get(col.doc(u.id)))
|
|
155
|
+
);
|
|
156
|
+
for (let i = 0; i < updateSnapshots.length; i++) {
|
|
157
|
+
if (!updateSnapshots[i]!.exists) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Document not found: ${collection as string}/${updates[i]!.id}`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const deleteSnapshots = await Promise.all(
|
|
164
|
+
deletes.map((id) => tx.get(col.doc(id)))
|
|
165
|
+
);
|
|
166
|
+
for (let i = 0; i < deleteSnapshots.length; i++) {
|
|
167
|
+
if (!deleteSnapshots[i]!.exists) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Document not found: ${collection as string}/${deletes[i]!}`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Writes — inserts, then updates, then deletes. Order within a
|
|
175
|
+
// transaction is not observable to readers (atomic commit) but we
|
|
176
|
+
// preserve it for determinism and log clarity.
|
|
177
|
+
const insertedIds: string[] = [];
|
|
178
|
+
for (const { id, row } of inserts) {
|
|
179
|
+
tx.set(col.doc(id), row);
|
|
180
|
+
insertedIds.push(id);
|
|
181
|
+
}
|
|
182
|
+
const updatedIds: string[] = [];
|
|
183
|
+
for (const { id, patch } of updates) {
|
|
184
|
+
tx.update(col.doc(id), patch);
|
|
185
|
+
updatedIds.push(id);
|
|
186
|
+
}
|
|
187
|
+
const deletedIds: string[] = [];
|
|
188
|
+
for (const id of deletes) {
|
|
189
|
+
tx.delete(col.doc(id));
|
|
190
|
+
deletedIds.push(id);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return { insertedIds, updatedIds, deletedIds };
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// The wire contract returns ids only, not the mutated rows. Consumers
|
|
199
|
+
// re-query if they need fresh snapshots (mirrors the REST semantics of
|
|
200
|
+
// a 200 with a summary body).
|
|
201
|
+
return res.status(200).json(response);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
try {
|
|
204
|
+
handleError(error);
|
|
205
|
+
} catch (handledError: any) {
|
|
206
|
+
// BulkCollisionError surfaces as 400 (invalid-argument) because the
|
|
207
|
+
// caller sent an incoherent payload — same class as schema failure.
|
|
208
|
+
const isCollision = error instanceof BulkCollisionError;
|
|
209
|
+
const status =
|
|
210
|
+
isCollision || handledError.code === 'invalid-argument' ? 400 : 500;
|
|
211
|
+
return res
|
|
212
|
+
.status(status)
|
|
213
|
+
.json({ error: handledError.message, code: handledError.code });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { DEFAULT_STATUS_VALUE } from '@donotdev/core/server';
|
|
13
12
|
import type { CreateEntityData } from '@donotdev/core/server';
|
|
14
13
|
import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
|
|
15
14
|
|
|
@@ -48,15 +47,17 @@ export default async function handler(
|
|
|
48
47
|
// W22: Validate collection name from client-supplied schema
|
|
49
48
|
validateCollectionName(schema.metadata.collection);
|
|
50
49
|
|
|
51
|
-
//
|
|
52
|
-
const status = payload.status ??
|
|
53
|
-
const isDraft = status === 'draft';
|
|
50
|
+
// New records start as drafts unless the caller explicitly sets a status.
|
|
51
|
+
const status = payload.status ?? 'draft';
|
|
54
52
|
|
|
55
|
-
// Validate the document against the schema
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
// Validate the document against the schema.
|
|
54
|
+
// Draft handling is schema-driven: `schemas.draft` must convert required
|
|
55
|
+
// fields into nullish/optional, so we still validate drafts.
|
|
56
|
+
const payloadForValidation = {
|
|
57
|
+
...(payload as Record<string, any>),
|
|
58
|
+
status,
|
|
59
|
+
};
|
|
60
|
+
validateDocument(payloadForValidation, schema);
|
|
60
61
|
|
|
61
62
|
// Prepare the document for Firestore and add metadata
|
|
62
63
|
// Always ensure status is set
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/src/vercel/api/index.ts
CHANGED
|
File without changes
|
|
File without changes
|
|
@@ -154,7 +154,10 @@ async function exchangeTokenLogic(
|
|
|
154
154
|
|
|
155
155
|
// Validate required fields
|
|
156
156
|
if (!tokenData.access_token) {
|
|
157
|
-
console.error(`Invalid token response`, {
|
|
157
|
+
console.error(`Invalid token response`, {
|
|
158
|
+
provider,
|
|
159
|
+
hasAccessToken: !!tokenData?.access_token,
|
|
160
|
+
});
|
|
158
161
|
throw new Error('Provider returned invalid token format');
|
|
159
162
|
}
|
|
160
163
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -54,7 +54,9 @@ export function handleCors(req: NextApiRequest, res: NextApiResponse): boolean {
|
|
|
54
54
|
// Warn if wildcard origin is used in production — consumers should override
|
|
55
55
|
const origin = corsHeaders['Access-Control-Allow-Origin'];
|
|
56
56
|
if (process.env.NODE_ENV === 'production' && origin === '*') {
|
|
57
|
-
console.warn(
|
|
57
|
+
console.warn(
|
|
58
|
+
'[DoNotDev] CORS origin is set to wildcard (*) in production. Override in your cors config.'
|
|
59
|
+
);
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
// Handle preflight OPTIONS request
|
|
File without changes
|