Simplified Ivago API wrapper in frontend

pull/53/head
Jef Roosens 2021-04-30 00:24:19 +02:00
parent 4b3ae8a9a4
commit f27775910f
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 15 additions and 30 deletions

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title> <title>Fej</title>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>

View File

@ -1,32 +1,18 @@
export class Street { export interface Street {
name: string; name: string
city: string; city: string
constructor(name: string, city: string) {
this.name = name;
this.city = city;
}
} }
export class Ivago { export async function search(search_term: string): Promise<Street[]> {
base_url: string; var base_url = import.meta.env.VITE_ENDPOINT as string
constructor(url: string) { var r = await fetch(`${base_url}/ivago/search?` + new URLSearchParams({
this.base_url = url; q: search_term,
}))
if (!r.ok) {
return Promise.reject()
} }
async search(search_term: string): Promise<Street[]> { return r.json()
var r = await fetch(`${this.base_url}/ivago/search?` + new URLSearchParams({
q: search_term,
}));
if (!r.ok) {
return Promise.reject();
}
var json = await r.json();
return json.map((o: {name: string, city: string}) => new Street(o.name, o.city));
}
} }

View File

@ -11,7 +11,7 @@
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue' import { defineComponent } from 'vue'
import { Street, Ivago } from '../api/ivago' import { Street, search } from '../api/ivago'
export default defineComponent({ export default defineComponent({
name: 'Ivago', name: 'Ivago',
@ -32,8 +32,7 @@ export default defineComponent({
return return
} }
new Ivago(import.meta.env.VITE_ENDPOINT as string) search(this.query)
.search(this.query)
.then((res: Street[]) => { .then((res: Street[]) => {
this.items = res this.items = res
this.msg = "" this.msg = ""