motionlink-cli
Version:
Making it easy to use Notion as a Content Management system for personal websites, portfolios, blogs, business homepages, and other kinds of static websites.
122 lines • 5.78 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
};
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resultOf = void 0;
const client_1 = require("@notionhq/client");
const src_1 = require("@notionhq/client/build/src");
const ALL = Number.POSITIVE_INFINITY;
class NotionService {
getDatabase(args) {
const notion = this.initNotionClient(args.withToken);
return resultOf(() => notion.databases.retrieve({
database_id: args.withId,
}));
}
queryForDatabasePages(args) {
return __asyncGenerator(this, arguments, function* queryForDatabasePages_1() {
// TODO: Unit test the logic here.
const notion = this.initNotionClient(args.withToken);
let pagesToReadCount = ALL;
if (args.takeOnly !== undefined)
pagesToReadCount = args.takeOnly;
let readPages = [];
function shouldReadMorePages() {
return readPages.length < pagesToReadCount;
}
let hasMore = true;
let nextCursor;
while (hasMore && shouldReadMorePages()) {
const response = yield __await(resultOf(() => notion.databases.query({
database_id: args.databaseId,
page_size: Math.min(pagesToReadCount - readPages.length, 100),
sorts: args.sort,
filter: args.filter,
start_cursor: nextCursor,
})));
readPages = readPages.concat(response.results);
hasMore = response.has_more;
nextCursor = response.next_cursor;
}
yield __await(yield* __asyncDelegator(__asyncValues(readPages)));
});
}
// TODO: Remove in favor of getBlockChildren
getPageBlocks(args) {
return this.getBlockChildren({
blockId: args.pageId,
withToken: args.withToken,
});
}
getBlockChildren(args) {
return __asyncGenerator(this, arguments, function* getBlockChildren_1() {
// TODO: Unit test the logic here
const notion = this.initNotionClient(args.withToken);
let hasMore = true;
let nextCursor;
while (hasMore) {
const response = yield __await(resultOf(() => notion.blocks.children.list({
block_id: args.blockId,
start_cursor: nextCursor,
})));
yield __await(yield* __asyncDelegator(__asyncValues(response.results)));
hasMore = response.has_more;
nextCursor = response.next_cursor;
}
});
}
initNotionClient(notionToken) {
return new client_1.Client({
auth: notionToken.token,
});
}
static get instance() {
var _a;
return (_a = this._instance) !== null && _a !== void 0 ? _a : (this._instance = new NotionService());
}
static setMockedInstance(instance) {
this._instance = instance;
}
}
exports.default = NotionService;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const allowedNotionApiCallsPerSecond = 3;
const sleepTimeInMillis = 1000 / allowedNotionApiCallsPerSecond;
async function resultOf(notionCall) {
try {
return await notionCall();
}
catch (e) {
if (e.code === src_1.APIErrorCode.RateLimited) {
await sleep(sleepTimeInMillis);
return resultOf(notionCall);
}
throw e;
}
}
exports.resultOf = resultOf;
//# sourceMappingURL=notion_service.js.map