@donotdev/functions 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/chunks/chunk-6NXIK35R.js +23 -0
- package/lib/chunks/chunk-6NXIK35R.js.map +7 -0
- package/lib/chunks/chunk-EZKRLLSQ.js +22 -0
- package/lib/chunks/chunk-EZKRLLSQ.js.map +7 -0
- package/lib/chunks/chunk-F3PHE627.js +22 -0
- package/lib/chunks/chunk-F3PHE627.js.map +7 -0
- package/lib/chunks/chunk-F3WBFWPO.js +151 -0
- package/lib/chunks/chunk-F3WBFWPO.js.map +7 -0
- package/lib/chunks/chunk-G5DRV3DH.js +22 -0
- package/lib/chunks/chunk-G5DRV3DH.js.map +7 -0
- package/lib/chunks/{chunk-5IPULSUX.js → chunk-GDIUOJMD.js} +2 -2
- package/lib/chunks/chunk-GDIUOJMD.js.map +7 -0
- package/lib/chunks/chunk-Q2WLRFOE.js +22 -0
- package/lib/chunks/chunk-Q2WLRFOE.js.map +7 -0
- package/lib/chunks/chunk-QXIOO2RL.js +22 -0
- package/lib/chunks/chunk-QXIOO2RL.js.map +7 -0
- package/lib/chunks/{chunk-3F4BWKQD.js → chunk-UH445BGK.js} +2 -2
- package/lib/chunks/chunk-W6W4L44F.js +22 -0
- package/lib/chunks/chunk-W6W4L44F.js.map +7 -0
- package/lib/chunks/chunk-W74UL7AY.js +22 -0
- package/lib/chunks/chunk-W74UL7AY.js.map +7 -0
- package/lib/chunks/{monitoring-E7JBYBHQ.js → monitoring-LXNVBD3G.js} +2 -2
- package/lib/chunks/{rateLimiter-SQISLOP5.js → rateLimiter-GG5LPWXJ.js} +2 -2
- package/lib/chunks/server-NODQAE3F.js +22 -0
- package/lib/firebase/billing/index.d.ts +2 -2
- package/lib/firebase/crud/aggregate.d.ts +1 -1
- package/lib/firebase/index.d.ts +1 -1
- package/lib/firebase/index.js +1 -1
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/registerCrudFunctions.d.ts +0 -5
- package/lib/shared/billing/checkoutPrice.d.ts +3 -6
- package/lib/shared/billing/checkoutValidation.d.ts +1 -1
- package/lib/shared/billing/createCheckout.d.ts +4 -4
- package/lib/shared/billing/firestoreIdempotency.d.ts +2 -2
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +2 -2
- package/lib/shared/billing/idempotency.d.ts +3 -23
- package/lib/shared/billing/idempotencyContract.d.ts +0 -20
- package/lib/shared/billing/orderWebhook.d.ts +29 -2
- package/lib/shared/billing/stripePinned.d.ts +43 -0
- package/lib/shared/denoEnv.d.ts +25 -0
- package/lib/shared/github.d.ts +0 -8
- package/lib/shared/index.js +1 -1
- package/lib/shared/logger.d.ts +14 -14
- package/lib/shared/utils/external/subscription.d.ts +0 -8
- package/lib/shared/utils/internal/auth.d.ts +1 -5
- package/lib/shared/utils.d.ts +12 -83
- package/lib/supabase/auth/deleteAccount.d.ts +34 -2
- package/lib/supabase/crud/create.d.ts +0 -8
- package/lib/supabase/crud/entityStore.d.ts +13 -0
- package/lib/supabase/crud/update.d.ts +0 -8
- package/lib/supabase/index.d.ts +1 -0
- package/lib/supabase/index.js +1 -1
- package/lib/supabase/index.js.map +4 -4
- package/lib/vercel/api/index.js +1 -1
- package/lib/vercel/api/index.js.map +3 -3
- package/package.json +9 -9
- package/src/firebase/auth/getCustomClaims.ts +2 -0
- package/src/firebase/auth/getUserAuthStatus.ts +2 -0
- package/src/firebase/auth/removeCustomClaims.ts +2 -0
- package/src/firebase/auth/setCustomClaims.ts +3 -0
- package/src/firebase/baseFunction.ts +8 -4
- package/src/firebase/billing/changePlan.ts +6 -5
- package/src/firebase/billing/createCustomerPortal.ts +6 -4
- package/src/firebase/billing/index.d.ts +0 -12
- package/src/firebase/billing/index.ts +0 -3
- package/src/firebase/billing/refreshSubscriptionStatus.ts +7 -5
- package/src/firebase/billing/webhookHandler.ts +4 -4
- package/src/firebase/crud/aggregate.ts +41 -12
- package/src/firebase/crud/bulk.ts +7 -0
- package/src/firebase/crud/create.ts +21 -8
- package/src/firebase/crud/delete.ts +6 -1
- package/src/firebase/crud/get.ts +9 -2
- package/src/firebase/crud/list.ts +18 -8
- package/src/firebase/crud/update.ts +15 -7
- package/src/firebase/helpers/githubAccessHelper.ts +3 -0
- package/src/firebase/helpers/githubTeamAccessHelper.ts +1 -0
- package/src/firebase/index.ts +0 -1
- package/src/firebase/oauth/disconnect.ts +2 -2
- package/src/firebase/oauth/exchangeToken.ts +6 -2
- package/src/firebase/oauth/getConnections.ts +3 -3
- package/src/firebase/oauth/githubAccess.ts +7 -4
- package/src/firebase/oauth/refreshToken.ts +3 -2
- package/src/firebase/registerCrudFunctions.ts +0 -19
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +4 -1
- package/src/shared/__tests__/idempotency.test.ts +0 -32
- package/src/shared/__tests__/utils.test.ts +0 -25
- package/src/shared/ai/classifyAIError.ts +18 -10
- package/src/shared/ai/createAIRoute.ts +2 -1
- package/src/shared/ai/resolveModelTier.ts +2 -1
- package/src/shared/billing/__tests__/createCheckout.compat.test.ts +4 -110
- package/src/shared/billing/__tests__/createCheckout.test.ts +94 -0
- package/src/shared/billing/__tests__/firestoreIdempotency.emulator.test.ts +1 -1
- package/src/shared/billing/__tests__/webhookHandler.test.ts +108 -0
- package/src/shared/billing/checkoutPrice.ts +7 -42
- package/src/shared/billing/checkoutValidation.ts +19 -2
- package/src/shared/billing/createCheckout.ts +59 -30
- package/src/shared/billing/firestoreIdempotency.ts +2 -21
- package/src/shared/billing/helpers/subscriptionManagement.ts +14 -10
- package/src/shared/billing/idempotency.ts +4 -54
- package/src/shared/billing/idempotencyContract.ts +0 -21
- package/src/shared/billing/orderWebhook.ts +69 -1
- package/src/shared/billing/stripeClient.ts +2 -1
- package/src/shared/billing/stripePinned.ts +71 -0
- package/src/shared/billing/tierWebhookEvents.ts +3 -2
- package/src/shared/billing/webhookHandler.ts +5 -1
- package/src/shared/crud/bulkCore.ts +3 -2
- package/src/shared/denoEnv.ts +29 -0
- package/src/shared/email/createEmailRoute.ts +2 -2
- package/src/shared/errorHandling.ts +8 -8
- package/src/shared/firebase.ts +26 -9
- package/src/shared/github.ts +15 -5
- package/src/shared/logger.ts +37 -38
- package/src/shared/schema.ts +6 -10
- package/src/shared/utils/external/date.ts +4 -4
- package/src/shared/utils/external/subscription.ts +2 -89
- package/src/shared/utils/firebaseHelpers.ts +2 -0
- package/src/shared/utils/internal/auth.ts +1 -6
- package/src/shared/utils/internal/idempotency.ts +3 -0
- package/src/shared/utils/internal/monitoring.ts +6 -5
- package/src/shared/utils/internal/validation.ts +2 -0
- package/src/shared/utils/schemaValidation.ts +1 -0
- package/src/shared/utils/validation.ts +10 -20
- package/src/shared/utils.ts +12 -138
- package/src/supabase/__tests__/aggregate.test.ts +132 -0
- package/src/supabase/auth/__tests__/deleteAccount.test.ts +102 -0
- package/src/supabase/auth/deleteAccount.ts +72 -5
- package/src/supabase/billing/refreshSubscriptionStatus.ts +6 -5
- package/src/supabase/crud/aggregate.ts +85 -72
- package/src/supabase/crud/bulk.ts +3 -0
- package/src/supabase/crud/create.ts +13 -9
- package/src/supabase/crud/delete.ts +2 -1
- package/src/supabase/crud/entityStore.ts +1 -1
- package/src/supabase/crud/get.ts +2 -1
- package/src/supabase/crud/list.ts +23 -17
- package/src/supabase/crud/update.ts +9 -6
- package/src/supabase/edgeFunction.ts +2 -1
- package/src/supabase/email/createEmailFunction.ts +2 -1
- package/src/supabase/index.ts +1 -0
- package/src/supabase/oauth/exchangeToken.ts +1 -0
- package/src/supabase/oauth/getConnections.ts +1 -1
- package/src/supabase/oauth/refreshToken.ts +1 -0
- package/src/supabase/utils/idempotency.ts +1 -1
- package/src/supabase/utils/monitoring.ts +1 -0
- package/src/vercel/api/billing/change-plan.ts +6 -5
- package/src/vercel/api/billing/refresh-subscription-status.ts +3 -1
- package/src/vercel/api/crud/bulk.ts +7 -2
- package/src/vercel/api/crud/create.ts +6 -3
- package/src/vercel/api/crud/delete.ts +5 -2
- package/src/vercel/api/crud/get.ts +5 -2
- package/src/vercel/api/crud/list.ts +6 -3
- package/src/vercel/api/crud/update.ts +6 -3
- package/src/vercel/api/oauth/check-github-access.ts +1 -0
- package/src/vercel/api/oauth/exchange-token.ts +5 -2
- package/src/vercel/api/oauth/grant-github-access.ts +1 -0
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -0
- package/src/vercel/baseFunction.ts +5 -1
- package/lib/chunks/chunk-5IPULSUX.js.map +0 -7
- package/lib/chunks/chunk-6USHTJED.js +0 -22
- package/lib/chunks/chunk-6USHTJED.js.map +0 -7
- package/lib/chunks/chunk-CXBSROEW.js +0 -23
- package/lib/chunks/chunk-CXBSROEW.js.map +0 -7
- package/lib/chunks/chunk-GYTCWMXV.js +0 -22
- package/lib/chunks/chunk-GYTCWMXV.js.map +0 -7
- package/lib/chunks/chunk-LCDLG4VG.js +0 -151
- package/lib/chunks/chunk-LCDLG4VG.js.map +0 -7
- package/lib/chunks/chunk-MQANUSY3.js +0 -22
- package/lib/chunks/chunk-MQANUSY3.js.map +0 -7
- package/lib/chunks/chunk-OFQVO3FD.js +0 -22
- package/lib/chunks/chunk-OFQVO3FD.js.map +0 -7
- package/lib/chunks/chunk-PWAGW4R6.js +0 -22
- package/lib/chunks/chunk-PWAGW4R6.js.map +0 -7
- package/lib/chunks/chunk-US2VOP4S.js +0 -22
- package/lib/chunks/chunk-US2VOP4S.js.map +0 -7
- package/lib/chunks/chunk-YTKDNZNW.js +0 -22
- package/lib/chunks/chunk-YTKDNZNW.js.map +0 -7
- package/lib/chunks/server-IQ5JNJOM.js +0 -22
- package/lib/shared/utils/functionWrapper.d.ts +0 -36
- package/src/shared/utils/__tests__/functionWrapper.test.ts +0 -122
- package/src/shared/utils/functionWrapper.ts +0 -73
- /package/lib/chunks/{chunk-3F4BWKQD.js.map → chunk-UH445BGK.js.map} +0 -0
- /package/lib/chunks/{monitoring-E7JBYBHQ.js.map → monitoring-LXNVBD3G.js.map} +0 -0
- /package/lib/chunks/{rateLimiter-SQISLOP5.js.map → rateLimiter-GG5LPWXJ.js.map} +0 -0
- /package/lib/chunks/{server-IQ5JNJOM.js.map → server-NODQAE3F.js.map} +0 -0
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { createRequire as __createRequire } from 'node:module';
|
|
2
|
-
import { fileURLToPath } from 'node:url';
|
|
3
|
-
import { dirname } from 'node:path';
|
|
4
|
-
import { Buffer } from 'node:buffer';
|
|
5
|
-
import process from 'node:process';
|
|
6
|
-
|
|
7
|
-
const require = __createRequire(import.meta.url);
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = dirname(__filename);
|
|
10
|
-
|
|
11
|
-
if (typeof globalThis !== 'undefined') {
|
|
12
|
-
globalThis.require = require;
|
|
13
|
-
globalThis.__filename = __filename;
|
|
14
|
-
globalThis.__dirname = __dirname;
|
|
15
|
-
globalThis.Buffer = Buffer;
|
|
16
|
-
globalThis.process = process;
|
|
17
|
-
if (typeof global === 'undefined') {
|
|
18
|
-
globalThis.global = globalThis;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
import{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}from"./chunk-CXBSROEW.js";import"./chunk-GYTCWMXV.js";import"./chunk-AYY7UXNI.js";export{M as FieldValue,f as FirebaseServerAuthAdapter,N as Query,O as Timestamp,s as batchCreate,u as batchDelete,t as batchUpdate,r as batchWrite,w as bulkGet,n as convertISOStringsToDates,H as createAdminFirestoreValidator,I as createFirestoreClientValidator,q as createFirestorePartialUpdate,G as createFirestoreValidator,j as createTimestamp,L as enhanceSchema,l as firestoreToISOString,i as generateServerId,x as getActiveSubscription,b as getFirebaseAdminApp,c as getFirebaseAdminAuth,d as getFirebaseAdminFirestore,e as getFirebaseAdminFunctions,g as getServerAdmin,h as getServerFirestore,E as getUniqueConstraintValidator,y as getUserSubscriptions,B as hasFeatureAccess,F as hasUniqueConstraintValidator,a as initFirebaseAdmin,m as isTimestamp,p as prepareForFirestore,D as registerUniqueConstraintValidator,v as runTransaction,z as saveSubscription,k as toTimestamp,o as transformFirestoreData,A as updateSubscriptionStatus,K as validateFirestoreDocument,J as validateUniqueness,C as verifyAndHandleStripeWebhook};
|
|
22
|
-
//# sourceMappingURL=server-IQ5JNJOM.js.map
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Simple DRY wrapper for functions with schema validation
|
|
3
|
-
* @description Eliminates duplication by providing a clean pattern for all functions
|
|
4
|
-
*
|
|
5
|
-
* @version 0.1.0
|
|
6
|
-
* @since 0.0.1
|
|
7
|
-
* @author AMBROISE PARK Consulting
|
|
8
|
-
*/
|
|
9
|
-
import type { CallableRequest } from 'firebase-functions/v2/https';
|
|
10
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
11
|
-
import * as v from 'valibot';
|
|
12
|
-
/**
|
|
13
|
-
* Simple wrapper that validates data against schema and calls the handler
|
|
14
|
-
* This is the ONLY pattern needed for all functions
|
|
15
|
-
*
|
|
16
|
-
* @version 0.1.0
|
|
17
|
-
* @since 0.0.1
|
|
18
|
-
* @author AMBROISE PARK Consulting
|
|
19
|
-
*/
|
|
20
|
-
export declare function withValidation<TData, TResponse>(schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>, handler: (data: TData) => Promise<TResponse>): (data: any) => Promise<TResponse>;
|
|
21
|
-
/**
|
|
22
|
-
* Firebase function wrapper
|
|
23
|
-
*
|
|
24
|
-
* @version 0.1.0
|
|
25
|
-
* @since 0.0.1
|
|
26
|
-
* @author AMBROISE PARK Consulting
|
|
27
|
-
*/
|
|
28
|
-
export declare function firebaseFunction<TData, TResponse>(schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>, handler: (data: TData) => Promise<TResponse>): (request: CallableRequest<TData>) => Promise<TResponse>;
|
|
29
|
-
/**
|
|
30
|
-
* Vercel function wrapper
|
|
31
|
-
*
|
|
32
|
-
* @version 0.1.0
|
|
33
|
-
* @since 0.0.1
|
|
34
|
-
* @author AMBROISE PARK Consulting
|
|
35
|
-
*/
|
|
36
|
-
export declare function vercelFunction<TData, TResponse>(schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>, handler: (req: NextApiRequest, res: NextApiResponse, data: TData) => Promise<TResponse>): (req: NextApiRequest, res: NextApiResponse) => Promise<TResponse>;
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
// packages/functions/src/shared/utils/__tests__/functionWrapper.test.ts
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @fileoverview Tests for withValidation, firebaseFunction, vercelFunction
|
|
5
|
-
* @description Unit tests for schema validation wrappers.
|
|
6
|
-
*
|
|
7
|
-
* @version 0.1.0
|
|
8
|
-
* @since 0.0.1
|
|
9
|
-
* @author AMBROISE PARK Consulting
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import * as v from 'valibot';
|
|
13
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
withValidation,
|
|
17
|
-
firebaseFunction,
|
|
18
|
-
vercelFunction,
|
|
19
|
-
} from '../functionWrapper';
|
|
20
|
-
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
// Schemas and fixtures
|
|
23
|
-
// ---------------------------------------------------------------------------
|
|
24
|
-
|
|
25
|
-
const TestSchema = v.object({
|
|
26
|
-
name: v.pipe(v.string(), v.minLength(1)),
|
|
27
|
-
count: v.optional(v.pipe(v.number(), v.minValue(0))),
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
type TestData = v.InferOutput<typeof TestSchema>;
|
|
31
|
-
|
|
32
|
-
// ---------------------------------------------------------------------------
|
|
33
|
-
// withValidation
|
|
34
|
-
// ---------------------------------------------------------------------------
|
|
35
|
-
|
|
36
|
-
describe('withValidation', () => {
|
|
37
|
-
it('returns handler result when data is valid', async () => {
|
|
38
|
-
const handler = vi.fn().mockResolvedValue({ id: '123' });
|
|
39
|
-
const wrapped = withValidation(TestSchema, handler);
|
|
40
|
-
const data = { name: 'foo', count: 2 };
|
|
41
|
-
|
|
42
|
-
const result = await wrapped(data);
|
|
43
|
-
|
|
44
|
-
expect(result).toEqual({ id: '123' });
|
|
45
|
-
expect(handler).toHaveBeenCalledWith({ name: 'foo', count: 2 });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('throws when data fails validation', async () => {
|
|
49
|
-
const handler = vi.fn();
|
|
50
|
-
const wrapped = withValidation(TestSchema, handler);
|
|
51
|
-
|
|
52
|
-
await expect(wrapped({ name: '' })).rejects.toThrow();
|
|
53
|
-
expect(handler).not.toHaveBeenCalled();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('passes validated (parsed) data to handler', async () => {
|
|
57
|
-
const handler = vi.fn().mockResolvedValue(undefined);
|
|
58
|
-
const wrapped = withValidation(TestSchema, handler);
|
|
59
|
-
await wrapped({ name: 'a', count: 0 });
|
|
60
|
-
|
|
61
|
-
expect(handler).toHaveBeenCalledWith(
|
|
62
|
-
expect.objectContaining({ name: 'a', count: 0 })
|
|
63
|
-
);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// ---------------------------------------------------------------------------
|
|
68
|
-
// firebaseFunction
|
|
69
|
-
// ---------------------------------------------------------------------------
|
|
70
|
-
|
|
71
|
-
describe('firebaseFunction', () => {
|
|
72
|
-
it('calls handler with parsed request.data', async () => {
|
|
73
|
-
const handler = vi.fn().mockResolvedValue({ success: true });
|
|
74
|
-
const fn = firebaseFunction(TestSchema, handler);
|
|
75
|
-
const request = {
|
|
76
|
-
data: { name: 'test', count: 1 },
|
|
77
|
-
rawRequest: {} as any,
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const result = await fn(request);
|
|
81
|
-
|
|
82
|
-
expect(result).toEqual({ success: true });
|
|
83
|
-
expect(handler).toHaveBeenCalledWith({ name: 'test', count: 1 });
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('throws when request.data is invalid', async () => {
|
|
87
|
-
const handler = vi.fn();
|
|
88
|
-
const fn = firebaseFunction(TestSchema, handler);
|
|
89
|
-
const request = { data: { name: '' }, rawRequest: {} as any };
|
|
90
|
-
|
|
91
|
-
await expect(fn(request)).rejects.toThrow();
|
|
92
|
-
expect(handler).not.toHaveBeenCalled();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// ---------------------------------------------------------------------------
|
|
97
|
-
// vercelFunction
|
|
98
|
-
// ---------------------------------------------------------------------------
|
|
99
|
-
|
|
100
|
-
describe('vercelFunction', () => {
|
|
101
|
-
it('parses req.body and calls handler with (req, res, data)', async () => {
|
|
102
|
-
const handler = vi.fn().mockResolvedValue({ ok: true });
|
|
103
|
-
const fn = vercelFunction(TestSchema, handler);
|
|
104
|
-
const req = { body: { name: 'v', count: 0 } } as any;
|
|
105
|
-
const res = {} as any;
|
|
106
|
-
|
|
107
|
-
const result = await fn(req, res);
|
|
108
|
-
|
|
109
|
-
expect(result).toEqual({ ok: true });
|
|
110
|
-
expect(handler).toHaveBeenCalledWith(req, res, { name: 'v', count: 0 });
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('throws when req.body is invalid', async () => {
|
|
114
|
-
const handler = vi.fn();
|
|
115
|
-
const fn = vercelFunction(TestSchema, handler);
|
|
116
|
-
const req = { body: {} } as any;
|
|
117
|
-
const res = {} as any;
|
|
118
|
-
|
|
119
|
-
await expect(fn(req, res)).rejects.toThrow();
|
|
120
|
-
expect(handler).not.toHaveBeenCalled();
|
|
121
|
-
});
|
|
122
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// packages/functions/src/shared/utils/functionWrapper.ts
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @fileoverview Simple DRY wrapper for functions with schema validation
|
|
5
|
-
* @description Eliminates duplication by providing a clean pattern for all functions
|
|
6
|
-
*
|
|
7
|
-
* @version 0.1.0
|
|
8
|
-
* @since 0.0.1
|
|
9
|
-
* @author AMBROISE PARK Consulting
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import type { CallableRequest } from 'firebase-functions/v2/https';
|
|
13
|
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
14
|
-
import * as v from 'valibot';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Simple wrapper that validates data against schema and calls the handler
|
|
18
|
-
* This is the ONLY pattern needed for all functions
|
|
19
|
-
*
|
|
20
|
-
* @version 0.1.0
|
|
21
|
-
* @since 0.0.1
|
|
22
|
-
* @author AMBROISE PARK Consulting
|
|
23
|
-
*/
|
|
24
|
-
export function withValidation<TData, TResponse>(
|
|
25
|
-
schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>,
|
|
26
|
-
handler: (data: TData) => Promise<TResponse>
|
|
27
|
-
) {
|
|
28
|
-
return async (data: any): Promise<TResponse> => {
|
|
29
|
-
// Schema handles ALL validation - no manual checks needed
|
|
30
|
-
const validatedData = v.parse(schema, data);
|
|
31
|
-
return await handler(validatedData);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Firebase function wrapper
|
|
37
|
-
*
|
|
38
|
-
* @version 0.1.0
|
|
39
|
-
* @since 0.0.1
|
|
40
|
-
* @author AMBROISE PARK Consulting
|
|
41
|
-
*/
|
|
42
|
-
export function firebaseFunction<TData, TResponse>(
|
|
43
|
-
schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>,
|
|
44
|
-
handler: (data: TData) => Promise<TResponse>
|
|
45
|
-
) {
|
|
46
|
-
return async (request: CallableRequest<TData>): Promise<TResponse> => {
|
|
47
|
-
return await withValidation(schema, handler)(request.data as any);
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Vercel function wrapper
|
|
53
|
-
*
|
|
54
|
-
* @version 0.1.0
|
|
55
|
-
* @since 0.0.1
|
|
56
|
-
* @author AMBROISE PARK Consulting
|
|
57
|
-
*/
|
|
58
|
-
export function vercelFunction<TData, TResponse>(
|
|
59
|
-
schema: v.BaseSchema<unknown, TData, v.BaseIssue<unknown>>,
|
|
60
|
-
handler: (
|
|
61
|
-
req: NextApiRequest,
|
|
62
|
-
res: NextApiResponse,
|
|
63
|
-
data: TData
|
|
64
|
-
) => Promise<TResponse>
|
|
65
|
-
) {
|
|
66
|
-
return async (
|
|
67
|
-
req: NextApiRequest,
|
|
68
|
-
res: NextApiResponse
|
|
69
|
-
): Promise<TResponse> => {
|
|
70
|
-
const validatedData = v.parse(schema, req.body);
|
|
71
|
-
return await handler(req, res, validatedData);
|
|
72
|
-
};
|
|
73
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|