mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-21 02:40:08 +00:00
* feat(rest-proxy): init docker * refactor(rest-proxy): smaller size --------- Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
44 lines
943 B
TypeScript
44 lines
943 B
TypeScript
import type { RequestMethods } from '@discordeno/rest'
|
|
import express from 'express'
|
|
import { REST } from './rest.js'
|
|
|
|
const AUTHORIZATION = process.env.AUTHORIZATION as string
|
|
|
|
const app = express()
|
|
|
|
app.use(
|
|
express.urlencoded({
|
|
extended: true,
|
|
}),
|
|
)
|
|
|
|
app.use(express.json())
|
|
|
|
app.all('/*', async (req, res) => {
|
|
if (!AUTHORIZATION || AUTHORIZATION !== req.headers.authorization) {
|
|
return res.status(401).json({ error: 'Invalid authorization key.' })
|
|
}
|
|
|
|
try {
|
|
let url = req.originalUrl
|
|
if (url.startsWith('/v')) {
|
|
url = url.slice(url.indexOf('/', 2))
|
|
}
|
|
|
|
const result = await REST.makeRequest(req.method as RequestMethods, url, req.body)
|
|
|
|
if (result) {
|
|
res.status(200).json(result)
|
|
} else {
|
|
res.status(204).json()
|
|
}
|
|
} catch (error: any) {
|
|
console.log(error)
|
|
res.status(500).json(error)
|
|
}
|
|
})
|
|
|
|
app.listen(8000, () => {
|
|
console.log(`REST listening on port #8000`)
|
|
})
|