Repository navigation
Expand file tree
/
Copy pathopinetHandlers.ts
More file actions
112 lines (103 loc) · 3.91 KB
/
Copy pathopinetHandlers.ts
File metadata and controls
112 lines (103 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { geocodeBindings } from '../utils/geocode.js';
import { getErrorMessage } from '../core/errors.js';
import {
fetchOpinetAveragePrices,
fetchOpinetLowestStations,
fetchOpinetStationDetail,
fetchOpinetStationsAround,
} from '../services/opinet/client.js';
import { type ApiContext, errorResponse, successResponse } from './response.js';
function parseNumber(value: string | undefined): number | undefined {
if (!value) {
return undefined;
}
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
function parseInteger(value: string | undefined): number | undefined {
const parsed = parseNumber(value);
return parsed === undefined ? undefined : Math.trunc(parsed);
}
export async function handleOpinetAveragePrices(c: ApiContext) {
try {
const result = await fetchOpinetAveragePrices({
apiKey: c.env?.OPINET_API_KEY,
timeoutMs: parseInteger(c.req.query('timeoutMs')),
});
return successResponse(c, result, { total: result.count, page: 1, pageSize: result.count });
} catch (error) {
return errorResponse(c, 'OPINET_AVERAGE_FAILED', getErrorMessage(error), 500);
}
}
export async function handleOpinetLowestStations(c: ApiContext) {
try {
const result = await fetchOpinetLowestStations(
{
fuelCode: c.req.query('fuelCode') || c.req.query('prodcd'),
areaCode: c.req.query('areaCode') || c.req.query('area'),
count: parseInteger(c.req.query('count') || c.req.query('cnt')),
},
{
apiKey: c.env?.OPINET_API_KEY,
timeoutMs: parseInteger(c.req.query('timeoutMs')),
},
);
return successResponse(c, result, { total: result.count, page: 1, pageSize: result.count });
} catch (error) {
return errorResponse(c, 'OPINET_LOWEST_FAILED', getErrorMessage(error), 500);
}
}
export async function handleOpinetStationsAround(c: ApiContext) {
const x = parseNumber(c.req.query('x'));
const y = parseNumber(c.req.query('y'));
const latitude = parseNumber(c.req.query('latitude') || c.req.query('lat'));
const longitude = parseNumber(c.req.query('longitude') || c.req.query('lng'));
const location = c.req.query('location') || c.req.query('keyword') || '';
const hasKatec = x !== undefined && y !== undefined;
const hasCoordinates = latitude !== undefined && longitude !== undefined;
const hasLocation = location.trim().length > 0;
if (!hasKatec && !hasCoordinates && !hasLocation) {
return errorResponse(
c,
'MISSING_LOCATION',
'KATEC x/y, 위도/경도(lat/lng), 또는 location 중 하나를 입력해주세요.',
);
}
try {
const result = await fetchOpinetStationsAround(
{
x,
y,
latitude,
longitude,
location,
radiusMeters: parseInteger(c.req.query('radiusMeters') || c.req.query('radius')),
fuelCode: c.req.query('fuelCode') || c.req.query('prodcd'),
sort: c.req.query('sort'),
},
{
apiKey: c.env?.OPINET_API_KEY,
...geocodeBindings(c.env),
timeoutMs: parseInteger(c.req.query('timeoutMs')),
},
);
return successResponse(c, result, { total: result.count, page: 1, pageSize: result.count });
} catch (error) {
return errorResponse(c, 'OPINET_AROUND_FAILED', getErrorMessage(error), 500);
}
}
export async function handleOpinetStationDetail(c: ApiContext) {
const stationId = c.req.query('stationId') || c.req.query('id') || '';
if (!stationId.trim()) {
return errorResponse(c, 'MISSING_STATION_ID', 'stationId 또는 id를 입력해주세요.');
}
try {
const result = await fetchOpinetStationDetail(stationId, {
apiKey: c.env?.OPINET_API_KEY,
timeoutMs: parseInteger(c.req.query('timeoutMs')),
});
return successResponse(c, result, { total: result.station ? 1 : 0, page: 1, pageSize: result.station ? 1 : 0 });
} catch (error) {
return errorResponse(c, 'OPINET_DETAIL_FAILED', getErrorMessage(error), 500);
}
}