@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
|
@@ -11,10 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import * as v from 'valibot';
|
|
13
13
|
|
|
14
|
-
import {
|
|
15
|
-
DEFAULT_STATUS_VALUE,
|
|
16
|
-
filterVisibleFields,
|
|
17
|
-
} from '@donotdev/core/server';
|
|
14
|
+
import { filterVisibleFields } from '@donotdev/core/server';
|
|
18
15
|
import type { UserRole, UniqueKeyDefinition } from '@donotdev/core/server';
|
|
19
16
|
import { defaultFieldMapper } from '@donotdev/supabase/server';
|
|
20
17
|
|
|
@@ -130,7 +127,8 @@ async function checkUniqueKeys(
|
|
|
130
127
|
*/
|
|
131
128
|
export function createSupabaseCreateEntity(
|
|
132
129
|
collection: string,
|
|
133
|
-
|
|
130
|
+
createDocumentSchema: v.BaseSchema<unknown, any, v.BaseIssue<unknown>>,
|
|
131
|
+
draftDocumentSchema: v.BaseSchema<unknown, any, v.BaseIssue<unknown>>,
|
|
134
132
|
requiredRole: UserRole
|
|
135
133
|
) {
|
|
136
134
|
const requestSchema = v.object({
|
|
@@ -157,12 +155,12 @@ export function createSupabaseCreateEntity(
|
|
|
157
155
|
}
|
|
158
156
|
}
|
|
159
157
|
|
|
160
|
-
//
|
|
161
|
-
const status = payload.status ??
|
|
158
|
+
// New records start as drafts unless the caller explicitly sets a status.
|
|
159
|
+
const status = payload.status ?? 'draft';
|
|
162
160
|
const isDraft = status === 'draft';
|
|
163
161
|
|
|
164
162
|
// Check unique keys if schema has metadata with uniqueKeys
|
|
165
|
-
const schemaWithMeta =
|
|
163
|
+
const schemaWithMeta = createDocumentSchema as {
|
|
166
164
|
metadata?: { uniqueKeys?: UniqueKeyDefinition[] };
|
|
167
165
|
};
|
|
168
166
|
const uniqueKeys = schemaWithMeta.metadata?.uniqueKeys;
|
|
@@ -180,7 +178,7 @@ export function createSupabaseCreateEntity(
|
|
|
180
178
|
// G67: Apply same visibility filtering as normal create path
|
|
181
179
|
return filterVisibleFields(
|
|
182
180
|
checkResult.existingDoc,
|
|
183
|
-
|
|
181
|
+
createDocumentSchema,
|
|
184
182
|
userRole
|
|
185
183
|
);
|
|
186
184
|
}
|
|
@@ -192,11 +190,14 @@ export function createSupabaseCreateEntity(
|
|
|
192
190
|
? normalizePayloadForUniqueKeys(payload, uniqueKeys)
|
|
193
191
|
: payload;
|
|
194
192
|
|
|
195
|
-
// Validate
|
|
196
|
-
//
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
193
|
+
// Validate against the appropriate schema.
|
|
194
|
+
// Drafts must validate with `schemas.draft` so required fields become
|
|
195
|
+
// nullish/optional (instead of failing strict `schemas.create`).
|
|
196
|
+
const payloadForValidation = { ...normalizedPayload, status };
|
|
197
|
+
validateDocument(
|
|
198
|
+
payloadForValidation,
|
|
199
|
+
isDraft ? draftDocumentSchema : createDocumentSchema
|
|
200
|
+
);
|
|
200
201
|
|
|
201
202
|
const metadata = createMetadata(uid);
|
|
202
203
|
const snakeMetadata = mapper.toBackendKeys(
|
|
File without changes
|
package/src/supabase/crud/get.ts
CHANGED
|
File without changes
|
|
@@ -15,6 +15,7 @@ export { createSupabaseUpdateEntity } from './update.js';
|
|
|
15
15
|
export { createSupabaseDeleteEntity } from './delete.js';
|
|
16
16
|
export { createSupabaseListEntities } from './list.js';
|
|
17
17
|
export { createSupabaseAggregateEntities } from './aggregate.js';
|
|
18
|
+
export { createSupabaseBulkEntity } from './bulk.js';
|
|
18
19
|
|
|
19
20
|
export type { GetEntityRequest } from './get.js';
|
|
20
21
|
export type { CreateEntityRequest } from './create.js';
|
|
@@ -22,3 +23,4 @@ export type { UpdateEntityRequest } from './update.js';
|
|
|
22
23
|
export type { DeleteEntityRequest, ReferenceMetadata } from './delete.js';
|
|
23
24
|
export type { ListEntityRequest } from './list.js';
|
|
24
25
|
export type { AggregateEntityRequest } from './aggregate.js';
|
|
26
|
+
export type { BulkEntityRequest } from './bulk.js';
|
|
File without changes
|
|
File without changes
|
|
@@ -79,7 +79,7 @@ export interface EdgeFunctionOptions {
|
|
|
79
79
|
*/
|
|
80
80
|
export function createEdgeFunction(
|
|
81
81
|
handler: (req: Request, ctx: EdgeFunctionContext) => Promise<Response>,
|
|
82
|
-
options?: EdgeFunctionOptions
|
|
82
|
+
options?: EdgeFunctionOptions
|
|
83
83
|
): (req: Request) => Promise<Response> {
|
|
84
84
|
const requireAuth = options?.requireAuth ?? true;
|
|
85
85
|
const baseHeaders = 'authorization, content-type, x-client-info, apikey';
|
|
@@ -140,7 +140,8 @@ export function createEdgeFunction(
|
|
|
140
140
|
userClient = createClient(supabaseUrl, anonKey, {
|
|
141
141
|
global: { headers: { Authorization: `Bearer ${jwt}` } },
|
|
142
142
|
});
|
|
143
|
-
const { data, error: authError } =
|
|
143
|
+
const { data, error: authError } =
|
|
144
|
+
await supabaseAdmin.auth.getUser(jwt);
|
|
144
145
|
if (!authError && data.user) {
|
|
145
146
|
user = { id: data.user.id, email: data.user.email ?? undefined };
|
|
146
147
|
}
|
|
@@ -176,7 +177,7 @@ export function createEdgeFunction(
|
|
|
176
177
|
function jsonResponse(
|
|
177
178
|
data: unknown,
|
|
178
179
|
status: number,
|
|
179
|
-
allowedOrigin: string
|
|
180
|
+
allowedOrigin: string
|
|
180
181
|
): Response {
|
|
181
182
|
return new Response(JSON.stringify(data), {
|
|
182
183
|
status,
|
|
@@ -10,10 +10,7 @@
|
|
|
10
10
|
* @author AMBROISE PARK Consulting
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import type {
|
|
14
|
-
EmailRouteConfig,
|
|
15
|
-
SendEmailRequest,
|
|
16
|
-
} from '@donotdev/core/server';
|
|
13
|
+
import type { EmailRouteConfig, SendEmailRequest } from '@donotdev/core/server';
|
|
17
14
|
import { SendEmailRequestSchema } from '@donotdev/core/server';
|
|
18
15
|
|
|
19
16
|
import { handleEmailRequest } from '../../shared/email/index.js';
|
|
@@ -48,7 +45,10 @@ export function createEmailFunction(config: EmailRouteConfig) {
|
|
|
48
45
|
const apiKey = getEnv('RESEND_API_KEY');
|
|
49
46
|
if (!apiKey) {
|
|
50
47
|
return new Response(
|
|
51
|
-
JSON.stringify({
|
|
48
|
+
JSON.stringify({
|
|
49
|
+
success: false,
|
|
50
|
+
error: 'Email provider not configured',
|
|
51
|
+
}),
|
|
52
52
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
53
53
|
);
|
|
54
54
|
}
|
|
@@ -56,13 +56,10 @@ export function createEmailFunction(config: EmailRouteConfig) {
|
|
|
56
56
|
const provider = createResendProvider(apiKey);
|
|
57
57
|
const result = await handleEmailRequest(data, config, provider);
|
|
58
58
|
|
|
59
|
-
return new Response(
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
headers: { 'Content-Type': 'application/json' },
|
|
64
|
-
}
|
|
65
|
-
);
|
|
59
|
+
return new Response(JSON.stringify(result), {
|
|
60
|
+
status: result.success ? 200 : 400,
|
|
61
|
+
headers: { 'Content-Type': 'application/json' },
|
|
62
|
+
});
|
|
66
63
|
}
|
|
67
64
|
);
|
|
68
65
|
}
|
|
File without changes
|
package/src/supabase/index.ts
CHANGED
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
export { createSupabaseHandler } from './baseFunction.js';
|
|
14
14
|
export type { SupabaseHandlerContext } from './baseFunction.js';
|
|
15
15
|
export { createEdgeFunction } from './edgeFunction.js';
|
|
16
|
-
export type {
|
|
16
|
+
export type {
|
|
17
|
+
EdgeFunctionContext,
|
|
18
|
+
EdgeFunctionOptions,
|
|
19
|
+
} from './edgeFunction.js';
|
|
17
20
|
|
|
18
21
|
// Helpers
|
|
19
22
|
export { createSupabaseAuthProvider } from './helpers/authProvider.js';
|
|
@@ -44,8 +47,14 @@ export {
|
|
|
44
47
|
// AI
|
|
45
48
|
export { createAIFunction } from './ai/createAIFunction.js';
|
|
46
49
|
export { resolveModelForTier } from '../shared/ai/resolveModelTier.js';
|
|
47
|
-
export {
|
|
48
|
-
|
|
50
|
+
export {
|
|
51
|
+
classifyAIError,
|
|
52
|
+
mapClassifiedToHTTP,
|
|
53
|
+
} from '../shared/ai/classifyAIError.js';
|
|
54
|
+
export type {
|
|
55
|
+
AIErrorCode,
|
|
56
|
+
ClassifiedAIError,
|
|
57
|
+
} from '../shared/ai/classifyAIError.js';
|
|
49
58
|
|
|
50
59
|
// Email
|
|
51
60
|
export { createEmailFunction } from './email/createEmailFunction.js';
|
|
@@ -67,8 +67,7 @@ export function createDisconnect() {
|
|
|
67
67
|
// Revoke token if possible
|
|
68
68
|
if (connection?.credentials?.accessToken) {
|
|
69
69
|
const endpoints = OAUTH_PARTNERS[provider];
|
|
70
|
-
const revokeUrl =
|
|
71
|
-
'revokeUrl' in endpoints ? endpoints.revokeUrl : null;
|
|
70
|
+
const revokeUrl = 'revokeUrl' in endpoints ? endpoints.revokeUrl : null;
|
|
72
71
|
if (revokeUrl) {
|
|
73
72
|
try {
|
|
74
73
|
await fetch(revokeUrl.toString(), {
|
|
@@ -48,9 +48,7 @@ function sanitizeProfileData(
|
|
|
48
48
|
|
|
49
49
|
/** Cross-runtime env var access (Deno Edge Functions + Node tests) */
|
|
50
50
|
function getEnv(key: string): string | undefined {
|
|
51
|
-
return typeof Deno !== 'undefined'
|
|
52
|
-
? Deno.env.get(key)
|
|
53
|
-
: process.env[key];
|
|
51
|
+
return typeof Deno !== 'undefined' ? Deno.env.get(key) : process.env[key];
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
declare const Deno:
|
|
@@ -42,10 +42,7 @@ export function createGetConnections() {
|
|
|
42
42
|
return createSupabaseHandler(
|
|
43
43
|
'get-connections',
|
|
44
44
|
getConnectionsSchema,
|
|
45
|
-
async (
|
|
46
|
-
data: { purpose?: OAuthPurpose },
|
|
47
|
-
ctx: SupabaseHandlerContext
|
|
48
|
-
) => {
|
|
45
|
+
async (data: { purpose?: OAuthPurpose }, ctx: SupabaseHandlerContext) => {
|
|
49
46
|
let query = ctx.supabaseAdmin
|
|
50
47
|
.from('oauth_connections')
|
|
51
48
|
.select('*')
|
|
File without changes
|
|
@@ -21,9 +21,7 @@ import type { SupabaseHandlerContext } from '../baseFunction.js';
|
|
|
21
21
|
|
|
22
22
|
/** Cross-runtime env var access (Deno Edge Functions + Node tests) */
|
|
23
23
|
function getEnv(key: string): string | undefined {
|
|
24
|
-
return typeof Deno !== 'undefined'
|
|
25
|
-
? Deno.env.get(key)
|
|
26
|
-
: process.env[key];
|
|
24
|
+
return typeof Deno !== 'undefined' ? Deno.env.get(key) : process.env[key];
|
|
27
25
|
}
|
|
28
26
|
|
|
29
27
|
declare const Deno:
|
|
@@ -36,10 +34,7 @@ const refreshTokenSchema = v.object({
|
|
|
36
34
|
OAuthRefreshRequest['provider'],
|
|
37
35
|
v.BaseIssue<unknown>
|
|
38
36
|
>,
|
|
39
|
-
refreshToken: v.pipe(
|
|
40
|
-
v.string(),
|
|
41
|
-
v.minLength(1, 'Refresh token is required')
|
|
42
|
-
),
|
|
37
|
+
refreshToken: v.pipe(v.string(), v.minLength(1, 'Refresh token is required')),
|
|
43
38
|
redirectUri: v.pipe(v.string(), v.url('Valid redirect URI is required')),
|
|
44
39
|
});
|
|
45
40
|
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
createSupabaseDeleteEntity,
|
|
20
20
|
createSupabaseListEntities,
|
|
21
21
|
createSupabaseAggregateEntities,
|
|
22
|
+
createSupabaseBulkEntity,
|
|
22
23
|
} from './crud/index.js';
|
|
23
24
|
import { createEdgeFunction } from './edgeFunction.js';
|
|
24
25
|
|
|
@@ -61,6 +62,7 @@ export function createSupabaseCrudFunctions(
|
|
|
61
62
|
handlers[`create_${col}`] = createSupabaseCreateEntity(
|
|
62
63
|
col,
|
|
63
64
|
schemas.create,
|
|
65
|
+
schemas.draft,
|
|
64
66
|
access.create
|
|
65
67
|
);
|
|
66
68
|
handlers[`get_${col}`] = createSupabaseGetEntity(
|
|
@@ -120,6 +122,13 @@ export function createSupabaseCrudFunctions(
|
|
|
120
122
|
schemas.get,
|
|
121
123
|
access.read
|
|
122
124
|
);
|
|
125
|
+
handlers[`bulk_${col}`] = createSupabaseBulkEntity(
|
|
126
|
+
col,
|
|
127
|
+
schemas.create,
|
|
128
|
+
schemas.draft,
|
|
129
|
+
schemas.update,
|
|
130
|
+
access
|
|
131
|
+
);
|
|
123
132
|
}
|
|
124
133
|
|
|
125
134
|
/**
|
|
@@ -127,32 +136,35 @@ export function createSupabaseCrudFunctions(
|
|
|
127
136
|
* Auth is NOT checked here (requireAuth: false) — individual handlers
|
|
128
137
|
* use createSupabaseHandler which enforces auth + RBAC per operation.
|
|
129
138
|
*/
|
|
130
|
-
const serve = createEdgeFunction(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
139
|
+
const serve = createEdgeFunction(
|
|
140
|
+
async (req, ctx) => {
|
|
141
|
+
const body = await req.json().catch(() => ({}));
|
|
142
|
+
const functionName = (body as Record<string, unknown>)
|
|
143
|
+
._functionName as string;
|
|
144
|
+
|
|
145
|
+
if (!functionName) {
|
|
146
|
+
return ctx.error('Missing _functionName in request body', 400);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const handler = handlers[functionName];
|
|
150
|
+
if (!handler) {
|
|
151
|
+
return ctx.error(`Unknown function: ${functionName}`, 404);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Remove _functionName from body before passing to handler
|
|
155
|
+
const { _functionName, ...handlerData } = body as Record<string, unknown>;
|
|
156
|
+
|
|
157
|
+
// Create new request with cleaned body
|
|
158
|
+
const handlerReq = new Request(req.url, {
|
|
159
|
+
method: req.method,
|
|
160
|
+
headers: req.headers,
|
|
161
|
+
body: JSON.stringify(handlerData),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return handler(handlerReq);
|
|
165
|
+
},
|
|
166
|
+
{ requireAuth: false }
|
|
167
|
+
);
|
|
156
168
|
|
|
157
169
|
return { handlers, serve };
|
|
158
170
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// packages/functions/src/vercel/__tests__/bulk.test.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Tests for the Vercel bulk CRUD API handler.
|
|
5
|
+
* @description Unit tests for `POST /api/crud/:collection/bulk`. The Firebase
|
|
6
|
+
* Admin SDK is mocked — these tests exercise the handler's wire contract:
|
|
7
|
+
* parsing, collision rejection, transactional atomicity, method guard, and
|
|
8
|
+
* response shape.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Mocks — must be declared before importing the handler.
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
// Auth: resolve to a fixed uid for all tests.
|
|
18
|
+
vi.mock('../../shared/utils/internal/auth.js', () => ({
|
|
19
|
+
verifyAuthToken: vi.fn(() => Promise.resolve('user-1')),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
// Collection-name validator: accept everything in tests.
|
|
23
|
+
vi.mock('../../shared/utils.js', () => ({
|
|
24
|
+
validateCollectionName: vi.fn(),
|
|
25
|
+
DoNotDevError: class DoNotDevError extends Error {
|
|
26
|
+
code: string;
|
|
27
|
+
details: any;
|
|
28
|
+
constructor(message: string, code = 'internal', opts?: any) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'DoNotDevError';
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.details = opts?.details;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
// Metadata helpers — deterministic output for snapshot-like assertions.
|
|
38
|
+
// `executeBulk` is the real orchestrator so end-to-end behaviour (collision
|
|
39
|
+
// detection, empty short-circuit, transact invocation) exercises the shared
|
|
40
|
+
// core.
|
|
41
|
+
vi.mock('../../shared/index.js', async () => {
|
|
42
|
+
const actual = await vi.importActual<typeof import('../../shared/index.js')>(
|
|
43
|
+
'../../shared/index.js'
|
|
44
|
+
);
|
|
45
|
+
return {
|
|
46
|
+
...actual,
|
|
47
|
+
prepareForFirestore: (obj: Record<string, unknown>) => ({ ...obj }),
|
|
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
|
+
};
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Firebase Admin SDK — shared spy surface so each test can assert calls.
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
type TxSpy = {
|
|
67
|
+
get: ReturnType<typeof vi.fn>;
|
|
68
|
+
set: ReturnType<typeof vi.fn>;
|
|
69
|
+
update: ReturnType<typeof vi.fn>;
|
|
70
|
+
delete: ReturnType<typeof vi.fn>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
let txSpy: TxSpy;
|
|
74
|
+
let existingDocIds: Set<string>;
|
|
75
|
+
let mintedIds: string[];
|
|
76
|
+
let mintedIdx: number;
|
|
77
|
+
|
|
78
|
+
vi.mock('@donotdev/firebase/server', () => {
|
|
79
|
+
const doc = (id?: string) => {
|
|
80
|
+
const docId = id ?? mintedIds[mintedIdx++] ?? `auto-${mintedIdx}`;
|
|
81
|
+
return { id: docId, _id: docId };
|
|
82
|
+
};
|
|
83
|
+
const collection = (_name: string) => ({ doc });
|
|
84
|
+
|
|
85
|
+
const runTransaction = vi.fn(async (work: (tx: any) => Promise<any>) => {
|
|
86
|
+
txSpy = {
|
|
87
|
+
get: vi.fn(async (ref: any) => ({
|
|
88
|
+
exists: existingDocIds.has(ref.id),
|
|
89
|
+
id: ref.id,
|
|
90
|
+
data: () => ({}),
|
|
91
|
+
})),
|
|
92
|
+
set: vi.fn(),
|
|
93
|
+
update: vi.fn(),
|
|
94
|
+
delete: vi.fn(),
|
|
95
|
+
};
|
|
96
|
+
return await work(txSpy);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
getFirebaseAdminFirestore: () => ({
|
|
101
|
+
collection,
|
|
102
|
+
runTransaction,
|
|
103
|
+
}),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Error handling: keep real `handleError` (throws DoNotDevError-shaped) so the
|
|
108
|
+
// response mapping in the handler runs end-to-end.
|
|
109
|
+
vi.mock('../../shared/errorHandling.js', async () => {
|
|
110
|
+
class DoNotDevError extends Error {
|
|
111
|
+
code: string;
|
|
112
|
+
constructor(message: string, code = 'internal') {
|
|
113
|
+
super(message);
|
|
114
|
+
this.name = 'DoNotDevError';
|
|
115
|
+
this.code = code;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
DoNotDevError,
|
|
120
|
+
handleError: (err: unknown) => {
|
|
121
|
+
if (err instanceof DoNotDevError) throw err;
|
|
122
|
+
if (err instanceof Error && err.name === 'BulkCollisionError') {
|
|
123
|
+
throw new DoNotDevError(err.message, 'invalid-argument');
|
|
124
|
+
}
|
|
125
|
+
if (err instanceof Error && err.name === 'ValiError') {
|
|
126
|
+
throw new DoNotDevError('Validation failed', 'invalid-argument');
|
|
127
|
+
}
|
|
128
|
+
throw new DoNotDevError(
|
|
129
|
+
err instanceof Error ? err.message : 'Internal',
|
|
130
|
+
'internal'
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
// Import handler AFTER mocks.
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
import handler from '../api/crud/bulk.js';
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Test helpers.
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
function makeRes() {
|
|
147
|
+
const res: any = {};
|
|
148
|
+
res.status = vi.fn((code: number) => {
|
|
149
|
+
res._status = code;
|
|
150
|
+
return res;
|
|
151
|
+
});
|
|
152
|
+
res.json = vi.fn((body: unknown) => {
|
|
153
|
+
res._body = body;
|
|
154
|
+
return res;
|
|
155
|
+
});
|
|
156
|
+
return res;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function makeReq(opts: {
|
|
160
|
+
method?: string;
|
|
161
|
+
collection?: string | string[];
|
|
162
|
+
body?: unknown;
|
|
163
|
+
}) {
|
|
164
|
+
return {
|
|
165
|
+
method: opts.method ?? 'POST',
|
|
166
|
+
query: { collection: opts.collection ?? 'events' },
|
|
167
|
+
body: opts.body,
|
|
168
|
+
headers: { authorization: 'Bearer test' },
|
|
169
|
+
} as any;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
beforeEach(() => {
|
|
173
|
+
existingDocIds = new Set();
|
|
174
|
+
mintedIds = [];
|
|
175
|
+
mintedIdx = 0;
|
|
176
|
+
vi.clearAllMocks();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
// Tests.
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
describe('vercel bulk handler — happy path', () => {
|
|
184
|
+
it('runs 2 inserts + 1 update + 1 delete in a single transaction', async () => {
|
|
185
|
+
existingDocIds = new Set(['u1', 'd1']);
|
|
186
|
+
mintedIds = ['new-1', 'new-2'];
|
|
187
|
+
|
|
188
|
+
const req = makeReq({
|
|
189
|
+
body: {
|
|
190
|
+
inserts: [{ title: 'A' }, { title: 'B' }],
|
|
191
|
+
updates: [{ id: 'u1', patch: { title: 'renamed' } }],
|
|
192
|
+
deletes: ['d1'],
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
const res = makeRes();
|
|
196
|
+
|
|
197
|
+
await handler(req, res);
|
|
198
|
+
|
|
199
|
+
expect(res._status).toBe(200);
|
|
200
|
+
expect(res._body).toEqual({
|
|
201
|
+
insertedIds: ['new-1', 'new-2'],
|
|
202
|
+
updatedIds: ['u1'],
|
|
203
|
+
deletedIds: ['d1'],
|
|
204
|
+
});
|
|
205
|
+
// One transaction, N writes inside it.
|
|
206
|
+
expect(txSpy.set).toHaveBeenCalledTimes(2);
|
|
207
|
+
expect(txSpy.update).toHaveBeenCalledTimes(1);
|
|
208
|
+
expect(txSpy.delete).toHaveBeenCalledTimes(1);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('vercel bulk handler — empty payload', () => {
|
|
213
|
+
it('returns zeroed response without touching the DB', async () => {
|
|
214
|
+
const req = makeReq({ body: {} });
|
|
215
|
+
const res = makeRes();
|
|
216
|
+
|
|
217
|
+
await handler(req, res);
|
|
218
|
+
|
|
219
|
+
expect(res._status).toBe(200);
|
|
220
|
+
expect(res._body).toEqual({
|
|
221
|
+
insertedIds: [],
|
|
222
|
+
updatedIds: [],
|
|
223
|
+
deletedIds: [],
|
|
224
|
+
});
|
|
225
|
+
// No transaction spy created → no DB work.
|
|
226
|
+
expect(txSpy).toBeUndefined();
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('vercel bulk handler — collision rejection', () => {
|
|
231
|
+
it('rejects id present in both updates and deletes', async () => {
|
|
232
|
+
const req = makeReq({
|
|
233
|
+
body: {
|
|
234
|
+
updates: [{ id: 'x', patch: { a: 1 } }],
|
|
235
|
+
deletes: ['x'],
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
const res = makeRes();
|
|
239
|
+
|
|
240
|
+
await handler(req, res);
|
|
241
|
+
|
|
242
|
+
expect(res._status).toBe(400);
|
|
243
|
+
expect((res._body as any).error).toMatch(/BulkCollisionError/);
|
|
244
|
+
expect(txSpy).toBeUndefined();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('vercel bulk handler — validation failure', () => {
|
|
249
|
+
it('rolls back when an update targets a non-existent doc', async () => {
|
|
250
|
+
// `u1` not in existingDocIds → tx.get returns `exists: false` → throw.
|
|
251
|
+
existingDocIds = new Set();
|
|
252
|
+
mintedIds = ['new-1'];
|
|
253
|
+
|
|
254
|
+
const req = makeReq({
|
|
255
|
+
body: {
|
|
256
|
+
inserts: [{ title: 'A' }],
|
|
257
|
+
updates: [{ id: 'u1', patch: { title: 'x' } }],
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
const res = makeRes();
|
|
261
|
+
|
|
262
|
+
await handler(req, res);
|
|
263
|
+
|
|
264
|
+
expect(res._status).toBe(500);
|
|
265
|
+
// No writes issued — existence check runs before any tx.set/update/delete.
|
|
266
|
+
expect(txSpy.set).not.toHaveBeenCalled();
|
|
267
|
+
expect(txSpy.update).not.toHaveBeenCalled();
|
|
268
|
+
expect(txSpy.delete).not.toHaveBeenCalled();
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('rejects structurally invalid body (ACL-equivalent schema check)', async () => {
|
|
272
|
+
// `updates[0]` missing required `id` — schema rejects before DB access.
|
|
273
|
+
const req = makeReq({
|
|
274
|
+
body: {
|
|
275
|
+
updates: [{ patch: { title: 'x' } }],
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
const res = makeRes();
|
|
279
|
+
|
|
280
|
+
await handler(req, res);
|
|
281
|
+
|
|
282
|
+
expect(res._status).toBe(400);
|
|
283
|
+
expect(txSpy).toBeUndefined();
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe('vercel bulk handler — method guard', () => {
|
|
288
|
+
it('rejects GET with 405', async () => {
|
|
289
|
+
const req = makeReq({ method: 'GET', body: {} });
|
|
290
|
+
const res = makeRes();
|
|
291
|
+
await handler(req, res);
|
|
292
|
+
expect(res._status).toBe(405);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('rejects PUT with 405', async () => {
|
|
296
|
+
const req = makeReq({ method: 'PUT', body: {} });
|
|
297
|
+
const res = makeRes();
|
|
298
|
+
await handler(req, res);
|
|
299
|
+
expect(res._status).toBe(405);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|