From 35ff2b34ff5554225dc22dc143ea8332cc977729 Mon Sep 17 00:00:00 2001 From: Ray C Date: Thu, 18 Apr 2024 22:21:56 +0800 Subject: [PATCH] Update util funciton --- src/app.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/app.js b/src/app.js index 7cd06a2..c2eaf96 100644 --- a/src/app.js +++ b/src/app.js @@ -6,13 +6,19 @@ const { log } = console; const utils = { log, async svg(name) { - return this.fetchText(`./src/components/svg/${name}.html`); + return this.getData(`./src/components/svg/${name}.html`, 'text'); }, - async fetchData(api) { - return await fetch(api).then(res => res.json()); + async getData(url, type = 'json') { + const response = await fetch(url); + return await response[type](); }, - async fetchText(path) { - return await fetch(path).then(res => res.text()); + async postData(url, data) { + const response = await fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data), + }); + return await response.json(); }, async delay(delay = 0) { await new Promise(resolve => { @@ -79,7 +85,7 @@ const router = { async getPageHTML(path = this.currentPath) { const page = this.routes.find(route => route.path === path)?.component || '/404.html'; - return await utils.fetchText(`./src/pages${page}`); + return await utils.getData(`./src/pages${page}`, 'text'); }, }; @@ -94,6 +100,6 @@ const shop = { : this.products; }, async loadProducts() { - this.products = await utils.fetchData(api); + this.products = await utils.getData(api); }, };