@donotdev/functions 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/guidelines/FUNCTIONS.md +9 -0
- package/guidelines/VERCEL.md +260 -0
- package/lib/firebase/billing/createCheckoutSession.d.ts +10 -13
- package/lib/firebase/index.js +3 -3
- package/lib/firebase/index.js.map +4 -4
- package/lib/shared/ai/classifyAIError.d.ts +35 -0
- package/lib/shared/ai/costTracker.d.ts +20 -0
- package/lib/shared/ai/createAIRoute.d.ts +20 -0
- package/lib/shared/ai/index.d.ts +6 -0
- package/lib/shared/ai/resolveModelTier.d.ts +14 -0
- package/lib/shared/billing/createCheckout.d.ts +8 -2
- package/lib/shared/email/createEmailRoute.d.ts +30 -0
- package/lib/shared/email/index.d.ts +3 -0
- package/lib/shared/email/provider.d.ts +30 -0
- package/lib/shared/email/resendProvider.d.ts +22 -0
- package/lib/shared/github.d.ts +5 -0
- package/lib/shared/index.d.ts +2 -0
- package/lib/shared/index.js +4 -3
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +2 -0
- package/lib/shared/oauth/exchangeToken.d.ts +1 -0
- package/lib/shared/oauth/grantAccess.d.ts +1 -0
- package/lib/shared/utils/internal/idempotency.d.ts +1 -0
- package/lib/supabase/ai/createAIFunction.d.ts +30 -0
- package/lib/supabase/auth/removeCustomClaims.d.ts +1 -1
- package/lib/supabase/auth/setCustomClaims.d.ts +12 -4
- package/lib/supabase/billing/changePlan.d.ts +1 -1
- package/lib/supabase/crud/aggregate.d.ts +1 -0
- package/lib/supabase/crud/create.d.ts +1 -0
- package/lib/supabase/crud/delete.d.ts +1 -0
- package/lib/supabase/crud/get.d.ts +1 -0
- package/lib/supabase/crud/list.d.ts +1 -0
- package/lib/supabase/crud/update.d.ts +1 -0
- package/lib/supabase/edgeFunction.d.ts +56 -0
- package/lib/supabase/email/createEmailFunction.d.ts +31 -0
- package/lib/supabase/index.d.ts +8 -0
- package/lib/supabase/index.js +3 -2
- package/lib/supabase/index.js.map +4 -4
- package/lib/vercel/api/ai/chat.d.ts +29 -0
- package/lib/vercel/api/ai/index.d.ts +1 -0
- package/lib/vercel/api/index.d.ts +1 -0
- package/lib/vercel/api/index.js +3 -2
- package/lib/vercel/api/index.js.map +4 -4
- package/package.json +38 -5
- package/src/firebase/billing/createCheckoutSession.ts +79 -187
- package/src/firebase/oauth/exchangeToken.ts +4 -4
- package/src/firebase/oauth/refreshToken.ts +4 -4
- package/src/shared/ai/__tests__/costTracker.test.ts +121 -0
- package/src/shared/ai/classifyAIError.ts +163 -0
- package/src/shared/ai/costTracker.ts +51 -0
- package/src/shared/ai/createAIRoute.ts +397 -0
- package/src/shared/ai/index.ts +8 -0
- package/src/shared/ai/resolveModelTier.ts +58 -0
- package/src/shared/billing/createCheckout.ts +153 -8
- package/src/shared/billing/helpers/subscriptionManagement.ts +9 -0
- package/src/shared/billing/idempotency.ts +5 -0
- package/src/shared/email/createEmailRoute.ts +81 -0
- package/src/shared/email/index.ts +5 -0
- package/src/shared/email/provider.ts +31 -0
- package/src/shared/email/resendProvider.ts +56 -0
- package/src/shared/errorHandling.ts +10 -4
- package/src/shared/github.ts +38 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/logger.ts +2 -0
- package/src/shared/oauth/exchangeToken.ts +1 -0
- package/src/shared/oauth/grantAccess.ts +1 -0
- package/src/shared/utils/internal/idempotency.ts +1 -0
- package/src/shared/utils/internal/rateLimiter.ts +6 -3
- package/src/shared/validation.ts +1 -1
- package/src/supabase/ai/createAIFunction.ts +75 -0
- package/src/supabase/auth/removeCustomClaims.ts +38 -2
- package/src/supabase/auth/setCustomClaims.ts +66 -5
- package/src/supabase/baseFunction.ts +26 -2
- package/src/supabase/billing/changePlan.ts +6 -1
- package/src/supabase/billing/createCheckoutSession.ts +16 -2
- package/src/supabase/crud/aggregate.ts +1 -0
- package/src/supabase/crud/create.ts +1 -0
- package/src/supabase/crud/delete.ts +1 -0
- package/src/supabase/crud/get.ts +1 -0
- package/src/supabase/crud/list.ts +1 -0
- package/src/supabase/crud/update.ts +1 -0
- package/src/supabase/edgeFunction.ts +195 -0
- package/src/supabase/email/createEmailFunction.ts +75 -0
- package/src/supabase/index.ts +12 -0
- package/src/supabase/oauth/exchangeToken.ts +38 -7
- package/src/supabase/oauth/refreshToken.ts +4 -5
- package/src/supabase/registerCrudFunctions.ts +29 -41
- package/src/supabase/utils/idempotency.ts +6 -2
- package/src/vercel/api/ai/chat.ts +77 -0
- package/src/vercel/api/ai/index.ts +1 -0
- package/src/vercel/api/billing/create-checkout-session.ts +27 -4
- package/src/vercel/api/billing/webhook-handler.ts +8 -1
- package/src/vercel/api/index.ts +1 -0
- package/src/vercel/api/oauth/exchange-token.ts +24 -1
- package/src/vercel/api/utils/cors.ts +6 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AI Error Classifier
|
|
3
|
+
* @description Classifies Anthropic API errors into actionable categories
|
|
4
|
+
* for structured error responses in edge functions.
|
|
5
|
+
*
|
|
6
|
+
* @version 0.1.0
|
|
7
|
+
* @since 0.6.0
|
|
8
|
+
* @author AMBROISE PARK Consulting
|
|
9
|
+
*/
|
|
10
|
+
/** Actionable error category for AI provider failures. */
|
|
11
|
+
export type AIErrorCode = 'rate_limited' | 'overloaded' | 'auth_error' | 'credits_exhausted' | 'network' | 'internal';
|
|
12
|
+
/** Result of classifying an AI provider error with retry and severity metadata. */
|
|
13
|
+
export interface ClassifiedAIError {
|
|
14
|
+
code: AIErrorCode;
|
|
15
|
+
retryable: boolean;
|
|
16
|
+
retryAfterMs?: number;
|
|
17
|
+
severity: 'transient' | 'critical';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Classify an error from an Anthropic API call into an actionable category.
|
|
21
|
+
*
|
|
22
|
+
* | HTTP/Error | Category | Retryable | Severity |
|
|
23
|
+
* |------------------|--------------------|-----------|------------|
|
|
24
|
+
* | 429 | rate_limited | yes | transient |
|
|
25
|
+
* | 529 / 503 | overloaded | yes | transient |
|
|
26
|
+
* | 401 | auth_error | no | critical |
|
|
27
|
+
* | 402 / credits | credits_exhausted | no | critical |
|
|
28
|
+
* | Network/timeout | network | yes | transient |
|
|
29
|
+
* | Other 5xx | internal | no | critical |
|
|
30
|
+
*/
|
|
31
|
+
export declare function classifyAIError(err: unknown): ClassifiedAIError;
|
|
32
|
+
/**
|
|
33
|
+
* Map a classified error code to an HTTP status for the edge function response.
|
|
34
|
+
*/
|
|
35
|
+
export declare function mapClassifiedToHTTP(code: AIErrorCode): number;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Server-Side Cost Calculator
|
|
3
|
+
* @description Calculate per-request AI costs using SSOT pricing from `@donotdev/core`.
|
|
4
|
+
* Cost records are written async by platform wrappers (Supabase, Vercel, Firebase).
|
|
5
|
+
*
|
|
6
|
+
* @version 0.1.0
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
* @author AMBROISE PARK Consulting
|
|
9
|
+
*/
|
|
10
|
+
import type { AICostResult, AIUsage } from '@donotdev/core/server';
|
|
11
|
+
/**
|
|
12
|
+
* Calculate cost from token usage for a given model.
|
|
13
|
+
*
|
|
14
|
+
* @param usage - Token usage counts
|
|
15
|
+
* @param model - Model ID (must exist in AI_TOKEN_PRICING)
|
|
16
|
+
* @param markup - Markup multiplier (default: 4.0)
|
|
17
|
+
* @returns Cost breakdown with provider and billed amounts
|
|
18
|
+
* @throws Error if model is not in pricing table
|
|
19
|
+
*/
|
|
20
|
+
export declare function calculateCost(usage: AIUsage, model: string, markup?: number): AICostResult;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AIChatRequest, AICostRecord, AIRouteConfig } from '@donotdev/core/server';
|
|
2
|
+
/** Per-request options passed to the AI route handler. */
|
|
3
|
+
export interface AIRouteOptions {
|
|
4
|
+
/** Authenticated user ID */
|
|
5
|
+
userId: string;
|
|
6
|
+
/** Request ID for cost tracking */
|
|
7
|
+
requestId?: string;
|
|
8
|
+
/** Callback to persist cost record (platform-specific) */
|
|
9
|
+
onCostRecord?: (record: AICostRecord) => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Handle an AI chat request using Vercel AI SDK.
|
|
13
|
+
* Supports both streaming (returns SSE Response) and non-streaming (returns JSON).
|
|
14
|
+
*
|
|
15
|
+
* @param request - Validated AI chat request
|
|
16
|
+
* @param config - Route configuration
|
|
17
|
+
* @param options - Per-request options (userId, cost callback)
|
|
18
|
+
* @returns Response (SSE stream or JSON)
|
|
19
|
+
*/
|
|
20
|
+
export declare function handleAIRequest(request: AIChatRequest, config: AIRouteConfig, options: AIRouteOptions): Promise<Response>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { handleAIRequest } from './createAIRoute.js';
|
|
2
|
+
export type { AIRouteOptions } from './createAIRoute.js';
|
|
3
|
+
export { calculateCost } from './costTracker.js';
|
|
4
|
+
export { resolveModelForTier } from './resolveModelTier.js';
|
|
5
|
+
export { classifyAIError, mapClassifiedToHTTP } from './classifyAIError.js';
|
|
6
|
+
export type { AIErrorCode, ClassifiedAIError } from './classifyAIError.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AITierId } from '@donotdev/core/server';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve the AI model ID for a given tier.
|
|
4
|
+
*
|
|
5
|
+
* Resolution chain (first non-empty wins):
|
|
6
|
+
* 1. `AI_MODEL_<FUNCTION_NAME>` env var (per-function override)
|
|
7
|
+
* 2. `AI_TIER_<TIER>` env var (per-tier override)
|
|
8
|
+
* 3. `AI_TIERS.<TIER>.defaultModel` (static SSOT from constants)
|
|
9
|
+
*
|
|
10
|
+
* @param tier - The tier ID ('fast', 'standard', 'premium')
|
|
11
|
+
* @param functionName - Optional function name for per-function env var lookup
|
|
12
|
+
* @returns The resolved model ID string
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveModelForTier(tier: AITierId, functionName?: string): string;
|
|
@@ -16,7 +16,13 @@ import type { CreateCheckoutSessionRequest, CreateCheckoutSessionResponse, Strip
|
|
|
16
16
|
*/
|
|
17
17
|
export interface StripeCheckoutProvider {
|
|
18
18
|
createCheckoutSession(params: {
|
|
19
|
-
priceId
|
|
19
|
+
priceId?: string;
|
|
20
|
+
/** Amount in cents (dynamic pricing mode) */
|
|
21
|
+
unitAmount?: number;
|
|
22
|
+
/** Product name for Stripe checkout (dynamic pricing mode) */
|
|
23
|
+
productName?: string;
|
|
24
|
+
/** Currency code, defaults to 'eur' */
|
|
25
|
+
currency?: string;
|
|
20
26
|
customerEmail?: string;
|
|
21
27
|
metadata: Record<string, string>;
|
|
22
28
|
allowPromotionCodes: boolean;
|
|
@@ -43,7 +49,7 @@ export interface AuthCheckoutProvider {
|
|
|
43
49
|
/**
|
|
44
50
|
* Create checkout session algorithm
|
|
45
51
|
*
|
|
46
|
-
* @version 0.
|
|
52
|
+
* @version 0.2.0
|
|
47
53
|
* @since 0.0.1
|
|
48
54
|
* @author AMBROISE PARK Consulting
|
|
49
55
|
*/
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Email Route Handler
|
|
3
|
+
* @description Platform-agnostic handler for email send requests.
|
|
4
|
+
* Validates request, resolves template, renders with locale, sends via provider.
|
|
5
|
+
*
|
|
6
|
+
* @version 0.1.0
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
* @author AMBROISE PARK Consulting
|
|
9
|
+
*/
|
|
10
|
+
import type { EmailRouteConfig, SendEmailRequest, SendEmailResponse } from '@donotdev/core/server';
|
|
11
|
+
import type { EmailProviderAdapter } from './provider.js';
|
|
12
|
+
/**
|
|
13
|
+
* Handle an email send request.
|
|
14
|
+
* Validates input, resolves the template from the registry, renders with locale, and sends.
|
|
15
|
+
*
|
|
16
|
+
* @param request - Email send request (templateId, to, data, locale)
|
|
17
|
+
* @param config - Email route configuration (provider, templates, defaultFrom)
|
|
18
|
+
* @param provider - Email provider adapter (Resend, etc.)
|
|
19
|
+
* @returns Send result with success status and messageId
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const result = await handleEmailRequest(
|
|
24
|
+
* { templateId: 'order-confirmation', to: 'user@example.com', data: { orderId: '123' }, locale: 'fr' },
|
|
25
|
+
* emailConfig,
|
|
26
|
+
* resendProvider
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function handleEmailRequest(request: SendEmailRequest, config: EmailRouteConfig, provider: EmailProviderAdapter): Promise<SendEmailResponse>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Email Provider Adapter Interface
|
|
3
|
+
* @description Abstract interface for email sending. Providers implement this.
|
|
4
|
+
*
|
|
5
|
+
* @version 0.1.0
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @author AMBROISE PARK Consulting
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Email provider adapter interface.
|
|
11
|
+
* Each provider (Resend, SES, etc.) implements this single method.
|
|
12
|
+
*/
|
|
13
|
+
export interface EmailProviderAdapter {
|
|
14
|
+
/**
|
|
15
|
+
* Send an email via the provider.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Email parameters
|
|
18
|
+
* @returns Provider message ID for tracking
|
|
19
|
+
* @throws On provider error (network, auth, etc.)
|
|
20
|
+
*/
|
|
21
|
+
send(params: {
|
|
22
|
+
from: string;
|
|
23
|
+
to: string;
|
|
24
|
+
subject: string;
|
|
25
|
+
html: string;
|
|
26
|
+
replyTo?: string;
|
|
27
|
+
}): Promise<{
|
|
28
|
+
messageId: string;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Resend Email Provider
|
|
3
|
+
* @description Zero-dependency Resend implementation using raw fetch.
|
|
4
|
+
*
|
|
5
|
+
* @version 0.1.0
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @author AMBROISE PARK Consulting
|
|
8
|
+
*/
|
|
9
|
+
import type { EmailProviderAdapter } from './provider.js';
|
|
10
|
+
/**
|
|
11
|
+
* Create a Resend email provider adapter.
|
|
12
|
+
*
|
|
13
|
+
* @param apiKey - Resend API key
|
|
14
|
+
* @returns EmailProviderAdapter implementation
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const provider = createResendProvider(Deno.env.get('RESEND_API_KEY')!);
|
|
19
|
+
* await provider.send({ from: 'noreply@app.com', to: 'user@example.com', subject: 'Hello', html: '<p>Hi</p>' });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function createResendProvider(apiKey: string): EmailProviderAdapter;
|
package/lib/shared/github.d.ts
CHANGED
|
@@ -6,10 +6,12 @@
|
|
|
6
6
|
* @since 0.0.1
|
|
7
7
|
* @author AMBROISE PARK Consulting
|
|
8
8
|
*/
|
|
9
|
+
/** Configuration for authenticating with the GitHub API. */
|
|
9
10
|
export interface GitHubApiConfig {
|
|
10
11
|
accessToken: string;
|
|
11
12
|
baseUrl?: string;
|
|
12
13
|
}
|
|
14
|
+
/** GitHub user profile returned by the API. */
|
|
13
15
|
export interface GitHubUser {
|
|
14
16
|
id: number;
|
|
15
17
|
login: string;
|
|
@@ -17,6 +19,7 @@ export interface GitHubUser {
|
|
|
17
19
|
name?: string;
|
|
18
20
|
avatar_url: string;
|
|
19
21
|
}
|
|
22
|
+
/** GitHub repository metadata returned by the API. */
|
|
20
23
|
export interface GitHubRepository {
|
|
21
24
|
id: number;
|
|
22
25
|
name: string;
|
|
@@ -27,12 +30,14 @@ export interface GitHubRepository {
|
|
|
27
30
|
id: number;
|
|
28
31
|
};
|
|
29
32
|
}
|
|
33
|
+
/** Request payload for adding a collaborator to a GitHub repository. */
|
|
30
34
|
export interface AddCollaboratorRequest {
|
|
31
35
|
owner: string;
|
|
32
36
|
repo: string;
|
|
33
37
|
username: string;
|
|
34
38
|
permission?: 'pull' | 'triage' | 'push' | 'maintain' | 'admin';
|
|
35
39
|
}
|
|
40
|
+
/** Error response structure from the GitHub API. */
|
|
36
41
|
export interface GitHubApiError {
|
|
37
42
|
message: string;
|
|
38
43
|
documentation_url?: string;
|
package/lib/shared/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export * from './github.js';
|
|
|
11
11
|
export * from './metadata.js';
|
|
12
12
|
export * from './schema.js';
|
|
13
13
|
export * from './utils/index.js';
|
|
14
|
+
export * from './ai/index.js';
|
|
14
15
|
export * from './billing/index.js';
|
|
16
|
+
export * from './email/index.js';
|
|
15
17
|
export * from './oauth/index.js';
|
|
16
18
|
export { createTimestamp, toTimestamp, isTimestamp, transformFirestoreData, prepareForFirestore, } from './firebase.js';
|