bright-client 0.1.1
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/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/index.cjs +79 -0
- package/dist/index.d.cts +46 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +78 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Bright JS Client
|
|
2
|
+
|
|
3
|
+
TypeScript/JavaScript client library for [Bright](https://github.com/nnstd/bright) search database.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @nnstd/bright-js
|
|
9
|
+
# or
|
|
10
|
+
yarn add @nnstd/bright-js
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @nnstd/bright-js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Create a Client
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { createClient } from '@nnstd/bright-js';
|
|
21
|
+
|
|
22
|
+
const client = createClient({
|
|
23
|
+
baseUrl: 'http://localhost:3000'
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Index Management
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// Create an index
|
|
31
|
+
await client.createIndex('products', 'id');
|
|
32
|
+
|
|
33
|
+
// Update index configuration
|
|
34
|
+
await client.updateIndex('products', { primaryKey: 'sku' });
|
|
35
|
+
|
|
36
|
+
// Delete an index
|
|
37
|
+
await client.deleteIndex('products');
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Document Operations
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
// Add documents
|
|
44
|
+
await client.addDocuments('products', [
|
|
45
|
+
{ id: '1', name: 'Widget', price: 19.99 },
|
|
46
|
+
{ id: '2', name: 'Gadget', price: 29.99 }
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
// Update a document
|
|
50
|
+
await client.updateDocument('products', '1', { price: 24.99 });
|
|
51
|
+
|
|
52
|
+
// Delete a document
|
|
53
|
+
await client.deleteDocument('products', '1');
|
|
54
|
+
|
|
55
|
+
// Delete multiple documents
|
|
56
|
+
await client.deleteDocuments('products', { ids: ['1', '2'] });
|
|
57
|
+
|
|
58
|
+
// Delete by filter
|
|
59
|
+
await client.deleteDocuments('products', { filter: 'category:electronics' });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Search
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Simple search
|
|
66
|
+
const results = await client.search('products', {
|
|
67
|
+
q: 'laptop'
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Advanced search with options
|
|
71
|
+
const results = await client.search('products', {
|
|
72
|
+
q: 'laptop',
|
|
73
|
+
limit: 20,
|
|
74
|
+
page: 1,
|
|
75
|
+
sort: ['-price', 'name'],
|
|
76
|
+
attributesToRetrieve: ['id', 'name', 'price']
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Search with TypeScript types
|
|
80
|
+
interface Product {
|
|
81
|
+
id: string;
|
|
82
|
+
name: string;
|
|
83
|
+
price: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const results = await client.search<Product>('products', {
|
|
87
|
+
q: 'laptop'
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
console.log(results.hits); // Product[]
|
|
91
|
+
console.log(results.totalHits); // number
|
|
92
|
+
console.log(results.totalPages); // number
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API Reference
|
|
96
|
+
|
|
97
|
+
### `createClient(options)`
|
|
98
|
+
|
|
99
|
+
Creates a new Bright client instance.
|
|
100
|
+
|
|
101
|
+
**Options:**
|
|
102
|
+
- `baseUrl` (string, required) - Base URL of the Bright server
|
|
103
|
+
- `fetch` (function, optional) - Custom fetch implementation
|
|
104
|
+
|
|
105
|
+
### Index Management
|
|
106
|
+
|
|
107
|
+
#### `createIndex(id, primaryKey?)`
|
|
108
|
+
|
|
109
|
+
Creates a new search index.
|
|
110
|
+
|
|
111
|
+
#### `updateIndex(id, config)`
|
|
112
|
+
|
|
113
|
+
Updates index configuration.
|
|
114
|
+
|
|
115
|
+
#### `deleteIndex(id)`
|
|
116
|
+
|
|
117
|
+
Deletes an index and all its documents.
|
|
118
|
+
|
|
119
|
+
### Document Operations
|
|
120
|
+
|
|
121
|
+
#### `addDocuments(indexId, documents, format?)`
|
|
122
|
+
|
|
123
|
+
Adds documents to an index. Documents with existing IDs will be updated.
|
|
124
|
+
|
|
125
|
+
#### `updateDocument(indexId, documentId, updates)`
|
|
126
|
+
|
|
127
|
+
Updates specific fields of a document.
|
|
128
|
+
|
|
129
|
+
#### `deleteDocument(indexId, documentId)`
|
|
130
|
+
|
|
131
|
+
Deletes a single document.
|
|
132
|
+
|
|
133
|
+
#### `deleteDocuments(indexId, options)`
|
|
134
|
+
|
|
135
|
+
Deletes multiple documents by IDs or filter query.
|
|
136
|
+
|
|
137
|
+
**Options:**
|
|
138
|
+
- `ids` (string[]) - Array of document IDs
|
|
139
|
+
- `filter` (string) - Query filter
|
|
140
|
+
|
|
141
|
+
### Search
|
|
142
|
+
|
|
143
|
+
#### `search<T>(indexId, params?)`
|
|
144
|
+
|
|
145
|
+
Searches for documents in an index.
|
|
146
|
+
|
|
147
|
+
**Params:**
|
|
148
|
+
- `q` (string) - Search query
|
|
149
|
+
- `offset` (number) - Number of results to skip
|
|
150
|
+
- `limit` (number) - Maximum results per page (default: 20)
|
|
151
|
+
- `page` (number) - Page number
|
|
152
|
+
- `sort` (string[]) - Sort fields (prefix with `-` for descending)
|
|
153
|
+
- `attributesToRetrieve` (string[]) - Fields to include
|
|
154
|
+
- `attributesToExclude` (string[]) - Fields to exclude
|
|
155
|
+
|
|
156
|
+
## TypeScript
|
|
157
|
+
|
|
158
|
+
This library is written in TypeScript and includes type definitions.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
import { BrightClient, SearchParams, SearchResponse } from '@nnstd/bright-js';
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
var BrightClient = class {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
6
|
+
this.fetchFn = options.fetch || globalThis.fetch;
|
|
7
|
+
}
|
|
8
|
+
async request(path, options) {
|
|
9
|
+
const url = `${this.baseUrl}${path}`;
|
|
10
|
+
const response = await this.fetchFn(url, {
|
|
11
|
+
...options,
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
...options?.headers
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
19
|
+
throw new Error(error.error || `HTTP ${response.status}`);
|
|
20
|
+
}
|
|
21
|
+
if (response.status === 204) return;
|
|
22
|
+
return response.json();
|
|
23
|
+
}
|
|
24
|
+
async createIndex(id, primaryKey) {
|
|
25
|
+
const params = new URLSearchParams({ id });
|
|
26
|
+
if (primaryKey) params.append("primaryKey", primaryKey);
|
|
27
|
+
return this.request(`/indexes?${params}`, { method: "POST" });
|
|
28
|
+
}
|
|
29
|
+
async updateIndex(id, config) {
|
|
30
|
+
return this.request(`/indexes/${id}`, {
|
|
31
|
+
method: "PATCH",
|
|
32
|
+
body: JSON.stringify(config)
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async deleteIndex(id) {
|
|
36
|
+
return this.request(`/indexes/${id}`, { method: "DELETE" });
|
|
37
|
+
}
|
|
38
|
+
async addDocuments(indexId, documents, format = "jsoneachrow") {
|
|
39
|
+
const body = documents.map((doc) => JSON.stringify(doc)).join("\n");
|
|
40
|
+
const params = new URLSearchParams({ format });
|
|
41
|
+
return this.request(`/indexes/${indexId}/documents?${params}`, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
body
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async updateDocument(indexId, documentId, updates) {
|
|
47
|
+
return this.request(`/indexes/${indexId}/documents/${documentId}`, {
|
|
48
|
+
method: "PATCH",
|
|
49
|
+
body: JSON.stringify(updates)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async deleteDocument(indexId, documentId) {
|
|
53
|
+
return this.request(`/indexes/${indexId}/documents/${documentId}`, { method: "DELETE" });
|
|
54
|
+
}
|
|
55
|
+
async deleteDocuments(indexId, options) {
|
|
56
|
+
const params = new URLSearchParams();
|
|
57
|
+
if (options.ids) options.ids.forEach((id) => params.append("ids[]", id));
|
|
58
|
+
if (options.filter) params.append("filter", options.filter);
|
|
59
|
+
return this.request(`/indexes/${indexId}/documents?${params}`, { method: "DELETE" });
|
|
60
|
+
}
|
|
61
|
+
async search(indexId, params) {
|
|
62
|
+
const searchParams = new URLSearchParams();
|
|
63
|
+
if (params?.q) searchParams.append("q", params.q);
|
|
64
|
+
if (params?.offset) searchParams.append("offset", params.offset.toString());
|
|
65
|
+
if (params?.limit) searchParams.append("limit", params.limit.toString());
|
|
66
|
+
if (params?.page) searchParams.append("page", params.page.toString());
|
|
67
|
+
if (params?.sort) params.sort.forEach((s) => searchParams.append("sort[]", s));
|
|
68
|
+
if (params?.attributesToRetrieve) params.attributesToRetrieve.forEach((attr) => searchParams.append("attributesToRetrieve[]", attr));
|
|
69
|
+
if (params?.attributesToExclude) params.attributesToExclude.forEach((attr) => searchParams.append("attributesToExclude[]", attr));
|
|
70
|
+
return this.request(`/indexes/${indexId}/searches?${searchParams}`, { method: "POST" });
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function createClient(options) {
|
|
74
|
+
return new BrightClient(options);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
exports.BrightClient = BrightClient;
|
|
79
|
+
exports.createClient = createClient;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
interface IndexConfig {
|
|
3
|
+
id: string;
|
|
4
|
+
primaryKey?: string;
|
|
5
|
+
}
|
|
6
|
+
interface SearchParams {
|
|
7
|
+
q?: string;
|
|
8
|
+
offset?: number;
|
|
9
|
+
limit?: number;
|
|
10
|
+
page?: number;
|
|
11
|
+
sort?: string[];
|
|
12
|
+
attributesToRetrieve?: string[];
|
|
13
|
+
attributesToExclude?: string[];
|
|
14
|
+
}
|
|
15
|
+
interface SearchResponse<T = Record<string, any>> {
|
|
16
|
+
hits: T[];
|
|
17
|
+
totalHits: number;
|
|
18
|
+
totalPages: number;
|
|
19
|
+
}
|
|
20
|
+
interface BrightClientOptions {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
fetch?: typeof fetch;
|
|
23
|
+
}
|
|
24
|
+
declare class BrightClient {
|
|
25
|
+
private baseUrl;
|
|
26
|
+
private fetchFn;
|
|
27
|
+
constructor(options: BrightClientOptions);
|
|
28
|
+
private request;
|
|
29
|
+
createIndex(id: string, primaryKey?: string): Promise<IndexConfig>;
|
|
30
|
+
updateIndex(id: string, config: Partial<IndexConfig>): Promise<IndexConfig>;
|
|
31
|
+
deleteIndex(id: string): Promise<void>;
|
|
32
|
+
addDocuments(indexId: string, documents: Record<string, any>[], format?: 'jsoneachrow'): Promise<{
|
|
33
|
+
indexed: number;
|
|
34
|
+
}>;
|
|
35
|
+
updateDocument(indexId: string, documentId: string, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
36
|
+
deleteDocument(indexId: string, documentId: string): Promise<void>;
|
|
37
|
+
deleteDocuments(indexId: string, options: {
|
|
38
|
+
ids?: string[];
|
|
39
|
+
filter?: string;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
search<T = Record<string, any>>(indexId: string, params?: SearchParams): Promise<SearchResponse<T>>;
|
|
42
|
+
}
|
|
43
|
+
declare function createClient(options: BrightClientOptions): BrightClient;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { BrightClient, BrightClientOptions, IndexConfig, SearchParams, SearchResponse, createClient };
|
|
46
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";UAAiB,WAAA;EAAA,EAAA,EAAA,MAAA;EAKA,UAAA,CAAA,EAAA,MAAY;AAU7B;AAMiB,UAhBA,YAAA,CAgBmB;EAKvB,CAAA,CAAA,EAAA,MAAA;EAIU,MAAA,CAAA,EAAA,MAAA;EAgCuC,KAAA,CAAA,EAAA,MAAA;EAAR,IAAA,CAAA,EAAA,MAAA;EASN,IAAA,CAAA,EAAA,MAAA,EAAA;EAAR,oBAAA,CAAA,EAAA,MAAA,EAAA;EAA+B,mBAAA,CAAA,EAAA,MAAA,EAAA;;AAOtC,UA/DhB,cA+DgB,CAAA,IA/DG,MA+DH,CAAA,MAAA,EAAA,GAAA,CAAA,CAAA,CAAA;EAUlB,IAAA,EAxEP,CAwEO,EAAA;EAEV,SAAA,EAAA,MAAA;EAaQ,UAAA,EAAA,MAAA;;AACR,UAnFY,mBAAA,CAmFZ;EAOwD,OAAA,EAAA,MAAA;EASxD,KAAA,CAAA,EAAA,OAjGY,KAiGZ;;AAoBQ,cAlHA,YAAA,CAkHA;EACe,QAAA,OAAA;EAAf,QAAA,OAAA;EAAR,WAAA,CAAA,OAAA,EA/GkB,mBA+GlB;EAAO,QAAA,OAAA;EA+BI,WAAA,CAAA,EAAA,EAAY,MAAA,EAAA,UAAU,CAAA,EAAA,MAAA,CAAA,EA9GgB,OA8GM,CA9GE,WA8GU,CAAA;kCArGhC,QAAQ,eAAe,QAAQ;2BAOtC;2CAUlB,gDAEV;;;+DAaQ,sBACR,QAAQ;uDAOgD;;;;MASxD;aAkBc,+CAEN,eACR,QAAQ,eAAe;;iBA+BZ,YAAA,UAAsB,sBAAsB"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
interface IndexConfig {
|
|
3
|
+
id: string;
|
|
4
|
+
primaryKey?: string;
|
|
5
|
+
}
|
|
6
|
+
interface SearchParams {
|
|
7
|
+
q?: string;
|
|
8
|
+
offset?: number;
|
|
9
|
+
limit?: number;
|
|
10
|
+
page?: number;
|
|
11
|
+
sort?: string[];
|
|
12
|
+
attributesToRetrieve?: string[];
|
|
13
|
+
attributesToExclude?: string[];
|
|
14
|
+
}
|
|
15
|
+
interface SearchResponse<T = Record<string, any>> {
|
|
16
|
+
hits: T[];
|
|
17
|
+
totalHits: number;
|
|
18
|
+
totalPages: number;
|
|
19
|
+
}
|
|
20
|
+
interface BrightClientOptions {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
fetch?: typeof fetch;
|
|
23
|
+
}
|
|
24
|
+
declare class BrightClient {
|
|
25
|
+
private baseUrl;
|
|
26
|
+
private fetchFn;
|
|
27
|
+
constructor(options: BrightClientOptions);
|
|
28
|
+
private request;
|
|
29
|
+
createIndex(id: string, primaryKey?: string): Promise<IndexConfig>;
|
|
30
|
+
updateIndex(id: string, config: Partial<IndexConfig>): Promise<IndexConfig>;
|
|
31
|
+
deleteIndex(id: string): Promise<void>;
|
|
32
|
+
addDocuments(indexId: string, documents: Record<string, any>[], format?: 'jsoneachrow'): Promise<{
|
|
33
|
+
indexed: number;
|
|
34
|
+
}>;
|
|
35
|
+
updateDocument(indexId: string, documentId: string, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
36
|
+
deleteDocument(indexId: string, documentId: string): Promise<void>;
|
|
37
|
+
deleteDocuments(indexId: string, options: {
|
|
38
|
+
ids?: string[];
|
|
39
|
+
filter?: string;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
search<T = Record<string, any>>(indexId: string, params?: SearchParams): Promise<SearchResponse<T>>;
|
|
42
|
+
}
|
|
43
|
+
declare function createClient(options: BrightClientOptions): BrightClient;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { BrightClient, BrightClientOptions, IndexConfig, SearchParams, SearchResponse, createClient };
|
|
46
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";UAAiB,WAAA;EAAA,EAAA,EAAA,MAAA;EAKA,UAAA,CAAA,EAAA,MAAY;AAU7B;AAMiB,UAhBA,YAAA,CAgBmB;EAKvB,CAAA,CAAA,EAAA,MAAA;EAIU,MAAA,CAAA,EAAA,MAAA;EAgCuC,KAAA,CAAA,EAAA,MAAA;EAAR,IAAA,CAAA,EAAA,MAAA;EASN,IAAA,CAAA,EAAA,MAAA,EAAA;EAAR,oBAAA,CAAA,EAAA,MAAA,EAAA;EAA+B,mBAAA,CAAA,EAAA,MAAA,EAAA;;AAOtC,UA/DhB,cA+DgB,CAAA,IA/DG,MA+DH,CAAA,MAAA,EAAA,GAAA,CAAA,CAAA,CAAA;EAUlB,IAAA,EAxEP,CAwEO,EAAA;EAEV,SAAA,EAAA,MAAA;EAaQ,UAAA,EAAA,MAAA;;AACR,UAnFY,mBAAA,CAmFZ;EAOwD,OAAA,EAAA,MAAA;EASxD,KAAA,CAAA,EAAA,OAjGY,KAiGZ;;AAoBQ,cAlHA,YAAA,CAkHA;EACe,QAAA,OAAA;EAAf,QAAA,OAAA;EAAR,WAAA,CAAA,OAAA,EA/GkB,mBA+GlB;EAAO,QAAA,OAAA;EA+BI,WAAA,CAAA,EAAA,EAAY,MAAA,EAAA,UAAU,CAAA,EAAA,MAAA,CAAA,EA9GgB,OA8GM,CA9GE,WA8GU,CAAA;kCArGhC,QAAQ,eAAe,QAAQ;2BAOtC;2CAUlB,gDAEV;;;+DAaQ,sBACR,QAAQ;uDAOgD;;;;MASxD;aAkBc,+CAEN,eACR,QAAQ,eAAe;;iBA+BZ,YAAA,UAAsB,sBAAsB"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
var BrightClient = class {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
5
|
+
this.fetchFn = options.fetch || globalThis.fetch;
|
|
6
|
+
}
|
|
7
|
+
async request(path, options) {
|
|
8
|
+
const url = `${this.baseUrl}${path}`;
|
|
9
|
+
const response = await this.fetchFn(url, {
|
|
10
|
+
...options,
|
|
11
|
+
headers: {
|
|
12
|
+
"Content-Type": "application/json",
|
|
13
|
+
...options?.headers
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
18
|
+
throw new Error(error.error || `HTTP ${response.status}`);
|
|
19
|
+
}
|
|
20
|
+
if (response.status === 204) return;
|
|
21
|
+
return response.json();
|
|
22
|
+
}
|
|
23
|
+
async createIndex(id, primaryKey) {
|
|
24
|
+
const params = new URLSearchParams({ id });
|
|
25
|
+
if (primaryKey) params.append("primaryKey", primaryKey);
|
|
26
|
+
return this.request(`/indexes?${params}`, { method: "POST" });
|
|
27
|
+
}
|
|
28
|
+
async updateIndex(id, config) {
|
|
29
|
+
return this.request(`/indexes/${id}`, {
|
|
30
|
+
method: "PATCH",
|
|
31
|
+
body: JSON.stringify(config)
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async deleteIndex(id) {
|
|
35
|
+
return this.request(`/indexes/${id}`, { method: "DELETE" });
|
|
36
|
+
}
|
|
37
|
+
async addDocuments(indexId, documents, format = "jsoneachrow") {
|
|
38
|
+
const body = documents.map((doc) => JSON.stringify(doc)).join("\n");
|
|
39
|
+
const params = new URLSearchParams({ format });
|
|
40
|
+
return this.request(`/indexes/${indexId}/documents?${params}`, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
body
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async updateDocument(indexId, documentId, updates) {
|
|
46
|
+
return this.request(`/indexes/${indexId}/documents/${documentId}`, {
|
|
47
|
+
method: "PATCH",
|
|
48
|
+
body: JSON.stringify(updates)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async deleteDocument(indexId, documentId) {
|
|
52
|
+
return this.request(`/indexes/${indexId}/documents/${documentId}`, { method: "DELETE" });
|
|
53
|
+
}
|
|
54
|
+
async deleteDocuments(indexId, options) {
|
|
55
|
+
const params = new URLSearchParams();
|
|
56
|
+
if (options.ids) options.ids.forEach((id) => params.append("ids[]", id));
|
|
57
|
+
if (options.filter) params.append("filter", options.filter);
|
|
58
|
+
return this.request(`/indexes/${indexId}/documents?${params}`, { method: "DELETE" });
|
|
59
|
+
}
|
|
60
|
+
async search(indexId, params) {
|
|
61
|
+
const searchParams = new URLSearchParams();
|
|
62
|
+
if (params?.q) searchParams.append("q", params.q);
|
|
63
|
+
if (params?.offset) searchParams.append("offset", params.offset.toString());
|
|
64
|
+
if (params?.limit) searchParams.append("limit", params.limit.toString());
|
|
65
|
+
if (params?.page) searchParams.append("page", params.page.toString());
|
|
66
|
+
if (params?.sort) params.sort.forEach((s) => searchParams.append("sort[]", s));
|
|
67
|
+
if (params?.attributesToRetrieve) params.attributesToRetrieve.forEach((attr) => searchParams.append("attributesToRetrieve[]", attr));
|
|
68
|
+
if (params?.attributesToExclude) params.attributesToExclude.forEach((attr) => searchParams.append("attributesToExclude[]", attr));
|
|
69
|
+
return this.request(`/indexes/${indexId}/searches?${searchParams}`, { method: "POST" });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
function createClient(options) {
|
|
73
|
+
return new BrightClient(options);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { BrightClient, createClient };
|
|
78
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["export interface IndexConfig {\n id: string;\n primaryKey?: string;\n}\n\nexport interface SearchParams {\n q?: string;\n offset?: number;\n limit?: number;\n page?: number;\n sort?: string[];\n attributesToRetrieve?: string[];\n attributesToExclude?: string[];\n}\n\nexport interface SearchResponse<T = Record<string, any>> {\n hits: T[];\n totalHits: number;\n totalPages: number;\n}\n\nexport interface BrightClientOptions {\n baseUrl: string;\n fetch?: typeof fetch;\n}\n\nexport class BrightClient {\n private baseUrl: string;\n private fetchFn: typeof fetch;\n\n constructor(options: BrightClientOptions) {\n this.baseUrl = options.baseUrl.replace(/\\/$/, '');\n this.fetchFn = options.fetch || globalThis.fetch;\n }\n\n private async request<T>(\n path: string,\n options?: RequestInit\n ): Promise<T> {\n const url = `${this.baseUrl}${path}`;\n const response = await this.fetchFn(url, {\n ...options,\n headers: {\n 'Content-Type': 'application/json',\n ...options?.headers,\n },\n });\n\n if (!response.ok) {\n const error = await response.json().catch(() => ({ error: response.statusText }));\n throw new Error(error.error || `HTTP ${response.status}`);\n }\n\n if (response.status === 204) {\n return undefined as T;\n }\n\n return response.json();\n }\n\n // Index Management\n\n async createIndex(id: string, primaryKey?: string): Promise<IndexConfig> {\n const params = new URLSearchParams({ id });\n if (primaryKey) params.append('primaryKey', primaryKey);\n \n return this.request<IndexConfig>(`/indexes?${params}`, {\n method: 'POST',\n });\n }\n\n async updateIndex(id: string, config: Partial<IndexConfig>): Promise<IndexConfig> {\n return this.request<IndexConfig>(`/indexes/${id}`, {\n method: 'PATCH',\n body: JSON.stringify(config),\n });\n }\n\n async deleteIndex(id: string): Promise<void> {\n return this.request<void>(`/indexes/${id}`, {\n method: 'DELETE',\n });\n }\n\n // Document Operations\n\n async addDocuments(\n indexId: string,\n documents: Record<string, any>[],\n format: 'jsoneachrow' = 'jsoneachrow'\n ): Promise<{ indexed: number }> {\n const body = documents.map(doc => JSON.stringify(doc)).join('\\n');\n const params = new URLSearchParams({ format });\n \n return this.request<{ indexed: number }>(`/indexes/${indexId}/documents?${params}`, {\n method: 'POST',\n body,\n });\n }\n\n async updateDocument(\n indexId: string,\n documentId: string,\n updates: Record<string, any>\n ): Promise<Record<string, any>> {\n return this.request<Record<string, any>>(`/indexes/${indexId}/documents/${documentId}`, {\n method: 'PATCH',\n body: JSON.stringify(updates),\n });\n }\n\n async deleteDocument(indexId: string, documentId: string): Promise<void> {\n return this.request<void>(`/indexes/${indexId}/documents/${documentId}`, {\n method: 'DELETE',\n });\n }\n\n async deleteDocuments(\n indexId: string,\n options: { ids?: string[]; filter?: string }\n ): Promise<void> {\n const params = new URLSearchParams();\n \n if (options.ids) {\n options.ids.forEach(id => params.append('ids[]', id));\n }\n \n if (options.filter) {\n params.append('filter', options.filter);\n }\n \n return this.request<void>(`/indexes/${indexId}/documents?${params}`, {\n method: 'DELETE',\n });\n }\n\n // Search\n\n async search<T = Record<string, any>>(\n indexId: string,\n params?: SearchParams\n ): Promise<SearchResponse<T>> {\n const searchParams = new URLSearchParams();\n \n if (params?.q) searchParams.append('q', params.q);\n if (params?.offset) searchParams.append('offset', params.offset.toString());\n if (params?.limit) searchParams.append('limit', params.limit.toString());\n if (params?.page) searchParams.append('page', params.page.toString());\n \n if (params?.sort) {\n params.sort.forEach(s => searchParams.append('sort[]', s));\n }\n \n if (params?.attributesToRetrieve) {\n params.attributesToRetrieve.forEach(attr => \n searchParams.append('attributesToRetrieve[]', attr)\n );\n }\n \n if (params?.attributesToExclude) {\n params.attributesToExclude.forEach(attr => \n searchParams.append('attributesToExclude[]', attr)\n );\n }\n \n return this.request<SearchResponse<T>>(`/indexes/${indexId}/searches?${searchParams}`, {\n method: 'POST',\n });\n }\n}\n\n// Convenience function\nexport function createClient(options: BrightClientOptions): BrightClient {\n return new BrightClient(options);\n}\n"],"mappings":"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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bright-client",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "TypeScript client library for Bright search database",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/index.cjs",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsdown src/index.ts --dts --format cjs,esm",
|
|
20
|
+
"prepublishOnly": "npm run build"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"search",
|
|
24
|
+
"full-text",
|
|
25
|
+
"bleve",
|
|
26
|
+
"database",
|
|
27
|
+
"client",
|
|
28
|
+
"typescript"
|
|
29
|
+
],
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/nnstd/bright-js.git"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"tsdown": "latest",
|
|
38
|
+
"typescript": "^5.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|