-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
74 lines (62 loc) · 1.81 KB
/
index.js
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
const Boom = require('@hapi/boom')
const express = require('express')
const { arm } = require('@kawaiioverflow/arm')
const app = express()
/**
* Return arm or Map by Service (return array)
*/
app.get('/api/arm', async (req, res, next) => {
try {
const { service } = req.query
if ( service ) {
if (service !== 'mal' && service !== 'annict' && service !== 'anilist' && service !== 'syobocal') {
return next(Boom.badRequest('service can be mal, annict, anilist or syobocal'))
}
return res.json(arm.map((anime) => anime[`${service}_id`]))
}
return res.json(arm)
} catch (error) {
return next(Boom.internal())
}
})
/**
* Endpoint
*/
app.get('/api/ids', async (req, res, next) => {
try {
const { service, id } = req.query
if (service == undefined || id == undefined) {
return next(Boom.badRequest('service and id are required'))
}
if (service !== 'mal' && service !== 'annict' && service !== 'anilist' && service !== 'syobocal') {
return next(Boom.badRequest('service can be mal, annict, anilist or syobocal'))
}
if (isNaN(Number(id))) {
return next(Boom.badRequest('id must be a number'))
}
const anime = arm.find((anime) => {
return anime[`${service}_id`] == Number(id) || anime[`${service}_tid`] == Number(id)
})
if (anime == undefined) {
return next(Boom.notFound())
}
return res.json(anime)
} catch (error) {
return next(Boom.internal())
}
})
/**
* Not Found Error Handler
*/
app.use((req, res, next) => {
return next(Boom.notFound())
})
/**
* Global Error Handler
*/
app.use((err, req, res, next) => {
return Boom.isBoom(err)
? res.status(err.output.statusCode).json(err.output.payload)
: res.status(500).json(Boom.internal().output.payload)
})
app.listen(process.env.PORT || 3000)