Skip to content

Commit 52b83ee

Browse files
committed
refactor: update API references and improve build scripts
- Changed the API server URL in the OpenAPI specification to a base URL. - Added a new build script for web examples. - Updated pnpm workspace to specify the web examples directory. - Removed unused playground configurations from Turbo. - Updated favicon links in the web example's HTML. - Enhanced API key handling in the API key input component. - Improved API reference page to handle server configurations better. - Cleaned up API key management logic. - Adjusted code snippet generation to include API key placeholders. - Updated request handling to ensure API key validation for pro endpoints. - Fixed server selection logic in the request example component.
1 parent b48dd0a commit 52b83ee

File tree

12 files changed

+51
-65
lines changed

12 files changed

+51
-65
lines changed

defillama-openapi-pro.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"servers": [
99
{
10-
"url": "https://pro-api.llama.fi/c9e9f83ece65eadff4c21f351436876944561d89c391e9ca516e40c618f3fa89"
10+
"url": "https://pro-api.llama.fi"
1111
}
1212
],
1313
"tags": [

examples/web/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
<link
66
rel="icon"
77
type="image/svg+xml"
8-
href="/favicon.svg" />
8+
href="https://defillama.com/favicon-32x32.png" />
99
<link
1010
rel="icon alternate"
1111
type="image/png"
12-
href="/favicon.png" />
12+
href="https://defillama.com/favicon-32x32.png" />
1313
<meta
1414
name="viewport"
1515
content="width=device-width, initial-scale=1.0" />
16-
<title>Scalar API Reference</title>
16+
<title>API Docs - DefiLlama</title>
1717
</head>
1818

1919
<body class="light-mode">

examples/web/src/components/ApiKeyInput.vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,7 @@ const handleKeyChange = (event: Event) => {
113113
Your API key will be injected into requests to
114114
<code>https://pro-api.llama.fi</code> endpoints.
115115
<br />
116-
<small>
117-
<strong>URL Format:</strong> https://pro-api.llama.fi/<span
118-
class="highlight"
119-
>your-api-key</span
120-
>/endpoint
121-
</small>
116+
When your API key is set, free endpoints will be changed to pro version to bypass rate limits"
122117
</p>
123118
</div>
124119
</div>

examples/web/src/pages/StandaloneApiReferencePage.vue

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import SlotPlaceholder from '../components/SlotPlaceholder.vue'
1313
const getUpdatedSpec = (baseUrl: string) => {
1414
if (baseUrl === 'https://pro-api.llama.fi') {
1515
const spec = JSON.parse(JSON.stringify(proSpecContent))
16-
spec.servers = [{ url: baseUrl }]
16+
if (!spec.servers) {
17+
spec.servers = [{ url: baseUrl }]
18+
}
1719
return spec
1820
}
1921
@@ -28,12 +30,21 @@ const getUpdatedSpec = (baseUrl: string) => {
2830
if (!freeSpec.paths[path]) {
2931
freeSpec.paths[path] = {}
3032
}
31-
freeSpec.paths[path][method] = proSpec.paths[path][method]
33+
34+
const proOperation = proSpec.paths[path][method]
35+
36+
// If the pro operation doesn't have its own server, use the pro spec's global server
37+
if (!proOperation.servers && proSpec.servers) {
38+
proOperation.servers = proSpec.servers
39+
}
40+
freeSpec.paths[path][method] = proOperation
3241
}
3342
}
3443
}
3544
36-
freeSpec.servers = [{ url: baseUrl }]
45+
if (!freeSpec.servers) {
46+
freeSpec.servers = [{ url: baseUrl }]
47+
}
3748
return freeSpec
3849
}
3950
@@ -71,7 +82,9 @@ const handleApiKeyChange = (apiKey: string | null) => {
7182
<template>
7283
<div class="standalone-api-reference-page">
7384
<ApiKeyInput @api-key-change="handleApiKeyChange" />
74-
<ApiReference :configuration="configuration">
85+
<ApiReference
86+
:configuration="configuration"
87+
:key="currentBaseUrl">
7588
<template #footer><SlotPlaceholder>footer</SlotPlaceholder></template>
7689
</ApiReference>
7790
</div>

package.json

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"scripts": {
4646
"build": "turbo build",
47+
"build:web": "turbo build --filter=@scalar-examples/web...",
4748
"build:packages": "turbo --filter './packages/**' --concurrency=100% build",
4849
"build:integrations": "turbo --filter ./integrations/** build",
4950
"build:api-reference": "turbo --filter ./packages/api-reference build",
@@ -56,24 +57,10 @@
5657
"clean:nuxt": "shx rm -rf \"**/.nuxt\"",
5758
"clean:dist": "shx rm -rf \"**/dist\"",
5859
"clean:turbo": "shx rm -rf \"**/.turbo\"",
59-
"dev": "turbo dev --concurrency=100% --filter './examples/*' --filter './integrations/*' --filter=@scalar/nuxt --filter=@scalar/draggable --filter @scalar/components --filter @scalar/api-client --filter @scalar/nextjs-openapi",
60-
"dev:client": "turbo dev --filter @scalar/api-client",
61-
"dev:client:desktop": "turbo dev --filter scalar-app",
62-
"dev:client:app": "turbo playground:app --filter @scalar/api-client",
63-
"dev:client:modal": "turbo playground:modal --filter @scalar/api-client",
64-
"dev:client:web": "turbo playground:web --filter @scalar/api-client",
65-
"dev:components": "turbo dev --filter @scalar/components",
66-
"dev:galaxy": "turbo dev --filter @scalar/galaxy",
67-
"dev:modal": "turbo playground:modal --filter @scalar/api-client",
68-
"dev:web-app": "turbo playground:web --filter @scalar/api-client",
69-
"dev:nuxt": "turbo dev --filter @scalar/nuxt",
70-
"dev:proxy-server": "pnpm --filter proxy-scalar-com dev",
71-
"dev:reference": "turbo dev --filter @scalar/api-reference",
72-
"dev:reference:react": "turbo playground --filter @scalar/api-reference-react",
73-
"dev:void-server": "pnpm --filter @scalar/void-server dev",
60+
"dev": "turbo dev --concurrency=100% --filter=@scalar-examples/web",
7461
"dev:web": "turbo dev --filter @scalar-examples/web",
7562
"format:check": "pnpm prettier --check . && biome format",
76-
"format": "prettier --write . && biome format --write",
63+
"format": "prettier --write . && biome format",
7764
"lint:check": "biome lint --diagnostic-level=error",
7865
"lint:vue": "pnpm eslint '**/*.vue'",
7966
"lint:fix": "biome lint --write && pnpm eslint '**/*.vue' --fix",

packages/api-client/src/components/AddressBar/AddressBar.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ function handleExecuteRequest() {
141141
if (isRequesting.value) {
142142
return
143143
}
144-
isRequesting.value = true
145144
events.executeRequest.emit({ requestUid: operation.uid })
146145
}
147146

packages/api-client/src/libs/api-key-manager.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const getApiKeyValue = (workspaceId: string): string | null => {
9393
/**
9494
* List of URLs that require API key injection as path segments
9595
*/
96-
const API_KEY_REQUIRED_URLS = ['https://pro-api.llama.fi']
96+
// const API_KEY_REQUIRED_URLS = ['https://pro-api.llama.fi']
9797

9898
/**
9999
* Checks if a given URL requires API key injection in the path
@@ -103,22 +103,7 @@ const API_KEY_REQUIRED_URLS = ['https://pro-api.llama.fi']
103103
*/
104104
export const doesUrlRequireApiKey = (url: string): boolean => {
105105
if (!url) return false
106-
107-
try {
108-
// Handle both full URLs and relative URLs
109-
const urlToCheck = url.startsWith('http') ? url : `https://${url}`
110-
const urlObj = new URL(urlToCheck)
111-
const baseUrl = `${urlObj.protocol}//${urlObj.host}`
112-
113-
return API_KEY_REQUIRED_URLS.some(
114-
(requiredUrl) =>
115-
baseUrl.toLowerCase() === requiredUrl.toLowerCase() ||
116-
baseUrl.toLowerCase().startsWith(requiredUrl.toLowerCase()),
117-
)
118-
} catch (error) {
119-
console.warn('Invalid URL format:', url, error)
120-
return false
121-
}
106+
return url.startsWith('https://pro-api.llama.fi')
122107
}
123108

124109
/**

packages/api-client/src/views/Components/CodeSnippet/helpers/get-snippet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import type { Request as HarRequest } from 'har-format'
55
/** Key used to hack around the invalid urls */
66
const INVALID_URLS_PREFIX = 'ws://replace.me'
77

8+
const injectApiKeyPlaceholder = (url: string) => {
9+
if (url.startsWith('https://pro-api.llama.fi')) {
10+
const urlParts = new URL(url)
11+
return `${urlParts.origin}/<API-KEY>${urlParts.pathname}${urlParts.search}`
12+
}
13+
return url
14+
}
15+
816
/**
917
* Returns a code example for given operation
1018
*/
@@ -18,6 +26,8 @@ export const getSnippet = <T extends TargetId>(
1826
return [new Error('Please enter a URL to see a code snippet'), null]
1927
}
2028

29+
harRequest.url = injectApiKeyPlaceholder(harRequest.url)
30+
2131
const separator = harRequest.url.startsWith('/') ? '' : '/'
2232

2333
// Hack to get around invalid URLS until we update the snippets lib

packages/api-client/src/views/Request/RequestRoot.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useClientConfig } from '@/hooks/useClientConfig'
1111
import { useSidebar } from '@/hooks/useSidebar'
1212
import { ERRORS } from '@/libs'
1313
import { createRequestOperation } from '@/libs/send-request'
14+
import { getApiKey, doesUrlRequireApiKey } from '@/libs/api-key-manager'
1415
import type { SendRequestResult } from '@/libs/send-request/create-request-operation'
1516
import { validateParameters } from '@/libs/validate-parameters'
1617
import { usePluginManager } from '@/plugins'
@@ -69,6 +70,17 @@ const executeRequest = async () => {
6970
return
7071
}
7172
73+
if (activeWorkspace.value) {
74+
const apiKey = getApiKey(activeWorkspace.value.uid)
75+
if (
76+
doesUrlRequireApiKey(activeServer.value?.url ?? '') &&
77+
(!apiKey || !apiKey.key)
78+
) {
79+
toast('Please set an API key to use this pro endpoint.', 'warn')
80+
return
81+
}
82+
}
83+
7284
invalidParams.value = validateParameters(activeExample.value)
7385
7486
const environmentValue =

packages/api-reference/src/v2/blocks/scalar-request-example-block/components/RequestExample.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const generatedCode = computed<string>(() => {
206206
clientId: localSelectedClient.value.id as AvailableClients[number],
207207
operation,
208208
method,
209-
server: selectedServer,
209+
server: operation.servers?.[0] ?? selectedServer,
210210
securitySchemes,
211211
contentType: selectedContentType,
212212
path,

0 commit comments

Comments
 (0)