@diffbot/typescript 0.1.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/LICENSE +21 -0
- package/README.md +131 -0
- package/dist/index.cjs +691 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +163 -0
- package/dist/index.d.ts +163 -0
- package/dist/index.js +642 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Diffbot
|
|
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,131 @@
|
|
|
1
|
+
# Diffbot TypeScript Library
|
|
2
|
+
|
|
3
|
+
TypeScript client library for [Diffbot](https://www.diffbot.com) APIs. This is a port of the [diffbot-python](https://github.com/diffbot/diffbot-python) public library API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @diffbot/typescript
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node.js 18+ (native `fetch`).
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
In TypeScript, all API functions are async. Use `await` or `for await` where the Python SDK used sync iteration.
|
|
16
|
+
|
|
17
|
+
Create a client once, then pass it to the API functions you need. Import only the functions you use so bundlers can tree-shake unused code.
|
|
18
|
+
|
|
19
|
+
### Authentication
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { DiffbotClient, extract, resolveToken } from "@diffbot/typescript";
|
|
23
|
+
|
|
24
|
+
const db = new DiffbotClient({ token: resolveToken() });
|
|
25
|
+
const data = await extract(db, "https://www.example.com");
|
|
26
|
+
await db.close();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Token resolution order:
|
|
30
|
+
|
|
31
|
+
1. Explicit argument to `resolveToken(token)`
|
|
32
|
+
2. `DIFFBOT_API_TOKEN` environment variable
|
|
33
|
+
3. `DIFFBOT_API_TOKEN=...` line in `~/.diffbot/credentials`
|
|
34
|
+
|
|
35
|
+
### Extract structured content
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { DiffbotClient, extract } from "@diffbot/typescript";
|
|
39
|
+
|
|
40
|
+
const db = new DiffbotClient({ token: "YOUR_TOKEN" });
|
|
41
|
+
const data = await extract(db, "https://www.example.com");
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Ask Diffbot LLM
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { ask } from "@diffbot/typescript";
|
|
48
|
+
|
|
49
|
+
for await (const chunk of ask(db, [{ role: "user", content: "What's the capital of France?" }])) {
|
|
50
|
+
process.stdout.write(chunk);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Crawl a site
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { crawl } from "@diffbot/typescript";
|
|
58
|
+
|
|
59
|
+
for await (const event of crawl(db, "https://www.example.com", { hops: 1 })) {
|
|
60
|
+
console.log(event);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Query the Knowledge Graph
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { dql } from "@diffbot/typescript";
|
|
68
|
+
|
|
69
|
+
const results = await dql(db, 'type:Organization name:"Diffbot"');
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Web Search
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { webSearch } from "@diffbot/typescript";
|
|
76
|
+
|
|
77
|
+
const results = await webSearch(db, "diffbot knowledge graph");
|
|
78
|
+
for (const r of results.search_results as Array<Record<string, unknown>>) {
|
|
79
|
+
console.log(r.score, r.title, r.pageUrl);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Entities (NLP)
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { entities } from "@diffbot/typescript";
|
|
87
|
+
|
|
88
|
+
const result = await entities(db, "Apple CEO Tim Cook announced record quarterly earnings.");
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Automatic cleanup with `await using`
|
|
92
|
+
|
|
93
|
+
`DiffbotClient` supports explicit resource management for automatic cleanup:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { DiffbotClient, extract } from "@diffbot/typescript";
|
|
97
|
+
|
|
98
|
+
await using db = new DiffbotClient({ token: "YOUR_TOKEN" });
|
|
99
|
+
const data = await extract(db, "https://www.example.com");
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Python ↔ TypeScript API mapping
|
|
103
|
+
|
|
104
|
+
| Python | TypeScript |
|
|
105
|
+
|--------|------------|
|
|
106
|
+
| `resolve_token()` | `resolveToken()` |
|
|
107
|
+
| `crawl_list_jobs()` | `crawlListJobs()` |
|
|
108
|
+
| `crawl_get_job()` | `crawlGetJob()` |
|
|
109
|
+
| `crawl_delete_job()` | `crawlDeleteJob()` |
|
|
110
|
+
| `dql_parallel()` | `dqlParallel()` |
|
|
111
|
+
| `dql_fetch_ontology()` | `dqlFetchOntology()` |
|
|
112
|
+
| `dql_refresh_ontology()` | `dqlRefreshOntology()` |
|
|
113
|
+
| `web_search()` | `webSearch()` |
|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
pnpm install
|
|
119
|
+
pnpm test
|
|
120
|
+
pnpm build
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Live integration tests (requires `DIFFBOT_API_TOKEN`):
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pnpm test:live
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|