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 037028a
Show file tree
Hide file tree
Showing 5 changed files with 1,432 additions and 1,421 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn

- name: Enable Corepack
run: corepack enable

- name: Install Dependencies
run: yarn --immutable

Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "JounQin (https://www.1stG.me) <[email protected]>",
"license": "MIT",
"private": true,
"packageManager": "yarn@4.0.1",
"packageManager": "yarn@4.1.0",
"scripts": {
"build": "tsx scripts/build",
"lint": "run-p 'lint:*'",
Expand Down
Loading

0 comments on commit 037028a

Please sign in to comment.