From f37b0415f45af8e995cebd56b09d7bb4fb8d4619 Mon Sep 17 00:00:00 2001 From: Tobias Buschor Date: Tue, 4 Apr 2023 06:34:28 +0000 Subject: [PATCH] Apr 4, 2023, 8:34 AM --- drivers/denoFs.js | 4 ++-- drivers/rest.js | 50 +++++++++++++++++------------------------ drivers/sql/Items/Db.js | 8 +++++++ tests/rest.html | 31 +++++++------------------ tests/syncWith.html | 2 +- tools/AsyncItem.js | 5 +++++ 6 files changed, 45 insertions(+), 55 deletions(-) diff --git a/drivers/denoFs.js b/drivers/denoFs.js index badc3e0..1ba6b2b 100644 --- a/drivers/denoFs.js +++ b/drivers/denoFs.js @@ -45,7 +45,7 @@ class FsItem extends AsyncItem { return Deno.readTextFile(this.fsPath); } if (info.isDirectory) { - // this.loadAll(); + // this.loadItems(); // return this; const list = Object.create(null); for await (const dirEntry of Deno.readDir(this.fsPath)) { @@ -54,7 +54,7 @@ class FsItem extends AsyncItem { return list; } } - // async loadAll() { // if directory, load all children, todo + // async loadItems() { // if directory, load all children, todo // for await (const dirEntry of Deno.readDir(this.fsPath)) { // this.item(dirEntry.name); // // this.item(dirEntry.name).type = dirEntry.isDirectory ? 'directory' : 'file'; diff --git a/drivers/rest.js b/drivers/rest.js index a37b39f..1087826 100644 --- a/drivers/rest.js +++ b/drivers/rest.js @@ -1,54 +1,46 @@ -// alfa version, todo! +// alpha! //import {Item} from '../item.js'; import {AsyncItem} from '../tools/AsyncItem.js'; // restApi -export function restApi(url, options){ +export function restApi(url, options={}){ // todo, cancel request if item turns into an object, great for proxified items class RestApiItem extends AsyncItem { static isPrimitive(){ return false; } createGetter() { - return itemRequest(this, 'GET').promise; + return itemRequest(this, 'GET'); } - createSetter(value) { - return itemRequest(this, 'PUT', JSON.stringify(value)).promise; + createSetter(value, signal) { + return itemRequest(this, 'PUT', JSON.stringify(value), signal); } + ChildClass = RestApiItem; } const root = new RestApiItem(); - function itemRequest(item, method, body){ + async function itemRequest(item, method, body, signal){ const headers = new Headers(); if (options?.auth) { const {username, password} = options.auth; headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password)); } - const controller = new AbortController(); - - setTimeout(() => controller.abort(), options?.timeout ?? 10000); - - const promise = fetchDelay(url + '/' + item.pathString, {headers, method, body, signal:controller.signal}).then(response => { - const value = response.json(); - //item.value = value; // no more a promise, trigger setter to update - return value; - }).catch(error => { - if (error.name === 'AbortError') { - console.log('fetch aborted'); - } else { - console.error(error); - } - }); - return {controller,promise}; - } - function fetchDelay(url, options){ // not direct fetch, can be aborted quickly - return new Promise(resolve => { - setTimeout(() => { - resolve(fetch(url, options)); - }, 1); - }); + //setTimeout(() => signal.abort(), options?.timeout ?? 10000); + + const endPoint = url + '/' + item.pathKeys.join('/'); + + const response = await fetch(endPoint, {headers, method, body, signal}); + let data = await response.json(); + + if (options.map) data = options.map(data); + + for (const[key, value] of Object.entries(data)) { + item.item(key); + } + + return data; } return root; diff --git a/drivers/sql/Items/Db.js b/drivers/sql/Items/Db.js index 330d4af..9bf842b 100644 --- a/drivers/sql/Items/Db.js +++ b/drivers/sql/Items/Db.js @@ -16,6 +16,14 @@ export class Db extends Item { return "'"+(value+'').replace(/'/g, "\'")+"'"; } + async loadItems(){ + const tables = await this.query("SHOW TABLES"); + for (const table of tables) { + this.item(table.key); + } + } + + // schema async setSchema(schema){ this.schema = schema; diff --git a/tests/rest.html b/tests/rest.html index 5fc8492..ab4063f 100644 --- a/tests/rest.html +++ b/tests/rest.html @@ -14,36 +14,21 @@

Test

import {restApi} from "../drivers/rest.js"; // rest api -/* +/* */ const api = restApi('https://restcountries.com/v3.1'); -api.item('currency').item('chf').value.then(chf => { - console.log(chf); -}); -*/ +const chf = await api.item('currency').item('chf').value; +console.log(chf) +/* * const api = restApi('https://reqres.in/api', {map: result => { return Object.fromEntries(result.data.map(item => [item.id, item])); }}); -api.item('user').value.then(data => { - console.log(api.item('user'), data) -}); - -const user2 = api.item('user').item('2'); - -user2.value.then(data => { - console.log(data) -}); - -user2.value = { - name: 'morpheus', - job: 'zion resident' -}; - -api.item('user').item('23').value.then(data => { - console.log(data) -}); +const i3 = api.item(2); +console.log(api) +console.log('3', await i3.value); +/* */ diff --git a/tests/syncWith.html b/tests/syncWith.html index 1562c0a..3feda1b 100644 --- a/tests/syncWith.html +++ b/tests/syncWith.html @@ -15,7 +15,7 @@

Test

import {item, effect} from "../item.js"; import {syncWith} from "../tools/syncWith.js"; -import {dump} from 'https://cdn.jsdelivr.net/gh/nuxodin/dump.js@1.2.1/mod.min.js'; +import {dump} from 'https://cdn.jsdelivr.net/gh/nuxodin/dump.js@1.2.2/mod.min.js'; const original = { diff --git a/tools/AsyncItem.js b/tools/AsyncItem.js index 7b83223..e17d4d4 100644 --- a/tools/AsyncItem.js +++ b/tools/AsyncItem.js @@ -15,12 +15,17 @@ export class AsyncItem extends Item { createGetter() { throw new Error('createGetter not implemented'); } createSetter(value) { throw new Error('createSetter not implemented (value: ' + value + ')'); } + //async loadKeys() { throw new Error('loadKeys not implemented'); } + //async loadItems() { throw new Error('loadItems not implemented'); } + $get() { return this.master.get(); } $set(value) { return this.master.set(value); } + + ChildClass = AsyncChild; }