-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add proper error handling, fix the logger
- Loading branch information
1 parent
00994da
commit 03914d9
Showing
12 changed files
with
292 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,27 @@ | ||
import { Router } from 'express'; | ||
const router = Router(); | ||
import { APIRouteInfos } from "../../utils"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const routeInfos: APIRouteInfos[] = []; | ||
const files = fs.readdirSync(__dirname).filter(file => file !== path.basename(__filename) && file !== "index.ts" && file !== "index.js"); | ||
for (const file of files) { | ||
const infos = require('./' + file).infos; | ||
routeInfos.push(infos); | ||
} | ||
|
||
router.get("/", (req, res) => { | ||
res.status(200).json([ | ||
{ | ||
"name": "random", | ||
"description": "Get random images", | ||
"path": "/random", | ||
"methods": ["GET"], | ||
"parameters": [ | ||
{ | ||
"name": "type", | ||
"description": "Type of image", | ||
"required": true, | ||
"type": "string", | ||
"values": ["neko", "kitsune", "pat", "kisse", "hug", "lewd", "slap"] | ||
} | ||
], | ||
"queries": [], | ||
"body": [] | ||
}, | ||
{ | ||
"name": "8ball", | ||
"description": "Get a random 8ball response", | ||
"path": "/8ball", | ||
"methods": ["GET"], | ||
"parameters": [], | ||
"queries": [ | ||
{ | ||
"name": "cute", | ||
"description": "If you want the response to be cute", | ||
"required": false, | ||
"type": "boolean" | ||
} | ||
], | ||
"body": [] | ||
}, | ||
{ | ||
"name": "owoify", | ||
"description": "Owoify text (make it cute)", | ||
"path": "/owoify", | ||
"methods": ["GET", "POST"], | ||
"parameters": [], | ||
"queries": [ | ||
{ | ||
"name": "text", | ||
"description": "Text to owoify", | ||
"required": true, | ||
"type": "string" | ||
} | ||
], | ||
"body": [ | ||
{ | ||
"name": "text", | ||
"description": "Text to owoify", | ||
"required": true, | ||
"type": "string", | ||
"max_length": 2000, | ||
"min_length": 1 | ||
} | ||
] | ||
} | ||
]); | ||
res.status(200).json(routeInfos); | ||
}); | ||
|
||
export default router; | ||
export default router; | ||
export const infos: APIRouteInfos = { | ||
name: "endpoints", | ||
description: "Get all available endpoints", | ||
path: "/endpoints", | ||
methods: ["GET"], | ||
parameters: [], | ||
queries: [], | ||
body: [] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,42 @@ | ||
import { Router } from 'express'; | ||
const router = Router(); | ||
import fs from 'fs'; | ||
import { getBaseURL } from "../../utils"; | ||
import fs from 'fs/promises'; | ||
import { APIRouteInfos, formatType, getBaseURL } from "../../utils"; | ||
import { BadRequestError } from "../../structures/errors"; | ||
|
||
router.get("/", (req, res) => { | ||
res.redirect("/api/v1/random/neko"); | ||
}); | ||
|
||
router.get('/:type', (req, res) => { | ||
router.get('/:type', async (req, res, next) => { | ||
const type = req.params.type; | ||
const types = ['neko', 'kitsune', 'pat', 'kiss', 'hug', 'lewd', 'slap']; | ||
if (!types.includes(type)) throw new BadRequestError("Invalid Type"); | ||
|
||
const types = ['all', 'neko', 'kitsune', 'pat', 'kiss', 'hug', 'lewd', 'slap']; | ||
if(!types.includes(type)) return res.status(400).json({ | ||
status: 400, | ||
error: 'Bad Request', | ||
message: 'Invalid type' | ||
}); | ||
|
||
let files: string[] = []; | ||
const scanType = type === 'kiss' ? 'kisses' : type + 's'; | ||
|
||
if(type === 'all') { | ||
for (const type of types) { | ||
if(type === 'all') continue; | ||
const scanType = type === 'kiss' ? 'kisses' : type + 's'; | ||
const dirFiles = fs.readdirSync(`./images/${scanType}`); | ||
files.push(...dirFiles); | ||
} | ||
} | ||
else files = fs.readdirSync(`./images/${scanType}`); | ||
|
||
const scanType = formatType(type); | ||
let files: string[] = await fs.readdir(`./images/${scanType}`); | ||
const image = files[Math.floor(Math.random() * files.length)]; | ||
|
||
return res.status(200).json({ | ||
"url": getBaseURL(req) + "/images/" + scanType + "/" + image | ||
url: getBaseURL(req) + "/images/" + scanType + "/" + image | ||
}); | ||
}); | ||
|
||
export default router; | ||
export default router; | ||
export const infos: APIRouteInfos = { | ||
name: "random", | ||
description: "Get a random image", | ||
path: "/random", | ||
methods: ["GET"], | ||
parameters: [ | ||
{ | ||
name: "type", | ||
description: "The type of image", | ||
required: true, | ||
type: "string", | ||
values: ["neko", "kitsune", "pat", "kiss", "hug", "lewd", "slap"] | ||
} | ||
], | ||
queries: [], | ||
body: [] | ||
}; |
Oops, something went wrong.