@visus-io/notion-sdk-ts 3.0.2 → 3.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/README.md +20 -87
- package/dist/api/asyncTasks.api.d.ts +72 -0
- package/dist/api/asyncTasks.api.js +70 -0
- package/dist/api/base.api.d.ts +15 -4
- package/dist/api/base.api.js +20 -8
- package/dist/api/blocks.api.d.ts +435 -73
- package/dist/api/blocks.api.js +43 -1
- package/dist/api/comments.api.d.ts +42 -5
- package/dist/api/comments.api.js +45 -1
- package/dist/api/customEmojis.api.d.ts +38 -0
- package/dist/api/customEmojis.api.js +38 -0
- package/dist/api/dataSources.api.d.ts +67 -14
- package/dist/api/dataSources.api.js +26 -1
- package/dist/api/databases.api.d.ts +47 -11
- package/dist/api/databases.api.js +4 -1
- package/dist/api/fileUploads.api.d.ts +8 -5
- package/dist/api/fileUploads.api.js +11 -7
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +7 -1
- package/dist/api/pages.api.d.ts +144 -10
- package/dist/api/pages.api.js +66 -0
- package/dist/api/search.api.d.ts +12 -2
- package/dist/api/search.api.js +5 -2
- package/dist/api/users.api.d.ts +3 -0
- package/dist/api/users.api.js +2 -0
- package/dist/api/views.api.d.ts +286 -0
- package/dist/api/views.api.js +172 -0
- package/dist/client.d.ts +17 -17
- package/dist/client.js +25 -30
- package/dist/errors.d.ts +19 -5
- package/dist/errors.js +16 -4
- package/dist/helpers/block.helpers.d.ts +49 -5
- package/dist/helpers/block.helpers.js +50 -6
- package/dist/helpers/file.helpers.d.ts +46 -5
- package/dist/helpers/file.helpers.js +36 -8
- package/dist/helpers/filter.helpers.d.ts +8 -6
- package/dist/helpers/filter.helpers.js +2 -0
- package/dist/helpers/index.d.ts +3 -2
- package/dist/helpers/index.js +5 -1
- package/dist/helpers/pagination.helpers.d.ts +85 -13
- package/dist/helpers/pagination.helpers.js +114 -8
- package/dist/helpers/parent.helpers.d.ts +5 -3
- package/dist/helpers/parent.helpers.js +5 -3
- package/dist/helpers/property.helpers.d.ts +31 -0
- package/dist/helpers/property.helpers.js +30 -1
- package/dist/helpers/richText.helpers.d.ts +10 -5
- package/dist/helpers/richText.helpers.js +11 -6
- package/dist/helpers/sort.helpers.d.ts +2 -0
- package/dist/helpers/sort.helpers.js +2 -0
- package/dist/helpers/webhook.helpers.d.ts +54 -0
- package/dist/helpers/webhook.helpers.js +83 -0
- package/dist/models/asyncTask.model.d.ts +61 -0
- package/dist/models/asyncTask.model.js +91 -0
- package/dist/models/base.model.d.ts +16 -4
- package/dist/models/base.model.js +17 -5
- package/dist/models/block.model.d.ts +6 -2
- package/dist/models/block.model.js +48 -33
- package/dist/models/comment.model.d.ts +4 -2
- package/dist/models/comment.model.js +4 -2
- package/dist/models/customEmoji.model.d.ts +30 -0
- package/dist/models/customEmoji.model.js +44 -0
- package/dist/models/dataSource.model.d.ts +7 -5
- package/dist/models/dataSource.model.js +5 -3
- package/dist/models/database.model.d.ts +13 -5
- package/dist/models/database.model.js +15 -5
- package/dist/models/fileUpload.model.d.ts +6 -4
- package/dist/models/fileUpload.model.js +6 -4
- package/dist/models/index.d.ts +3 -0
- package/dist/models/index.js +7 -1
- package/dist/models/page.model.d.ts +8 -2
- package/dist/models/page.model.js +14 -4
- package/dist/models/richText.model.d.ts +3 -1
- package/dist/models/richText.model.js +3 -1
- package/dist/models/user.model.d.ts +4 -2
- package/dist/models/user.model.js +4 -2
- package/dist/models/view.model.d.ts +81 -0
- package/dist/models/view.model.js +121 -0
- package/dist/notion.d.ts +32 -8
- package/dist/notion.js +25 -4
- package/dist/schemas/asyncTask.schema.d.ts +53 -0
- package/dist/schemas/asyncTask.schema.js +88 -0
- package/dist/schemas/block.schema.d.ts +390 -73
- package/dist/schemas/block.schema.js +39 -8
- package/dist/schemas/codeLanguages.d.ts +7 -1
- package/dist/schemas/codeLanguages.js +4 -1
- package/dist/schemas/colors.d.ts +7 -1
- package/dist/schemas/colors.js +4 -1
- package/dist/schemas/comment.schema.d.ts +28 -5
- package/dist/schemas/comment.schema.js +11 -2
- package/dist/schemas/customEmoji.schema.d.ts +18 -0
- package/dist/schemas/customEmoji.schema.js +50 -0
- package/dist/schemas/dataSource.schema.d.ts +84 -10
- package/dist/schemas/dataSource.schema.js +31 -6
- package/dist/schemas/database.schema.d.ts +51 -7
- package/dist/schemas/database.schema.js +10 -3
- package/dist/schemas/emoji.schema.d.ts +7 -2
- package/dist/schemas/emoji.schema.js +4 -2
- package/dist/schemas/file.schema.d.ts +18 -2
- package/dist/schemas/file.schema.js +6 -2
- package/dist/schemas/fileUpload.schema.d.ts +9 -3
- package/dist/schemas/fileUpload.schema.js +5 -2
- package/dist/schemas/icon.schema.d.ts +116 -0
- package/dist/schemas/icon.schema.js +100 -0
- package/dist/schemas/index.d.ts +6 -0
- package/dist/schemas/index.js +6 -0
- package/dist/schemas/meetingNotesQuery.schema.d.ts +4198 -0
- package/dist/schemas/meetingNotesQuery.schema.js +67 -0
- package/dist/schemas/page.schema.d.ts +51 -9
- package/dist/schemas/page.schema.js +6 -2
- package/dist/schemas/pageMarkdown.schema.d.ts +70 -0
- package/dist/schemas/pageMarkdown.schema.js +70 -0
- package/dist/schemas/pageProperties.schema.d.ts +105 -21
- package/dist/schemas/pageProperties.schema.js +8 -6
- package/dist/schemas/pagination.schema.d.ts +57 -7
- package/dist/schemas/pagination.schema.js +25 -1
- package/dist/schemas/parent.schema.d.ts +39 -3
- package/dist/schemas/parent.schema.js +17 -5
- package/dist/schemas/propertyObjects.schema.d.ts +93 -15
- package/dist/schemas/propertyObjects.schema.js +11 -5
- package/dist/schemas/richText.schema.d.ts +23 -6
- package/dist/schemas/richText.schema.js +7 -4
- package/dist/schemas/shared.schema.d.ts +6 -3
- package/dist/schemas/shared.schema.js +6 -3
- package/dist/schemas/user.schema.d.ts +17 -0
- package/dist/schemas/user.schema.js +7 -3
- package/dist/schemas/view.schema.d.ts +1111 -0
- package/dist/schemas/view.schema.js +134 -0
- package/dist/validation.d.ts +11 -4
- package/dist/validation.js +11 -4
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -13,10 +13,10 @@ A type-safe TypeScript SDK for the Notion API with Zod validation, OOP models, a
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
15
15
|
- **Type-safe** Zod v4 runtime validation on every API response; full TypeScript declarations
|
|
16
|
-
- **Complete API coverage** Pages, Blocks, Databases, Data Sources, Comments, Search, Users, File Uploads
|
|
17
|
-
- **Ergonomic helpers** `block`, `richText`, `filter`, `sort`, `prop`, `parent`, `icon`, `cover`, `
|
|
18
|
-
- **OOP models** `Page`, `Block`, `Database`, `User`, `Comment`, `DataSource`, `FileUpload`, `RichText` with convenience methods
|
|
19
|
-
- **Automatic pagination** `paginate()` and `
|
|
16
|
+
- **Complete API coverage** Pages, Blocks, Databases, Data Sources, Comments, Search, Users, File Uploads, Async Tasks, Custom Emojis, Views
|
|
17
|
+
- **Ergonomic helpers** `block`, `richText`, `filter`, `sort`, `prop`, `parent`, `icon`, `cover`, and `notionFile` factories, plus a `webhook` signature-verification helper, eliminate verbose JSON and boilerplate
|
|
18
|
+
- **OOP models** `Page`, `Block`, `Database`, `User`, `Comment`, `DataSource`, `FileUpload`, `RichText`, `AsyncTask`, `CustomEmoji`, `View` with convenience methods
|
|
19
|
+
- **Automatic pagination** `paginate()`, `paginateIterator()`, and `paginateWithMetadata()` helpers automatically fetch all pages, plus `iterateAllDataSourceRows()`/`collectAllDataSourceRows()` to work around the 10,000-result query cap
|
|
20
20
|
- **Automatic rate limiting** Respects `Retry-After` header with exponential backoff fallback (configurable)
|
|
21
21
|
- **Client-side size validation** Enforces Notion API size limits before sending requests
|
|
22
22
|
- **Zero bloat** Single runtime dependency (`zod`); uses built-in `fetch` (Node 18+)
|
|
@@ -76,91 +76,16 @@ const results = await notion.databases.query('database-id', {
|
|
|
76
76
|
|
|
77
77
|
## Documentation
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
Full documentation is hosted at [nts.projects.visus.io](https://nts.projects.visus.io). It includes guides and a generated API reference. Source lives in [`docs/`](./docs). It has its own dependencies, separate from the root project. Install them once, then run the site locally:
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
- [**Migration Guide**](https://github.com/visus-io/notion-sdk-ts/wiki/Migration-Guide) - Migrating between API versions
|
|
85
|
-
- [**Common Use Cases**](https://github.com/visus-io/notion-sdk-ts/wiki/Common-Use-Cases) - Practical examples and workflows
|
|
86
|
-
|
|
87
|
-
### Core Concepts
|
|
88
|
-
|
|
89
|
-
- [**Helpers**](https://github.com/visus-io/notion-sdk-ts/wiki/Helpers) - Rich Text, Block Builder, Properties, Filters, Sorting
|
|
90
|
-
- [**Models**](https://github.com/visus-io/notion-sdk-ts/wiki/Models) - Page, Block, Database, DataSource, User, Comment, FileUpload
|
|
91
|
-
- [**API Reference**](https://github.com/visus-io/notion-sdk-ts/wiki/API-Reference) - Complete API endpoint documentation
|
|
92
|
-
|
|
93
|
-
### Configuration & Advanced Topics
|
|
94
|
-
|
|
95
|
-
- [**Configuration & Features**](https://github.com/visus-io/notion-sdk-ts/wiki/Configuration) - Client options, rate limiting, retries
|
|
96
|
-
- [**Error Handling**](https://github.com/visus-io/notion-sdk-ts/wiki/Error-Handling) - Error types, codes, and handling patterns
|
|
97
|
-
- [**Pagination**](https://github.com/visus-io/notion-sdk-ts/wiki/Pagination) - Automatic pagination helpers
|
|
98
|
-
- [**Request Size Limits**](https://github.com/visus-io/notion-sdk-ts/wiki/Request-Size-Limits) - Notion API size limits
|
|
99
|
-
|
|
100
|
-
### Development
|
|
101
|
-
|
|
102
|
-
- [**TypeScript Support**](https://github.com/visus-io/notion-sdk-ts/wiki/TypeScript-Support) - Types, schemas, and type safety
|
|
103
|
-
- [**Development & Contributing**](https://github.com/visus-io/notion-sdk-ts/wiki/Development) - Project structure and architecture
|
|
104
|
-
|
|
105
|
-
## Migration Notice
|
|
106
|
-
|
|
107
|
-
**This SDK now targets Notion API version `2026-03-11`** (upgraded from `2025-09-03` in v3.x; originally `2022-06-28` in v1.x). The API version is fixed — it cannot be overridden via client options.
|
|
108
|
-
|
|
109
|
-
### Key Changes (v3.x — 2026-03-11)
|
|
110
|
-
|
|
111
|
-
- **`archived` → `in_trash`**: Field renamed across all schemas, models, and API request bodies
|
|
112
|
-
- **`after` → `position` object**: `blocks.children.append()` now accepts a typed `position` union
|
|
113
|
-
- **`transcription` → `meeting_notes`**: Block type and helper renamed
|
|
114
|
-
- **`notionVersion` removed**: Use the exported `NOTION_VERSION` constant to inspect the target version
|
|
115
|
-
|
|
116
|
-
### Quick Migration Example (v2.x → v3.x)
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
// OLD (v2.x / 2025-09-03)
|
|
120
|
-
console.log(page.archived);
|
|
121
|
-
await notion.blocks.children.append('page-id', {
|
|
122
|
-
children: [block.paragraph('text')],
|
|
123
|
-
after: 'block-id',
|
|
124
|
-
});
|
|
125
|
-
block.transcription('Transcription text');
|
|
126
|
-
|
|
127
|
-
// NEW (v3.x / 2026-03-11)
|
|
128
|
-
import { NOTION_VERSION } from '@visus-io/notion-sdk-ts';
|
|
129
|
-
console.log(page.inTrash);
|
|
130
|
-
await notion.blocks.children.append('page-id', {
|
|
131
|
-
children: [block.paragraph('text')],
|
|
132
|
-
position: { type: 'after_block', after_block: { id: 'block-id' } },
|
|
133
|
-
});
|
|
134
|
-
block.meetingNotes('Meeting notes text');
|
|
81
|
+
```bash
|
|
82
|
+
bun run docs:install
|
|
83
|
+
bun run docs:dev
|
|
135
84
|
```
|
|
136
85
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
- **Database creation**: Properties moved to `initial_data_source.properties`
|
|
140
|
-
- **Database updates**: Use Data Sources API for property changes
|
|
141
|
-
- **Page creation**: Requires both data source ID and database ID
|
|
142
|
-
- **Search API**: Returns `DataSource` objects instead of `Database`
|
|
143
|
-
|
|
144
|
-
### Quick Migration Example (v1.x → v2.x)
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
// OLD (v1.x / 2022-06-28)
|
|
148
|
-
await notion.pages.create({
|
|
149
|
-
parent: parent.database('database-id'),
|
|
150
|
-
properties: { Name: prop.title('Task') },
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// NEW (v2.x / 2025-09-03)
|
|
154
|
-
const db = await notion.databases.retrieve('database-id');
|
|
155
|
-
const dataSourceId = db.dataSources[0].id;
|
|
156
|
-
|
|
157
|
-
await notion.pages.create({
|
|
158
|
-
parent: parent.dataSource(dataSourceId, db.id),
|
|
159
|
-
properties: { Name: prop.title('Task') },
|
|
160
|
-
});
|
|
161
|
-
```
|
|
86
|
+
## Migration Notice
|
|
162
87
|
|
|
163
|
-
See the [
|
|
88
|
+
**This SDK now targets Notion API version `2026-03-11`** (upgraded from `2025-09-03` in v3.x; originally `2022-06-28` in v1.x). The API version is fixed — it cannot be overridden via client options. See the [Migration Guide](./docs/src/content/docs/migration-guide.md) for complete upgrade details.
|
|
164
89
|
|
|
165
90
|
## Development
|
|
166
91
|
|
|
@@ -187,15 +112,23 @@ bun run test:coverage # Coverage report
|
|
|
187
112
|
bun run lint # ESLint
|
|
188
113
|
bun run lint:fix # Auto-fix
|
|
189
114
|
bun run format # Prettier
|
|
115
|
+
|
|
116
|
+
bun run docs:install # Install the docs site's dependencies (run once, or after they change)
|
|
117
|
+
bun run docs:dev # Run the docs site locally
|
|
118
|
+
bun run docs:build # Build the docs site
|
|
190
119
|
```
|
|
191
120
|
|
|
192
121
|
> **Note:** While this project uses Bun for development, the published package works with both Node.js 18+ and Bun 1.3.10+.
|
|
193
122
|
|
|
194
|
-
See [**
|
|
123
|
+
See [**ARCHITECTURE.md**](./ARCHITECTURE.md) for project structure and architecture.
|
|
124
|
+
|
|
125
|
+
## Contributing
|
|
126
|
+
|
|
127
|
+
Contributions are welcome! See [**CONTRIBUTING.md**](./CONTRIBUTING.md) for how to get started.
|
|
195
128
|
|
|
196
129
|
## Links
|
|
197
130
|
|
|
198
|
-
- [**Documentation
|
|
131
|
+
- [**Documentation**](https://nts.projects.visus.io)
|
|
199
132
|
- [**GitHub Repository**](https://github.com/visus-io/notion-sdk-ts)
|
|
200
133
|
- [**npm Package**](https://www.npmjs.com/package/@visus-io/notion-sdk-ts)
|
|
201
134
|
- [**Notion API Documentation**](https://developers.notion.com/reference/intro)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { NotionClient } from '../client';
|
|
2
|
+
import { AsyncTask } from '../models';
|
|
3
|
+
import { type NotionAsyncTask } from '../schemas';
|
|
4
|
+
import { BaseAPI } from './base.api';
|
|
5
|
+
/**
|
|
6
|
+
* Options for polling an async task until completion.
|
|
7
|
+
*/
|
|
8
|
+
export interface PollAsyncTaskOptions {
|
|
9
|
+
/** Maximum time to wait for the task to complete, in milliseconds (default: 300000 / 5 minutes) */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Async Tasks API client for polling long-running Notion operations.
|
|
14
|
+
*
|
|
15
|
+
* @category Async Tasks
|
|
16
|
+
*/
|
|
17
|
+
export declare class AsyncTasksAPI extends BaseAPI<NotionAsyncTask, AsyncTask> {
|
|
18
|
+
protected readonly client: NotionClient;
|
|
19
|
+
protected config: {
|
|
20
|
+
schema: import("zod").ZodObject<{
|
|
21
|
+
object: import("zod").ZodLiteral<"async_task">;
|
|
22
|
+
id: import("zod").ZodUUID;
|
|
23
|
+
status: import("zod").ZodEnum<{
|
|
24
|
+
queued: "queued";
|
|
25
|
+
running: "running";
|
|
26
|
+
retrying: "retrying";
|
|
27
|
+
succeeded: "succeeded";
|
|
28
|
+
failed: "failed";
|
|
29
|
+
}>;
|
|
30
|
+
status_url: import("zod").ZodURL;
|
|
31
|
+
created_time: import("zod").ZodUnion<readonly [import("zod").ZodISODateTime, import("zod").ZodISODate]>;
|
|
32
|
+
operation: import("zod").ZodObject<{
|
|
33
|
+
surface: import("zod").ZodEnum<{
|
|
34
|
+
rest: "rest";
|
|
35
|
+
mcp: "mcp";
|
|
36
|
+
}>;
|
|
37
|
+
name: import("zod").ZodString;
|
|
38
|
+
}, import("zod/v4/core").$strip>;
|
|
39
|
+
poll_after_seconds: import("zod").ZodOptional<import("zod").ZodNumber>;
|
|
40
|
+
result: import("zod").ZodOptional<import("zod").ZodUnknown>;
|
|
41
|
+
error: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
42
|
+
object: import("zod").ZodLiteral<"error">;
|
|
43
|
+
status: import("zod").ZodNumber;
|
|
44
|
+
code: import("zod").ZodString;
|
|
45
|
+
message: import("zod").ZodString;
|
|
46
|
+
}, import("zod/v4/core").$strip>>;
|
|
47
|
+
}, import("zod/v4/core").$strip>;
|
|
48
|
+
ModelClass: typeof AsyncTask;
|
|
49
|
+
};
|
|
50
|
+
constructor(client: NotionClient);
|
|
51
|
+
/**
|
|
52
|
+
* Retrieve the current status of an async task.
|
|
53
|
+
*
|
|
54
|
+
* @param taskId - The ID of the async task to retrieve
|
|
55
|
+
* @returns The task wrapped in an AsyncTask model
|
|
56
|
+
*
|
|
57
|
+
* @see https://developers.notion.com/reference/retrieve-async-task
|
|
58
|
+
*/
|
|
59
|
+
retrieve(taskId: string): Promise<AsyncTask>;
|
|
60
|
+
/**
|
|
61
|
+
* Poll an async task until it reaches a terminal status: succeeded or failed.
|
|
62
|
+
* Wait at least `poll_after_seconds` between each poll, per the API's guidance.
|
|
63
|
+
* This value comes from the response body, not a header.
|
|
64
|
+
*
|
|
65
|
+
* @param taskId - The ID of the async task to poll
|
|
66
|
+
* @param options - Polling options
|
|
67
|
+
* @returns The task wrapped in an AsyncTask model, once it reaches a terminal status
|
|
68
|
+
*
|
|
69
|
+
* @throws {NotionRequestTimeoutError} If the task doesn't complete within `timeoutMs`
|
|
70
|
+
*/
|
|
71
|
+
poll(taskId: string, options?: PollAsyncTaskOptions): Promise<AsyncTask>;
|
|
72
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncTasksAPI = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const models_1 = require("../models");
|
|
6
|
+
const schemas_1 = require("../schemas");
|
|
7
|
+
const base_api_1 = require("./base.api");
|
|
8
|
+
function sleep(ms) {
|
|
9
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Async Tasks API client for polling long-running Notion operations.
|
|
13
|
+
*
|
|
14
|
+
* @category Async Tasks
|
|
15
|
+
*/
|
|
16
|
+
class AsyncTasksAPI extends base_api_1.BaseAPI {
|
|
17
|
+
constructor(client) {
|
|
18
|
+
super(client);
|
|
19
|
+
this.client = client;
|
|
20
|
+
this.config = {
|
|
21
|
+
schema: schemas_1.asyncTaskSchema,
|
|
22
|
+
ModelClass: models_1.AsyncTask,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Retrieve the current status of an async task.
|
|
27
|
+
*
|
|
28
|
+
* @param taskId - The ID of the async task to retrieve
|
|
29
|
+
* @returns The task wrapped in an AsyncTask model
|
|
30
|
+
*
|
|
31
|
+
* @see https://developers.notion.com/reference/retrieve-async-task
|
|
32
|
+
*/
|
|
33
|
+
async retrieve(taskId) {
|
|
34
|
+
return this.retrieveResource(`/async_tasks/${taskId}`);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Poll an async task until it reaches a terminal status: succeeded or failed.
|
|
38
|
+
* Wait at least `poll_after_seconds` between each poll, per the API's guidance.
|
|
39
|
+
* This value comes from the response body, not a header.
|
|
40
|
+
*
|
|
41
|
+
* @param taskId - The ID of the async task to poll
|
|
42
|
+
* @param options - Polling options
|
|
43
|
+
* @returns The task wrapped in an AsyncTask model, once it reaches a terminal status
|
|
44
|
+
*
|
|
45
|
+
* @throws {NotionRequestTimeoutError} If the task doesn't complete within `timeoutMs`
|
|
46
|
+
*/
|
|
47
|
+
async poll(taskId, options) {
|
|
48
|
+
const deadline = Date.now() + (options?.timeoutMs ?? 300_000);
|
|
49
|
+
for (;;) {
|
|
50
|
+
const task = await this.retrieve(taskId);
|
|
51
|
+
if (task.isTerminal()) {
|
|
52
|
+
return task;
|
|
53
|
+
}
|
|
54
|
+
const remainingMs = deadline - Date.now();
|
|
55
|
+
if (remainingMs <= 0) {
|
|
56
|
+
throw new errors_1.NotionRequestTimeoutError(`Task ${taskId} did not complete within timeout`);
|
|
57
|
+
}
|
|
58
|
+
const desiredSleepMs = Math.max((task.pollAfterSeconds ?? 1) * 1000, 250);
|
|
59
|
+
if (desiredSleepMs >= remainingMs) {
|
|
60
|
+
// Sleeping the full poll_after_seconds would overshoot the deadline -- cap the
|
|
61
|
+
// sleep to what's left and time out immediately after, instead of sleeping the
|
|
62
|
+
// full interval and only detecting the timeout on the next loop iteration.
|
|
63
|
+
await sleep(remainingMs);
|
|
64
|
+
throw new errors_1.NotionRequestTimeoutError(`Task ${taskId} did not complete within timeout`);
|
|
65
|
+
}
|
|
66
|
+
await sleep(desiredSleepMs);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.AsyncTasksAPI = AsyncTasksAPI;
|
package/dist/api/base.api.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { NotionClient } from '../client';
|
|
2
|
+
import { TRUSTED } from '../models/base.model';
|
|
2
3
|
import type { PaginatedList, PaginatedListType, PaginationParameters } from '../schemas';
|
|
3
4
|
import type * as z from 'zod';
|
|
4
5
|
/**
|
|
5
|
-
* Configuration for API resource operations
|
|
6
|
+
* Configuration for API resource operations. It defines the schema, model class,
|
|
7
|
+
* and list type that parse and wrap API responses.
|
|
6
8
|
*
|
|
7
9
|
* @template TResponse - The raw response type from the API
|
|
8
10
|
* @template TModel - The model class type that wraps the response
|
|
@@ -11,7 +13,7 @@ interface ResourceConfig<TResponse, TModel> {
|
|
|
11
13
|
/** Zod schema to validate the API response */
|
|
12
14
|
schema: z.ZodSchema<TResponse>;
|
|
13
15
|
/** Model class constructor to wrap the validated response */
|
|
14
|
-
ModelClass: new (data: TResponse) => TModel;
|
|
16
|
+
ModelClass: new (data: TResponse, trusted?: typeof TRUSTED) => TModel;
|
|
15
17
|
/** Type of items in the paginated list (used for typing the results) */
|
|
16
18
|
listType?: PaginatedListType;
|
|
17
19
|
}
|
|
@@ -24,7 +26,15 @@ interface ResourceConfig<TResponse, TModel> {
|
|
|
24
26
|
export declare abstract class BaseAPI<TResponse, TModel> {
|
|
25
27
|
protected readonly client: NotionClient;
|
|
26
28
|
protected abstract config: ResourceConfig<TResponse, TModel>;
|
|
29
|
+
private cachedPaginatedListSchema?;
|
|
27
30
|
protected constructor(client: NotionClient);
|
|
31
|
+
/**
|
|
32
|
+
* Return whether a query object has at least one entry.
|
|
33
|
+
*
|
|
34
|
+
* @param query - Query object to check
|
|
35
|
+
* @returns `true` if `query` has at least one own enumerable key
|
|
36
|
+
*/
|
|
37
|
+
private hasQueryParams;
|
|
28
38
|
/**
|
|
29
39
|
* Delete a resource via DELETE request.
|
|
30
40
|
*
|
|
@@ -49,8 +59,9 @@ export declare abstract class BaseAPI<TResponse, TModel> {
|
|
|
49
59
|
/**
|
|
50
60
|
* Build filter_properties query parameter from an array of property names.
|
|
51
61
|
*
|
|
52
|
-
* Notion sends this as a repeated query key
|
|
53
|
-
* not a single comma-joined value
|
|
62
|
+
* Notion sends this parameter as a repeated query key
|
|
63
|
+
* (`filter_properties=a&filter_properties=b`), not as a single comma-joined value.
|
|
64
|
+
* This method keeps the array intact so `NotionClient` can expand it.
|
|
54
65
|
*
|
|
55
66
|
* @param filterProperties - Optional array of property names to include in the response
|
|
56
67
|
* @returns Query object with filter_properties parameter for API requests
|
package/dist/api/base.api.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseAPI = void 0;
|
|
4
|
+
const base_model_1 = require("../models/base.model");
|
|
4
5
|
const schemas_1 = require("../schemas");
|
|
5
6
|
const validation_1 = require("../validation");
|
|
6
7
|
/**
|
|
@@ -13,6 +14,15 @@ class BaseAPI {
|
|
|
13
14
|
constructor(client) {
|
|
14
15
|
this.client = client;
|
|
15
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Return whether a query object has at least one entry.
|
|
19
|
+
*
|
|
20
|
+
* @param query - Query object to check
|
|
21
|
+
* @returns `true` if `query` has at least one own enumerable key
|
|
22
|
+
*/
|
|
23
|
+
hasQueryParams(query) {
|
|
24
|
+
return !!query && Object.keys(query).length > 0;
|
|
25
|
+
}
|
|
16
26
|
/**
|
|
17
27
|
* Delete a resource via DELETE request.
|
|
18
28
|
*
|
|
@@ -52,8 +62,9 @@ class BaseAPI {
|
|
|
52
62
|
/**
|
|
53
63
|
* Build filter_properties query parameter from an array of property names.
|
|
54
64
|
*
|
|
55
|
-
* Notion sends this as a repeated query key
|
|
56
|
-
* not a single comma-joined value
|
|
65
|
+
* Notion sends this parameter as a repeated query key
|
|
66
|
+
* (`filter_properties=a&filter_properties=b`), not as a single comma-joined value.
|
|
67
|
+
* This method keeps the array intact so `NotionClient` can expand it.
|
|
57
68
|
*
|
|
58
69
|
* @param filterProperties - Optional array of property names to include in the response
|
|
59
70
|
* @returns Query object with filter_properties parameter for API requests
|
|
@@ -127,7 +138,7 @@ class BaseAPI {
|
|
|
127
138
|
const response = await this.client.request({
|
|
128
139
|
method: 'GET',
|
|
129
140
|
path: resourcePath,
|
|
130
|
-
query:
|
|
141
|
+
query: this.hasQueryParams(query) ? query : undefined,
|
|
131
142
|
});
|
|
132
143
|
return this.parseAndWrap(response);
|
|
133
144
|
}
|
|
@@ -148,7 +159,7 @@ class BaseAPI {
|
|
|
148
159
|
const response = await this.client.request({
|
|
149
160
|
method: 'GET',
|
|
150
161
|
path: resourcePath,
|
|
151
|
-
query:
|
|
162
|
+
query: this.hasQueryParams(query) ? query : undefined,
|
|
152
163
|
});
|
|
153
164
|
return this.parsePaginatedList(response);
|
|
154
165
|
}
|
|
@@ -185,7 +196,7 @@ class BaseAPI {
|
|
|
185
196
|
*/
|
|
186
197
|
parseAndWrap(response) {
|
|
187
198
|
const parsed = this.config.schema.parse(response);
|
|
188
|
-
return new this.config.ModelClass(parsed);
|
|
199
|
+
return new this.config.ModelClass(parsed, base_model_1.TRUSTED);
|
|
189
200
|
}
|
|
190
201
|
/**
|
|
191
202
|
* Parse a paginated list response and wrap each item in the specified model class.
|
|
@@ -198,14 +209,15 @@ class BaseAPI {
|
|
|
198
209
|
* return this.parsePaginatedList(response);
|
|
199
210
|
*/
|
|
200
211
|
parsePaginatedList(response) {
|
|
201
|
-
|
|
202
|
-
const parsed =
|
|
212
|
+
this.cachedPaginatedListSchema ??= (0, schemas_1.paginatedListSchema)(this.config.schema);
|
|
213
|
+
const parsed = this.cachedPaginatedListSchema.parse(response);
|
|
203
214
|
return {
|
|
204
215
|
object: 'list',
|
|
205
|
-
results: parsed.results.map((item) => new this.config.ModelClass(item)),
|
|
216
|
+
results: parsed.results.map((item) => new this.config.ModelClass(item, base_model_1.TRUSTED)),
|
|
206
217
|
next_cursor: parsed.next_cursor,
|
|
207
218
|
has_more: parsed.has_more,
|
|
208
219
|
type: this.config.listType || 'unknown',
|
|
220
|
+
request_status: parsed.request_status,
|
|
209
221
|
};
|
|
210
222
|
}
|
|
211
223
|
}
|