Skip to content

Commit 9b3503f

Browse files
committed
docs: add vql client & falcon frame integration docs
1 parent 7af517d commit 9b3503f

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ VQL empowers you to:
3737
- **[VQLS](./lang/VQLS.md):** Dive deep into the syntax and capabilities of the Simple VQL string language.
3838
- **[VQLR](./lang/VQLR.md):** Explore the structure and power of the Runtime VQL object format.
3939
- **[Permissions](permissions.md):** Discover how VQL's integrated permission system can secure your data interactions.
40+
- **[HTTP API Integration](integration_http.md):** Learn how to expose VQL over an HTTP API for use in web applications.

docs/integration_http.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# HTTP API Integration
2+
3+
VQL is designed to work seamlessly not just within a single application, but also across network boundaries. You can easily expose your `VQLProcessor` as an HTTP endpoint, allowing frontend applications, microservices, or external scripts to execute queries securely and efficiently.
4+
5+
This guide outlines the recommended way to create a VQL-powered API using [Falcon Frame](https://github.com/wxn0brP/falcon-frame) on the backend and a lightweight VQL client on the frontend.
6+
7+
## Server-Side: Exposing VQL with `FF_VQL`
8+
9+
The `FF_VQL` helper function is the quickest way to create a dedicated VQL endpoint. It handles incoming requests, passes them to the `VQLProcessor`, and returns the results in a standardized format.
10+
11+
### Basic Setup
12+
13+
Here is an example of a simple web server that exposes a VQL endpoint.
14+
15+
**`server.ts`**
16+
```typescript
17+
import { FalconFrame } from "@wxn0brp/falcon-frame";
18+
import { VQLProcessor, FF_VQL } from "@wxn0brp/vql";
19+
import { Valthera } from "@wxn0brp/db";
20+
21+
// 1. Initialize your Database and VQLProcessor
22+
const db = new Valthera("./data/my-db");
23+
const processor = new VQLProcessor({ mainDB: db });
24+
25+
// 2. Initialize Falcon Frame
26+
const app = new FalconFrame();
27+
28+
// 3. Add the VQL endpoint using the helper
29+
FF_VQL(app, processor, {
30+
// These options are optional
31+
path: "/vql-api", // Default is "/VQL"
32+
dev: true // Enables a GET endpoint for debugging queries
33+
});
34+
35+
// 4. Start the server
36+
app.l(3000);
37+
```
38+
39+
### Passing User Context
40+
41+
A critical feature of `FF_VQL` is the ability to extract user context from each request and pass it to the `VQLProcessor` for permission checks. This is done via the `getUser` option.
42+
43+
```typescript
44+
FF_VQL(app, processor, {
45+
getUser: async (req) => {
46+
// Example: Extract user info from an Authorization header
47+
const token = req.headers.get("Authorization")?.replace("Bearer ", "");
48+
49+
if (token === "SECRET_ADMIN_TOKEN") {
50+
return { _id: "admin-user", role: "admin" };
51+
}
52+
53+
// For permission resolvers, it's important to provide a context
54+
return { _id: "guest-user", role: "guest" };
55+
}
56+
});
57+
```
58+
The object returned by `getUser` is passed as the `user` argument to `processor.execute()` and is available within your [permission resolvers](./permissions.md).
59+
60+
## Frontend: Using a VQL Client
61+
62+
To communicate with the VQL endpoint from a browser or another service, you should use the official **`@wxn0brp/vql-client`** library. This client simplifies interaction with your VQL API, providing convenient functions for sending queries and managing configuration.
63+
64+
### Installation
65+
66+
Install the `@wxn0brp/vql-client` package in your frontend project:
67+
68+
```bash
69+
npm install @wxn0brp/vql-client
70+
# or yarn add @wxn0brp/vql-client
71+
# or bun add @wxn0brp/vql-client
72+
```
73+
74+
### Core Client Functions
75+
76+
The `@wxn0brp/vql-client` package exports the following key functions and objects:
77+
78+
- `VQLClient.fetchVQL(query, vars)`: The primary function for sending queries to the VQL API.
79+
- `query`: Can be a VQLS string or a VQLR object. If a string, variables can be provided separately.
80+
- `vars` (optional): An object containing variables to be substituted in the query (e.g., for `s.age=$age`).
81+
- `VQLClient.V`: A tagged template literal for a more ergonomic, inline VQLS query syntax directly within your code. It's a convenient wrapper around `VQLClient.fetchVQL`.
82+
- `VQLClient.cfg`: A global configuration object allowing you to set the endpoint URL (`VQLClient.cfg.url`), default HTTP headers (`VQLClient.cfg.headers`), and other options for all client requests.
83+
- `VQLClient.defTransport`: The default transport function used for making HTTP requests. You can override this for custom networking logic.
84+
- `VQLClient.VQLHooks`: An interface for defining hooks that can run before, after, or on error of a VQL request. You can set `VQLClient.cfg.hooks`.
85+
86+
### Example Usage
87+
88+
Here’s how you might use the `@wxn0brp/vql-client` in your frontend JavaScript or TypeScript code.
89+
90+
```typescript
91+
import { VQLClient } from "@wxn0brp/vql-client";
92+
93+
// Configure the client to point to your API endpoint
94+
VQLClient.cfg.url = "http://localhost:3000/vql-api";
95+
96+
// Set a default header for authorization
97+
VQLClient.cfg.headers = {
98+
"Authorization": "Bearer SECRET_ADMIN_TOKEN"
99+
};
100+
101+
async function loadData() {
102+
try {
103+
// Example 1: Use fetchVQL with variables
104+
console.log("Fetching active users...");
105+
const activeUsers = await VQLClient.fetchVQL("mainDB users s.status=$status", { status: "active" });
106+
console.log("Active Users:", activeUsers);
107+
108+
// Example 2: Use the V template literal for a simple query
109+
console.log("Fetching item by ID...");
110+
const item = await VQLClient.V`mainDB items! s._id="item-001"`;
111+
console.log("Item:", item);
112+
113+
} catch (e) {
114+
console.error("An error occurred while fetching VQL data:", e);
115+
}
116+
}
117+
118+
loadData();
119+
```
120+
121+
This client-server combination provides a powerful, secure, and maintainable way to build modern data-driven applications with VQL. The server exposes the full power of VQL through a single endpoint, while the client offers a simple and pleasant developer experience for consuming it.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ nav:
1717
- VQLS: lang/VQLS.md
1818
- VQLR: lang/VQLR.md
1919
- Permissions: permissions.md
20+
- HTTP API Integration: integration_http.md
2021

2122
markdown_extensions:
2223
- pymdownx.highlight

0 commit comments

Comments
 (0)