/
agent.ts
232 lines (196 loc) · 6.38 KB
/
agent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/** Import other sripts. */
import { post } from "./httputil"
import { log } from "./log"
import { Default } from "./strings"
import { APIError } from "./error/APIError"
/** Import typings. */
import { API } from "./interfaces"
/**
* Options to config a Notion API agent instance.
*
* @category Library
*/
interface CreateAgentOptions {
/** Web address of the API server. Default: `"https://www.notion.so"`. */
server?: string
/**
* Notion API token. Default: `""`.
* {@link https://github.com/dragonman225/notionapi-agent/blob/master/documentation/obtain-token/obtain-token.md | How to get it ?}
*/
token?: string
/** Whether to trun on debug message. Default: `false`. */
debug?: boolean
}
/**
* Notion API agent instance.
*
* @category Library
*/
interface Agent {
/**
* POST /api/v3/getActivityLog
*
* Get user activities of a navigable block, e.g. a page.
* Equivalent to the "Updates" button in Notion's UI.
*
* @remark Must be authenticated even for public blocks.
*/
getActivityLog: (req: API.GetActivityLog.Request) =>
Promise<API.GetActivityLog.Response>
/**
* POST /api/v3/getAssetsJson
*
* Get a list of static asset paths, current version number, and more.
*/
getAssetsJson: (req: API.GetAssetsJson.Request) =>
Promise<API.GetAssetsJson.Response>
/**
* POST /api/v3/getRecordValues
*
* Get records by table name and id.
*
* If a {@link RecordRequest} is the i<sup>th</sup> element
* of {@link GetRecordValues.Request.requests}, its result is
* the i<sup>th</sup> element of {@link GetRecordValues.Response.results}.
*/
getRecordValues: (req: API.GetRecordValues.Request) =>
Promise<API.GetRecordValues.Response>
/**
* POST /api/v3/getSnapshotsList
*
* @remark Must be authenticated even for public blocks.
*/
getSnapshotsList: (req: API.GetSnapshotsList.Request) =>
Promise<API.GetSnapshotsList.Response>
/**
* POST /api/v3/getUserSharedPages
*
* Get ids of pages created with **+ New Page** button at the top level
* of the user's workspace *AND* those not created at the top level of
* the user's workspace but had been moved to the top level some time
* after created.
*
* To always get the top level pages of the user's workspace,
* use {@link Agent.loadUserContent}.
*/
getUserSharedPages: (req: API.GetUserSharedPages.Request) =>
Promise<API.GetUserSharedPages.Response>
/**
* POST /api/v3/loadPageChunk
*
* Load some data related to a page.
*/
loadPageChunk: (req: API.LoadPageChunk.Request) =>
Promise<API.LoadPageChunk.Response>
/**
* POST /api/v3/loadUserContent
*
* Get top level page blocks (`block` in
* {@link LoadUserContent.Response.recordMap}),
* user information, and workspace information.
*/
loadUserContent: (req: API.LoadUserContent.Request) =>
Promise<API.LoadUserContent.Response>
/**
* POST /api/v3/queryCollection
*
* Query a collection by id, view id,
* with aggregate, filter, sort functions.
*
* To configure aggregate, filter, sort parameters, see
* {@link QueryCollection.Request.query}.
*
* Set `limit` in {@link QueryCollection.Request.loader}
* to limit maximum number of items in response data.
*/
queryCollection: (req: API.QueryCollection.Request) =>
Promise<API.QueryCollection.Response>
/**
* POST /api/v3/submitTransaction
*
* Make changes to documents and settings.
*/
submitTransaction: (req: API.SubmitTransaction.Request) =>
Promise<API.SubmitTransaction.Response>
}
/**
* Create an asynchronous Notion API function.
* @param url The API's full URL.
* @param token The API token for authentication.
*
* @category Library Internal
*/
function createAPI<Req, Res>(url: string, token: string) {
log.debug(`agent.ts: Create API function with\
URL "${url}"`)
return async function (req: Req): Promise<Res> {
log.debug(`agent.ts: Call API "${url}".`)
const result = await post(url)
.setHeader("accept", "*/*")
.setHeader("accept-language", "en-US,en;q=0.9")
.setHeader("cookie", `token_v2=${token};`)
.setHeader("origin", Default.server)
.setHeader("referer", Default.server)
.setHeader("user-agent", Default.userAgent)
.sendAsJson(req)
if (result.hasOwnProperty("errorId")) {
const error = result as API.ErrorResponse
throw new APIError(error)
}
return result
}
}
/**
* Create a Notion API agent.
* @param opts A config object.
*
* @category Library
*/
function createAgent(opts: CreateAgentOptions = {}): Agent {
const token = opts.token ? opts.token : ""
const server = opts.server ? opts.server : Default.server
if (opts.debug) {
log.setLogLevel("debug")
}
log.debug(`agent.ts: Create API agent with\
server "${server}" and token "${token.substr(0, 9)}..."`)
const getActivityLog =
createAPI<API.GetActivityLog.Request, API.GetActivityLog.Response>(
`${server}/api/v3/getActivityLog`, token)
const getAssetsJson =
createAPI<API.GetAssetsJson.Request, API.GetAssetsJson.Response>(
`${server}/api/v3/getAssetsJson`, token)
const getRecordValues =
createAPI<API.GetRecordValues.Request, API.GetRecordValues.Response>(
`${server}/api/v3/getRecordValues`, token)
const getSnapshotsList =
createAPI<API.GetSnapshotsList.Request, API.GetSnapshotsList.Response>(
`${server}/api/v3/getSnapshotsList`, token)
const getUserSharedPages =
createAPI<API.GetUserSharedPages.Request, API.GetUserSharedPages.Response>(
`${server}/api/v3/getUserSharedPages`, token)
const loadPageChunk =
createAPI<API.LoadPageChunk.Request, API.LoadPageChunk.Response>(
`${server}/api/v3/loadPageChunk`, token)
const loadUserContent =
createAPI<API.LoadUserContent.Request, API.LoadUserContent.Response>(
`${server}/api/v3/loadUserContent`, token)
const queryCollection =
createAPI<API.QueryCollection.Request, API.QueryCollection.Response>(
`${server}/api/v3/queryCollection`, token)
const submitTransaction =
createAPI<API.SubmitTransaction.Request, API.SubmitTransaction.Response>(
`${server}/api/v3/submitTransaction`, token)
return {
getActivityLog,
getAssetsJson,
getRecordValues,
getSnapshotsList,
getUserSharedPages,
loadPageChunk,
loadUserContent,
queryCollection,
submitTransaction
}
}
export { createAgent }