-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenoPoke.ts
84 lines (70 loc) · 1.58 KB
/
denoPoke.ts
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
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
interface Pokemon {
name: string;
num: number;
type: string;
}
let pokemons: Array<Pokemon> = [
{
name: "Bulbasaur",
num: 1,
type: "plant",
},
{
name: "Ivysaur",
num: 2,
type: "plant",
},
{
name: "Venusaur",
num: 3,
type: "plant",
},
];
const env = Deno.env.toObject();
const PORT = env.PORT || 4000;
const HOST = env.HOST || "127.0.0.1";
export const getPokemon = ({
params,
response,
}: {
params: { name: string };
response: any;
}) => {
const pokemon = pokemons.filter((pokemon) => pokemon.name === params.name);
if (pokemon.length) {
response.status = 200;
response.body = pokemon[0];
return;
}
response.status = 400;
response.body = { msg: `Cannot find dog ${params.name}` };
};
export const addPokemon = async ({
request,
response,
}: {
request: any;
response: any;
}) => {
const body = await request.body();
const pokemon: Pokemon = body.value;
pokemons.push(pokemon);
response.body = { msg: "OK" };
response.status = 200;
};
export const getPokemons = ({ response }: { response: any }) => {
response.body = pokemons;
};
const router = new Router();
router
.get("/pokemons", getPokemons)
.get("/pokemons/:name", getPokemon)
.post("/pokemons", addPokemon);
// .put("/pokemons/:name", updatePokemon)
// .delete("/pokemons/:name", removePokemon);
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
console.log(`Listening on port ${PORT}...`);
await app.listen(`${HOST}:${PORT}`);