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 @@