@wxn0brp/vql-client 0.0.13 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -62
- package/dist/index.d.ts +13 -13
- package/dist/index.js +11 -20
- package/dist/min.js +1 -0
- package/dist/vql.d.ts +49 -83
- package/package.json +9 -6
- package/dist/vql-client.cjs +0 -78
- package/dist/vql-client.min.js +0 -1
package/README.md
CHANGED
|
@@ -5,21 +5,16 @@ Minimalistic, pluggable client for **VQL** query execution.
|
|
|
5
5
|
Supports:
|
|
6
6
|
|
|
7
7
|
- ✅ ESM (`import`)
|
|
8
|
-
- ✅ CJS (`require`)
|
|
9
8
|
- ✅ CDN / `<script>` (`VQLClient`)
|
|
10
9
|
- ✅ Fully typed with TypeScript
|
|
11
10
|
- ✅ Custom transport layers and lifecycle hooks
|
|
12
11
|
|
|
13
|
-
##
|
|
12
|
+
## 🚀 Usage
|
|
14
13
|
|
|
15
|
-
###
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install @wxn0brp/vql-client
|
|
19
|
-
```
|
|
14
|
+
### ESM
|
|
20
15
|
|
|
21
16
|
```ts
|
|
22
|
-
import { fetchVQL,
|
|
17
|
+
import { fetchVQL, VConfig } from "@wxn0brp/vql-client";
|
|
23
18
|
|
|
24
19
|
const result = await fetchVQL("db1 user! s._id = xyz");
|
|
25
20
|
```
|
|
@@ -27,51 +22,43 @@ const result = await fetchVQL("db1 user! s._id = xyz");
|
|
|
27
22
|
### CDN
|
|
28
23
|
|
|
29
24
|
```html
|
|
30
|
-
<script src="/
|
|
25
|
+
<script src="https://unpkg.com/@wxn0brp/vql-client/dist/min.js"></script>
|
|
31
26
|
<script>
|
|
32
27
|
VQLClient.fetchVQL("db1 user! s._id = xyz").then(console.log);
|
|
33
28
|
</script>
|
|
34
29
|
```
|
|
35
30
|
|
|
36
|
-
You can also serve `vql-client.min.js` from your own CDN or static server.
|
|
37
|
-
|
|
38
31
|
## 🧠 Usage
|
|
39
32
|
|
|
40
33
|
### `fetchVQL<T = any>(query: string | object): Promise<T>`
|
|
41
34
|
|
|
42
35
|
Executes a VQL query and returns the result (unwrapped from `{ result }`, unless an error is present).
|
|
43
36
|
|
|
44
|
-
### `
|
|
37
|
+
### `VConfig`
|
|
45
38
|
|
|
46
39
|
Customize client behavior.
|
|
47
40
|
|
|
48
41
|
#### Example – custom transport and hooks:
|
|
49
42
|
|
|
50
43
|
```ts
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
});
|
|
44
|
+
VConfig.transport = async (query) => {
|
|
45
|
+
return await fetch("/VQL", {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "Content-Type": "application/json" },
|
|
48
|
+
body: JSON.stringify({ query })
|
|
49
|
+
}).then(res => res.json());
|
|
50
|
+
}
|
|
51
|
+
VConfig.hooks = {
|
|
52
|
+
onStart: (q) => console.log("VQL start", q),
|
|
53
|
+
onEnd: (q, ms, r) => console.log("VQL end", ms + "ms", r),
|
|
54
|
+
onError: (q, e) => console.error("VQL error", e)
|
|
55
|
+
}
|
|
65
56
|
```
|
|
66
57
|
|
|
67
|
-
### `resetVQLClient()`
|
|
68
|
-
|
|
69
|
-
Reset transport and hooks to default.
|
|
70
|
-
|
|
71
58
|
## ✈️ Default Transport
|
|
72
59
|
|
|
73
60
|
```ts
|
|
74
|
-
|
|
61
|
+
defTransport(query): Promise<any>
|
|
75
62
|
```
|
|
76
63
|
|
|
77
64
|
Sends:
|
|
@@ -91,45 +78,15 @@ Returns:
|
|
|
91
78
|
}
|
|
92
79
|
```
|
|
93
80
|
|
|
94
|
-
Use `initVQLClient({ transport })` to override with WebSocket, RPC, etc.
|
|
95
|
-
|
|
96
81
|
## 📁 Output Files
|
|
97
82
|
|
|
98
83
|
```
|
|
99
84
|
dist/
|
|
100
85
|
├── index.js # ESM build (import)
|
|
101
86
|
├── index.d.ts # TypeScript types
|
|
102
|
-
├── vql-client.cjs # CommonJS build (require)
|
|
103
87
|
├── vql-client.min.js # Global (CDN, window.VQLClient)
|
|
104
88
|
```
|
|
105
89
|
|
|
106
|
-
## 🧾 Typings in Browser
|
|
107
|
-
|
|
108
|
-
You can manually copy `index.d.ts` to your local project or declare global types:
|
|
109
|
-
|
|
110
|
-
```ts
|
|
111
|
-
declare const VQLClient: {
|
|
112
|
-
fetchVQL<T = any>(query: string | object): Promise<T>;
|
|
113
|
-
initVQLClient(config: {
|
|
114
|
-
transport?: (query: string | object) => Promise<any>,
|
|
115
|
-
hooks?: {
|
|
116
|
-
onStart?: (query: string | object) => void,
|
|
117
|
-
onEnd?: (query: string | object, ms: number, result: any) => void,
|
|
118
|
-
onError?: (query: string | object, error: unknown) => void
|
|
119
|
-
}
|
|
120
|
-
}): void;
|
|
121
|
-
resetVQLClient(): void;
|
|
122
|
-
defaultFetchTransport(query: string | object): Promise<any>;
|
|
123
|
-
};
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
## 🧪 Example
|
|
127
|
-
|
|
128
|
-
```ts
|
|
129
|
-
const result = await fetchVQL("api getUser! s._id = abc123");
|
|
130
|
-
console.log(result.name);
|
|
131
|
-
```
|
|
132
|
-
|
|
133
90
|
## 📜 License
|
|
134
91
|
|
|
135
|
-
MIT
|
|
92
|
+
MIT wxn0brP
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { VQLUQ } from "./vql";
|
|
2
2
|
export type VQLResult<T = any> = Promise<T>;
|
|
3
|
-
export type VQLTransport = (query:
|
|
4
|
-
export
|
|
5
|
-
onStart?: (query:
|
|
6
|
-
onEnd?: (query:
|
|
7
|
-
onError?: (query:
|
|
8
|
-
}
|
|
9
|
-
export
|
|
3
|
+
export type VQLTransport = (query: VQLUQ) => VQLResult;
|
|
4
|
+
export interface VQLHooks {
|
|
5
|
+
onStart?: (query: VQLUQ, hookContext: any) => void;
|
|
6
|
+
onEnd?: (query: VQLUQ, durationMs: number, result: any, hookContext: any) => void;
|
|
7
|
+
onError?: (query: VQLUQ, error: unknown, result?: any, hookContext?: any) => void;
|
|
8
|
+
}
|
|
9
|
+
export interface Config {
|
|
10
10
|
transport?: VQLTransport;
|
|
11
11
|
hooks?: VQLHooks;
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
export declare
|
|
15
|
-
export declare function
|
|
16
|
-
export declare function
|
|
12
|
+
url?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const VConfig: Config;
|
|
15
|
+
export declare function fetchVQL<T = any>(query: VQLUQ<T>, hookContext?: any): Promise<T>;
|
|
16
|
+
export declare function defTransport(query: VQLUQ): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (config.hooks)
|
|
8
|
-
hooks = config.hooks;
|
|
9
|
-
if (config.defaultFetchUrl)
|
|
10
|
-
defaultFetchUrl = config.defaultFetchUrl;
|
|
11
|
-
}
|
|
1
|
+
;
|
|
2
|
+
export const VConfig = {
|
|
3
|
+
transport: defTransport,
|
|
4
|
+
hooks: {},
|
|
5
|
+
url: "/VQL"
|
|
6
|
+
};
|
|
12
7
|
export async function fetchVQL(query, hookContext = {}) {
|
|
8
|
+
const { transport, hooks } = VConfig;
|
|
13
9
|
const start = Date.now();
|
|
14
10
|
try {
|
|
15
11
|
hooks.onStart?.(query, hookContext);
|
|
@@ -30,12 +26,8 @@ export async function fetchVQL(query, hookContext = {}) {
|
|
|
30
26
|
throw e;
|
|
31
27
|
}
|
|
32
28
|
}
|
|
33
|
-
export function
|
|
34
|
-
|
|
35
|
-
hooks = {};
|
|
36
|
-
}
|
|
37
|
-
export async function defaultFetchTransport(query) {
|
|
38
|
-
const res = await fetch(defaultFetchUrl, {
|
|
29
|
+
export async function defTransport(query) {
|
|
30
|
+
const res = await fetch(VConfig.url, {
|
|
39
31
|
method: "POST",
|
|
40
32
|
headers: {
|
|
41
33
|
"Content-Type": "application/json"
|
|
@@ -49,8 +41,7 @@ export async function defaultFetchTransport(query) {
|
|
|
49
41
|
if (typeof window !== "undefined") {
|
|
50
42
|
window.VQLClient = {
|
|
51
43
|
fetchVQL,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
defaultFetchTransport
|
|
44
|
+
defTransport,
|
|
45
|
+
cfg: VConfig
|
|
55
46
|
};
|
|
56
47
|
}
|
package/dist/min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var VQLClient=(()=>{var s=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var T=(n,r)=>{for(var a in r)s(n,a,{get:r[a],enumerable:!0})},m=(n,r,a,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of w(r))!l.call(n,e)&&e!==a&&s(n,e,{get:()=>r[e],enumerable:!(t=L(r,e))||t.enumerable});return n};var x=n=>m(s({},"__esModule",{value:!0}),n);var E={};T(E,{VConfig:()=>i,defTransport:()=>Q,fetchVQL:()=>d});var i={transport:Q,hooks:{},url:"/VQL"};async function d(n,r={}){var u,f,p,c;let{transport:a,hooks:t}=i,e=Date.now();try{(u=t.onStart)==null||u.call(t,n,r);let o=await a(n),V=Date.now()-e;if((f=t.onEnd)==null||f.call(t,n,V,o,r),o!=null&&o.err){let y=new Error(o.err);throw(p=t.onError)==null||p.call(t,n,y,o,r),y}return o.result!==void 0?o.result:o}catch(o){throw(c=t.onError)==null||c.call(t,n,o,null,r),o}}async function Q(n){let r=await fetch(i.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:n})});if(!r.ok)throw new Error(`VQL request failed: ${r.status}`);return await r.json()}typeof window!="undefined"&&(window.VQLClient={fetchVQL:d,defTransport:Q,cfg:i});return x(E);})();
|
package/dist/vql.d.ts
CHANGED
|
@@ -137,106 +137,79 @@ declare namespace RelationTypes {
|
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
|
-
export interface
|
|
141
|
-
find: VQLFind<T>;
|
|
142
|
-
findOne: VQLFindOne<T>;
|
|
143
|
-
f: VQLFindOne<T>;
|
|
144
|
-
add: VQLAdd<T>;
|
|
145
|
-
update: VQLUpdate<T>;
|
|
146
|
-
updateOne: VQLUpdateOne<T>;
|
|
147
|
-
remove: VQLRemove<T>;
|
|
148
|
-
removeOne: VQLRemoveOne<T>;
|
|
149
|
-
updateOneOrAdd: VQLUpdateOneOrAdd<T>;
|
|
150
|
-
removeCollection: VQLCollectionOperation;
|
|
151
|
-
ensureCollection: VQLCollectionOperation;
|
|
152
|
-
issetCollection: VQLCollectionOperation;
|
|
153
|
-
getCollections: {};
|
|
154
|
-
}
|
|
155
|
-
export type VQLQueryData<T = any> = {
|
|
156
|
-
find: VQLFind<T>;
|
|
157
|
-
} | {
|
|
158
|
-
findOne: VQLFindOne<T>;
|
|
159
|
-
} | {
|
|
160
|
-
f: VQLFindOne<T>;
|
|
161
|
-
} | {
|
|
162
|
-
add: VQLAdd<T>;
|
|
163
|
-
} | {
|
|
164
|
-
update: VQLUpdate<T>;
|
|
165
|
-
} | {
|
|
166
|
-
updateOne: VQLUpdateOne<T>;
|
|
167
|
-
} | {
|
|
168
|
-
remove: VQLRemove<T>;
|
|
169
|
-
} | {
|
|
170
|
-
removeOne: VQLRemoveOne<T>;
|
|
171
|
-
} | {
|
|
172
|
-
updateOneOrAdd: VQLUpdateOneOrAdd<T>;
|
|
173
|
-
} | {
|
|
174
|
-
removeCollection: VQLCollectionOperation;
|
|
175
|
-
} | {
|
|
176
|
-
ensureCollection: VQLCollectionOperation;
|
|
177
|
-
} | {
|
|
178
|
-
issetCollection: VQLCollectionOperation;
|
|
179
|
-
} | {
|
|
180
|
-
getCollections: {};
|
|
181
|
-
};
|
|
182
|
-
export interface VQLRequest<T = any> {
|
|
183
|
-
db: string;
|
|
184
|
-
d: VQLQueryData<T>;
|
|
185
|
-
}
|
|
186
|
-
export interface VQLFind<T = any> {
|
|
140
|
+
export interface VQL_OP_Find<T = any> {
|
|
187
141
|
collection: string;
|
|
188
142
|
search?: Search<T>;
|
|
189
143
|
limit?: number;
|
|
190
|
-
fields?:
|
|
191
|
-
select?:
|
|
192
|
-
relations?: VQLRelations;
|
|
144
|
+
fields?: VQL_Fields;
|
|
145
|
+
select?: VQL_Fields;
|
|
193
146
|
options?: DbFindOpts<T>;
|
|
194
147
|
searchOpts?: FindOpts<T>;
|
|
195
148
|
}
|
|
196
|
-
export interface
|
|
149
|
+
export interface VQL_OP_FindOne<T = any> {
|
|
197
150
|
collection: string;
|
|
198
151
|
search: Search<T>;
|
|
199
|
-
fields?:
|
|
200
|
-
select?:
|
|
201
|
-
relations?: VQLRelations;
|
|
152
|
+
fields?: VQL_Fields;
|
|
153
|
+
select?: VQL_Fields;
|
|
202
154
|
searchOpts?: FindOpts<T>;
|
|
203
155
|
}
|
|
204
|
-
export interface
|
|
156
|
+
export interface VQL_OP_Add<T = any> {
|
|
205
157
|
collection: string;
|
|
206
158
|
data: Arg<T>;
|
|
207
159
|
id_gen?: boolean;
|
|
208
160
|
}
|
|
209
|
-
export interface
|
|
210
|
-
collection: string;
|
|
211
|
-
search: Search<T>;
|
|
212
|
-
updater: UpdaterArg<T>;
|
|
213
|
-
}
|
|
214
|
-
export interface VQLUpdateOne<T = any> {
|
|
161
|
+
export interface VQL_OP_Update<T = any> {
|
|
215
162
|
collection: string;
|
|
216
163
|
search: Search<T>;
|
|
217
164
|
updater: UpdaterArg<T>;
|
|
218
165
|
}
|
|
219
|
-
export interface
|
|
166
|
+
export interface VQL_OP_Remove<T = any> {
|
|
220
167
|
collection: string;
|
|
221
168
|
search: Search<T>;
|
|
222
169
|
}
|
|
223
|
-
export interface
|
|
224
|
-
collection: string;
|
|
225
|
-
search: Search<T>;
|
|
226
|
-
}
|
|
227
|
-
export interface VQLUpdateOneOrAdd<T = any> {
|
|
170
|
+
export interface VQL_OP_UpdateOneOrAdd<T = any> {
|
|
228
171
|
collection: string;
|
|
229
172
|
search: Search<T>;
|
|
230
173
|
updater: UpdaterArg<T>;
|
|
231
174
|
add_arg?: Arg<T>;
|
|
232
175
|
id_gen?: boolean;
|
|
233
176
|
}
|
|
234
|
-
export interface
|
|
177
|
+
export interface VQL_OP_CollectionOperation {
|
|
235
178
|
collection: string;
|
|
236
179
|
}
|
|
237
|
-
export type
|
|
238
|
-
export type
|
|
239
|
-
|
|
180
|
+
export type VQL_Fields = Record<string, boolean | number> | string[];
|
|
181
|
+
export type VQL_Query_CRUD_Data<T = any> = {
|
|
182
|
+
find: VQL_OP_Find<T>;
|
|
183
|
+
} | {
|
|
184
|
+
findOne: VQL_OP_FindOne<T>;
|
|
185
|
+
} | {
|
|
186
|
+
f: VQL_OP_FindOne<T>;
|
|
187
|
+
} | {
|
|
188
|
+
add: VQL_OP_Add<T>;
|
|
189
|
+
} | {
|
|
190
|
+
update: VQL_OP_Update<T>;
|
|
191
|
+
} | {
|
|
192
|
+
updateOne: VQL_OP_Update<T>;
|
|
193
|
+
} | {
|
|
194
|
+
remove: VQL_OP_Remove<T>;
|
|
195
|
+
} | {
|
|
196
|
+
removeOne: VQL_OP_Remove<T>;
|
|
197
|
+
} | {
|
|
198
|
+
updateOneOrAdd: VQL_OP_UpdateOneOrAdd<T>;
|
|
199
|
+
} | {
|
|
200
|
+
removeCollection: VQL_OP_CollectionOperation;
|
|
201
|
+
} | {
|
|
202
|
+
ensureCollection: VQL_OP_CollectionOperation;
|
|
203
|
+
} | {
|
|
204
|
+
issetCollection: VQL_OP_CollectionOperation;
|
|
205
|
+
} | {
|
|
206
|
+
getCollections: {};
|
|
207
|
+
};
|
|
208
|
+
export interface VQL_Query_CRUD<T = any> {
|
|
209
|
+
db: string;
|
|
210
|
+
d: VQL_Query_CRUD_Data<T>;
|
|
211
|
+
}
|
|
212
|
+
export interface VQL_Query_Relation {
|
|
240
213
|
r: {
|
|
241
214
|
path: RelationTypes.Path;
|
|
242
215
|
search: Search;
|
|
@@ -246,25 +219,18 @@ export interface RelationQuery {
|
|
|
246
219
|
select?: RelationTypes.FieldPath[];
|
|
247
220
|
};
|
|
248
221
|
}
|
|
249
|
-
export interface
|
|
250
|
-
ref?: string;
|
|
222
|
+
export interface VQL_Var {
|
|
251
223
|
var?: {
|
|
252
224
|
[k: string]: any;
|
|
253
225
|
};
|
|
254
226
|
}
|
|
255
|
-
export type
|
|
256
|
-
export type
|
|
257
|
-
|
|
258
|
-
};
|
|
259
|
-
export type VQL<T = any> = (VQLRequest<T> | RelationQuery) & VQLRef;
|
|
260
|
-
export type VQLR<T = any> = VQL<T> | (DeepPartial<VQL<T>> & VQLRefRequired) | VQLRefRequired;
|
|
227
|
+
export type VQL_Query<T = any> = (VQL_Query_CRUD<T> | VQL_Query_Relation) & VQL_Var;
|
|
228
|
+
export type VQLUQ<T = any> = VQL_Query<T> | string | {
|
|
229
|
+
query: string;
|
|
230
|
+
} & VQL_Var;
|
|
261
231
|
export interface VQLError {
|
|
262
232
|
err: true;
|
|
263
233
|
msg: string;
|
|
264
234
|
c: number;
|
|
265
|
-
why?: string;
|
|
266
235
|
}
|
|
267
|
-
export type VqlQueryRaw<T = any> = VQLR<T> | string | {
|
|
268
|
-
query: string;
|
|
269
|
-
} & VQLRef;
|
|
270
236
|
export {};
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/vql-client",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
|
+
"description": "VQL Client",
|
|
7
|
+
"homepage": "https://github.com/wxn0brP/VQL-client",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/wxn0brP/VQL-client.git"
|
|
11
|
+
},
|
|
6
12
|
"author": "wxn0brP",
|
|
7
13
|
"license": "MIT",
|
|
8
14
|
"type": "module",
|
|
9
15
|
"devDependencies": {
|
|
10
|
-
"esbuild": "^0.25.
|
|
11
|
-
"
|
|
12
|
-
"typescript": "^5.7.3"
|
|
16
|
+
"esbuild": "^0.25.8",
|
|
17
|
+
"typescript": "^5.8.3"
|
|
13
18
|
},
|
|
14
19
|
"files": [
|
|
15
20
|
"dist"
|
|
@@ -18,13 +23,11 @@
|
|
|
18
23
|
".": {
|
|
19
24
|
"types": "./dist/index.d.ts",
|
|
20
25
|
"import": "./dist/index.js",
|
|
21
|
-
"require": "./dist/index.cjs",
|
|
22
26
|
"default": "./dist/index.js"
|
|
23
27
|
},
|
|
24
28
|
"./*": {
|
|
25
29
|
"types": "./dist/*.d.ts",
|
|
26
30
|
"import": "./dist/*.js",
|
|
27
|
-
"require": "./dist/*.cjs",
|
|
28
31
|
"default": "./dist/*.js"
|
|
29
32
|
}
|
|
30
33
|
}
|
package/dist/vql-client.cjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/index.ts
|
|
20
|
-
var index_exports = {};
|
|
21
|
-
__export(index_exports, {
|
|
22
|
-
defaultFetchTransport: () => defaultFetchTransport,
|
|
23
|
-
fetchVQL: () => fetchVQL,
|
|
24
|
-
initVQLClient: () => initVQLClient,
|
|
25
|
-
resetVQLClient: () => resetVQLClient
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
var transport = defaultFetchTransport;
|
|
29
|
-
var hooks = {};
|
|
30
|
-
var defaultFetchUrl = "/VQL";
|
|
31
|
-
function initVQLClient(config) {
|
|
32
|
-
if (config.transport) transport = config.transport;
|
|
33
|
-
if (config.hooks) hooks = config.hooks;
|
|
34
|
-
if (config.defaultFetchUrl) defaultFetchUrl = config.defaultFetchUrl;
|
|
35
|
-
}
|
|
36
|
-
async function fetchVQL(query, hookContext = {}) {
|
|
37
|
-
var _a, _b, _c, _d;
|
|
38
|
-
const start = Date.now();
|
|
39
|
-
try {
|
|
40
|
-
(_a = hooks.onStart) == null ? void 0 : _a.call(hooks, query, hookContext);
|
|
41
|
-
const res = await transport(query);
|
|
42
|
-
const duration = Date.now() - start;
|
|
43
|
-
(_b = hooks.onEnd) == null ? void 0 : _b.call(hooks, query, duration, res, hookContext);
|
|
44
|
-
if (res == null ? void 0 : res.err) {
|
|
45
|
-
const error = new Error(res.err);
|
|
46
|
-
(_c = hooks.onError) == null ? void 0 : _c.call(hooks, query, error, res, hookContext);
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
if (res.result !== void 0) return res.result;
|
|
50
|
-
return res;
|
|
51
|
-
} catch (e) {
|
|
52
|
-
(_d = hooks.onError) == null ? void 0 : _d.call(hooks, query, e, null, hookContext);
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function resetVQLClient() {
|
|
57
|
-
transport = defaultFetchTransport;
|
|
58
|
-
hooks = {};
|
|
59
|
-
}
|
|
60
|
-
async function defaultFetchTransport(query) {
|
|
61
|
-
const res = await fetch(defaultFetchUrl, {
|
|
62
|
-
method: "POST",
|
|
63
|
-
headers: {
|
|
64
|
-
"Content-Type": "application/json"
|
|
65
|
-
},
|
|
66
|
-
body: JSON.stringify({ query })
|
|
67
|
-
});
|
|
68
|
-
if (!res.ok) throw new Error(`VQL request failed: ${res.status}`);
|
|
69
|
-
return await res.json();
|
|
70
|
-
}
|
|
71
|
-
if (typeof window !== "undefined") {
|
|
72
|
-
window.VQLClient = {
|
|
73
|
-
fetchVQL,
|
|
74
|
-
initVQLClient,
|
|
75
|
-
resetVQLClient,
|
|
76
|
-
defaultFetchTransport
|
|
77
|
-
};
|
|
78
|
-
}
|
package/dist/vql-client.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var VQLClient=(()=>{var i=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var R=(r,t)=>{for(var a in t)i(r,a,{get:t[a],enumerable:!0})},q=(r,t,a,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of T(t))!h.call(r,o)&&o!==a&&i(r,o,{get:()=>t[o],enumerable:!(s=L(t,o))||s.enumerable});return r};var m=r=>q(i({},"__esModule",{value:!0}),r);var x={};R(x,{defaultFetchTransport:()=>u,fetchVQL:()=>V,initVQLClient:()=>Q,resetVQLClient:()=>f});var l=u,e={},w="/VQL";function Q(r){r.transport&&(l=r.transport),r.hooks&&(e=r.hooks),r.defaultFetchUrl&&(w=r.defaultFetchUrl)}async function V(r,t={}){var s,o,y,p;let a=Date.now();try{(s=e.onStart)==null||s.call(e,r,t);let n=await l(r),c=Date.now()-a;if((o=e.onEnd)==null||o.call(e,r,c,n,t),n!=null&&n.err){let d=new Error(n.err);throw(y=e.onError)==null||y.call(e,r,d,n,t),d}return n.result!==void 0?n.result:n}catch(n){throw(p=e.onError)==null||p.call(e,r,n,null,t),n}}function f(){l=u,e={}}async function u(r){let t=await fetch(w,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:r})});if(!t.ok)throw new Error(`VQL request failed: ${t.status}`);return await t.json()}typeof window!="undefined"&&(window.VQLClient={fetchVQL:V,initVQLClient:Q,resetVQLClient:f,defaultFetchTransport:u});return m(x);})();
|