Files
namae/api/services/appstore.js
T

33 lines
847 B
JavaScript
Raw Normal View History

2019-08-06 01:17:29 +09:00
const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => {
2019-08-07 13:38:27 +09:00
const { query, country } = req.query
2019-08-06 01:17:29 +09:00
if (!query) {
return res.status(400).json({ error: 'no query given' })
}
const term = encodeURIComponent(query)
2019-08-07 13:38:27 +09:00
// const country = 'us'
2019-08-06 01:17:29 +09:00
const limit = 3
try {
const response = await fetch(
2019-08-07 13:38:27 +09:00
`https://itunes.apple.com/search?media=software&entity=software,iPadSoftware,macSoftware&country=${country}&limit=${limit}&term=${term}`,
2019-08-06 01:17:29 +09:00
'GET'
)
const body = await response.json()
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)
}
}