Skip to content

Commit 80a89ce

Browse files
Update lib utilities and add morph implementation
1 parent 76cc42a commit 80a89ce

19 files changed

+210
-2616
lines changed

lib/analytics-events.ts

Lines changed: 0 additions & 419 deletions
This file was deleted.

lib/analytics-service.ts

Lines changed: 0 additions & 434 deletions
This file was deleted.

lib/api-errors.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export interface APIError {
2+
statusCode?: number
3+
message: string
4+
}
5+
6+
export function isRateLimitError(error: any): boolean {
7+
return (
8+
error &&
9+
(error.statusCode === 429 ||
10+
error.message.toLowerCase().includes('limit') ||
11+
error.message.toLowerCase().includes('billing'))
12+
)
13+
}
14+
15+
export function isOverloadedError(error: any): boolean {
16+
return error && (error.statusCode === 529 || error.statusCode === 503)
17+
}
18+
19+
export function isAccessDeniedError(error: any): boolean {
20+
return error && (error.statusCode === 403 || error.statusCode === 401)
21+
}
22+
23+
export function handleAPIError(
24+
error: any,
25+
context?: { hasOwnApiKey?: boolean },
26+
): Response {
27+
// Log the error for debugging
28+
console.error('API Error:', error)
29+
30+
if (isRateLimitError(error)) {
31+
const message = context?.hasOwnApiKey
32+
? 'The provider is currently unavailable due to request limit.'
33+
: 'The provider is currently unavailable due to request limit. Try using your own API key.'
34+
35+
return new Response(message, { status: 429 })
36+
}
37+
38+
if (isOverloadedError(error)) {
39+
return new Response(
40+
'The provider is currently unavailable. Please try again later.',
41+
{ status: 529 },
42+
)
43+
}
44+
45+
if (isAccessDeniedError(error)) {
46+
return new Response(
47+
'Access denied. Please make sure your API key is valid.',
48+
{ status: 403 },
49+
)
50+
}
51+
52+
// Generic error handling
53+
return new Response(
54+
'An unexpected error has occurred. Please try again later.',
55+
{ status: 500 },
56+
)
57+
}
58+
59+
export function createRateLimitResponse(limit: {
60+
amount: number
61+
remaining: number
62+
reset: number
63+
}): Response {
64+
return new Response('You have reached your request limit for the day.', {
65+
status: 429,
66+
headers: {
67+
'X-RateLimit-Limit': limit.amount.toString(),
68+
'X-RateLimit-Remaining': limit.remaining.toString(),
69+
'X-RateLimit-Reset': limit.reset.toString(),
70+
},
71+
})
72+
}

lib/auth-utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { NextResponse } from 'next/server'
22
import { createServerClient } from '@/lib/supabase-server'
33
import type { User } from '@supabase/supabase-js'
44

5-
/**
6-
* Authenticate a user from a request and return the user object or an error response
7-
* @returns Object containing either the user or an error response
8-
*/
95
export async function authenticateUser(): Promise<{ user: User; error: null } | { user: null; error: NextResponse }> {
106
try {
117
const supabase = createServerClient()
@@ -33,12 +29,6 @@ export async function authenticateUser(): Promise<{ user: User; error: null } |
3329
}
3430
}
3531
}
36-
37-
/**
38-
* Higher-order function that wraps API route handlers with authentication
39-
* @param handler The API route handler function
40-
* @returns Wrapped handler that includes authentication
41-
*/
4232
export function withAuth<T extends any[]>(
4333
handler: (user: User, ...args: T) => Promise<NextResponse>
4434
) {

0 commit comments

Comments
 (0)