-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawesome-data-view.ts
More file actions
134 lines (116 loc) · 3.39 KB
/
awesome-data-view.ts
File metadata and controls
134 lines (116 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { reactive, toRaw } from "vue";
import Pipelined from "./interface/Pipelined";
import TableRenderer from "./table-renderer";
export default class AwesomeDataView {
protected _data: Array<object> | object;
protected _server: {
endpoint: null | string;
options: {
method: string;
headers: {
[key: string]: any;
}
},
onPrepare?: null | Function,
} = {
endpoint: null,
options: {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
},
},
onPrepare: null,
};
protected _columns: Array<object>;
protected _pipelines: Array<Pipelined> = [];
protected _options = {
worker: false,
compression: false,
};
constructor() {
this._data = reactive([]);
this._columns = reactive([]);
}
endpoint(endpoint: string, options = null) {
if (!/^https?:\/\/(www.)?(\w+)\.(\w+)/.test(endpoint)) {
throw new Error("Endpoint should be url");
}
this._server.endpoint = endpoint;
if (options) {
this._server.options = options;
}
return this;
}
onPrepareQuery(fn: Function) {
this._server.onPrepare = fn;
return this;
}
data(data: Array<object>) {
this._data = reactive(data);
return this;
}
pipeline(pipeline: Pipelined) {
this._pipelines.push(pipeline);
return this;
}
pipelines(pipelines: Array<Pipelined>) {
this._pipelines = pipelines;
return this;
}
enableOptimization(
options = {
compression: false,
}
) {
this._options.worker = true;
this._options.compression = options.compression;
}
export(data) { }
protected processData() {
let data = toRaw(this._data);
this._pipelines.forEach((pipeline) => {
data = pipeline.handle(data);
});
return data;
}
query() {
let _q = '';
let body = {};
this._pipelines.forEach((pipeline, i) => {
let params = pipeline.toQuery();
if (!params) {
return;
}
body = { ...body, ...params }
});
if (this._server.onPrepare) {
body = this._server.onPrepare(body);
}
body = new URLSearchParams(body);
return _q + '&' + body;
}
protected async handleServerSide() {
let query = this._server.endpoint + "?" + this.query();
try {
const response = await fetch(query, this._server.options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Or text() for plain text
} catch (error) {
console.error("Fetch Error:", error);
throw error; // Rethrow for error handling elsewhere
}
}
async process() {
if (this._server.endpoint) {
return await this.handleServerSide();
}
return this.processData();
}
render(container: HTMLElement) {
// return TableRenderer.container(container).lightTheme().render(this.process());
}
}