2020-08-20 00:57:33 +09:00
|
|
|
import { send, sendError, fetch } from '../../../util/http'
|
|
|
|
import { NowRequest, NowResponse } from '@vercel/node'
|
2019-08-06 01:17:29 +09:00
|
|
|
|
2019-08-30 16:40:22 +09:00
|
|
|
interface App {
|
2020-08-20 00:57:33 +09:00
|
|
|
trackId: string
|
|
|
|
trackName: string
|
|
|
|
kind: string
|
|
|
|
version: string
|
|
|
|
price: string
|
|
|
|
trackViewUrl: string
|
2019-08-30 16:40:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
interface AppStoreResponse {
|
2020-08-20 00:57:33 +09:00
|
|
|
results: App[]
|
2019-08-30 16:40:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function handler(
|
2020-06-19 16:15:53 +09:00
|
|
|
req: NowRequest,
|
2020-08-20 00:57:33 +09:00
|
|
|
res: NowResponse
|
2019-12-24 01:57:07 +09:00
|
|
|
): Promise<void> {
|
2020-08-20 00:57:33 +09:00
|
|
|
const { query, country } = req.query
|
2019-08-06 01:17:29 +09:00
|
|
|
|
2020-06-19 16:15:53 +09:00
|
|
|
if (!query || typeof query !== 'string') {
|
2020-08-20 00:57:33 +09:00
|
|
|
return sendError(res, new Error('No query given'))
|
2019-08-06 01:17:29 +09:00
|
|
|
}
|
|
|
|
|
2020-08-20 00:57:33 +09:00
|
|
|
const term = encodeURIComponent(query)
|
|
|
|
const countryCode = country || 'us'
|
|
|
|
const limit = 10
|
2019-08-06 01:17:29 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
2019-08-07 14:08:45 +09:00
|
|
|
`https://itunes.apple.com/search?media=software&entity=software,iPadSoftware,macSoftware&country=${countryCode}&limit=${limit}&term=${term}`,
|
2020-08-20 00:57:33 +09:00
|
|
|
'GET'
|
|
|
|
)
|
|
|
|
const body: AppStoreResponse = await response.json()
|
2019-08-06 01:17:29 +09:00
|
|
|
const apps = body.results.map((app) => ({
|
|
|
|
id: app.trackId,
|
|
|
|
name: app.trackName,
|
|
|
|
kind: app.kind,
|
|
|
|
version: app.version,
|
|
|
|
price: app.price,
|
|
|
|
viewURL: app.trackViewUrl,
|
2020-08-20 00:57:33 +09:00
|
|
|
}))
|
|
|
|
send(res, { result: apps || [] })
|
2019-08-06 01:17:29 +09:00
|
|
|
} catch (err) {
|
2020-08-20 00:57:33 +09:00
|
|
|
sendError(res, err)
|
2019-08-06 01:17:29 +09:00
|
|
|
}
|
|
|
|
}
|