1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-19 05:20:33 +09:00
namae/api/services/appstore.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-08-30 16:40:22 +09:00
import { send, sendError, fetch, NowRequest, NowResponse } from '../util/http'
2019-08-06 01:17:29 +09:00
2019-08-30 16:40:22 +09:00
interface App {
trackId: string
trackName: string
kind: string
version: string
price: string
trackViewUrl: string
}
interface AppStoreResponse {
results: App[]
}
export default async function handler(
req: NowRequest<{ query: string; country: string }>,
res: NowResponse
) {
const { query, country } = req.query
2019-08-06 01:17:29 +09:00
if (!query) {
2019-08-27 03:15:35 +09:00
return sendError(res, new Error('no query given'))
2019-08-06 01:17:29 +09:00
}
const term = encodeURIComponent(query)
2019-08-07 14:08:45 +09:00
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}`,
2019-08-06 01:17:29 +09:00
'GET'
)
2019-08-30 16:40:22 +09:00
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,
}))
send(res, { result: apps || [] })
} catch (err) {
sendError(res, err)
}
}