Comparing version 0.5.2 to 0.6.0
@@ -0,0 +0,0 @@ { |
{ | ||
"name": "notabase", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "API Wrapper For Notion's Database", | ||
@@ -5,0 +5,0 @@ "main": "src/notabase.js", |
@@ -17,24 +17,4 @@ # Notabase (WIP) | ||
import Notabase from 'notabase' | ||
// node env | ||
// just Read public data | ||
let nb = new Notabase() | ||
// CRUD support | ||
let nb = new Notabase({ | ||
token: <token_v2> | ||
}) | ||
// browser env | ||
// without authCode Read public data | ||
// with authCode CRUD support | ||
let nb = new Notabase({ | ||
proxy: { | ||
url: <cloudflare worker url>, | ||
authCode: <> | ||
} | ||
}) | ||
// get my music data | ||
@@ -41,0 +21,0 @@ let db = await nb.fetch({ |
@@ -0,0 +0,0 @@ const token_v2 = undefined |
@@ -1,10 +0,11 @@ | ||
const NOTION_BASE_URL = "https://www.notion.so" | ||
import Collection from './collection' | ||
import { getBlockHashId, getFullBlockId } from './utils' | ||
let blockStore = {} | ||
let collectionSchemaStore = {} | ||
const NOTION_BASE_URL = "https://www.notion.so" | ||
class Notabase { | ||
export default class Notabase { | ||
constructor(options = {}) { | ||
this.blockStore = {} | ||
this.collectionSchemaStore = {} | ||
const { proxy, token } = options | ||
@@ -21,3 +22,2 @@ // proxy > browser env + cloudflare worker | ||
this.authCode = authCode | ||
this.reqeust = { | ||
@@ -36,6 +36,9 @@ async post(path, data) { | ||
} else { | ||
// node env | ||
// token node env | ||
this.token = token | ||
let tkHeader = token ? { 'cookie': `token_v2=${token}` } : {} | ||
const fetch = require("node-fetch") | ||
// non-token browse ext env | ||
let credentials = !token ? { credentials: 'include' } : {} | ||
this.reqeust = { | ||
@@ -53,3 +56,4 @@ async post(path, data) { | ||
}, | ||
body: JSON.stringify(data) | ||
body: JSON.stringify(data), | ||
...credentials | ||
}) | ||
@@ -62,16 +66,3 @@ return await r.json() | ||
getUrlBloackId(url) { | ||
let pUrl | ||
if (!process.browser) { | ||
const parse = require('url').parse | ||
pUrl = parse(url) | ||
} else { | ||
pUrl = new URL(url) | ||
} | ||
let pathList = pUrl.pathname.split('/') | ||
let blockID = pathList[pathList.length - 1] | ||
return blockID | ||
} | ||
async getBrowseableUrlByCollectionPageId(pageId) { | ||
@@ -81,3 +72,3 @@ let r = await this.getRecordValues([pageId], []) | ||
let browseableUrl = `${NOTION_BASE_URL}${this.getBlockHashId(pageId)}?v=${this.getBlockHashId(viewId)}` | ||
let browseableUrl = `${NOTION_BASE_URL}${getBlockHashId(pageId)}?v=${getBlockHashId(viewId)}` | ||
return browseableUrl | ||
@@ -96,21 +87,11 @@ } | ||
getBlockHashId(blockId) { | ||
return blockId.split('-').join('') | ||
async loadPageChunk(pageId) { | ||
let data = await this.reqeust.post(`/api/v3/loadPageChunk`, | ||
{ "pageId": getFullBlockId(pageId), "limit": 50, "cursor": { "stack": [] }, "chunkNumber": 0, "verticalColumns": false } | ||
) | ||
} | ||
getFullBlockId(blockId) { | ||
if (blockId.match("^[a-zA-Z0-9]+$")) { | ||
return blockId.substr(0, 8) + "-" | ||
+ blockId.substr(8, 4) + "-" | ||
+ blockId.substr(12, 4) + "-" | ||
+ blockId.substr(16, 4) + "-" | ||
+ blockId.substr(20, 32) | ||
} else { | ||
return blockId | ||
} | ||
} | ||
async getPageCollectionInfo(pageId) { | ||
console.log(`>>>> getPageChunk:${pageId}`) | ||
let data = await this.reqeust.post(`/api/v3/loadPageChunk`, | ||
{ "pageId": this.getFullBlockId(pageId), "limit": 50, "cursor": { "stack": [] }, "chunkNumber": 0, "verticalColumns": false } | ||
{ "pageId": getFullBlockId(pageId), "limit": 50, "cursor": { "stack": [] }, "chunkNumber": 0, "verticalColumns": false } | ||
) | ||
@@ -122,25 +103,2 @@ let collectionId = Object.entries(data.recordMap.collection)[0][0] | ||
getBrowseableUrl(blockID) { | ||
return `${NOTION_BASE_URL}/${blockID.split('-').join('')}` | ||
} | ||
parseImageUrl(url, width) { | ||
let rUrl | ||
if (url.startsWith("https://s3")) { | ||
let [parsedOriginUrl] = url.split("?") | ||
rUrl = `${NOTION_BASE_URL}/image/${encodeURIComponent(parsedOriginUrl).replace("s3.us-west", "s3-us-west")}` | ||
} else if (url.startsWith("/image")) { | ||
rUrl = `${NOTION_BASE_URL}${url}` | ||
} else { | ||
rUrl = url | ||
} | ||
if (width) { | ||
return `${rUrl}?width=${width}` | ||
} else { | ||
return rUrl | ||
} | ||
} | ||
async fetchCollectionData(collectionId, collectionViewId) { | ||
@@ -151,3 +109,9 @@ | ||
collectionViewId, | ||
loader: { type: "table" } | ||
loader: { | ||
"type": "table", | ||
"limit": 1000, | ||
"userTimeZone": "Asia/Shanghai", | ||
"userLocale": "zh-tw", | ||
"loadContentCover": true | ||
} | ||
}) | ||
@@ -157,5 +121,4 @@ console.log(`>>>> queryCollection:${collectionId}`) | ||
let schema = data.recordMap.collection[collectionId].value.schema | ||
collectionSchemaStore[collectionId] = schema | ||
blockStore = { ...blockStore, ...data.recordMap.block } | ||
return new Collection(collectionId, collectionViewId, data) | ||
this.collectionSchemaStore[collectionId] = schema | ||
return new Collection(collectionId, collectionViewId, data, this) | ||
} | ||
@@ -166,3 +129,3 @@ async _fetch(urlOrPageId) { | ||
// pageId with '-' split | ||
[collectionId, collectionViewId] = await this.getPageCollectionInfo(this.getBlockHashId(urlOrPageId)) | ||
[collectionId, collectionViewId] = await this.getPageCollectionInfo(getBlockHashId(urlOrPageId)) | ||
} else if (urlOrPageId.startsWith("http")) { | ||
@@ -198,125 +161,2 @@ // url | ||
} | ||
} | ||
class Collection { | ||
constructor(collectionId, collectionViewId, rawData) { | ||
this.collectionId = collectionId | ||
this.collectionViewId = collectionViewId | ||
this.rawData = rawData | ||
this.schema = rawData.recordMap.collection[collectionId].value.schema | ||
this.total = rawData.result.total | ||
this.value = rawData.recordMap.collection[collectionId].value | ||
this.props = Object.entries(this.schema).map(item => { | ||
let [key, v] = item | ||
return v.name | ||
}) | ||
this.props.push('_raw') | ||
this.propsKeyMap = {} | ||
Object.entries(this.schema).map(item => { | ||
let [key, v] = item | ||
this.propsKeyMap[v.name] = { | ||
key, | ||
...v | ||
} | ||
}) | ||
// 缓存 | ||
// blockStore = { ...blockStore, ...rawData.recordMap.block } | ||
} | ||
makeRow(rowBlockId, schema) { | ||
let rowData | ||
try { | ||
rowData = blockStore[rowBlockId].value | ||
} catch (err) { | ||
// console.log(blockStore, rowBlockId) | ||
} | ||
let props = Object.entries(schema).map(item => { | ||
let [key, v] = item | ||
return v.name | ||
}) | ||
props.push('_raw') | ||
let propsKeyMap = {} | ||
Object.entries(schema).map(item => { | ||
let [key, v] = item | ||
propsKeyMap[v.name] = { | ||
key, | ||
...v | ||
} | ||
}) | ||
if (rowData) { | ||
let handlers = { | ||
get: (target, property) => { | ||
if (props.indexOf(property) > -1) { | ||
if (property === '_raw') { | ||
return target | ||
} else { | ||
const { key, type, collection_id } = propsKeyMap[property] | ||
let res | ||
let rawValue = target.properties ? target.properties[key] : false | ||
if (rawValue) { | ||
switch (type) { | ||
case 'title': | ||
case 'url': | ||
case 'number': | ||
res = rawValue[0][0] | ||
break | ||
case 'date': | ||
res = rawValue[0][0][0][1][0][1].start_date | ||
break | ||
case 'multi_select': | ||
res = rawValue[0][0].split(',') | ||
break | ||
case 'file': | ||
res = rawValue.filter(item => { | ||
let content = item[1] | ||
return Boolean(content) | ||
}).map(item => { | ||
return item[1][0][1] | ||
}) | ||
break | ||
case 'relation': | ||
res = rawValue.filter(item => item.length > 1).map(item => { | ||
let _schema = collectionSchemaStore[collection_id] | ||
let _blockId = item[1][0][1] | ||
return this.makeRow(_blockId, _schema) | ||
}) | ||
break | ||
case 'rollup': | ||
res = rawValue.filter(item => item.length > 1).map(item => item[1][0]) | ||
break | ||
default: | ||
res = rawValue | ||
} | ||
} | ||
return res | ||
// if (res instanceof Array && res.length === 1) { | ||
// return res[0] | ||
// } else { | ||
// return res | ||
// } | ||
} | ||
} else { | ||
return undefined | ||
} | ||
} | ||
} | ||
let proxy = new Proxy(rowData, handlers) | ||
proxy.toString = Function.prototype.toString.bind(rowData) | ||
return proxy | ||
} | ||
} | ||
get rows() { | ||
const { blockIds } = this.rawData.result | ||
return blockIds.map(blockId => this.makeRow(blockId, this.schema)) | ||
} | ||
} | ||
module.exports = Notabase | ||
} |
@@ -0,0 +0,0 @@ const Notabase = require('./src/notabase') |
Sorry, the diff of this file is not supported yet
24429
11
483
48