UNPKG

18.2 kBJavaScriptView Raw
1"use strict";
2var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3 if (kind === "m") throw new TypeError("Private method is not writable");
4 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7};
8var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12};
13var _Client_auth, _Client_logLevel, _Client_logger, _Client_prefixUrl, _Client_timeoutMs, _Client_notionVersion, _Client_fetch, _Client_agent, _Client_userAgent;
14Object.defineProperty(exports, "__esModule", { value: true });
15const logging_1 = require("./logging");
16const errors_1 = require("./errors");
17const helpers_1 = require("./helpers");
18const api_endpoints_1 = require("./api-endpoints");
19const node_fetch_1 = require("node-fetch");
20const package_json_1 = require("../package.json");
21class Client {
22 constructor(options) {
23 var _a, _b, _c, _d, _e, _f;
24 _Client_auth.set(this, void 0);
25 _Client_logLevel.set(this, void 0);
26 _Client_logger.set(this, void 0);
27 _Client_prefixUrl.set(this, void 0);
28 _Client_timeoutMs.set(this, void 0);
29 _Client_notionVersion.set(this, void 0);
30 _Client_fetch.set(this, void 0);
31 _Client_agent.set(this, void 0);
32 _Client_userAgent.set(this, void 0);
33 /*
34 * Notion API endpoints
35 */
36 this.blocks = {
37 /**
38 * Retrieve block
39 */
40 retrieve: (args) => {
41 return this.request({
42 path: api_endpoints_1.getBlock.path(args),
43 method: api_endpoints_1.getBlock.method,
44 query: helpers_1.pick(args, api_endpoints_1.getBlock.queryParams),
45 body: helpers_1.pick(args, api_endpoints_1.getBlock.bodyParams),
46 auth: args === null || args === void 0 ? void 0 : args.auth,
47 });
48 },
49 /**
50 * Update block
51 */
52 update: (args) => {
53 return this.request({
54 path: api_endpoints_1.updateBlock.path(args),
55 method: api_endpoints_1.updateBlock.method,
56 query: helpers_1.pick(args, api_endpoints_1.updateBlock.queryParams),
57 body: helpers_1.pick(args, api_endpoints_1.updateBlock.bodyParams),
58 auth: args === null || args === void 0 ? void 0 : args.auth,
59 });
60 },
61 /**
62 * Delete block
63 */
64 delete: (args) => {
65 return this.request({
66 path: api_endpoints_1.deleteBlock.path(args),
67 method: api_endpoints_1.deleteBlock.method,
68 query: helpers_1.pick(args, api_endpoints_1.deleteBlock.queryParams),
69 body: helpers_1.pick(args, api_endpoints_1.deleteBlock.bodyParams),
70 auth: args === null || args === void 0 ? void 0 : args.auth,
71 });
72 },
73 children: {
74 /**
75 * Append block children
76 */
77 append: (args) => {
78 return this.request({
79 path: api_endpoints_1.appendBlockChildren.path(args),
80 method: api_endpoints_1.appendBlockChildren.method,
81 query: helpers_1.pick(args, api_endpoints_1.appendBlockChildren.queryParams),
82 body: helpers_1.pick(args, api_endpoints_1.appendBlockChildren.bodyParams),
83 auth: args === null || args === void 0 ? void 0 : args.auth,
84 });
85 },
86 /**
87 * Retrieve block children
88 */
89 list: (args) => {
90 return this.request({
91 path: api_endpoints_1.listBlockChildren.path(args),
92 method: api_endpoints_1.listBlockChildren.method,
93 query: helpers_1.pick(args, api_endpoints_1.listBlockChildren.queryParams),
94 body: helpers_1.pick(args, api_endpoints_1.listBlockChildren.bodyParams),
95 auth: args === null || args === void 0 ? void 0 : args.auth,
96 });
97 },
98 },
99 };
100 this.databases = {
101 /**
102 * List databases
103 *
104 * @deprecated Please use `search`
105 */
106 list: (args) => {
107 return this.request({
108 path: api_endpoints_1.listDatabases.path(),
109 method: api_endpoints_1.listDatabases.method,
110 query: helpers_1.pick(args, api_endpoints_1.listDatabases.queryParams),
111 body: helpers_1.pick(args, api_endpoints_1.listDatabases.bodyParams),
112 auth: args === null || args === void 0 ? void 0 : args.auth,
113 });
114 },
115 /**
116 * Retrieve a database
117 */
118 retrieve: (args) => {
119 return this.request({
120 path: api_endpoints_1.getDatabase.path(args),
121 method: api_endpoints_1.getDatabase.method,
122 query: helpers_1.pick(args, api_endpoints_1.getDatabase.queryParams),
123 body: helpers_1.pick(args, api_endpoints_1.getDatabase.bodyParams),
124 auth: args === null || args === void 0 ? void 0 : args.auth,
125 });
126 },
127 /**
128 * Query a database
129 */
130 query: (args) => {
131 return this.request({
132 path: api_endpoints_1.queryDatabase.path(args),
133 method: api_endpoints_1.queryDatabase.method,
134 query: helpers_1.pick(args, api_endpoints_1.queryDatabase.queryParams),
135 body: helpers_1.pick(args, api_endpoints_1.queryDatabase.bodyParams),
136 auth: args === null || args === void 0 ? void 0 : args.auth,
137 });
138 },
139 /**
140 * Create a database
141 */
142 create: (args) => {
143 return this.request({
144 path: api_endpoints_1.createDatabase.path(),
145 method: api_endpoints_1.createDatabase.method,
146 query: helpers_1.pick(args, api_endpoints_1.createDatabase.queryParams),
147 body: helpers_1.pick(args, api_endpoints_1.createDatabase.bodyParams),
148 auth: args === null || args === void 0 ? void 0 : args.auth,
149 });
150 },
151 /**
152 * Update a database
153 */
154 update: (args) => {
155 return this.request({
156 path: api_endpoints_1.updateDatabase.path(args),
157 method: api_endpoints_1.updateDatabase.method,
158 query: helpers_1.pick(args, api_endpoints_1.updateDatabase.queryParams),
159 body: helpers_1.pick(args, api_endpoints_1.updateDatabase.bodyParams),
160 auth: args === null || args === void 0 ? void 0 : args.auth,
161 });
162 },
163 };
164 this.pages = {
165 /**
166 * Create a page
167 */
168 create: (args) => {
169 return this.request({
170 path: api_endpoints_1.createPage.path(),
171 method: api_endpoints_1.createPage.method,
172 query: helpers_1.pick(args, api_endpoints_1.createPage.queryParams),
173 body: helpers_1.pick(args, api_endpoints_1.createPage.bodyParams),
174 auth: args === null || args === void 0 ? void 0 : args.auth,
175 });
176 },
177 /**
178 * Retrieve a page
179 */
180 retrieve: (args) => {
181 return this.request({
182 path: api_endpoints_1.getPage.path(args),
183 method: api_endpoints_1.getPage.method,
184 query: helpers_1.pick(args, api_endpoints_1.getPage.queryParams),
185 body: helpers_1.pick(args, api_endpoints_1.getPage.bodyParams),
186 auth: args === null || args === void 0 ? void 0 : args.auth,
187 });
188 },
189 /**
190 * Update page properties
191 */
192 update: (args) => {
193 return this.request({
194 path: api_endpoints_1.updatePage.path(args),
195 method: api_endpoints_1.updatePage.method,
196 query: helpers_1.pick(args, api_endpoints_1.updatePage.queryParams),
197 body: helpers_1.pick(args, api_endpoints_1.updatePage.bodyParams),
198 auth: args === null || args === void 0 ? void 0 : args.auth,
199 });
200 },
201 properties: {
202 /**
203 * Retrieve page property
204 */
205 retrieve: (args) => {
206 return this.request({
207 path: api_endpoints_1.getPageProperty.path(args),
208 method: api_endpoints_1.getPageProperty.method,
209 query: helpers_1.pick(args, api_endpoints_1.getPageProperty.queryParams),
210 body: helpers_1.pick(args, api_endpoints_1.getPageProperty.bodyParams),
211 auth: args === null || args === void 0 ? void 0 : args.auth,
212 });
213 },
214 },
215 };
216 this.users = {
217 /**
218 * Retrieve a user
219 */
220 retrieve: (args) => {
221 return this.request({
222 path: api_endpoints_1.getUser.path(args),
223 method: api_endpoints_1.getUser.method,
224 query: helpers_1.pick(args, api_endpoints_1.getUser.queryParams),
225 body: helpers_1.pick(args, api_endpoints_1.getUser.bodyParams),
226 auth: args === null || args === void 0 ? void 0 : args.auth,
227 });
228 },
229 /**
230 * List all users
231 */
232 list: (args) => {
233 return this.request({
234 path: api_endpoints_1.listUsers.path(),
235 method: api_endpoints_1.listUsers.method,
236 query: helpers_1.pick(args, api_endpoints_1.listUsers.queryParams),
237 body: helpers_1.pick(args, api_endpoints_1.listUsers.bodyParams),
238 auth: args === null || args === void 0 ? void 0 : args.auth,
239 });
240 },
241 /**
242 * Get details about bot
243 */
244 me: (args) => {
245 return this.request({
246 path: api_endpoints_1.getSelf.path(),
247 method: api_endpoints_1.getSelf.method,
248 query: helpers_1.pick(args, api_endpoints_1.getSelf.queryParams),
249 body: helpers_1.pick(args, api_endpoints_1.getSelf.bodyParams),
250 auth: args === null || args === void 0 ? void 0 : args.auth,
251 });
252 },
253 };
254 __classPrivateFieldSet(this, _Client_auth, options === null || options === void 0 ? void 0 : options.auth, "f");
255 __classPrivateFieldSet(this, _Client_logLevel, (_a = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _a !== void 0 ? _a : logging_1.LogLevel.WARN, "f");
256 __classPrivateFieldSet(this, _Client_logger, (_b = options === null || options === void 0 ? void 0 : options.logger) !== null && _b !== void 0 ? _b : logging_1.makeConsoleLogger(package_json_1.name), "f");
257 __classPrivateFieldSet(this, _Client_prefixUrl, ((_c = options === null || options === void 0 ? void 0 : options.baseUrl) !== null && _c !== void 0 ? _c : "https://api.notion.com") + "/v1/", "f");
258 __classPrivateFieldSet(this, _Client_timeoutMs, (_d = options === null || options === void 0 ? void 0 : options.timeoutMs) !== null && _d !== void 0 ? _d : 60000, "f");
259 __classPrivateFieldSet(this, _Client_notionVersion, (_e = options === null || options === void 0 ? void 0 : options.notionVersion) !== null && _e !== void 0 ? _e : Client.defaultNotionVersion, "f");
260 __classPrivateFieldSet(this, _Client_fetch, (_f = options === null || options === void 0 ? void 0 : options.fetch) !== null && _f !== void 0 ? _f : node_fetch_1.default, "f");
261 __classPrivateFieldSet(this, _Client_agent, options === null || options === void 0 ? void 0 : options.agent, "f");
262 __classPrivateFieldSet(this, _Client_userAgent, `notionhq-client/${package_json_1.version}`, "f");
263 }
264 /**
265 * Sends a request.
266 *
267 * @param path
268 * @param method
269 * @param query
270 * @param body
271 * @returns
272 */
273 async request({ path, method, query, body, auth, }) {
274 this.log(logging_1.LogLevel.INFO, "request start", { method, path });
275 // If the body is empty, don't send the body in the HTTP request
276 const bodyAsJsonString = !body || Object.entries(body).length === 0
277 ? undefined
278 : JSON.stringify(body);
279 const url = new URL(`${__classPrivateFieldGet(this, _Client_prefixUrl, "f")}${path}`);
280 if (query) {
281 for (const [key, value] of Object.entries(query)) {
282 if (value !== undefined) {
283 url.searchParams.append(key, String(value));
284 }
285 }
286 }
287 const headers = {
288 ...this.authAsHeaders(auth),
289 "Notion-Version": __classPrivateFieldGet(this, _Client_notionVersion, "f"),
290 "user-agent": __classPrivateFieldGet(this, _Client_userAgent, "f"),
291 };
292 if (bodyAsJsonString !== undefined) {
293 headers["content-type"] = "application/json";
294 }
295 try {
296 const response = await errors_1.RequestTimeoutError.rejectAfterTimeout(__classPrivateFieldGet(this, _Client_fetch, "f").call(this, url.toString(), {
297 method,
298 headers,
299 body: bodyAsJsonString,
300 agent: __classPrivateFieldGet(this, _Client_agent, "f"),
301 }), __classPrivateFieldGet(this, _Client_timeoutMs, "f"));
302 const responseText = await response.text();
303 if (!response.ok) {
304 throw errors_1.buildRequestError(response, responseText);
305 }
306 const responseJson = JSON.parse(responseText);
307 this.log(logging_1.LogLevel.INFO, `request success`, { method, path });
308 return responseJson;
309 }
310 catch (error) {
311 if (!errors_1.isNotionClientError(error)) {
312 throw error;
313 }
314 // Log the error if it's one of our known error types
315 this.log(logging_1.LogLevel.WARN, `request fail`, {
316 code: error.code,
317 message: error.message,
318 });
319 if (errors_1.isHTTPResponseError(error)) {
320 // The response body may contain sensitive information so it is logged separately at the DEBUG level
321 this.log(logging_1.LogLevel.DEBUG, `failed response body`, {
322 body: error.body,
323 });
324 }
325 throw error;
326 }
327 }
328 /**
329 * Search
330 */
331 search(args) {
332 return this.request({
333 path: api_endpoints_1.search.path(),
334 method: api_endpoints_1.search.method,
335 query: helpers_1.pick(args, api_endpoints_1.search.queryParams),
336 body: helpers_1.pick(args, api_endpoints_1.search.bodyParams),
337 auth: args === null || args === void 0 ? void 0 : args.auth,
338 });
339 }
340 /**
341 * Emits a log message to the console.
342 *
343 * @param level The level for this message
344 * @param args Arguments to send to the console
345 */
346 log(level, message, extraInfo) {
347 if (logging_1.logLevelSeverity(level) >= logging_1.logLevelSeverity(__classPrivateFieldGet(this, _Client_logLevel, "f"))) {
348 __classPrivateFieldGet(this, _Client_logger, "f").call(this, level, message, extraInfo);
349 }
350 }
351 /**
352 * Transforms an API key or access token into a headers object suitable for an HTTP request.
353 *
354 * This method uses the instance's value as the default when the input is undefined. If neither are defined, it returns
355 * an empty object
356 *
357 * @param auth API key or access token
358 * @returns headers key-value object
359 */
360 authAsHeaders(auth) {
361 const headers = {};
362 const authHeaderValue = auth !== null && auth !== void 0 ? auth : __classPrivateFieldGet(this, _Client_auth, "f");
363 if (authHeaderValue !== undefined) {
364 headers["authorization"] = `Bearer ${authHeaderValue}`;
365 }
366 return headers;
367 }
368}
369exports.default = Client;
370_Client_auth = new WeakMap(), _Client_logLevel = new WeakMap(), _Client_logger = new WeakMap(), _Client_prefixUrl = new WeakMap(), _Client_timeoutMs = new WeakMap(), _Client_notionVersion = new WeakMap(), _Client_fetch = new WeakMap(), _Client_agent = new WeakMap(), _Client_userAgent = new WeakMap();
371Client.defaultNotionVersion = "2022-02-22";
372//# sourceMappingURL=Client.js.map
\No newline at end of file