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" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<title>Fej</title>
</head>
<body>
<div id="app"></div>

View File

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

View File

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