1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 20:40:32 +09:00
namae/api/services/appstore/[query].ts

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-08-31 08:41:53 +09:00
import { send, sendError, fetch } from '../../../util/http';
2022-03-09 15:16:54 +09:00
import { VercelRequest, VercelResponse } from '@vercel/node';
2019-08-06 01:17:29 +09:00
2019-08-30 16:40:22 +09:00
interface App {
2020-08-31 08:41:53 +09:00
trackId: string;
trackName: string;
kind: string;
version: string;
price: string;
trackViewUrl: string;
2022-04-13 15:23:55 +09:00
sellerName: string;
formattedPrice: string;
2019-08-30 16:40:22 +09:00
}
interface AppStoreResponse {
2020-08-31 08:41:53 +09:00
results: App[];
2019-08-30 16:40:22 +09:00
}
export default async function handler(
2022-03-09 15:16:54 +09:00
req: VercelRequest,
res: VercelResponse
2019-12-24 01:57:07 +09:00
): Promise<void> {
2020-08-31 08:41:53 +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-31 08:41:53 +09:00
return sendError(res, new Error('No query given'));
2019-08-06 01:17:29 +09:00
}
2020-08-31 08:41:53 +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'
2020-08-31 08:41:53 +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,
2022-04-13 15:23:55 +09:00
author: app.sellerName,
2019-08-06 01:17:29 +09:00
viewURL: app.trackViewUrl,
2020-08-31 08:41:53 +09:00
}));
2020-08-31 09:45:25 +09:00
send(res, { result: apps });
2022-03-09 15:16:54 +09:00
} catch (err: any) {
2020-08-31 08:41:53 +09:00
sendError(res, err);
2019-08-06 01:17:29 +09:00
}
}