tako-sdk 0.1.2 → 0.1.4

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 CHANGED
@@ -75,20 +75,47 @@ Parameters:
75
75
 
76
76
  Returns: `Promise<KnowledgeSearchResponse>`
77
77
 
78
+ The response contains an array of knowledge cards in the `outputs.knowledge_cards` field. Each knowledge card contains:
79
+ - `card_id`: Unique identifier for the card
80
+ - `title`: Card title
81
+ - `description`: Detailed description of the card's content
82
+ - `webpage_url`: URL of a webpage hosting the interactive knowledge card
83
+ - `image_url`: URL of a static image of the knowledge card
84
+ - `embed_url`: URL of an embeddable iframe of the knowledge card
85
+ - `sources`: The sources of the knowledge card
86
+ - `methodologies`: The methodologies of the knowledge card
87
+
88
+ For detailed API response types and subfield structure, see the [Tako API Documentation](https://docs.trytako.com/api-reference/search).
89
+
78
90
  ## Error Handling
79
91
 
80
- The SDK throws typed errors with status codes and messages:
92
+ The SDK throws typed exceptions for different errors:
81
93
 
82
94
  ```typescript
95
+ import {
96
+ TakoException,
97
+ TakoUnauthorizedException,
98
+ TakoRateLimitException,
99
+ } from 'tako-sdk';
100
+
83
101
  try {
84
102
  const results = await tako.knowledgeSearch(query);
85
- } catch (error: any) {
86
- if (error.status === 401) {
103
+ } catch (error) {
104
+ if (error instanceof TakoUnauthorizedException) {
87
105
  console.error('Authentication error:', error.message);
88
- } else if (error.status === 429) {
106
+ } else if (error instanceof TakoRateLimitException) {
89
107
  console.error('Rate limit exceeded:', error.message);
108
+ } else if (error instanceof TakoNotFoundException) {
109
+ console.error('Resource not found:', error.message);
110
+ } else if (error instanceof TakoException) {
111
+ console.error('API error:', error.message);
90
112
  } else {
91
113
  console.error('Unexpected error:', error);
92
114
  }
93
115
  }
94
116
  ```
117
+
118
+ Each exception includes:
119
+ - `status`: HTTP status code
120
+ - `message`: Error message
121
+ - `details`: Additional error details from the API (if available)
package/dist/index.cjs CHANGED
@@ -21,6 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  SourceIndex: () => SourceIndex,
24
+ TakoException: () => TakoException,
25
+ TakoRateLimitException: () => TakoRateLimitException,
26
+ TakoUnauthorizedException: () => TakoUnauthorizedException,
24
27
  createTakoClient: () => createTakoClient
25
28
  });
26
29
  module.exports = __toCommonJS(src_exports);
@@ -31,6 +34,26 @@ var SourceIndex = /* @__PURE__ */ ((SourceIndex2) => {
31
34
  SourceIndex2["WEB"] = "web";
32
35
  return SourceIndex2;
33
36
  })(SourceIndex || {});
37
+ var TakoException = class extends Error {
38
+ constructor(status, message, details) {
39
+ super(message);
40
+ this.status = status;
41
+ this.details = details;
42
+ this.name = "TakoException";
43
+ }
44
+ };
45
+ var TakoUnauthorizedException = class extends TakoException {
46
+ constructor(message = "Unauthorized", details) {
47
+ super(401, message, details);
48
+ this.name = "TakoUnauthorizedException";
49
+ }
50
+ };
51
+ var TakoRateLimitException = class extends TakoException {
52
+ constructor(message = "Rate limit exceeded", details) {
53
+ super(429, message, details);
54
+ this.name = "TakoRateLimitException";
55
+ }
56
+ };
34
57
 
35
58
  // src/client.ts
36
59
  var TakoClient = class {
@@ -43,7 +66,8 @@ var TakoClient = class {
43
66
  throw new Error("API key is required");
44
67
  }
45
68
  this.apiKey = config.apiKey;
46
- this.baseUrl = config.baseUrl || "https://trytako.com/api/v1";
69
+ this.baseUrl = config.baseUrl || "https://trytako.com";
70
+ this.pathPrefix = config.pathPrefix || "/api/v1";
47
71
  }
48
72
  /**
49
73
  * Make a request to the Tako API
@@ -53,7 +77,7 @@ var TakoClient = class {
53
77
  * @returns Response data
54
78
  */
55
79
  async request(path, method, body) {
56
- const url = `${this.baseUrl}${path}`;
80
+ const url = `${this.baseUrl}${this.pathPrefix}${path}`;
57
81
  const headers = {
58
82
  "X-API-Key": this.apiKey,
59
83
  "Content-Type": "application/json"
@@ -66,11 +90,17 @@ var TakoClient = class {
66
90
  const response = await fetch(url, options);
67
91
  if (!response.ok) {
68
92
  const errorData = await response.json();
69
- throw {
70
- status: response.status,
71
- message: errorData.message || response.statusText,
72
- details: errorData
73
- };
93
+ const message = errorData.message || response.statusText;
94
+ switch (response.status) {
95
+ case 401:
96
+ throw new TakoUnauthorizedException(message, errorData);
97
+ case 429:
98
+ throw new TakoRateLimitException(message, errorData);
99
+ case 404:
100
+ return null;
101
+ default:
102
+ throw new TakoException(response.status, message, errorData);
103
+ }
74
104
  }
75
105
  return await response.json();
76
106
  }
@@ -86,18 +116,12 @@ var TakoClient = class {
86
116
  }
87
117
  const requestBody = {
88
118
  inputs: {
89
- text,
90
- ...sourceIndexes && sourceIndexes.length > 0 ? { source_indexes: sourceIndexes } : { source_indexes: ["tako" /* TAKO */] }
91
- }
119
+ text
120
+ },
121
+ ...sourceIndexes && sourceIndexes.length > 0 ? { source_indexes: sourceIndexes } : { source_indexes: ["tako" /* TAKO */] }
92
122
  };
93
- try {
94
- return await this.request("/knowledge_search", "POST", requestBody);
95
- } catch (error) {
96
- if (error.status === 404) {
97
- return { outputs: { knowledge_cards: [] } };
98
- }
99
- throw error;
100
- }
123
+ const response = await this.request("/knowledge_search", "POST", requestBody);
124
+ return response || { outputs: { knowledge_cards: [] } };
101
125
  }
102
126
  };
103
127
 
@@ -108,5 +132,8 @@ function createTakoClient(apiKey, baseUrl) {
108
132
  // Annotate the CommonJS export names for ESM import in node:
109
133
  0 && (module.exports = {
110
134
  SourceIndex,
135
+ TakoException,
136
+ TakoRateLimitException,
137
+ TakoUnauthorizedException,
111
138
  createTakoClient
112
139
  });
package/dist/index.d.cts CHANGED
@@ -8,13 +8,14 @@ declare enum SourceIndex {
8
8
  interface TakoConfig {
9
9
  apiKey: string;
10
10
  baseUrl?: string;
11
+ pathPrefix?: string;
11
12
  }
12
13
  interface KnowledgeSearchInput {
13
14
  text: string;
14
- source_indexes?: SourceIndex[];
15
15
  }
16
16
  interface KnowledgeSearchRequest {
17
17
  inputs: KnowledgeSearchInput;
18
+ source_indexes?: SourceIndex[];
18
19
  }
19
20
  interface Source {
20
21
  source_name: string;
@@ -46,6 +47,17 @@ interface TakoError {
46
47
  message: string;
47
48
  details?: any;
48
49
  }
50
+ declare class TakoException extends Error {
51
+ status: number;
52
+ details?: any | undefined;
53
+ constructor(status: number, message: string, details?: any | undefined);
54
+ }
55
+ declare class TakoUnauthorizedException extends TakoException {
56
+ constructor(message?: string, details?: any);
57
+ }
58
+ declare class TakoRateLimitException extends TakoException {
59
+ constructor(message?: string, details?: any);
60
+ }
49
61
 
50
62
  /**
51
63
  * Tako API Client
@@ -53,6 +65,7 @@ interface TakoError {
53
65
  declare class TakoClient {
54
66
  private apiKey;
55
67
  private baseUrl;
68
+ private pathPrefix;
56
69
  /**
57
70
  * Create a new Tako API client
58
71
  * @param config Configuration for the Tako API client
@@ -83,4 +96,4 @@ declare class TakoClient {
83
96
  */
84
97
  declare function createTakoClient(apiKey: string, baseUrl?: string): TakoClient;
85
98
 
86
- export { KnowledgeCard, KnowledgeSearchInput, KnowledgeSearchRequest, KnowledgeSearchResponse, Methodology, Source, SourceIndex, TakoConfig, TakoError, createTakoClient };
99
+ export { KnowledgeCard, KnowledgeSearchInput, KnowledgeSearchRequest, KnowledgeSearchResponse, Methodology, Source, SourceIndex, TakoConfig, TakoError, TakoException, TakoRateLimitException, TakoUnauthorizedException, createTakoClient };
package/dist/index.d.ts CHANGED
@@ -8,13 +8,14 @@ declare enum SourceIndex {
8
8
  interface TakoConfig {
9
9
  apiKey: string;
10
10
  baseUrl?: string;
11
+ pathPrefix?: string;
11
12
  }
12
13
  interface KnowledgeSearchInput {
13
14
  text: string;
14
- source_indexes?: SourceIndex[];
15
15
  }
16
16
  interface KnowledgeSearchRequest {
17
17
  inputs: KnowledgeSearchInput;
18
+ source_indexes?: SourceIndex[];
18
19
  }
19
20
  interface Source {
20
21
  source_name: string;
@@ -46,6 +47,17 @@ interface TakoError {
46
47
  message: string;
47
48
  details?: any;
48
49
  }
50
+ declare class TakoException extends Error {
51
+ status: number;
52
+ details?: any | undefined;
53
+ constructor(status: number, message: string, details?: any | undefined);
54
+ }
55
+ declare class TakoUnauthorizedException extends TakoException {
56
+ constructor(message?: string, details?: any);
57
+ }
58
+ declare class TakoRateLimitException extends TakoException {
59
+ constructor(message?: string, details?: any);
60
+ }
49
61
 
50
62
  /**
51
63
  * Tako API Client
@@ -53,6 +65,7 @@ interface TakoError {
53
65
  declare class TakoClient {
54
66
  private apiKey;
55
67
  private baseUrl;
68
+ private pathPrefix;
56
69
  /**
57
70
  * Create a new Tako API client
58
71
  * @param config Configuration for the Tako API client
@@ -83,4 +96,4 @@ declare class TakoClient {
83
96
  */
84
97
  declare function createTakoClient(apiKey: string, baseUrl?: string): TakoClient;
85
98
 
86
- export { KnowledgeCard, KnowledgeSearchInput, KnowledgeSearchRequest, KnowledgeSearchResponse, Methodology, Source, SourceIndex, TakoConfig, TakoError, createTakoClient };
99
+ export { KnowledgeCard, KnowledgeSearchInput, KnowledgeSearchRequest, KnowledgeSearchResponse, Methodology, Source, SourceIndex, TakoConfig, TakoError, TakoException, TakoRateLimitException, TakoUnauthorizedException, createTakoClient };
package/dist/index.js CHANGED
@@ -4,6 +4,26 @@ var SourceIndex = /* @__PURE__ */ ((SourceIndex2) => {
4
4
  SourceIndex2["WEB"] = "web";
5
5
  return SourceIndex2;
6
6
  })(SourceIndex || {});
7
+ var TakoException = class extends Error {
8
+ constructor(status, message, details) {
9
+ super(message);
10
+ this.status = status;
11
+ this.details = details;
12
+ this.name = "TakoException";
13
+ }
14
+ };
15
+ var TakoUnauthorizedException = class extends TakoException {
16
+ constructor(message = "Unauthorized", details) {
17
+ super(401, message, details);
18
+ this.name = "TakoUnauthorizedException";
19
+ }
20
+ };
21
+ var TakoRateLimitException = class extends TakoException {
22
+ constructor(message = "Rate limit exceeded", details) {
23
+ super(429, message, details);
24
+ this.name = "TakoRateLimitException";
25
+ }
26
+ };
7
27
 
8
28
  // src/client.ts
9
29
  var TakoClient = class {
@@ -16,7 +36,8 @@ var TakoClient = class {
16
36
  throw new Error("API key is required");
17
37
  }
18
38
  this.apiKey = config.apiKey;
19
- this.baseUrl = config.baseUrl || "https://trytako.com/api/v1";
39
+ this.baseUrl = config.baseUrl || "https://trytako.com";
40
+ this.pathPrefix = config.pathPrefix || "/api/v1";
20
41
  }
21
42
  /**
22
43
  * Make a request to the Tako API
@@ -26,7 +47,7 @@ var TakoClient = class {
26
47
  * @returns Response data
27
48
  */
28
49
  async request(path, method, body) {
29
- const url = `${this.baseUrl}${path}`;
50
+ const url = `${this.baseUrl}${this.pathPrefix}${path}`;
30
51
  const headers = {
31
52
  "X-API-Key": this.apiKey,
32
53
  "Content-Type": "application/json"
@@ -39,11 +60,17 @@ var TakoClient = class {
39
60
  const response = await fetch(url, options);
40
61
  if (!response.ok) {
41
62
  const errorData = await response.json();
42
- throw {
43
- status: response.status,
44
- message: errorData.message || response.statusText,
45
- details: errorData
46
- };
63
+ const message = errorData.message || response.statusText;
64
+ switch (response.status) {
65
+ case 401:
66
+ throw new TakoUnauthorizedException(message, errorData);
67
+ case 429:
68
+ throw new TakoRateLimitException(message, errorData);
69
+ case 404:
70
+ return null;
71
+ default:
72
+ throw new TakoException(response.status, message, errorData);
73
+ }
47
74
  }
48
75
  return await response.json();
49
76
  }
@@ -59,18 +86,12 @@ var TakoClient = class {
59
86
  }
60
87
  const requestBody = {
61
88
  inputs: {
62
- text,
63
- ...sourceIndexes && sourceIndexes.length > 0 ? { source_indexes: sourceIndexes } : { source_indexes: ["tako" /* TAKO */] }
64
- }
89
+ text
90
+ },
91
+ ...sourceIndexes && sourceIndexes.length > 0 ? { source_indexes: sourceIndexes } : { source_indexes: ["tako" /* TAKO */] }
65
92
  };
66
- try {
67
- return await this.request("/knowledge_search", "POST", requestBody);
68
- } catch (error) {
69
- if (error.status === 404) {
70
- return { outputs: { knowledge_cards: [] } };
71
- }
72
- throw error;
73
- }
93
+ const response = await this.request("/knowledge_search", "POST", requestBody);
94
+ return response || { outputs: { knowledge_cards: [] } };
74
95
  }
75
96
  };
76
97
 
@@ -80,5 +101,8 @@ function createTakoClient(apiKey, baseUrl) {
80
101
  }
81
102
  export {
82
103
  SourceIndex,
104
+ TakoException,
105
+ TakoRateLimitException,
106
+ TakoUnauthorizedException,
83
107
  createTakoClient
84
108
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tako-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "JavaScript/TypeScript SDK for the Tako API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -30,9 +30,6 @@
30
30
  "devDependencies": {
31
31
  "@types/jest": "^29.5.3",
32
32
  "@types/node": "^20.4.5",
33
- "@typescript-eslint/eslint-plugin": "^6.2.0",
34
- "@typescript-eslint/parser": "^6.2.0",
35
- "eslint": "^8.46.0",
36
33
  "jest": "^29.6.2",
37
34
  "ts-jest": "^29.1.1",
38
35
  "ts-node": "^10.9.2",
@@ -40,7 +37,10 @@
40
37
  "typescript": "^5.1.6"
41
38
  },
42
39
  "peerDependencies": {
43
- "typescript": ">=4.7.0"
40
+ "typescript": ">=4.7.0",
41
+ "eslint": "^7.0.0 || ^8.0.0",
42
+ "@typescript-eslint/eslint-plugin": "^5.0.0 || ^6.0.0",
43
+ "@typescript-eslint/parser": "^5.0.0 || ^6.0.0"
44
44
  },
45
45
  "engines": {
46
46
  "node": ">=14.0.0"