@visus-io/notion-sdk-ts 2.0.0 → 2.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/README.md +66 -921
- package/dist/api/base.api.d.ts +161 -0
- package/dist/api/base.api.js +228 -0
- package/dist/api/blocks.api.d.ts +3867 -4
- package/dist/api/blocks.api.js +16 -47
- package/dist/api/comments.api.d.ts +259 -4
- package/dist/api/comments.api.js +11 -29
- package/dist/api/dataSources.api.d.ts +788 -4
- package/dist/api/dataSources.api.js +14 -28
- package/dist/api/databases.api.d.ts +491 -4
- package/dist/api/databases.api.js +20 -45
- package/dist/api/fileUploads.api.d.ts +25 -3
- package/dist/api/fileUploads.api.js +7 -2
- package/dist/api/index.d.ts +0 -1
- package/dist/api/index.js +0 -1
- package/dist/api/pages.api.d.ts +864 -3
- package/dist/api/pages.api.js +14 -28
- package/dist/api/search.api.d.ts +0 -1
- package/dist/api/search.api.js +5 -11
- package/dist/api/users.api.d.ts +40 -4
- package/dist/api/users.api.js +12 -35
- package/dist/client.d.ts +0 -1
- package/dist/client.js +0 -1
- package/dist/errors.d.ts +0 -1
- package/dist/errors.js +0 -1
- package/dist/helpers/block.helpers.d.ts +4 -1
- package/dist/helpers/block.helpers.js +11 -1
- package/dist/helpers/file.helpers.d.ts +0 -1
- package/dist/helpers/file.helpers.js +0 -1
- package/dist/helpers/filter.helpers.d.ts +0 -1
- package/dist/helpers/filter.helpers.js +0 -1
- package/dist/helpers/index.d.ts +0 -1
- package/dist/helpers/index.js +0 -1
- package/dist/helpers/pagination.helpers.d.ts +0 -1
- package/dist/helpers/pagination.helpers.js +0 -1
- package/dist/helpers/parent.helpers.d.ts +0 -1
- package/dist/helpers/parent.helpers.js +0 -1
- package/dist/helpers/property.helpers.d.ts +0 -1
- package/dist/helpers/property.helpers.js +0 -1
- package/dist/helpers/richText.helpers.d.ts +0 -1
- package/dist/helpers/richText.helpers.js +0 -1
- package/dist/helpers/sort.helpers.d.ts +0 -1
- package/dist/helpers/sort.helpers.js +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/models/base.model.d.ts +2 -3
- package/dist/models/base.model.js +0 -1
- package/dist/models/block.model.d.ts +0 -1
- package/dist/models/block.model.js +0 -1
- package/dist/models/comment.model.d.ts +0 -1
- package/dist/models/comment.model.js +0 -1
- package/dist/models/dataSource.model.d.ts +0 -1
- package/dist/models/dataSource.model.js +0 -1
- package/dist/models/database.model.d.ts +0 -1
- package/dist/models/database.model.js +0 -1
- package/dist/models/fileUpload.model.d.ts +0 -1
- package/dist/models/fileUpload.model.js +0 -1
- package/dist/models/index.d.ts +0 -1
- package/dist/models/index.js +0 -1
- package/dist/models/page.model.d.ts +0 -1
- package/dist/models/page.model.js +0 -1
- package/dist/models/richText.model.d.ts +0 -1
- package/dist/models/richText.model.js +0 -1
- package/dist/models/user.model.d.ts +0 -1
- package/dist/models/user.model.js +0 -1
- package/dist/notion.d.ts +0 -1
- package/dist/notion.js +0 -1
- package/dist/schemas/block.schema.d.ts +261 -82
- package/dist/schemas/block.schema.js +157 -82
- package/dist/schemas/codeLanguages.d.ts +0 -1
- package/dist/schemas/codeLanguages.js +0 -1
- package/dist/schemas/colors.d.ts +0 -1
- package/dist/schemas/colors.js +0 -1
- package/dist/schemas/comment.schema.d.ts +9 -10
- package/dist/schemas/comment.schema.js +54 -21
- package/dist/schemas/dataSource.schema.d.ts +13 -14
- package/dist/schemas/dataSource.schema.js +46 -13
- package/dist/schemas/database.schema.d.ts +13 -14
- package/dist/schemas/database.schema.js +51 -18
- package/dist/schemas/emoji.schema.d.ts +1 -2
- package/dist/schemas/emoji.schema.js +37 -5
- package/dist/schemas/file.schema.d.ts +3 -4
- package/dist/schemas/file.schema.js +49 -16
- package/dist/schemas/fileUpload.schema.d.ts +3 -4
- package/dist/schemas/fileUpload.schema.js +47 -14
- package/dist/schemas/index.d.ts +1 -1
- package/dist/schemas/index.js +1 -1
- package/dist/schemas/page.schema.d.ts +30 -31
- package/dist/schemas/page.schema.js +47 -14
- package/dist/schemas/pageProperties.schema.d.ts +47 -48
- package/dist/schemas/pageProperties.schema.js +162 -129
- package/dist/schemas/pagination.schema.d.ts +1 -2
- package/dist/schemas/pagination.schema.js +40 -8
- package/dist/schemas/parent.schema.d.ts +1 -2
- package/dist/schemas/parent.schema.js +51 -19
- package/dist/schemas/propertyObjects.schema.d.ts +1 -2
- package/dist/schemas/propertyObjects.schema.js +194 -162
- package/dist/schemas/richText.schema.d.ts +7 -8
- package/dist/schemas/richText.schema.js +95 -57
- package/dist/schemas/shared.schema.d.ts +15 -0
- package/dist/schemas/shared.schema.js +54 -0
- package/dist/schemas/user.schema.d.ts +3 -4
- package/dist/schemas/user.schema.js +63 -31
- package/dist/validation.d.ts +0 -1
- package/dist/validation.js +0 -1
- package/package.json +3 -1
- package/dist/api/blocks.api.d.ts.map +0 -1
- package/dist/api/blocks.api.js.map +0 -1
- package/dist/api/comments.api.d.ts.map +0 -1
- package/dist/api/comments.api.js.map +0 -1
- package/dist/api/dataSources.api.d.ts.map +0 -1
- package/dist/api/dataSources.api.js.map +0 -1
- package/dist/api/databases.api.d.ts.map +0 -1
- package/dist/api/databases.api.js.map +0 -1
- package/dist/api/fileUploads.api.d.ts.map +0 -1
- package/dist/api/fileUploads.api.js.map +0 -1
- package/dist/api/index.d.ts.map +0 -1
- package/dist/api/index.js.map +0 -1
- package/dist/api/pages.api.d.ts.map +0 -1
- package/dist/api/pages.api.js.map +0 -1
- package/dist/api/search.api.d.ts.map +0 -1
- package/dist/api/search.api.js.map +0 -1
- package/dist/api/users.api.d.ts.map +0 -1
- package/dist/api/users.api.js.map +0 -1
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/helpers/block.helpers.d.ts.map +0 -1
- package/dist/helpers/block.helpers.js.map +0 -1
- package/dist/helpers/file.helpers.d.ts.map +0 -1
- package/dist/helpers/file.helpers.js.map +0 -1
- package/dist/helpers/filter.helpers.d.ts.map +0 -1
- package/dist/helpers/filter.helpers.js.map +0 -1
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js.map +0 -1
- package/dist/helpers/pagination.helpers.d.ts.map +0 -1
- package/dist/helpers/pagination.helpers.js.map +0 -1
- package/dist/helpers/parent.helpers.d.ts.map +0 -1
- package/dist/helpers/parent.helpers.js.map +0 -1
- package/dist/helpers/property.helpers.d.ts.map +0 -1
- package/dist/helpers/property.helpers.js.map +0 -1
- package/dist/helpers/richText.helpers.d.ts.map +0 -1
- package/dist/helpers/richText.helpers.js.map +0 -1
- package/dist/helpers/sort.helpers.d.ts.map +0 -1
- package/dist/helpers/sort.helpers.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/models/base.model.d.ts.map +0 -1
- package/dist/models/base.model.js.map +0 -1
- package/dist/models/block.model.d.ts.map +0 -1
- package/dist/models/block.model.js.map +0 -1
- package/dist/models/comment.model.d.ts.map +0 -1
- package/dist/models/comment.model.js.map +0 -1
- package/dist/models/dataSource.model.d.ts.map +0 -1
- package/dist/models/dataSource.model.js.map +0 -1
- package/dist/models/database.model.d.ts.map +0 -1
- package/dist/models/database.model.js.map +0 -1
- package/dist/models/fileUpload.model.d.ts.map +0 -1
- package/dist/models/fileUpload.model.js.map +0 -1
- package/dist/models/index.d.ts.map +0 -1
- package/dist/models/index.js.map +0 -1
- package/dist/models/page.model.d.ts.map +0 -1
- package/dist/models/page.model.js.map +0 -1
- package/dist/models/richText.model.d.ts.map +0 -1
- package/dist/models/richText.model.js.map +0 -1
- package/dist/models/user.model.d.ts.map +0 -1
- package/dist/models/user.model.js.map +0 -1
- package/dist/notion.d.ts.map +0 -1
- package/dist/notion.js.map +0 -1
- package/dist/schemas/block.schema.d.ts.map +0 -1
- package/dist/schemas/block.schema.js.map +0 -1
- package/dist/schemas/codeLanguages.d.ts.map +0 -1
- package/dist/schemas/codeLanguages.js.map +0 -1
- package/dist/schemas/colors.d.ts.map +0 -1
- package/dist/schemas/colors.js.map +0 -1
- package/dist/schemas/comment.schema.d.ts.map +0 -1
- package/dist/schemas/comment.schema.js.map +0 -1
- package/dist/schemas/dataSource.schema.d.ts.map +0 -1
- package/dist/schemas/dataSource.schema.js.map +0 -1
- package/dist/schemas/database.schema.d.ts.map +0 -1
- package/dist/schemas/database.schema.js.map +0 -1
- package/dist/schemas/emoji.schema.d.ts.map +0 -1
- package/dist/schemas/emoji.schema.js.map +0 -1
- package/dist/schemas/file.schema.d.ts.map +0 -1
- package/dist/schemas/file.schema.js.map +0 -1
- package/dist/schemas/fileUpload.schema.d.ts.map +0 -1
- package/dist/schemas/fileUpload.schema.js.map +0 -1
- package/dist/schemas/index.d.ts.map +0 -1
- package/dist/schemas/index.js.map +0 -1
- package/dist/schemas/page.schema.d.ts.map +0 -1
- package/dist/schemas/page.schema.js.map +0 -1
- package/dist/schemas/pageProperties.schema.d.ts.map +0 -1
- package/dist/schemas/pageProperties.schema.js.map +0 -1
- package/dist/schemas/pagination.schema.d.ts.map +0 -1
- package/dist/schemas/pagination.schema.js.map +0 -1
- package/dist/schemas/parent.schema.d.ts.map +0 -1
- package/dist/schemas/parent.schema.js.map +0 -1
- package/dist/schemas/propertyObjects.schema.d.ts.map +0 -1
- package/dist/schemas/propertyObjects.schema.js.map +0 -1
- package/dist/schemas/richText.schema.d.ts.map +0 -1
- package/dist/schemas/richText.schema.js.map +0 -1
- package/dist/schemas/user.schema.d.ts.map +0 -1
- package/dist/schemas/user.schema.js.map +0 -1
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js.map +0 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { NotionClient } from '../client';
|
|
2
|
+
import type { PaginatedList, PaginatedListType, PaginationParameters } from '../schemas';
|
|
3
|
+
import type * as z from 'zod';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for API resource operations, including paths, schemas, and model classes.
|
|
6
|
+
*
|
|
7
|
+
* @template TResponse - The raw response type from the API
|
|
8
|
+
* @template TModel - The model class type that wraps the response
|
|
9
|
+
*/
|
|
10
|
+
interface ResourceConfig<TResponse, TModel> {
|
|
11
|
+
/** Zod schema to validate the API response */
|
|
12
|
+
schema: z.ZodSchema<TResponse>;
|
|
13
|
+
/** Model class constructor to wrap the validated response */
|
|
14
|
+
ModelClass: new (data: TResponse) => TModel;
|
|
15
|
+
/** Type of items in the paginated list (used for typing the results) */
|
|
16
|
+
listType?: PaginatedListType;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Base API class providing common functionality for all Notion API clients.
|
|
20
|
+
*
|
|
21
|
+
* @template TResponse - The raw response type from the API
|
|
22
|
+
* @template TModel - The model class type that wraps the response
|
|
23
|
+
*/
|
|
24
|
+
export declare abstract class BaseAPI<TResponse, TModel> {
|
|
25
|
+
protected readonly client: NotionClient;
|
|
26
|
+
protected abstract config: ResourceConfig<TResponse, TModel>;
|
|
27
|
+
protected constructor(client: NotionClient);
|
|
28
|
+
/**
|
|
29
|
+
* Delete a resource via DELETE request.
|
|
30
|
+
*
|
|
31
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
32
|
+
* @returns Instance of the model class representing the deleted resource
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* return this.deleteResource(`/pages/${pageId}`);
|
|
36
|
+
*/
|
|
37
|
+
protected deleteResource(resourcePath: string): Promise<TModel>;
|
|
38
|
+
/**
|
|
39
|
+
* Build filter_properties body parameter from an array of property names.
|
|
40
|
+
*
|
|
41
|
+
* @param filterProperties - Optional array of property names to include in the response
|
|
42
|
+
* @returns Body object with filter_properties parameter for API requests
|
|
43
|
+
*
|
|
44
|
+
* @throws {NotionValidationError} If array exceeds LIMITS.ARRAY_ELEMENTS
|
|
45
|
+
* @example
|
|
46
|
+
* const body = this.buildFilterPropertiesBody(['Name', 'Status']);
|
|
47
|
+
* // body will be: { filter_properties: ['Name', 'Status'] }
|
|
48
|
+
*/
|
|
49
|
+
protected buildFilterPropertiesBody(filterProperties?: string[]): Record<string, unknown>;
|
|
50
|
+
/**
|
|
51
|
+
* Build pagination body parameters from PaginationParameters.
|
|
52
|
+
*
|
|
53
|
+
* @param params - Optional pagination parameters
|
|
54
|
+
* @returns Body object with pagination parameters for API requests
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const body = this.buildPaginationBody({ page_size: 50, start_cursor: 'abc123' });
|
|
58
|
+
* // body will be: { page_size: 50, start_cursor: 'abc123' }
|
|
59
|
+
*/
|
|
60
|
+
protected buildPaginationBody(params?: PaginationParameters): Record<string, unknown>;
|
|
61
|
+
/**
|
|
62
|
+
* Build filter_properties query parameter from an array of property names.
|
|
63
|
+
*
|
|
64
|
+
* @param filterProperties - Optional array of property names to include in the response
|
|
65
|
+
* @returns Query object with filter_properties parameter for API requests
|
|
66
|
+
*
|
|
67
|
+
* @throws {NotionValidationError} If array exceeds LIMITS.ARRAY_ELEMENTS
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* const query = this.buildFilterPropertiesQuery(['Name', 'Status']);
|
|
71
|
+
* // query will be: { filter_properties: 'Name,Status' }
|
|
72
|
+
*/
|
|
73
|
+
protected buildFilterPropertiesQuery(filterProperties?: string[]): Record<string, string>;
|
|
74
|
+
/**
|
|
75
|
+
* Build pagination query parameters from PaginationParameters.
|
|
76
|
+
*
|
|
77
|
+
* @param params - Optional pagination parameters
|
|
78
|
+
* @returns Query object with pagination parameters for API requests
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* const query = this.buildPaginationQuery({ page_size: 50, start_cursor: 'abc123' });
|
|
82
|
+
* // query will be: { page_size: '50', start_cursor: 'abc123' }
|
|
83
|
+
*/
|
|
84
|
+
protected buildPaginationQuery(params?: PaginationParameters): Record<string, string>;
|
|
85
|
+
/**
|
|
86
|
+
* Create a new resource via POST request.
|
|
87
|
+
*
|
|
88
|
+
* @param resourcePath - Path to the resource collection (e.g., '/pages', '/databases')
|
|
89
|
+
* @param body - Request body to send
|
|
90
|
+
* @returns Instance of the model class representing the created resource
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* return this.createResource(
|
|
94
|
+
* '/pages',
|
|
95
|
+
* { parent: { ... }, properties: { ... } },
|
|
96
|
+
* );
|
|
97
|
+
*/
|
|
98
|
+
protected createResource(resourcePath: string, body: unknown): Promise<TModel>;
|
|
99
|
+
/**
|
|
100
|
+
* Retrieve a resource via GET request.
|
|
101
|
+
*
|
|
102
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
103
|
+
* @param query - Optional query parameters for the GET request
|
|
104
|
+
* @returns Instance of the model class representing the retrieved resource
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* return this.retrieveResource(`/pages/${pageId}`, query);
|
|
108
|
+
*/
|
|
109
|
+
protected retrieveResource(resourcePath: string, query?: Record<string, string>): Promise<TModel>;
|
|
110
|
+
/**
|
|
111
|
+
* Retrieve a paginated list of resources via GET request.
|
|
112
|
+
*
|
|
113
|
+
* @param resourcePath - Path to the resource collection (e.g., '/pages', '/databases')
|
|
114
|
+
* @param query - Optional query parameters for pagination and filtering
|
|
115
|
+
* @returns PaginatedList containing instances of the model class for each item in the results array
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* return this.listResources(
|
|
119
|
+
* '/pages',
|
|
120
|
+
* { page_size: '50' },
|
|
121
|
+
* );
|
|
122
|
+
*/
|
|
123
|
+
protected listResources(resourcePath: string, query?: Record<string, string>): Promise<PaginatedList<TModel>>;
|
|
124
|
+
/**
|
|
125
|
+
* Update an existing resource via PATCH request.
|
|
126
|
+
*
|
|
127
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
128
|
+
* @param body - Request body with updated fields
|
|
129
|
+
* @returns Instance of the model class representing the updated resource
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* return this.updateResource(
|
|
133
|
+
* `/pages/${pageId}`,
|
|
134
|
+
* { properties: { ... } },
|
|
135
|
+
* );
|
|
136
|
+
*/
|
|
137
|
+
protected updateResource(resourcePath: string, body: unknown): Promise<TModel>;
|
|
138
|
+
/**
|
|
139
|
+
* Parse response data using the provided schema and wrap it in the specified model class.
|
|
140
|
+
*
|
|
141
|
+
* @param response - The raw response data to parse
|
|
142
|
+
* @returns Instance of the model class representing the parsed resource
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* const response = await this.client.request<NotionPage>({ ... });
|
|
146
|
+
* return this.parseAndWrap(response);
|
|
147
|
+
*/
|
|
148
|
+
private parseAndWrap;
|
|
149
|
+
/**
|
|
150
|
+
* Parse a paginated list response and wrap each item in the specified model class.
|
|
151
|
+
*
|
|
152
|
+
* @param response - The raw paginated list response to parse
|
|
153
|
+
* @returns PaginatedList containing instances of the model class for each item in the results array
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* const response = await this.client.request<PaginatedList<NotionPage>>({ ... });
|
|
157
|
+
* return this.parsePaginatedList(response);
|
|
158
|
+
*/
|
|
159
|
+
private parsePaginatedList;
|
|
160
|
+
}
|
|
161
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseAPI = void 0;
|
|
4
|
+
const schemas_1 = require("../schemas");
|
|
5
|
+
const validation_1 = require("../validation");
|
|
6
|
+
/**
|
|
7
|
+
* Base API class providing common functionality for all Notion API clients.
|
|
8
|
+
*
|
|
9
|
+
* @template TResponse - The raw response type from the API
|
|
10
|
+
* @template TModel - The model class type that wraps the response
|
|
11
|
+
*/
|
|
12
|
+
class BaseAPI {
|
|
13
|
+
constructor(client) {
|
|
14
|
+
this.client = client;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Delete a resource via DELETE request.
|
|
18
|
+
*
|
|
19
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
20
|
+
* @returns Instance of the model class representing the deleted resource
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* return this.deleteResource(`/pages/${pageId}`);
|
|
24
|
+
*/
|
|
25
|
+
async deleteResource(resourcePath) {
|
|
26
|
+
const response = await this.client.request({
|
|
27
|
+
method: 'DELETE',
|
|
28
|
+
path: resourcePath,
|
|
29
|
+
});
|
|
30
|
+
return this.parseAndWrap(response);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build filter_properties body parameter from an array of property names.
|
|
34
|
+
*
|
|
35
|
+
* @param filterProperties - Optional array of property names to include in the response
|
|
36
|
+
* @returns Body object with filter_properties parameter for API requests
|
|
37
|
+
*
|
|
38
|
+
* @throws {NotionValidationError} If array exceeds LIMITS.ARRAY_ELEMENTS
|
|
39
|
+
* @example
|
|
40
|
+
* const body = this.buildFilterPropertiesBody(['Name', 'Status']);
|
|
41
|
+
* // body will be: { filter_properties: ['Name', 'Status'] }
|
|
42
|
+
*/
|
|
43
|
+
buildFilterPropertiesBody(filterProperties) {
|
|
44
|
+
const body = {};
|
|
45
|
+
if (filterProperties) {
|
|
46
|
+
(0, validation_1.validateArrayLength)(filterProperties, validation_1.LIMITS.ARRAY_ELEMENTS, 'filter_properties');
|
|
47
|
+
body.filter_properties = filterProperties;
|
|
48
|
+
}
|
|
49
|
+
return body;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Build pagination body parameters from PaginationParameters.
|
|
53
|
+
*
|
|
54
|
+
* @param params - Optional pagination parameters
|
|
55
|
+
* @returns Body object with pagination parameters for API requests
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* const body = this.buildPaginationBody({ page_size: 50, start_cursor: 'abc123' });
|
|
59
|
+
* // body will be: { page_size: 50, start_cursor: 'abc123' }
|
|
60
|
+
*/
|
|
61
|
+
buildPaginationBody(params) {
|
|
62
|
+
const body = {};
|
|
63
|
+
if (params?.page_size) {
|
|
64
|
+
body.page_size = params.page_size;
|
|
65
|
+
}
|
|
66
|
+
if (params?.start_cursor) {
|
|
67
|
+
body.start_cursor = params.start_cursor;
|
|
68
|
+
}
|
|
69
|
+
return body;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build filter_properties query parameter from an array of property names.
|
|
73
|
+
*
|
|
74
|
+
* @param filterProperties - Optional array of property names to include in the response
|
|
75
|
+
* @returns Query object with filter_properties parameter for API requests
|
|
76
|
+
*
|
|
77
|
+
* @throws {NotionValidationError} If array exceeds LIMITS.ARRAY_ELEMENTS
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* const query = this.buildFilterPropertiesQuery(['Name', 'Status']);
|
|
81
|
+
* // query will be: { filter_properties: 'Name,Status' }
|
|
82
|
+
*/
|
|
83
|
+
buildFilterPropertiesQuery(filterProperties) {
|
|
84
|
+
const query = {};
|
|
85
|
+
if (filterProperties) {
|
|
86
|
+
(0, validation_1.validateArrayLength)(filterProperties, validation_1.LIMITS.ARRAY_ELEMENTS, 'filter_properties');
|
|
87
|
+
query.filter_properties = filterProperties.join(',');
|
|
88
|
+
}
|
|
89
|
+
return query;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Build pagination query parameters from PaginationParameters.
|
|
93
|
+
*
|
|
94
|
+
* @param params - Optional pagination parameters
|
|
95
|
+
* @returns Query object with pagination parameters for API requests
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const query = this.buildPaginationQuery({ page_size: 50, start_cursor: 'abc123' });
|
|
99
|
+
* // query will be: { page_size: '50', start_cursor: 'abc123' }
|
|
100
|
+
*/
|
|
101
|
+
buildPaginationQuery(params) {
|
|
102
|
+
const query = {};
|
|
103
|
+
if (params?.page_size) {
|
|
104
|
+
query.page_size = String(params.page_size);
|
|
105
|
+
}
|
|
106
|
+
if (params?.start_cursor) {
|
|
107
|
+
query.start_cursor = params.start_cursor;
|
|
108
|
+
}
|
|
109
|
+
return query;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Create a new resource via POST request.
|
|
113
|
+
*
|
|
114
|
+
* @param resourcePath - Path to the resource collection (e.g., '/pages', '/databases')
|
|
115
|
+
* @param body - Request body to send
|
|
116
|
+
* @returns Instance of the model class representing the created resource
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* return this.createResource(
|
|
120
|
+
* '/pages',
|
|
121
|
+
* { parent: { ... }, properties: { ... } },
|
|
122
|
+
* );
|
|
123
|
+
*/
|
|
124
|
+
async createResource(resourcePath, body) {
|
|
125
|
+
const response = await this.client.request({
|
|
126
|
+
method: 'POST',
|
|
127
|
+
path: resourcePath,
|
|
128
|
+
body,
|
|
129
|
+
});
|
|
130
|
+
return this.parseAndWrap(response);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Retrieve a resource via GET request.
|
|
134
|
+
*
|
|
135
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
136
|
+
* @param query - Optional query parameters for the GET request
|
|
137
|
+
* @returns Instance of the model class representing the retrieved resource
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* return this.retrieveResource(`/pages/${pageId}`, query);
|
|
141
|
+
*/
|
|
142
|
+
async retrieveResource(resourcePath, query) {
|
|
143
|
+
const response = await this.client.request({
|
|
144
|
+
method: 'GET',
|
|
145
|
+
path: resourcePath,
|
|
146
|
+
query: Object.keys(query || {}).length > 0 ? query : undefined,
|
|
147
|
+
});
|
|
148
|
+
return this.parseAndWrap(response);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Retrieve a paginated list of resources via GET request.
|
|
152
|
+
*
|
|
153
|
+
* @param resourcePath - Path to the resource collection (e.g., '/pages', '/databases')
|
|
154
|
+
* @param query - Optional query parameters for pagination and filtering
|
|
155
|
+
* @returns PaginatedList containing instances of the model class for each item in the results array
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* return this.listResources(
|
|
159
|
+
* '/pages',
|
|
160
|
+
* { page_size: '50' },
|
|
161
|
+
* );
|
|
162
|
+
*/
|
|
163
|
+
async listResources(resourcePath, query) {
|
|
164
|
+
const response = await this.client.request({
|
|
165
|
+
method: 'GET',
|
|
166
|
+
path: resourcePath,
|
|
167
|
+
query: Object.keys(query || {}).length > 0 ? query : undefined,
|
|
168
|
+
});
|
|
169
|
+
return this.parsePaginatedList(response);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Update an existing resource via PATCH request.
|
|
173
|
+
*
|
|
174
|
+
* @param resourcePath - Path to the specific resource (e.g., '/pages/{page_id}', '/databases/{database_id}')
|
|
175
|
+
* @param body - Request body with updated fields
|
|
176
|
+
* @returns Instance of the model class representing the updated resource
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* return this.updateResource(
|
|
180
|
+
* `/pages/${pageId}`,
|
|
181
|
+
* { properties: { ... } },
|
|
182
|
+
* );
|
|
183
|
+
*/
|
|
184
|
+
async updateResource(resourcePath, body) {
|
|
185
|
+
const response = await this.client.request({
|
|
186
|
+
method: 'PATCH',
|
|
187
|
+
path: resourcePath,
|
|
188
|
+
body,
|
|
189
|
+
});
|
|
190
|
+
return this.parseAndWrap(response);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Parse response data using the provided schema and wrap it in the specified model class.
|
|
194
|
+
*
|
|
195
|
+
* @param response - The raw response data to parse
|
|
196
|
+
* @returns Instance of the model class representing the parsed resource
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* const response = await this.client.request<NotionPage>({ ... });
|
|
200
|
+
* return this.parseAndWrap(response);
|
|
201
|
+
*/
|
|
202
|
+
parseAndWrap(response) {
|
|
203
|
+
const parsed = this.config.schema.parse(response);
|
|
204
|
+
return new this.config.ModelClass(parsed);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Parse a paginated list response and wrap each item in the specified model class.
|
|
208
|
+
*
|
|
209
|
+
* @param response - The raw paginated list response to parse
|
|
210
|
+
* @returns PaginatedList containing instances of the model class for each item in the results array
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* const response = await this.client.request<PaginatedList<NotionPage>>({ ... });
|
|
214
|
+
* return this.parsePaginatedList(response);
|
|
215
|
+
*/
|
|
216
|
+
parsePaginatedList(response) {
|
|
217
|
+
const schema = (0, schemas_1.paginatedListSchema)(this.config.schema);
|
|
218
|
+
const parsed = schema.parse(response);
|
|
219
|
+
return {
|
|
220
|
+
object: 'list',
|
|
221
|
+
results: parsed.results.map((item) => new this.config.ModelClass(item)),
|
|
222
|
+
next_cursor: parsed.next_cursor,
|
|
223
|
+
has_more: parsed.has_more,
|
|
224
|
+
type: this.config.listType || 'unknown',
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.BaseAPI = BaseAPI;
|