diff --git a/src/class_library_client.ts b/src/class_library_client.ts new file mode 100644 index 0000000..6812ca4 --- /dev/null +++ b/src/class_library_client.ts @@ -0,0 +1,83 @@ +// https://gist.github.com/didiercrunch/8fbe1e26595e41817c92bed4efc44bcd +import {Book} from "./library_server.ts" + +class LibraryClient{ + host: string; + constructor(host: string) { + this.host = host; + } + + + async getPing(): Promise { + const resp = await fetch(this.host + "/ping") + const text = await resp.text() + console.log(text) + } + + extractIsbn(rawResponse: any): string[]{ + const ret: string[] = []; + for(const elm of rawResponse.books){ + ret.push(elm.isbn) + } + return ret; + } + + async getAllIsbn(): Promise{ + const resp = await fetch(this.host + "/books"); + const ret = await resp.json(); + return this.extractIsbn(ret); + + } + + async createBook(isbn: string, + author: string, + title: string, + publicationYear: number): Promise{ + const book = {isbn: isbn, author: author, title: title, publicationYear: publicationYear}; + + const resp = await fetch(this.host + "/books", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(book), + }); + return resp.status < 300 && resp.status > 199; + } + + async createFourBooks(): Promise{ + await this.createBook("0000", "jk rolling", "harry potter I", 2004); + await this.createBook("1111", "jk rolling", "harry potter II", 2005); + await this.createBook("2222", "jk rolling", "harry potter III", 2006); + await this.createBook("3333", "jk rolling", "harry potter IV", 2007); + } + + async getBook(isbn: string): Promise{ + const url = this.host + "/books/" + isbn; + const resp = await fetch(url); + const ret = await resp.json(); + return ret; + + } + + async getBookByAuthor(author: string): Promise{ + const urlParams = new URLSearchParams({ + author: author, + }); + + const url = this.host + "/books?" + urlParams; + const resp = await fetch(url); + const ret = await resp.json(); + return this.extractIsbn(ret); + } + +} + + + + +const client = new LibraryClient("http://localhost:3000"); +await client.createFourBooks(); +console.log(await client.getBookByAuthor("jk rolling")) + +