forked from ashish7802/devsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_utils.py
More file actions
98 lines (85 loc) · 2.26 KB
/
Copy pathapi_utils.py
File metadata and controls
98 lines (85 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# API Directory lib/utils.ts
api_utils = '''import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export interface API {
id: string
name: string
category: string
description: string
base_url: string
auth_type: 'none' | 'apikey' | 'oauth'
docs_url: string
example_endpoint: string
tags: string[]
status: 'online' | 'offline' | 'unknown'
}
export const CATEGORIES = [
'All',
'Weather',
'Finance',
'AI',
'Maps',
'Entertainment',
'Social',
'Developer Tools',
'News',
'Science',
'Data',
'Food & Drink',
'Books',
'Images',
'Security',
'Games'
] as const
export type Category = typeof CATEGORIES[number]
export function getAuthBadgeColor(authType: string): string {
switch (authType) {
case 'none':
return 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400'
case 'apikey':
return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400'
case 'oauth':
return 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400'
default:
return 'bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400'
}
}
export function generateCurlExample(api: API): string {
const url = api.base_url + api.example_endpoint
if (api.auth_type === 'apikey') {
return `curl "${url}" \\\
-H "Authorization: Bearer YOUR_API_KEY"`
} else if (api.auth_type === 'oauth') {
return `curl "${url}" \\\
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"`
}
return `curl "${url}"`
}
export function generateFetchExample(api: API): string {
const url = api.base_url + api.example_endpoint
let headers = ''
if (api.auth_type === 'apikey') {
headers = `, {
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
}`
} else if (api.auth_type === 'oauth') {
headers = `, {
headers: {
'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
}
}`
}
return `fetch('${url}'${headers})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));`
}
'''
with open(f"{base_path}/packages/api-directory/lib/utils.ts", "w") as f:
f.write(api_utils)
print("✅ API Directory utils.ts created")