@wxn0brp/vql-client 0.2.4 → 0.2.5
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/dist/index.d.ts +11 -3
- package/dist/index.js +18 -10
- package/dist/min.js +1 -1
- package/dist/vql.d.ts +10 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { VQLUQ } from "./vql";
|
|
2
2
|
export type VQLResult<T = any> = Promise<T>;
|
|
3
|
-
export type VQLTransport = (query: VQLUQ) => VQLResult;
|
|
3
|
+
export type VQLTransport = (query: VQLUQ, fetchOptions?: FetchOptions) => VQLResult;
|
|
4
4
|
export interface VQLHooks {
|
|
5
5
|
onStart?: (query: VQLUQ, hookContext: any) => void;
|
|
6
6
|
onEnd?: (query: VQLUQ, durationMs: number, result: any, hookContext: any) => void;
|
|
@@ -8,10 +8,18 @@ export interface VQLHooks {
|
|
|
8
8
|
}
|
|
9
9
|
export interface Config {
|
|
10
10
|
transport?: VQLTransport;
|
|
11
|
+
fetchImplementation: typeof fetch;
|
|
11
12
|
hooks?: VQLHooks;
|
|
12
13
|
url?: string;
|
|
13
14
|
body?: Record<string, any>;
|
|
15
|
+
headers?: Record<string, any>;
|
|
16
|
+
hookContext?: any;
|
|
17
|
+
}
|
|
18
|
+
export interface FetchOptions {
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
headers?: Record<string, any>;
|
|
21
|
+
body?: Record<string, any>;
|
|
14
22
|
}
|
|
15
23
|
export declare const VConfig: Config;
|
|
16
|
-
export declare function fetchVQL<T = any>(query: VQLUQ<T>, vars?: any, hookContext?: any): Promise<T>;
|
|
17
|
-
export declare function defTransport(query: VQLUQ): Promise<any>;
|
|
24
|
+
export declare function fetchVQL<T = any>(query: VQLUQ<T>, vars?: any, hookContext?: any, fetchOptions?: FetchOptions): Promise<T>;
|
|
25
|
+
export declare function defTransport(query: VQLUQ, fetchOptions?: FetchOptions): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
;
|
|
2
2
|
export const VConfig = {
|
|
3
3
|
transport: defTransport,
|
|
4
|
+
fetchImplementation: fetch,
|
|
4
5
|
hooks: {},
|
|
5
6
|
url: "/VQL"
|
|
6
7
|
};
|
|
7
|
-
export async function fetchVQL(query, vars = {}, hookContext = {}) {
|
|
8
|
+
export async function fetchVQL(query, vars = {}, hookContext = {}, fetchOptions = {}) {
|
|
8
9
|
const { transport, hooks } = VConfig;
|
|
9
10
|
const start = Date.now();
|
|
10
11
|
try {
|
|
11
|
-
hookContext = Object.assign({}, vars, hookContext);
|
|
12
|
+
hookContext = Object.assign({}, VConfig.hookContext, vars, hookContext);
|
|
12
13
|
hooks.onStart?.(query, hookContext);
|
|
13
14
|
if (typeof query === "string" && Object.keys(vars).length) {
|
|
14
15
|
query = {
|
|
@@ -16,7 +17,7 @@ export async function fetchVQL(query, vars = {}, hookContext = {}) {
|
|
|
16
17
|
var: vars
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
|
-
const res = await transport(query);
|
|
20
|
+
const res = await transport(query, fetchOptions);
|
|
20
21
|
const duration = Date.now() - start;
|
|
21
22
|
hooks.onEnd?.(query, duration, res, hookContext);
|
|
22
23
|
if (res?.err) {
|
|
@@ -33,14 +34,21 @@ export async function fetchVQL(query, vars = {}, hookContext = {}) {
|
|
|
33
34
|
throw e;
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
export async function defTransport(query) {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
body: JSON.stringify({ query, ...(VConfig.body || {}) })
|
|
37
|
+
export async function defTransport(query, fetchOptions) {
|
|
38
|
+
const headers = Object.assign({
|
|
39
|
+
"Content-Type": "application/json"
|
|
40
|
+
}, VConfig.headers, fetchOptions.headers);
|
|
41
|
+
const body = Object.assign({}, VConfig.body, fetchOptions.body, {
|
|
42
|
+
query
|
|
43
43
|
});
|
|
44
|
+
const queryConfig = {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers,
|
|
47
|
+
body: JSON.stringify(body)
|
|
48
|
+
};
|
|
49
|
+
if (fetchOptions.signal)
|
|
50
|
+
queryConfig.signal = fetchOptions.signal;
|
|
51
|
+
const res = await VConfig.fetchImplementation(VConfig.url, queryConfig);
|
|
44
52
|
if (!res.ok)
|
|
45
53
|
throw new Error(`VQL request failed: ${res.status}`);
|
|
46
54
|
return await res.json();
|
package/dist/min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var VQLClient=(()=>{var
|
|
1
|
+
var VQLClient=(()=>{var c=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var m=(n,t)=>{for(var e in t)c(n,e,{get:t[e],enumerable:!0})},b=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of w(t))!h.call(n,a)&&a!==e&&c(n,a,{get:()=>t[a],enumerable:!(s=L(t,a))||s.enumerable});return n};var T=n=>b(c({},"__esModule",{value:!0}),n);var x={};m(x,{VConfig:()=>i,defTransport:()=>y,fetchVQL:()=>u});var i={transport:y,fetchImplementation:fetch,hooks:{},url:"/VQL"};async function u(n,t={},e={},s={}){var d,f,p,Q;let{transport:a,hooks:r}=i,g=Date.now();try{e=Object.assign({},i.hookContext,t,e),(d=r.onStart)==null||d.call(r,n,e),typeof n=="string"&&Object.keys(t).length&&(n={query:n,var:t});let o=await a(n,s),V=Date.now()-g;if((f=r.onEnd)==null||f.call(r,n,V,o,e),o!=null&&o.err){let l=new Error(o.err);throw(p=r.onError)==null||p.call(r,n,l,o,e),l}return o.result!==void 0?o.result:o}catch(o){throw(Q=r.onError)==null||Q.call(r,n,o,null,e),o}}async function y(n,t){let e=Object.assign({"Content-Type":"application/json"},i.headers,t.headers),s=Object.assign({},i.body,t.body,{query:n}),a={method:"POST",headers:e,body:JSON.stringify(s)};t.signal&&(a.signal=t.signal);let r=await i.fetchImplementation(i.url,a);if(!r.ok)throw new Error(`VQL request failed: ${r.status}`);return await r.json()}typeof window!="undefined"&&(window.VQLClient={fetchVQL:u,defTransport:y,cfg:i});return T(x);})();
|
package/dist/vql.d.ts
CHANGED
|
@@ -98,6 +98,7 @@ declare class CollectionManager<D = Data> {
|
|
|
98
98
|
remove<T = Data>(search: Search<T & Data>, context?: VContext): Promise<boolean>;
|
|
99
99
|
removeOne<T = Data>(search: Search<T & Data>, context?: VContext): Promise<boolean>;
|
|
100
100
|
updateOneOrAdd<T = Data>(search: Search<T & Data>, updater: Updater<T & Data>, { add_arg, context, id_gen }: UpdateOneOrAdd<T & Data>): Promise<boolean>;
|
|
101
|
+
toggleOne<T = Data>(search: Search<T & Data>, data?: Arg<T & Data>, context?: VContext): Promise<boolean>;
|
|
101
102
|
}
|
|
102
103
|
export interface ValtheraCompatible {
|
|
103
104
|
c(collection: string): CollectionManager;
|
|
@@ -113,6 +114,7 @@ export interface ValtheraCompatible {
|
|
|
113
114
|
removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
|
|
114
115
|
removeCollection(collection: string): Promise<boolean>;
|
|
115
116
|
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, opts?: UpdateOneOrAdd<T>): Promise<boolean>;
|
|
117
|
+
toggleOne<T = Data>(collection: string, search: Search<T>, data?: Arg<T>, context?: VContext): Promise<boolean>;
|
|
116
118
|
}
|
|
117
119
|
export interface UpdateOneOrAdd<T> {
|
|
118
120
|
add_arg?: Arg<T>;
|
|
@@ -185,6 +187,11 @@ export interface VQL_OP_UpdateOneOrAdd<T = any> {
|
|
|
185
187
|
add_arg?: Arg<T>;
|
|
186
188
|
id_gen?: boolean;
|
|
187
189
|
}
|
|
190
|
+
export interface VQL_OP_ToggleOne<T = any> {
|
|
191
|
+
collection: string;
|
|
192
|
+
search: Search<T>;
|
|
193
|
+
data?: Arg<T>;
|
|
194
|
+
}
|
|
188
195
|
export interface VQL_OP_CollectionOperation {
|
|
189
196
|
collection: string;
|
|
190
197
|
}
|
|
@@ -207,6 +214,8 @@ export type VQL_Query_CRUD_Data<T = any> = {
|
|
|
207
214
|
removeOne: VQL_OP_Remove<T>;
|
|
208
215
|
} | {
|
|
209
216
|
updateOneOrAdd: VQL_OP_UpdateOneOrAdd<T>;
|
|
217
|
+
} | {
|
|
218
|
+
toggleOne: VQL_OP_ToggleOne<T>;
|
|
210
219
|
} | {
|
|
211
220
|
removeCollection: VQL_OP_CollectionOperation;
|
|
212
221
|
} | {
|
|
@@ -216,6 +225,7 @@ export type VQL_Query_CRUD_Data<T = any> = {
|
|
|
216
225
|
} | {
|
|
217
226
|
getCollections: {};
|
|
218
227
|
};
|
|
228
|
+
export type VQL_Query_CRUD_Keys = VQL_Query_CRUD_Data extends infer U ? U extends Record<string, unknown> ? keyof U : never : never;
|
|
219
229
|
export interface VQL_Query_CRUD<T = any> {
|
|
220
230
|
db: string;
|
|
221
231
|
d: VQL_Query_CRUD_Data<T>;
|