Skip to content

Commit

Permalink
fix: handle upstream response error
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Feb 5, 2024
1 parent baf5fec commit 405c6b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Stable link that redirects to unstable [App Center][] download link via Vercel.

## Examples

1. `ClashX Pro`: https://appcenter.vercel.app/clashx/clashx-pro/1.30.3.2

2. `Timeless`: The followings version and short version are both fine.
1. `Timeless`: The followings version and short version are both fine.

- https://appcenter.vercel.app/dangercove/timeless/2020.7
- https://appcenter.vercel.app/dangercove/timeless/37

![Timeless](https://user-images.githubusercontent.com/8336744/98822766-30e03500-246c-11eb-8cab-f7c31d196f5a.png)

2. ~~`ClashX Pro`: https://appcenter.vercel.app/clashx/clashx-pro/1.30.3.2~~ (The source project is not available anymore.)

## Sponsors

| 1stG | RxTS | UnTS |
Expand Down
26 changes: 23 additions & 3 deletions api/[owner]/[app]/[version].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export interface ReleaseInfo {
short_version: string
}

export interface ErrorResponse {
code: string
message: string
}

export interface DownloadInfo {
download_url: string
}
Expand Down Expand Up @@ -40,7 +45,14 @@ export default async (req: Request): Promise<Response> => {
console.log(`Fetching ${releasesUrl}`)

const releasesRes = await fetch(releasesUrl, FETCH_OPTIONS)
const releases = (await releasesRes.json()) as ReleaseInfo[]

const releases = (await releasesRes.clone().json()) as
| ErrorResponse
| ReleaseInfo[]

if (!releasesRes.ok || !Array.isArray(releases)) {
return releasesRes
}

const matched = releases.find(
it => it.version === version || it.short_version === version,
Expand All @@ -60,8 +72,16 @@ export default async (req: Request): Promise<Response> => {
console.log(`Fetching ${releaseUrl}`)

const downloadInfoRes = await fetch(releaseUrl, FETCH_OPTIONS)
const { download_url: downloadUrl } =
(await downloadInfoRes.json()) as DownloadInfo

const downloadInfo = (await downloadInfoRes.json()) as
| DownloadInfo
| ErrorResponse

if (!downloadInfoRes.ok) {
return downloadInfoRes
}

const { download_url: downloadUrl } = downloadInfo as DownloadInfo

console.log(`Redirect to ${downloadUrl}`)

Expand Down

0 comments on commit 405c6b8

Please sign in to comment.