Skip to content

Commit

Permalink
feat: migrate to vercel edge function (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Oct 27, 2023
1 parent ec2e38c commit 063bd02
Show file tree
Hide file tree
Showing 4 changed files with 4,131 additions and 2,145 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.*cache
.vercel
*.tsbuildinfo
/public/index.html
node_modules
52 changes: 30 additions & 22 deletions api/[owner]/[app]/[version].ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import { VercelRequest, VercelRequestQuery, VercelResponse } from '@vercel/node'
import got from 'got'

export interface RequestParams extends VercelRequestQuery {
owner: string
app: string
version: string
}

export interface ReleaseInfo {
id: string
version: string
Expand All @@ -17,13 +8,21 @@ export interface DownloadInfo {
download_url: string
}

const REDIRECT = 302
const NOT_FOUND = 404

export default async (
req: VercelRequest,
res: VercelResponse,
): Promise<void> => {
let { owner, app, version } = req.query as RequestParams
export const config = {
runtime: 'experimental-edge',
}

export default async (req: Request): Promise<Response> => {
const url = new URL(req.url, 'https://example.com')

const searchParams = url.searchParams

const owner = searchParams.get('owner')!
const app = searchParams.get('app')!
let version = searchParams.get('version')!

if (version.includes(',')) {
version = version.split(',')[0]
Expand All @@ -36,27 +35,36 @@ export default async (

console.log(`Fetching ${releasesUrl}`)

const releases = await got(releasesUrl).json<ReleaseInfo[]>()
const releasesRes = await fetch(releasesUrl)
const releases = (await releasesRes.json()) as ReleaseInfo[]

const matched = releases.find(
it => it.version === version || it.short_version === version,
)

if (!matched) {
res.status(NOT_FOUND)
res.send(`No matched version ${version} found for ${owner}/${app}`)
return
return new Response(
`No matched version ${version} found for ${owner}/${app}`,
{
status: NOT_FOUND,
},
)
}

const releaseUrl = `https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/releases/${matched.id}`

console.log(`Fetching ${releaseUrl}`)

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

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

res.redirect(downloadUrl)
return new Response(null, {
status: REDIRECT,
headers: {
Location: downloadUrl,
},
})
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
"start": "vc dev"
},
"dependencies": {
"got": "^11.8.5"
"@vercel/edge": "^0.3.1"
},
"devDependencies": {
"@1stg/common-config": "^7.2.0",
"@octokit/request": "^6.2.3",
"tsx": "^3.12.3",
"typescript": "^4.8.4",
"vercel": "^28.4.12",
"@types/web": "^0.0.99",
"tsx": "^3.12.6",
"typescript": "^5.0.2",
"vercel": "^28.18.2",
"yarn-deduplicate": "^6.0.1"
},
"resolutions": {
"prettier": "^2.8.4"
"prettier": "^2.8.7"
},
"commitlint": {
"extends": "@1stg"
Expand Down
Loading

0 comments on commit 063bd02

Please sign in to comment.