@or-sdk/lookup 1.1.0-beta.1830.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 +983 -0
- package/dist/cjs/Lookup.js +413 -0
- package/dist/cjs/Lookup.js.map +1 -0
- package/dist/cjs/__tests__/QnA.collections.spec.js +177 -0
- package/dist/cjs/__tests__/QnA.collections.spec.js.map +1 -0
- package/dist/cjs/__tests__/fixtures/collection.json +34 -0
- package/dist/cjs/constants.js +5 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/error-parser/OrNetworkError.js +45 -0
- package/dist/cjs/error-parser/OrNetworkError.js.map +1 -0
- package/dist/cjs/error-parser/index.js +34 -0
- package/dist/cjs/error-parser/index.js.map +1 -0
- package/dist/cjs/error-parser/parse-axios-error.js +38 -0
- package/dist/cjs/error-parser/parse-axios-error.js.map +1 -0
- package/dist/cjs/error-parser/processors.js +22 -0
- package/dist/cjs/error-parser/processors.js.map +1 -0
- package/dist/cjs/error-parser/types.js +3 -0
- package/dist/cjs/error-parser/types.js.map +1 -0
- package/dist/cjs/guards.js +9 -0
- package/dist/cjs/guards.js.map +1 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types.js +11 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/Lookup.js +221 -0
- package/dist/esm/Lookup.js.map +1 -0
- package/dist/esm/__tests__/QnA.collections.spec.js +89 -0
- package/dist/esm/__tests__/QnA.collections.spec.js.map +1 -0
- package/dist/esm/__tests__/fixtures/collection.json +34 -0
- package/dist/esm/constants.js +2 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/error-parser/OrNetworkError.js +14 -0
- package/dist/esm/error-parser/OrNetworkError.js.map +1 -0
- package/dist/esm/error-parser/index.js +12 -0
- package/dist/esm/error-parser/index.js.map +1 -0
- package/dist/esm/error-parser/parse-axios-error.js +34 -0
- package/dist/esm/error-parser/parse-axios-error.js.map +1 -0
- package/dist/esm/error-parser/processors.js +16 -0
- package/dist/esm/error-parser/processors.js.map +1 -0
- package/dist/esm/error-parser/types.js +2 -0
- package/dist/esm/error-parser/types.js.map +1 -0
- package/dist/esm/guards.js +5 -0
- package/dist/esm/guards.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types.js +8 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/types/Lookup.d.ts +27 -0
- package/dist/types/Lookup.d.ts.map +1 -0
- package/dist/types/__tests__/QnA.collections.spec.d.ts +2 -0
- package/dist/types/__tests__/QnA.collections.spec.d.ts.map +1 -0
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/error-parser/OrNetworkError.d.ts +14 -0
- package/dist/types/error-parser/OrNetworkError.d.ts.map +1 -0
- package/dist/types/error-parser/index.d.ts +5 -0
- package/dist/types/error-parser/index.d.ts.map +1 -0
- package/dist/types/error-parser/parse-axios-error.d.ts +3 -0
- package/dist/types/error-parser/parse-axios-error.d.ts.map +1 -0
- package/dist/types/error-parser/processors.d.ts +7 -0
- package/dist/types/error-parser/processors.d.ts.map +1 -0
- package/dist/types/error-parser/types.d.ts +6 -0
- package/dist/types/error-parser/types.d.ts.map +1 -0
- package/dist/types/guards.d.ts +3 -0
- package/dist/types/guards.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types.d.ts +146 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +37 -0
- package/src/Lookup.ts +353 -0
- package/src/__tests__/QnA.collections.spec.ts +108 -0
- package/src/__tests__/fixtures/collection.json +34 -0
- package/src/constants.ts +1 -0
- package/src/error-parser/OrNetworkError.ts +23 -0
- package/src/error-parser/index.ts +16 -0
- package/src/error-parser/parse-axios-error.ts +40 -0
- package/src/error-parser/processors.ts +21 -0
- package/src/error-parser/types.ts +6 -0
- package/src/guards.ts +5 -0
- package/src/index.ts +4 -0
- package/src/types.ts +629 -0
- package/tsconfig.dev.json +8 -0
- package/tsconfig.esm.json +12 -0
- package/tsconfig.json +7 -0
- package/tsconfig.types.json +10 -0
- package/vitest.config.js +24 -0
package/src/Lookup.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { Base, List, makeList } from '@or-sdk/base';
|
|
2
|
+
import { SERVICE_KEY } from './constants';
|
|
3
|
+
import {
|
|
4
|
+
LoadDocument, CreateCollection, Search, Ask, UpdateCollection,
|
|
5
|
+
UpdateDocument, LookupConfig, Document, Collection, SearchResult,
|
|
6
|
+
AskResults, Find, Property, CreatePassage, Passage, UpdatePassage,
|
|
7
|
+
DeletedPassage, FindPassages,
|
|
8
|
+
} from './types';
|
|
9
|
+
import { createErrorParser, processors } from './error-parser';
|
|
10
|
+
|
|
11
|
+
const errorParser = createErrorParser(processors.AXIOS);
|
|
12
|
+
|
|
13
|
+
export class Lookup extends Base {
|
|
14
|
+
constructor(params: LookupConfig) {
|
|
15
|
+
const { token, discoveryUrl, accountId, serviceUrl } = params;
|
|
16
|
+
|
|
17
|
+
super({
|
|
18
|
+
token,
|
|
19
|
+
discoveryUrl,
|
|
20
|
+
serviceKey: SERVICE_KEY,
|
|
21
|
+
accountId,
|
|
22
|
+
serviceUrl,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
parseError(err: unknown) {
|
|
27
|
+
return errorParser(err);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async loadDocument(collectionId: string, params: LoadDocument): Promise<Document> {
|
|
31
|
+
const response = await this.callApiV2<Document>({
|
|
32
|
+
method: 'POST',
|
|
33
|
+
route: `collections/${collectionId}/documents`,
|
|
34
|
+
data: params,
|
|
35
|
+
});
|
|
36
|
+
return response;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a new collection.
|
|
41
|
+
* @param params - The collection creation parameters.
|
|
42
|
+
* @returns The created collection.
|
|
43
|
+
*/
|
|
44
|
+
async createCollection(params: CreateCollection): Promise<Collection> {
|
|
45
|
+
const response = await this.callApiV2<Collection>({
|
|
46
|
+
method: 'POST',
|
|
47
|
+
route: 'collections',
|
|
48
|
+
data: params,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create a new passage within the collection.
|
|
56
|
+
* @param collectionId - The ID of the collection the passage belongs to.
|
|
57
|
+
* @param passage - The passage creation parameters.
|
|
58
|
+
* @returns The created passage.
|
|
59
|
+
*/
|
|
60
|
+
async createPassage<T extends Record<string, unknown>>(
|
|
61
|
+
collectionId: string,
|
|
62
|
+
passage: CreatePassage<T>,
|
|
63
|
+
): Promise<Passage<T>> {
|
|
64
|
+
const response = await this.callApiV2<Passage<T>>({
|
|
65
|
+
method: 'POST',
|
|
66
|
+
route: `collections/${collectionId}/passages`,
|
|
67
|
+
data: passage,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return response;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Create batch of passages within the collection.
|
|
75
|
+
* @param collectionId - The ID of the collection the passage belongs to.
|
|
76
|
+
* @param passages - The passages to create.
|
|
77
|
+
*/
|
|
78
|
+
async createManyPassages<T extends Record<string, unknown>>(
|
|
79
|
+
collectionId: string,
|
|
80
|
+
passages: CreatePassage<T>[],
|
|
81
|
+
): Promise<void> {
|
|
82
|
+
await this.callApiV2({
|
|
83
|
+
method: 'POST',
|
|
84
|
+
route: `collections/${collectionId}/passages/batch`,
|
|
85
|
+
data: passages,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Delete a collection by ID.
|
|
91
|
+
* @param collectionId - The ID of the collection to delete.
|
|
92
|
+
* @returns The deleted collection.
|
|
93
|
+
*/
|
|
94
|
+
async deleteCollection(collectionId: string): Promise<Collection> {
|
|
95
|
+
const response = await this.callApiV2<Collection>({
|
|
96
|
+
method: 'DELETE',
|
|
97
|
+
route: `collections/${collectionId}`,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Delete a passage from a collection.
|
|
105
|
+
* @param collectionId - The ID of the collection the passage belongs to.
|
|
106
|
+
* @param passageId - The ID of the passage to delete.
|
|
107
|
+
* @returns The deleted passage.
|
|
108
|
+
*/
|
|
109
|
+
async deletePassage(collectionId: string, passageId: string): Promise<DeletedPassage> {
|
|
110
|
+
const response = await this.callApiV2<DeletedPassage>({
|
|
111
|
+
method: 'DELETE',
|
|
112
|
+
route: `collections/${collectionId}/passages/${passageId}`,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return response;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Search for documents in a collection.
|
|
120
|
+
* @param collectionId - The ID of the collection to search in.
|
|
121
|
+
* @param params - The search parameters.
|
|
122
|
+
* @returns An array of search results.
|
|
123
|
+
*/
|
|
124
|
+
async search(collectionId: string, params: Search): Promise<SearchResult[]> {
|
|
125
|
+
const response = await this.callApiV2<SearchResult[]>({
|
|
126
|
+
method: 'POST',
|
|
127
|
+
route: `collections/${collectionId}/search`,
|
|
128
|
+
data: params,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return response;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Ask a question and generate an answer based on the documents in a collection.
|
|
136
|
+
* @param collectionId - The ID of the collection to use for generating the answer.
|
|
137
|
+
* @param params - The ask question parameters.
|
|
138
|
+
* @returns The generated answer and search result.
|
|
139
|
+
*/
|
|
140
|
+
async ask(collectionId: string, params: Ask): Promise<AskResults> {
|
|
141
|
+
const response = await this.callApiV2<AskResults>({
|
|
142
|
+
method: 'POST',
|
|
143
|
+
route: `collections/${collectionId}/ask`,
|
|
144
|
+
data: params,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return response;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Update a document's description.
|
|
152
|
+
* @param collectionId - The ID of the collection the document belongs to.
|
|
153
|
+
* @param documentId - The ID of the document to update.
|
|
154
|
+
* @param params - The update document parameters.
|
|
155
|
+
* @returns The updated document.
|
|
156
|
+
*/
|
|
157
|
+
async updateDocument(collectionId: string, documentId: string, params: UpdateDocument): Promise<Document> {
|
|
158
|
+
const response = await this.callApiV2<Document>({
|
|
159
|
+
method: 'PUT',
|
|
160
|
+
route: `collections/${collectionId}/documents/${documentId}`,
|
|
161
|
+
data: params,
|
|
162
|
+
});
|
|
163
|
+
return response;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Update a passage in a collection.
|
|
168
|
+
* @param collectionId - The ID of the collection the passage belongs to.
|
|
169
|
+
* @param passageId - The ID of the passage to update.
|
|
170
|
+
* @param params - The update passage parameters.
|
|
171
|
+
* @returns The updated passage.
|
|
172
|
+
*/
|
|
173
|
+
async updatePassage<T extends Record<string, unknown>>(
|
|
174
|
+
collectionId: string,
|
|
175
|
+
passageId: string,
|
|
176
|
+
params: UpdatePassage<T>,
|
|
177
|
+
): Promise<Passage<T>> {
|
|
178
|
+
const response = await this.callApiV2<Passage<T>>({
|
|
179
|
+
method: 'PUT',
|
|
180
|
+
route: `collections/${collectionId}/passages/${passageId}`,
|
|
181
|
+
data: params,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return response;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
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.
|
|
193
|
+
*/
|
|
194
|
+
async updateCollection(collectionId: string, params: UpdateCollection): Promise<Collection> {
|
|
195
|
+
const response = await this.callApiV2<Collection>({
|
|
196
|
+
method: 'PUT',
|
|
197
|
+
route: `collections/${collectionId}`,
|
|
198
|
+
data: params,
|
|
199
|
+
});
|
|
200
|
+
return response;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
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.
|
|
208
|
+
*/
|
|
209
|
+
async getDocument(collectionId: string, documentId: string): Promise<Document> {
|
|
210
|
+
const response = await this.callApiV2<Document>({
|
|
211
|
+
method: 'GET',
|
|
212
|
+
route: `collections/${collectionId}/documents/${documentId}`,
|
|
213
|
+
});
|
|
214
|
+
return response;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Get a collection by its ID.
|
|
219
|
+
* @param collectionId - The ID of the collection to retrieve.
|
|
220
|
+
* @returns The retrieved collection.
|
|
221
|
+
*/
|
|
222
|
+
async getCollection(collectionId: string): Promise<Collection> {
|
|
223
|
+
const response = await this.callApiV2<Collection>({
|
|
224
|
+
method: 'GET',
|
|
225
|
+
route: `collections/${collectionId}`,
|
|
226
|
+
});
|
|
227
|
+
return response;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Get a single passage from a collection.
|
|
232
|
+
* @param collectionId - The ID of the collection the passage belongs to.
|
|
233
|
+
* @param passageId - The ID of the passage to retrieve.
|
|
234
|
+
* @returns The retrieved passage.
|
|
235
|
+
*/
|
|
236
|
+
async getPassage<T extends Record<string, unknown>>(collectionId: string, passageId: string): Promise<Passage<T>> {
|
|
237
|
+
const response = await this.callApiV2<Passage<T>>({
|
|
238
|
+
method: 'GET',
|
|
239
|
+
route: `collections/${collectionId}/passages/${passageId}`,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
return response;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Add a custom property to an existing collection.
|
|
247
|
+
* Custom properties can be used to store additional metadata about the collection or its passages.
|
|
248
|
+
* The available data types for properties can be found https://weaviate.io/developers/weaviate/config-refs/datatypes.
|
|
249
|
+
* Note that there are reserved property names:
|
|
250
|
+
* ['accountId', 'collection', 'document', 'content', 'loaderMetadata', 'sourceUrl']
|
|
251
|
+
* @param collectionId - The ID of the collection to add the property to.
|
|
252
|
+
* @param property - The property to add to the collection.
|
|
253
|
+
* @returns A promise that resolves when the property has been added.
|
|
254
|
+
*/
|
|
255
|
+
async addProperty(collectionId: string, property: Property): Promise<void> {
|
|
256
|
+
await this.callApiV2({
|
|
257
|
+
method: 'POST',
|
|
258
|
+
route: `collections/${collectionId}/properties`,
|
|
259
|
+
data: property,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* List all documents in a collection with optional pagination and query.
|
|
265
|
+
* @param collectionId - The ID of the collection to retrieve documents from.
|
|
266
|
+
* @param find - Optional find parameters.
|
|
267
|
+
* @returns An array of documents.
|
|
268
|
+
*/
|
|
269
|
+
async listDocuments(
|
|
270
|
+
collectionId: string,
|
|
271
|
+
params: Find = {},
|
|
272
|
+
): Promise<List<Document>> {
|
|
273
|
+
const response = await this.callApiV2<Document[]>({
|
|
274
|
+
method: 'GET',
|
|
275
|
+
route: `collections/${collectionId}/documents`,
|
|
276
|
+
params,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
return makeList(response);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* List collections with optional pagination and query.
|
|
284
|
+
* @param find - Optional find parameters.
|
|
285
|
+
* @returns An array of collections.
|
|
286
|
+
*/
|
|
287
|
+
async listCollections(
|
|
288
|
+
params: Find = {},
|
|
289
|
+
): Promise<List<Collection>> {
|
|
290
|
+
const response = await this.callApiV2<Collection[]>({
|
|
291
|
+
method: 'GET',
|
|
292
|
+
route: 'collections',
|
|
293
|
+
params,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
return makeList(response);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* List all passages in a collection with optional pagination and query.
|
|
301
|
+
* @param collectionId - The ID of the collection to retrieve passages from.
|
|
302
|
+
* @param find - Optional find parameters.
|
|
303
|
+
* @returns An array of passages.
|
|
304
|
+
*/
|
|
305
|
+
async listPassages<T extends Record<string, unknown>>(
|
|
306
|
+
collectionId: string,
|
|
307
|
+
params: FindPassages = {},
|
|
308
|
+
): Promise<List<Passage<T>>> {
|
|
309
|
+
const response = await this.callApiV2<Passage<T>[]>({
|
|
310
|
+
method: 'GET',
|
|
311
|
+
route: `collections/${collectionId}/passages`,
|
|
312
|
+
params,
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
return makeList(response);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* List all passages in a document with optional pagination, query, and ordering.
|
|
320
|
+
* @param collectionId - The ID of the collection containing the document.
|
|
321
|
+
* @param documentId - The ID of the document to retrieve passages from.
|
|
322
|
+
* @param find - Optional find parameters.
|
|
323
|
+
* @returns An array of passages with pagination info.
|
|
324
|
+
*/
|
|
325
|
+
async listPassagesInDocument<T extends Record<string, unknown>>(
|
|
326
|
+
collectionId: string,
|
|
327
|
+
documentId: string,
|
|
328
|
+
find: FindPassages = {},
|
|
329
|
+
): Promise<List<Passage<T>>> {
|
|
330
|
+
const response = await this.callApiV2<Passage<T>[]>({
|
|
331
|
+
method: 'GET',
|
|
332
|
+
route: `collections/${collectionId}/documents/${documentId}/passages`,
|
|
333
|
+
params: find,
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
return makeList(response);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Delete a document from a collection.
|
|
341
|
+
* @param collectionId - The ID of the collection to delete the document from.
|
|
342
|
+
* @param documentId - The ID of the document to delete.
|
|
343
|
+
* @returns The deleted document.
|
|
344
|
+
*/
|
|
345
|
+
async deleteDocument(collectionId: string, documentId: string): Promise<Document> {
|
|
346
|
+
const response = await this.callApiV2<Document>({
|
|
347
|
+
method: 'DELETE',
|
|
348
|
+
route: `collections/${collectionId}/documents/${documentId}`,
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
return response;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
|
|
2
|
+
import { rest } from 'msw';
|
|
3
|
+
import { setupServer } from 'msw/node';
|
|
4
|
+
|
|
5
|
+
import { Lookup } from '..';
|
|
6
|
+
import type { LookupConfig, CreateCollection } from '..';
|
|
7
|
+
|
|
8
|
+
import collection from './fixtures/collection.json';
|
|
9
|
+
|
|
10
|
+
const server = setupServer(
|
|
11
|
+
rest.post('https://lookup-api.svc.staging.api.onereach.ai/collections', async (req, res, ctx) => {
|
|
12
|
+
if (req.headers.get('authorization') !== 'valid-auth-token') {
|
|
13
|
+
return res(ctx.status(401));
|
|
14
|
+
}
|
|
15
|
+
const body = await req.json();
|
|
16
|
+
if (body.ise === true) {
|
|
17
|
+
return res(
|
|
18
|
+
ctx.status(500),
|
|
19
|
+
ctx.json({
|
|
20
|
+
error: 'Some tech error message here',
|
|
21
|
+
statusCode: 500,
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (typeof body.name === 'string' && body.name.length < 3) {
|
|
26
|
+
return res(
|
|
27
|
+
ctx.status(400),
|
|
28
|
+
ctx.json({
|
|
29
|
+
error: [
|
|
30
|
+
'name must be longer than or equal to 5 characters',
|
|
31
|
+
'Can start only with a letter.',
|
|
32
|
+
],
|
|
33
|
+
statusCode: 400,
|
|
34
|
+
})
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return res(ctx.json(collection));
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const createLookupInstance = (opts: Partial<LookupConfig> = {}) => {
|
|
42
|
+
return new Lookup({
|
|
43
|
+
serviceUrl: 'https://lookup-api.svc.staging.api.onereach.ai',
|
|
44
|
+
token: 'valid-auth-token',
|
|
45
|
+
...opts,
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
describe('Lookup', () => {
|
|
50
|
+
beforeAll(() => server.listen());
|
|
51
|
+
afterEach(() => server.resetHandlers());
|
|
52
|
+
afterAll(() => server.close());
|
|
53
|
+
|
|
54
|
+
it('should create an instance successfully', () => {
|
|
55
|
+
const lookup = createLookupInstance();
|
|
56
|
+
expect(lookup).toBeInstanceOf(Lookup);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should create a new collection properly', async () => {
|
|
60
|
+
const lookup = createLookupInstance();
|
|
61
|
+
const resp = await lookup.createCollection({
|
|
62
|
+
name: 'dev collection',
|
|
63
|
+
});
|
|
64
|
+
expect(resp).toEqual(collection);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should show UnAuth Error if token is invalid', async () => {
|
|
68
|
+
const lookup = createLookupInstance({
|
|
69
|
+
token: 'invalid-token',
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const promise = lookup.createCollection({
|
|
73
|
+
name: 'dev collection',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
await expect(promise).rejects.toMatchObject({
|
|
77
|
+
status: 401,
|
|
78
|
+
message: 'We are not able to authenticate you',
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should show validation errors properly', async () => {
|
|
83
|
+
const lookup = createLookupInstance();
|
|
84
|
+
|
|
85
|
+
const promise = lookup.createCollection({
|
|
86
|
+
name: '1',
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
await expect(promise).rejects.toMatchObject({
|
|
90
|
+
status: 400,
|
|
91
|
+
message: 'name must be longer than or equal to 5 characters. Can start only with a letter.',
|
|
92
|
+
});
|
|
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
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "c5cd0f6d-cf98-4f2e-822e-42be148012b1",
|
|
3
|
+
"accountId": "d40ff964-dd12-4cbd-bb85-6ad35af3f908",
|
|
4
|
+
"description": "some lorem description here",
|
|
5
|
+
"name": "dev collection",
|
|
6
|
+
"createdAt": "2023-06-21T05:38:51.211Z",
|
|
7
|
+
"updatedAt": "2023-06-21T05:38:51.211Z",
|
|
8
|
+
"properties": [
|
|
9
|
+
{
|
|
10
|
+
"name": "document",
|
|
11
|
+
"dataType": "Document",
|
|
12
|
+
"description": "A document the passage belongs to."
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "content",
|
|
16
|
+
"dataType": "text",
|
|
17
|
+
"description": "the searchable passage content."
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "loaderMetadata",
|
|
21
|
+
"dataType": "text",
|
|
22
|
+
"description": "JSON encoded metadata of the content loader."
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "sourceUrl",
|
|
26
|
+
"dataType": "text",
|
|
27
|
+
"description": "The url of the document this passage belongs to."
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "dddddd",
|
|
31
|
+
"dataType": "date[]"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SERVICE_KEY = 'lookup-api';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type CtorOptions = {
|
|
2
|
+
message: string;
|
|
3
|
+
description?: string;
|
|
4
|
+
status?: number;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export class OrNetworkError extends Error {
|
|
8
|
+
private _status: number | undefined;
|
|
9
|
+
private _description: string | undefined;
|
|
10
|
+
constructor({ description, message, status }: CtorOptions) {
|
|
11
|
+
super(message);
|
|
12
|
+
this._status = status;
|
|
13
|
+
this._description = description;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get status() {
|
|
17
|
+
return this._status;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get description() {
|
|
21
|
+
return this._description;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ErrorProcessor } from './types';
|
|
2
|
+
|
|
3
|
+
export * from './OrNetworkError';
|
|
4
|
+
export * from './processors';
|
|
5
|
+
|
|
6
|
+
export function createErrorParser<E>(...processors: ErrorProcessor<E>[]) {
|
|
7
|
+
return function parseError(error: unknown) {
|
|
8
|
+
const processor = processors.find((p) => p.guard(error));
|
|
9
|
+
|
|
10
|
+
if (processor) {
|
|
11
|
+
return processor.convertError(error as E);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return new Error('Unknown Error');
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AxiosError } from 'axios';
|
|
2
|
+
|
|
3
|
+
import { OrNetworkError } from './OrNetworkError';
|
|
4
|
+
|
|
5
|
+
const MESSAGES: Record<number, string> = {
|
|
6
|
+
401: 'We are not able to authenticate you',
|
|
7
|
+
500: 'Internal server error',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function parseAxiosError(e: AxiosError): Error {
|
|
11
|
+
if (e.response) {
|
|
12
|
+
const data = e.response.data as Record<string, unknown>;
|
|
13
|
+
const status = e.response.status;
|
|
14
|
+
const generalMessage = MESSAGES[status] || e.message;
|
|
15
|
+
const message = normalizeMessage(data.message || data.error) || generalMessage;
|
|
16
|
+
|
|
17
|
+
if (status < 500) {
|
|
18
|
+
return new OrNetworkError({
|
|
19
|
+
message,
|
|
20
|
+
status,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return new OrNetworkError({
|
|
24
|
+
message: generalMessage,
|
|
25
|
+
status,
|
|
26
|
+
description: message,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return new Error(e.message);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function normalizeMessage(message: unknown): string | undefined {
|
|
34
|
+
if (typeof message === 'string') {
|
|
35
|
+
return message;
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(message)) {
|
|
38
|
+
return message.join('. ');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parseAxiosError as deprecatedParseAxiosError } from '@or-sdk/base';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import type { AxiosError } from 'axios';
|
|
4
|
+
|
|
5
|
+
import { parseAxiosError } from './parse-axios-error';
|
|
6
|
+
import type { ErrorParser, ErrorProcessor } from './types';
|
|
7
|
+
|
|
8
|
+
const DEPRECATED_AXIOS: ErrorProcessor<AxiosError> = {
|
|
9
|
+
guard: axios.isAxiosError,
|
|
10
|
+
convertError: deprecatedParseAxiosError as ErrorParser<AxiosError>,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const AXIOS: ErrorProcessor<AxiosError> = {
|
|
14
|
+
guard: axios.isAxiosError,
|
|
15
|
+
convertError: parseAxiosError,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const processors = {
|
|
19
|
+
AXIOS,
|
|
20
|
+
DEPRECATED_AXIOS,
|
|
21
|
+
};
|
package/src/guards.ts
ADDED
package/src/index.ts
ADDED