mirror of
https://github.com/rjNemo/chuck-jokes
synced 2026-06-06 02:26:41 +00:00
small project
This commit is contained in:
commit
75f4989593
8 changed files with 3057 additions and 0 deletions
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/releases
|
||||
!.yarn/plugins
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
.pnp.*
|
||||
.idea/
|
||||
631
.yarn/releases/yarn-berry.cjs
vendored
Executable file
631
.yarn/releases/yarn-berry.cjs
vendored
Executable file
File diff suppressed because one or more lines are too long
2
.yarnrc.yml
Normal file
2
.yarnrc.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
yarnPath: ".yarn/releases/yarn-berry.cjs"
|
||||
nodeLinker: "pnp"
|
||||
3
README.md
Normal file
3
README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Chuck Jokes
|
||||
|
||||
Sample application to try `tinyhttp` framework.
|
||||
23
package.json
Normal file
23
package.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "chuck",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"start": "node src/index.js",
|
||||
"build": "tsc src/index.ts",
|
||||
"dev": "nodemon --watch './src' -e ts --exec 'ts-node src/index.ts'"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tinyhttp/app": "^1.3.8",
|
||||
"@tinyhttp/logger": "^1.3.0",
|
||||
"axios": "^0.21.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^15.12.2",
|
||||
"nodemon": "^2.0.7",
|
||||
"prettier": "^2.3.1",
|
||||
"ts-node": "^10.0.0",
|
||||
"typescript": "^4.3.2"
|
||||
}
|
||||
}
|
||||
64
src/index.ts
Normal file
64
src/index.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { App, Request, Response } from "@tinyhttp/app";
|
||||
import { logger } from "@tinyhttp/logger";
|
||||
import axios from "axios";
|
||||
|
||||
const PORT = 5000;
|
||||
|
||||
const jokeHandler = async (_req: Request, res: Response) => {
|
||||
const BASE_URL = "https://api.icndb.com/jokes/random?escape=javascript";
|
||||
|
||||
type Joke = {
|
||||
type: "success" | string;
|
||||
value: {
|
||||
id: number;
|
||||
joke: string;
|
||||
categories: string[];
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
const { data: joke } = await axios.get<Joke>(BASE_URL);
|
||||
|
||||
const body =
|
||||
joke.type === "success"
|
||||
? { joke: joke.value.joke }
|
||||
: { error: "Could not fetch joke" };
|
||||
|
||||
res.json(body);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new Error("Something bad happened");
|
||||
}
|
||||
};
|
||||
|
||||
type RouteConfig = {
|
||||
path: string;
|
||||
handler: (req: Request, res: Response) => void;
|
||||
};
|
||||
|
||||
const buildApplication = (routes: RouteConfig[]) => {
|
||||
const app = new App({
|
||||
onError: (err, _req, res) => {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
},
|
||||
}).use(logger());
|
||||
|
||||
routes.forEach(({ path, handler }) => {
|
||||
app.get(path, handler);
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
const routes = [{ path: "/", handler: jokeHandler }];
|
||||
|
||||
const app = buildApplication(routes);
|
||||
|
||||
const startApp = (app: App, port: number) => {
|
||||
app.listen(port, () =>
|
||||
console.log(`Server started on http://localhost:${port}`)
|
||||
);
|
||||
};
|
||||
|
||||
startApp(app, PORT);
|
||||
21
tsconfig.json
Normal file
21
tsconfig.json
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
|
||||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true /* Enable all strict type-checking options. */,
|
||||
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
|
||||
"strictNullChecks": true /* Enable strict null checks. */,
|
||||
/* Additional Checks */
|
||||
"noUnusedLocals": true /* Report errors on unused locals. */,
|
||||
"noUnusedParameters": true /* Report errors on unused parameters. */,
|
||||
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
|
||||
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
|
||||
/* Module Resolution Options */
|
||||
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
|
||||
/* Advanced Options */
|
||||
"skipLibCheck": true /* Skip type checking of declaration files. */,
|
||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue