Spaces:
Build error
Build error
File size: 19,801 Bytes
7d9f678 |
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
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");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Client_auth, _Client_logLevel, _Client_logger, _Client_prefixUrl, _Client_timeoutMs, _Client_notionVersion, _Client_fetch, _Client_agent, _Client_userAgent;
Object.defineProperty(exports, "__esModule", { value: true });
const logging_1 = require("./logging");
const errors_1 = require("./errors");
const utils_1 = require("./utils");
const api_endpoints_1 = require("./api-endpoints");
const node_fetch_1 = require("node-fetch");
const package_json_1 = require("../package.json");
class Client {
constructor(options) {
var _a, _b, _c, _d, _e, _f;
_Client_auth.set(this, void 0);
_Client_logLevel.set(this, void 0);
_Client_logger.set(this, void 0);
_Client_prefixUrl.set(this, void 0);
_Client_timeoutMs.set(this, void 0);
_Client_notionVersion.set(this, void 0);
_Client_fetch.set(this, void 0);
_Client_agent.set(this, void 0);
_Client_userAgent.set(this, void 0);
/*
* Notion API endpoints
*/
this.blocks = {
/**
* Retrieve block
*/
retrieve: (args) => {
return this.request({
path: api_endpoints_1.getBlock.path(args),
method: api_endpoints_1.getBlock.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getBlock.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getBlock.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Update block
*/
update: (args) => {
return this.request({
path: api_endpoints_1.updateBlock.path(args),
method: api_endpoints_1.updateBlock.method,
query: (0, utils_1.pick)(args, api_endpoints_1.updateBlock.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.updateBlock.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Delete block
*/
delete: (args) => {
return this.request({
path: api_endpoints_1.deleteBlock.path(args),
method: api_endpoints_1.deleteBlock.method,
query: (0, utils_1.pick)(args, api_endpoints_1.deleteBlock.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.deleteBlock.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
children: {
/**
* Append block children
*/
append: (args) => {
return this.request({
path: api_endpoints_1.appendBlockChildren.path(args),
method: api_endpoints_1.appendBlockChildren.method,
query: (0, utils_1.pick)(args, api_endpoints_1.appendBlockChildren.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.appendBlockChildren.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Retrieve block children
*/
list: (args) => {
return this.request({
path: api_endpoints_1.listBlockChildren.path(args),
method: api_endpoints_1.listBlockChildren.method,
query: (0, utils_1.pick)(args, api_endpoints_1.listBlockChildren.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.listBlockChildren.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
},
};
this.databases = {
/**
* List databases
*
* @deprecated Please use `search`
*/
list: (args) => {
return this.request({
path: api_endpoints_1.listDatabases.path(),
method: api_endpoints_1.listDatabases.method,
query: (0, utils_1.pick)(args, api_endpoints_1.listDatabases.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.listDatabases.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Retrieve a database
*/
retrieve: (args) => {
return this.request({
path: api_endpoints_1.getDatabase.path(args),
method: api_endpoints_1.getDatabase.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getDatabase.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getDatabase.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Query a database
*/
query: (args) => {
return this.request({
path: api_endpoints_1.queryDatabase.path(args),
method: api_endpoints_1.queryDatabase.method,
query: (0, utils_1.pick)(args, api_endpoints_1.queryDatabase.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.queryDatabase.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Create a database
*/
create: (args) => {
return this.request({
path: api_endpoints_1.createDatabase.path(),
method: api_endpoints_1.createDatabase.method,
query: (0, utils_1.pick)(args, api_endpoints_1.createDatabase.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.createDatabase.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Update a database
*/
update: (args) => {
return this.request({
path: api_endpoints_1.updateDatabase.path(args),
method: api_endpoints_1.updateDatabase.method,
query: (0, utils_1.pick)(args, api_endpoints_1.updateDatabase.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.updateDatabase.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
};
this.pages = {
/**
* Create a page
*/
create: (args) => {
return this.request({
path: api_endpoints_1.createPage.path(),
method: api_endpoints_1.createPage.method,
query: (0, utils_1.pick)(args, api_endpoints_1.createPage.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.createPage.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Retrieve a page
*/
retrieve: (args) => {
return this.request({
path: api_endpoints_1.getPage.path(args),
method: api_endpoints_1.getPage.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getPage.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getPage.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Update page properties
*/
update: (args) => {
return this.request({
path: api_endpoints_1.updatePage.path(args),
method: api_endpoints_1.updatePage.method,
query: (0, utils_1.pick)(args, api_endpoints_1.updatePage.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.updatePage.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
properties: {
/**
* Retrieve page property
*/
retrieve: (args) => {
return this.request({
path: api_endpoints_1.getPageProperty.path(args),
method: api_endpoints_1.getPageProperty.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getPageProperty.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getPageProperty.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
},
};
this.users = {
/**
* Retrieve a user
*/
retrieve: (args) => {
return this.request({
path: api_endpoints_1.getUser.path(args),
method: api_endpoints_1.getUser.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getUser.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getUser.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* List all users
*/
list: (args) => {
return this.request({
path: api_endpoints_1.listUsers.path(),
method: api_endpoints_1.listUsers.method,
query: (0, utils_1.pick)(args, api_endpoints_1.listUsers.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.listUsers.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* Get details about bot
*/
me: (args) => {
return this.request({
path: api_endpoints_1.getSelf.path(),
method: api_endpoints_1.getSelf.method,
query: (0, utils_1.pick)(args, api_endpoints_1.getSelf.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.getSelf.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
};
this.comments = {
/**
* Create a comment
*/
create: (args) => {
return this.request({
path: api_endpoints_1.createComment.path(),
method: api_endpoints_1.createComment.method,
query: (0, utils_1.pick)(args, api_endpoints_1.createComment.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.createComment.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
/**
* List comments
*/
list: (args) => {
return this.request({
path: api_endpoints_1.listComments.path(),
method: api_endpoints_1.listComments.method,
query: (0, utils_1.pick)(args, api_endpoints_1.listComments.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.listComments.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
},
};
/**
* Search
*/
this.search = (args) => {
return this.request({
path: api_endpoints_1.search.path(),
method: api_endpoints_1.search.method,
query: (0, utils_1.pick)(args, api_endpoints_1.search.queryParams),
body: (0, utils_1.pick)(args, api_endpoints_1.search.bodyParams),
auth: args === null || args === void 0 ? void 0 : args.auth,
});
};
__classPrivateFieldSet(this, _Client_auth, options === null || options === void 0 ? void 0 : options.auth, "f");
__classPrivateFieldSet(this, _Client_logLevel, (_a = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _a !== void 0 ? _a : logging_1.LogLevel.WARN, "f");
__classPrivateFieldSet(this, _Client_logger, (_b = options === null || options === void 0 ? void 0 : options.logger) !== null && _b !== void 0 ? _b : (0, logging_1.makeConsoleLogger)(package_json_1.name), "f");
__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");
__classPrivateFieldSet(this, _Client_timeoutMs, (_d = options === null || options === void 0 ? void 0 : options.timeoutMs) !== null && _d !== void 0 ? _d : 60000, "f");
__classPrivateFieldSet(this, _Client_notionVersion, (_e = options === null || options === void 0 ? void 0 : options.notionVersion) !== null && _e !== void 0 ? _e : Client.defaultNotionVersion, "f");
__classPrivateFieldSet(this, _Client_fetch, (_f = options === null || options === void 0 ? void 0 : options.fetch) !== null && _f !== void 0 ? _f : node_fetch_1.default, "f");
__classPrivateFieldSet(this, _Client_agent, options === null || options === void 0 ? void 0 : options.agent, "f");
__classPrivateFieldSet(this, _Client_userAgent, `notionhq-client/${package_json_1.version}`, "f");
}
/**
* Sends a request.
*
* @param path
* @param method
* @param query
* @param body
* @returns
*/
async request({ path, method, query, body, auth, }) {
this.log(logging_1.LogLevel.INFO, "request start", { method, path });
// If the body is empty, don't send the body in the HTTP request
const bodyAsJsonString = !body || Object.entries(body).length === 0
? undefined
: JSON.stringify(body);
const url = new URL(`${__classPrivateFieldGet(this, _Client_prefixUrl, "f")}${path}`);
if (query) {
for (const [key, value] of Object.entries(query)) {
if (value !== undefined) {
if (Array.isArray(value)) {
value.forEach(val => url.searchParams.append(key, String(val)));
}
else {
url.searchParams.append(key, String(value));
}
}
}
}
const headers = {
...this.authAsHeaders(auth),
"Notion-Version": __classPrivateFieldGet(this, _Client_notionVersion, "f"),
"user-agent": __classPrivateFieldGet(this, _Client_userAgent, "f"),
};
if (bodyAsJsonString !== undefined) {
headers["content-type"] = "application/json";
}
try {
const response = await errors_1.RequestTimeoutError.rejectAfterTimeout(__classPrivateFieldGet(this, _Client_fetch, "f").call(this, url.toString(), {
method: method.toUpperCase(),
headers,
body: bodyAsJsonString,
agent: __classPrivateFieldGet(this, _Client_agent, "f"),
}), __classPrivateFieldGet(this, _Client_timeoutMs, "f"));
const responseText = await response.text();
if (!response.ok) {
throw (0, errors_1.buildRequestError)(response, responseText);
}
const responseJson = JSON.parse(responseText);
this.log(logging_1.LogLevel.INFO, `request success`, { method, path });
return responseJson;
}
catch (error) {
if (!(0, errors_1.isNotionClientError)(error)) {
throw error;
}
// Log the error if it's one of our known error types
this.log(logging_1.LogLevel.WARN, `request fail`, {
code: error.code,
message: error.message,
});
if ((0, errors_1.isHTTPResponseError)(error)) {
// The response body may contain sensitive information so it is logged separately at the DEBUG level
this.log(logging_1.LogLevel.DEBUG, `failed response body`, {
body: error.body,
});
}
throw error;
}
}
/**
* Emits a log message to the console.
*
* @param level The level for this message
* @param args Arguments to send to the console
*/
log(level, message, extraInfo) {
if ((0, logging_1.logLevelSeverity)(level) >= (0, logging_1.logLevelSeverity)(__classPrivateFieldGet(this, _Client_logLevel, "f"))) {
__classPrivateFieldGet(this, _Client_logger, "f").call(this, level, message, extraInfo);
}
}
/**
* Transforms an API key or access token into a headers object suitable for an HTTP request.
*
* This method uses the instance's value as the default when the input is undefined. If neither are defined, it returns
* an empty object
*
* @param auth API key or access token
* @returns headers key-value object
*/
authAsHeaders(auth) {
const headers = {};
const authHeaderValue = auth !== null && auth !== void 0 ? auth : __classPrivateFieldGet(this, _Client_auth, "f");
if (authHeaderValue !== undefined) {
headers["authorization"] = `Bearer ${authHeaderValue}`;
}
return headers;
}
}
exports.default = Client;
_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();
Client.defaultNotionVersion = "2022-06-28";
//# sourceMappingURL=Client.js.map |