@or-sdk/lookup 1.1.0 → 1.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.
Files changed (33) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +28 -30
  3. package/dist/cjs/Lookup.js +71 -114
  4. package/dist/cjs/Lookup.js.map +1 -1
  5. package/dist/cjs/__tests__/QnA.collections.spec.js +0 -20
  6. package/dist/cjs/__tests__/QnA.collections.spec.js.map +1 -1
  7. package/dist/cjs/error-parser/OrNetworkError.js +1 -9
  8. package/dist/cjs/error-parser/OrNetworkError.js.map +1 -1
  9. package/dist/cjs/error-parser/parse-axios-error.js +1 -8
  10. package/dist/cjs/error-parser/parse-axios-error.js.map +1 -1
  11. package/dist/cjs/types.js.map +1 -1
  12. package/dist/esm/Lookup.js +40 -114
  13. package/dist/esm/Lookup.js.map +1 -1
  14. package/dist/esm/__tests__/QnA.collections.spec.js +0 -11
  15. package/dist/esm/__tests__/QnA.collections.spec.js.map +1 -1
  16. package/dist/esm/error-parser/OrNetworkError.js +1 -5
  17. package/dist/esm/error-parser/OrNetworkError.js.map +1 -1
  18. package/dist/esm/error-parser/parse-axios-error.js +1 -8
  19. package/dist/esm/error-parser/parse-axios-error.js.map +1 -1
  20. package/dist/esm/types.js.map +1 -1
  21. package/dist/types/Lookup.d.ts +21 -21
  22. package/dist/types/Lookup.d.ts.map +1 -1
  23. package/dist/types/error-parser/OrNetworkError.d.ts +1 -3
  24. package/dist/types/error-parser/OrNetworkError.d.ts.map +1 -1
  25. package/dist/types/error-parser/parse-axios-error.d.ts.map +1 -1
  26. package/dist/types/types.d.ts +3 -0
  27. package/dist/types/types.d.ts.map +1 -1
  28. package/package.json +2 -2
  29. package/src/Lookup.ts +110 -28
  30. package/src/__tests__/QnA.collections.spec.ts +1 -15
  31. package/src/error-parser/OrNetworkError.ts +1 -7
  32. package/src/error-parser/parse-axios-error.ts +1 -8
  33. package/src/types.ts +4 -0
package/src/Lookup.ts CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  LoadDocument, CreateCollection, Search, Ask, UpdateCollection,
5
5
  UpdateDocument, LookupConfig, Document, Collection, SearchResult,
6
6
  AskResults, Find, Property, CreatePassage, Passage, UpdatePassage,
7
- DeletedPassage, FindPassages,
7
+ DeletedPassage, FindPassages, CallOptions,
8
8
  } from './types';
9
9
  import { createErrorParser, processors } from './error-parser';
10
10
 
@@ -27,25 +27,40 @@ export class Lookup extends Base {
27
27
  return errorParser(err);
28
28
  }
29
29
 
30
- async loadDocument(collectionId: string, params: LoadDocument): Promise<Document> {
30
+ /**
31
+ * Load the document into collection.
32
+ * @param collectionId - The ID of the collection the document belongs to.
33
+ * @param params - The document loading parameters.
34
+ * @param [options] - The API call options.
35
+ * @returns The created document.
36
+ */
37
+ async loadDocument(
38
+ collectionId: string,
39
+ params: LoadDocument,
40
+ options: CallOptions = {},
41
+ ): Promise<Document> {
31
42
  const response = await this.callApiV2<Document>({
32
43
  method: 'POST',
33
44
  route: `collections/${collectionId}/documents`,
34
45
  data: params,
46
+ ...options,
35
47
  });
48
+
36
49
  return response;
37
50
  }
38
51
 
39
52
  /**
40
53
  * Create a new collection.
41
54
  * @param params - The collection creation parameters.
55
+ * @param [options={}] - The API call options.
42
56
  * @returns The created collection.
43
57
  */
44
- async createCollection(params: CreateCollection): Promise<Collection> {
58
+ async createCollection(params: CreateCollection, options: CallOptions = {}): Promise<Collection> {
45
59
  const response = await this.callApiV2<Collection>({
46
60
  method: 'POST',
47
61
  route: 'collections',
48
62
  data: params,
63
+ ...options,
49
64
  });
50
65
 
51
66
  return response;
@@ -55,16 +70,19 @@ export class Lookup extends Base {
55
70
  * Create a new passage within the collection.
56
71
  * @param collectionId - The ID of the collection the passage belongs to.
57
72
  * @param passage - The passage creation parameters.
73
+ * @param [options={}] - The API call options.
58
74
  * @returns The created passage.
59
75
  */
60
76
  async createPassage<T extends Record<string, unknown>>(
61
77
  collectionId: string,
62
78
  passage: CreatePassage<T>,
79
+ options: CallOptions = {},
63
80
  ): Promise<Passage<T>> {
64
81
  const response = await this.callApiV2<Passage<T>>({
65
82
  method: 'POST',
66
83
  route: `collections/${collectionId}/passages`,
67
84
  data: passage,
85
+ ...options,
68
86
  });
69
87
 
70
88
  return response;
@@ -74,27 +92,32 @@ export class Lookup extends Base {
74
92
  * Create batch of passages within the collection.
75
93
  * @param collectionId - The ID of the collection the passage belongs to.
76
94
  * @param passages - The passages to create.
95
+ * @param [options={}] - The API call options.
77
96
  */
78
97
  async createManyPassages<T extends Record<string, unknown>>(
79
98
  collectionId: string,
80
99
  passages: CreatePassage<T>[],
100
+ options: CallOptions = {},
81
101
  ): Promise<void> {
82
102
  await this.callApiV2({
83
103
  method: 'POST',
84
104
  route: `collections/${collectionId}/passages/batch`,
85
105
  data: passages,
106
+ ...options,
86
107
  });
87
108
  }
88
109
 
89
110
  /**
90
111
  * Delete a collection by ID.
91
112
  * @param collectionId - The ID of the collection to delete.
113
+ * @param [options={}] - The API call options.
92
114
  * @returns The deleted collection.
93
115
  */
94
- async deleteCollection(collectionId: string): Promise<Collection> {
116
+ async deleteCollection(collectionId: string, options: CallOptions = {}): Promise<Collection> {
95
117
  const response = await this.callApiV2<Collection>({
96
118
  method: 'DELETE',
97
119
  route: `collections/${collectionId}`,
120
+ ...options,
98
121
  });
99
122
 
100
123
  return response;
@@ -104,12 +127,14 @@ export class Lookup extends Base {
104
127
  * Delete a passage from a collection.
105
128
  * @param collectionId - The ID of the collection the passage belongs to.
106
129
  * @param passageId - The ID of the passage to delete.
130
+ * @param [options={}] - The API call options.
107
131
  * @returns The deleted passage.
108
132
  */
109
- async deletePassage(collectionId: string, passageId: string): Promise<DeletedPassage> {
133
+ async deletePassage(collectionId: string, passageId: string, options: CallOptions = {}): Promise<DeletedPassage> {
110
134
  const response = await this.callApiV2<DeletedPassage>({
111
135
  method: 'DELETE',
112
136
  route: `collections/${collectionId}/passages/${passageId}`,
137
+ ...options,
113
138
  });
114
139
 
115
140
  return response;
@@ -119,13 +144,15 @@ export class Lookup extends Base {
119
144
  * Search for documents in a collection.
120
145
  * @param collectionId - The ID of the collection to search in.
121
146
  * @param params - The search parameters.
147
+ * @param [options={}] - The API call options.
122
148
  * @returns An array of search results.
123
149
  */
124
- async search(collectionId: string, params: Search): Promise<SearchResult[]> {
150
+ async search(collectionId: string, params: Search, options: CallOptions = {}): Promise<SearchResult[]> {
125
151
  const response = await this.callApiV2<SearchResult[]>({
126
152
  method: 'POST',
127
153
  route: `collections/${collectionId}/search`,
128
154
  data: params,
155
+ ...options,
129
156
  });
130
157
 
131
158
  return response;
@@ -135,13 +162,15 @@ export class Lookup extends Base {
135
162
  * Ask a question and generate an answer based on the documents in a collection.
136
163
  * @param collectionId - The ID of the collection to use for generating the answer.
137
164
  * @param params - The ask question parameters.
165
+ * @param [options={}] - The API call options.
138
166
  * @returns The generated answer and search result.
139
167
  */
140
- async ask(collectionId: string, params: Ask): Promise<AskResults> {
168
+ async ask(collectionId: string, params: Ask, options: CallOptions = {}): Promise<AskResults> {
141
169
  const response = await this.callApiV2<AskResults>({
142
170
  method: 'POST',
143
171
  route: `collections/${collectionId}/ask`,
144
172
  data: params,
173
+ ...options,
145
174
  });
146
175
 
147
176
  return response;
@@ -152,13 +181,20 @@ export class Lookup extends Base {
152
181
  * @param collectionId - The ID of the collection the document belongs to.
153
182
  * @param documentId - The ID of the document to update.
154
183
  * @param params - The update document parameters.
184
+ * @param [options={}] - The API call options.
155
185
  * @returns The updated document.
156
186
  */
157
- async updateDocument(collectionId: string, documentId: string, params: UpdateDocument): Promise<Document> {
187
+ async updateDocument(
188
+ collectionId: string,
189
+ documentId: string,
190
+ params: UpdateDocument,
191
+ options: CallOptions = {},
192
+ ): Promise<Document> {
158
193
  const response = await this.callApiV2<Document>({
159
194
  method: 'PUT',
160
195
  route: `collections/${collectionId}/documents/${documentId}`,
161
196
  data: params,
197
+ ...options,
162
198
  });
163
199
  return response;
164
200
  }
@@ -168,48 +204,62 @@ export class Lookup extends Base {
168
204
  * @param collectionId - The ID of the collection the passage belongs to.
169
205
  * @param passageId - The ID of the passage to update.
170
206
  * @param params - The update passage parameters.
207
+ * @param [options={}] - The API call options.
171
208
  * @returns The updated passage.
172
209
  */
173
210
  async updatePassage<T extends Record<string, unknown>>(
174
211
  collectionId: string,
175
212
  passageId: string,
176
213
  params: UpdatePassage<T>,
214
+ options: CallOptions = {},
177
215
  ): Promise<Passage<T>> {
178
216
  const response = await this.callApiV2<Passage<T>>({
179
217
  method: 'PUT',
180
218
  route: `collections/${collectionId}/passages/${passageId}`,
181
219
  data: params,
220
+ ...options,
182
221
  });
183
222
 
184
223
  return response;
185
224
  }
186
225
 
187
226
  /**
188
- * Update a document's description.
189
- * @param collectionId - The ID of the collection the document belongs to.
190
- * @param documentId - The ID of the document to update.
191
- * @param params - The update document parameters.
192
- * @returns The updated document.
227
+ * Update a collection's description.
228
+ * @param collectionId - The ID of the collection to update.
229
+ * @param params - The update collection parameters.
230
+ * @param [options={}] - The API call options.
231
+ * @returns The updated collection.
193
232
  */
194
- async updateCollection(collectionId: string, params: UpdateCollection): Promise<Collection> {
233
+ async updateCollection(
234
+ collectionId: string,
235
+ params: UpdateCollection,
236
+ options: CallOptions = {},
237
+ ): Promise<Collection> {
195
238
  const response = await this.callApiV2<Collection>({
196
239
  method: 'PUT',
197
240
  route: `collections/${collectionId}`,
198
241
  data: params,
242
+ ...options,
199
243
  });
200
244
  return response;
201
245
  }
202
246
 
203
247
  /**
204
- * Update a collection's description.
205
- * @param collectionId - The ID of the collection to update.
206
- * @param params - The update collection parameters.
207
- * @returns The updated collection.
248
+ * Get a document by its ID.
249
+ * @param collectionId - The ID of the collection the document belongs to.
250
+ * @param documentId - The ID of the document to retrieve.
251
+ * @param [options={}] - The API call options.
252
+ * @returns The retrieved document.
208
253
  */
209
- async getDocument(collectionId: string, documentId: string): Promise<Document> {
254
+ async getDocument(
255
+ collectionId: string,
256
+ documentId: string,
257
+ options: CallOptions = {},
258
+ ): Promise<Document> {
210
259
  const response = await this.callApiV2<Document>({
211
260
  method: 'GET',
212
261
  route: `collections/${collectionId}/documents/${documentId}`,
262
+ ...options,
213
263
  });
214
264
  return response;
215
265
  }
@@ -217,12 +267,17 @@ export class Lookup extends Base {
217
267
  /**
218
268
  * Get a collection by its ID.
219
269
  * @param collectionId - The ID of the collection to retrieve.
270
+ * @param [options={}] - The API call options.
220
271
  * @returns The retrieved collection.
221
272
  */
222
- async getCollection(collectionId: string): Promise<Collection> {
273
+ async getCollection(
274
+ collectionId: string,
275
+ options: CallOptions = {},
276
+ ): Promise<Collection> {
223
277
  const response = await this.callApiV2<Collection>({
224
278
  method: 'GET',
225
279
  route: `collections/${collectionId}`,
280
+ ...options,
226
281
  });
227
282
  return response;
228
283
  }
@@ -231,12 +286,18 @@ export class Lookup extends Base {
231
286
  * Get a single passage from a collection.
232
287
  * @param collectionId - The ID of the collection the passage belongs to.
233
288
  * @param passageId - The ID of the passage to retrieve.
289
+ * @param [options={}] - The API call options.
234
290
  * @returns The retrieved passage.
235
291
  */
236
- async getPassage<T extends Record<string, unknown>>(collectionId: string, passageId: string): Promise<Passage<T>> {
292
+ async getPassage<T extends Record<string, unknown>>(
293
+ collectionId: string,
294
+ passageId: string,
295
+ options: CallOptions = {},
296
+ ): Promise<Passage<T>> {
237
297
  const response = await this.callApiV2<Passage<T>>({
238
298
  method: 'GET',
239
299
  route: `collections/${collectionId}/passages/${passageId}`,
300
+ ...options,
240
301
  });
241
302
 
242
303
  return response;
@@ -250,13 +311,19 @@ export class Lookup extends Base {
250
311
  * ['accountId', 'collection', 'document', 'content', 'loaderMetadata', 'sourceUrl']
251
312
  * @param collectionId - The ID of the collection to add the property to.
252
313
  * @param property - The property to add to the collection.
314
+ * @param [options={}] - The API call options.
253
315
  * @returns A promise that resolves when the property has been added.
254
316
  */
255
- async addProperty(collectionId: string, property: Property): Promise<void> {
317
+ async addProperty(
318
+ collectionId: string,
319
+ property: Property,
320
+ options: CallOptions = {},
321
+ ): Promise<void> {
256
322
  await this.callApiV2({
257
323
  method: 'POST',
258
324
  route: `collections/${collectionId}/properties`,
259
325
  data: property,
326
+ ...options,
260
327
  });
261
328
  }
262
329
 
@@ -264,16 +331,19 @@ export class Lookup extends Base {
264
331
  * List all documents in a collection with optional pagination and query.
265
332
  * @param collectionId - The ID of the collection to retrieve documents from.
266
333
  * @param find - Optional find parameters.
334
+ * @param [options={}] - The API call options.
267
335
  * @returns An array of documents.
268
336
  */
269
337
  async listDocuments(
270
338
  collectionId: string,
271
339
  params: Find = {},
340
+ options: CallOptions = {},
272
341
  ): Promise<List<Document>> {
273
342
  const response = await this.callApiV2<Document[]>({
274
343
  method: 'GET',
275
344
  route: `collections/${collectionId}/documents`,
276
345
  params,
346
+ ...options,
277
347
  });
278
348
 
279
349
  return makeList(response);
@@ -281,16 +351,16 @@ export class Lookup extends Base {
281
351
 
282
352
  /**
283
353
  * List collections with optional pagination and query.
284
- * @param find - Optional find parameters.
354
+ * @param params - Optional find parameters.
355
+ * @param [options={}] - The API call options.
285
356
  * @returns An array of collections.
286
357
  */
287
- async listCollections(
288
- params: Find = {},
289
- ): Promise<List<Collection>> {
358
+ async listCollections(params: Find = {}, options: CallOptions = {}): Promise<List<Collection>> {
290
359
  const response = await this.callApiV2<Collection[]>({
291
360
  method: 'GET',
292
361
  route: 'collections',
293
362
  params,
363
+ ...options,
294
364
  });
295
365
 
296
366
  return makeList(response);
@@ -299,17 +369,20 @@ export class Lookup extends Base {
299
369
  /**
300
370
  * List all passages in a collection with optional pagination and query.
301
371
  * @param collectionId - The ID of the collection to retrieve passages from.
302
- * @param find - Optional find parameters.
372
+ * @param params - Optional find parameters.
373
+ * @param [options={}] - The API call options.
303
374
  * @returns An array of passages.
304
375
  */
305
376
  async listPassages<T extends Record<string, unknown>>(
306
377
  collectionId: string,
307
378
  params: FindPassages = {},
379
+ options: CallOptions = {},
308
380
  ): Promise<List<Passage<T>>> {
309
381
  const response = await this.callApiV2<Passage<T>[]>({
310
382
  method: 'GET',
311
383
  route: `collections/${collectionId}/passages`,
312
384
  params,
385
+ ...options,
313
386
  });
314
387
 
315
388
  return makeList(response);
@@ -320,17 +393,20 @@ export class Lookup extends Base {
320
393
  * @param collectionId - The ID of the collection containing the document.
321
394
  * @param documentId - The ID of the document to retrieve passages from.
322
395
  * @param find - Optional find parameters.
396
+ * @param [options={}] - The API call options.
323
397
  * @returns An array of passages with pagination info.
324
398
  */
325
399
  async listPassagesInDocument<T extends Record<string, unknown>>(
326
400
  collectionId: string,
327
401
  documentId: string,
328
402
  find: FindPassages = {},
403
+ options: CallOptions = {},
329
404
  ): Promise<List<Passage<T>>> {
330
405
  const response = await this.callApiV2<Passage<T>[]>({
331
406
  method: 'GET',
332
407
  route: `collections/${collectionId}/documents/${documentId}/passages`,
333
408
  params: find,
409
+ ...options,
334
410
  });
335
411
 
336
412
  return makeList(response);
@@ -340,12 +416,18 @@ export class Lookup extends Base {
340
416
  * Delete a document from a collection.
341
417
  * @param collectionId - The ID of the collection to delete the document from.
342
418
  * @param documentId - The ID of the document to delete.
419
+ * @param [options={}] - The API call options.
343
420
  * @returns The deleted document.
344
421
  */
345
- async deleteDocument(collectionId: string, documentId: string): Promise<Document> {
422
+ async deleteDocument(
423
+ collectionId: string,
424
+ documentId: string,
425
+ options: CallOptions = {},
426
+ ): Promise<Document> {
346
427
  const response = await this.callApiV2<Document>({
347
428
  method: 'DELETE',
348
429
  route: `collections/${collectionId}/documents/${documentId}`,
430
+ ...options,
349
431
  });
350
432
 
351
433
  return response;
@@ -3,7 +3,7 @@ import { rest } from 'msw';
3
3
  import { setupServer } from 'msw/node';
4
4
 
5
5
  import { Lookup } from '..';
6
- import type { LookupConfig, CreateCollection } from '..';
6
+ import type { LookupConfig } from '..';
7
7
 
8
8
  import collection from './fixtures/collection.json';
9
9
 
@@ -91,18 +91,4 @@ describe('Lookup', () => {
91
91
  message: 'name must be longer than or equal to 5 characters. Can start only with a letter.',
92
92
  });
93
93
  });
94
-
95
- it('should show ISE properly', async () => {
96
- const lookup = createLookupInstance();
97
-
98
- const promise = lookup.createCollection({
99
- ise: true,
100
- } as unknown as CreateCollection);
101
-
102
- await expect(promise).rejects.toMatchObject({
103
- status: 500,
104
- message: 'Internal server error',
105
- description: 'Some tech error message here',
106
- });
107
- });
108
94
  });
@@ -1,23 +1,17 @@
1
1
  type CtorOptions = {
2
2
  message: string;
3
- description?: string;
4
3
  status?: number;
5
4
  };
6
5
 
7
6
  export class OrNetworkError extends Error {
8
7
  private _status: number | undefined;
9
8
  private _description: string | undefined;
10
- constructor({ description, message, status }: CtorOptions) {
9
+ constructor({ message, status }: CtorOptions) {
11
10
  super(message);
12
11
  this._status = status;
13
- this._description = description;
14
12
  }
15
13
 
16
14
  get status() {
17
15
  return this._status;
18
16
  }
19
-
20
- get description() {
21
- return this._description;
22
- }
23
17
  }
@@ -14,16 +14,9 @@ export function parseAxiosError(e: AxiosError): Error {
14
14
  const generalMessage = MESSAGES[status] || e.message;
15
15
  const message = normalizeMessage(data.message || data.error) || generalMessage;
16
16
 
17
- if (status < 500) {
18
- return new OrNetworkError({
19
- message,
20
- status,
21
- });
22
- }
23
17
  return new OrNetworkError({
24
- message: generalMessage,
18
+ message,
25
19
  status,
26
- description: message,
27
20
  });
28
21
  }
29
22
 
package/src/types.ts CHANGED
@@ -4,6 +4,10 @@ export type { OrNetworkError } from './error-parser';
4
4
 
5
5
  export type SearchMode = 'vector' | 'bm25';
6
6
 
7
+ export type CallOptions = {
8
+ signal?: AbortSignal;
9
+ };
10
+
7
11
  export type LookupConfig = {
8
12
  /**
9
13
  * token