notion-api-js
Advanced tools
Comparing version 2.3.0 to 2.3.4
@@ -0,1 +1,2 @@ | ||
import { NotionResponse } from "./types"; | ||
declare function request({ endpoint, creds: { token }, body }: { | ||
@@ -6,4 +7,7 @@ endpoint: string; | ||
}; | ||
body?: object; | ||
}): Promise<any>; | ||
body?: { | ||
limit?: number; | ||
pageId: string; | ||
}; | ||
}): Promise<NotionResponse>; | ||
export default request; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const node_fetch_1 = require("node-fetch"); | ||
const BASEURL = "https://www.notion.so/api/v3/"; | ||
function request({ endpoint, creds: { token }, body }) { | ||
return node_fetch_1.default(`${BASEURL}${endpoint}`, { | ||
const getAllBlocks = ({ url, token, limit, stack, chunkNumber, res, resolve, reject, body, }) => __awaiter(this, void 0, void 0, function* () { | ||
return node_fetch_1.default(url, { | ||
headers: { | ||
@@ -13,8 +21,52 @@ accept: "*/*", | ||
}, | ||
body: JSON.stringify(Object.assign({}, body, { limit: 50, cursor: { stack: [] }, chunkNumber: 0, verticalColumns: false })), | ||
body: JSON.stringify(Object.assign({ limit, cursor: { stack }, chunkNumber }, body, { verticalColumns: false })), | ||
method: "POST" | ||
}) | ||
.then(response => response.json()) | ||
.then(r => { | ||
if (((r.cursor || {}).stack || {}).length) { | ||
getAllBlocks({ | ||
url, token, limit, stack: r.cursor.stack, | ||
chunkNumber: chunkNumber + 1, | ||
res: { | ||
recordMap: { | ||
block: Object.assign({}, res.recordMap.block, r.recordMap.block) | ||
} | ||
}, | ||
resolve, | ||
reject, | ||
body | ||
}); | ||
} | ||
else { | ||
if (r.errorId) { | ||
reject(r); | ||
} | ||
const ret = { | ||
recordMap: { | ||
block: Object.assign({}, res.recordMap.block, (r.recordMap || {}).block) | ||
} | ||
}; | ||
resolve(ret); | ||
} | ||
}) | ||
.catch((error) => console.error(error)); | ||
}); | ||
function request({ endpoint, creds: { token }, body }) { | ||
return new Promise((resolve, reject) => { | ||
getAllBlocks({ | ||
url: `${BASEURL}${endpoint}`, | ||
token, | ||
limit: (body || { limit: 50 }).limit || 50, | ||
stack: [], | ||
chunkNumber: 0, | ||
res: { | ||
recordMap: { block: {} } | ||
}, | ||
resolve, | ||
reject, | ||
body | ||
}); | ||
}); | ||
} | ||
exports.default = request; |
@@ -1,5 +0,5 @@ | ||
import { NotionObject, Options, PageDTO } from './types'; | ||
declare function toHTMLPage(ObjectList: Array<NotionObject>, options: Options): PageDTO; | ||
import { NotionObject, Options, PageDTO, formatter } from './types'; | ||
declare function toHTMLPage(ObjectList: Array<NotionObject>, options: Options, htmlFormatter?: formatter): PageDTO; | ||
export declare function handleNotionError(err: Error): void; | ||
export declare function isNotionID(id: string): boolean; | ||
export default toHTMLPage; |
@@ -14,3 +14,4 @@ "use strict"; | ||
numbered_list: 'ol', | ||
bulleted_list: 'ul' | ||
bulleted_list: 'ul', | ||
image: 'img' | ||
}; | ||
@@ -26,2 +27,4 @@ function formatToHtml(ObjectToParse, options, index) { | ||
properties.title[0][0].replace(/\[.*\]:.{1,}/, ''); | ||
const source = properties && | ||
properties.source; | ||
const tags = (content && content[0] ? content[0][0] : '').match(/\[.{1,}\]: .{1,}/); | ||
@@ -38,3 +41,3 @@ const attrib = tags && tags[0].replace(/(\[|\])/g, '').split(':'); | ||
const style = color ? ` ${property}` : ''; | ||
if (!content && type !== 'divider') { | ||
if (!content && type !== 'divider' && !source) { | ||
type = 'break'; | ||
@@ -59,2 +62,5 @@ } | ||
} | ||
case types.image: { | ||
return `<${types.image}${style} src="${source}" />`; | ||
} | ||
default: { | ||
@@ -67,3 +73,3 @@ if (types[type]) | ||
} | ||
function formatList(ObjectList, options) { | ||
function formatList(ObjectList, options, htmlFormatter) { | ||
const items = []; | ||
@@ -73,3 +79,9 @@ const attributes = {}; | ||
const element = ObjectList[index]; | ||
let html = formatToHtml(element, options, index); | ||
let html; | ||
if (htmlFormatter) { | ||
html = htmlFormatter(element, options, index, ObjectList); | ||
} | ||
else { | ||
html = formatToHtml(element, options, index); | ||
} | ||
if (html && typeof html === 'object') { | ||
@@ -98,3 +110,3 @@ const keys = Object.keys(html); | ||
const { format, properties } = ObjectList[0]; | ||
const title = (properties && properties.title[0][0]) || ''; | ||
const title = (properties && properties.title && properties.title[0][0]) || ''; | ||
const cover = format && format.page_cover | ||
@@ -118,4 +130,4 @@ ? format.page_cover.includes('http') | ||
} | ||
function toHTMLPage(ObjectList, options) { | ||
const { items, attributes } = formatList(ObjectList, options); | ||
function toHTMLPage(ObjectList, options, htmlFormatter) { | ||
const { items, attributes } = formatList(ObjectList, options, htmlFormatter); | ||
const elementsString = items.join(''); | ||
@@ -122,0 +134,0 @@ return { |
@@ -5,3 +5,4 @@ export declare type NotionObject = { | ||
properties: { | ||
title: Array<Array<string>>; | ||
source?: Array<Array<string>>; | ||
title?: Array<Array<string>>; | ||
}; | ||
@@ -14,2 +15,3 @@ format: { | ||
}; | ||
content: Array<string>; | ||
}; | ||
@@ -45,1 +47,5 @@ export declare type NotionResponse = { | ||
} | ||
export declare type htmlResponse = string | { | ||
[x: string]: string; | ||
} | null; | ||
export declare type formatter = ((ObjectToParse: NotionObject, options: Options, index: number, ObjectList: Array<NotionObject>) => htmlResponse); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,2 +0,2 @@ | ||
import { Options, PageDTO } from './lib/types'; | ||
import { Options, PageDTO, formatter } from './lib/types'; | ||
declare class Notion { | ||
@@ -12,3 +12,3 @@ creds: { | ||
getPages(): Promise<string[]>; | ||
getPageById(pageId: string): Promise<PageDTO>; | ||
getPageById(pageId: string, htmlFormatter?: formatter, limit?: number): Promise<PageDTO>; | ||
getPagesByIndexId(startingPageId: string): Promise<PageDTO[]>; | ||
@@ -15,0 +15,0 @@ getAllHTML(): Promise<PageDTO[]>; |
@@ -38,7 +38,7 @@ "use strict"; | ||
} | ||
getPageById(pageId) { | ||
getPageById(pageId, htmlFormatter, limit) { | ||
return fetch_1.default({ | ||
endpoint: 'loadPageChunk', | ||
creds: this.creds, | ||
body: { pageId } | ||
body: { pageId, limit } | ||
}) | ||
@@ -48,6 +48,6 @@ .then((r) => { | ||
const values = Object.values(entries).map(value => { | ||
const { id, type, properties, format } = value.value; | ||
return { id, type, properties, format }; | ||
const { id, type, properties, format, content } = value.value; | ||
return { id, type, properties, format, content }; | ||
}); | ||
return helpers_1.default(values, this.options); | ||
return helpers_1.default(values, this.options, htmlFormatter); | ||
}) | ||
@@ -54,0 +54,0 @@ .catch((e) => { |
{ | ||
"name": "notion-api-js", | ||
"version": "2.3.0", | ||
"version": "2.3.4", | ||
"description": "", | ||
"repository": "https://github.com/Frexeptabel/notion-api/", | ||
"main": "dist/notion.js", | ||
"types": "dist/index.d.ts", | ||
"types": "dist/notion.d.ts", | ||
"scripts": { | ||
@@ -9,0 +9,0 @@ "prepublish": "npm run build", |
@@ -1,2 +0,2 @@ | ||
# Unofficial Notion.so Wrapper (WIP) | ||
# Unofficial Notion.so API Wrapper (WIP) | ||
@@ -6,5 +6,5 @@ ![npm](https://img.shields.io/npm/v/notion-api-js.svg) | ||
This Repository contains an unofficial port of the [Notion](https://notion.so) to Node.js. **Important**: It only works using Node.js in backend and not in a client-side environment. | ||
This repository contains an unofficial port of the [Notion](https://notion.so) API to Node.js. **Important**: It only works in the backend using Node.js and not in a client-side environment. | ||
**IMPORTANT**: You need a token to use the Notion API. You can obtain them by reading you local cookie. You can find instructions for that | ||
**IMPORTANT**: You need a token to use the Notion API. You can obtain one by reading your local cookie. You can find instructions for that below. | ||
@@ -22,3 +22,3 @@ # Documentation | ||
You can either use npm or yarn to install it: | ||
You can either use `npm ` or `yarn ` to install it: | ||
@@ -37,3 +37,3 @@ ``` | ||
To create an instance, simply pass an Object with your notion `token_v2`. | ||
To create an instance, simply pass an object with the token you read from the cookie: | ||
@@ -52,3 +52,3 @@ ```js | ||
You can also provide options for the HTML parsing. | ||
You can also provide options for the HTML parsing: | ||
@@ -69,7 +69,7 @@ ```js | ||
Right now there is no official way how to access the Notion API, but there is a little work-around to get your credentials. | ||
Right now there is no official way of accessing the Notion API but there is a little work-around to get your credentials. | ||
## Prerequisites | ||
You need to have an Account on Notion.so and need to be logged in. | ||
You need to have an account on [Notion.so](https://notion.so/) and need to be logged in. | ||
@@ -84,11 +84,11 @@ ## Getting your credentials | ||
After you found the Notion.so cookie, look for `token_v2`. It is the necessary credential for the `Notion` instance. Simply copy them when you create the instance | ||
After you found the Notion.so cookie, look for an entry called `token_v2`. It is the necessary credential for the `Notion` instance. Simply copy it into your code when you create the instance. | ||
# Instance Options | ||
The Options are optionally passed to the instance as a parameter. Those options contain information on how the HTML will be parsed and returned using the instance methods. | ||
The options are optionally passed to the instance as a parameter. Those options contain information on how the HTML will be parsed and returned using the instance methods. | ||
### Colors (Object) | ||
Contains definitions for the colors. If this option is omitted the default html colors like orange, pink and blue are used. You can change this behaviour by passing an object containing colour definitions. Example: | ||
Contains definitions for the colors. If this option is omitted the default HTML colors like orange, pink and blue are used. You can change this behavior by passing an object containing color definitions. Example: | ||
@@ -119,3 +119,3 @@ ```js | ||
The PageUrl is the string passed to the `<a>` tag and is used to build the href of it. The id is inserted after the passed string. | ||
By default it looks like this `/page?id=`, which results into `<a href="/page?id=SOME_ID">Hello World</a>` | ||
By default it looks like this `/page?id=`, which results in `<a href="/page?id=SOME_ID">Hello World</a>` | ||
@@ -131,3 +131,3 @@ # Instance Methods | ||
Gets all pages of the user by the userId passed to the Notion instance. All pages are parsed to HTML. | ||
Gets all pages of the user by the userId passed to the `Notion ` instance. All pages are parsed to HTML. | ||
@@ -144,3 +144,3 @@ **Example** | ||
Gets a Notion page by the pageId and returns it parsed to HTML. | ||
Gets a Notion page by the pageId and returns the parsed HTML. | ||
@@ -156,4 +156,4 @@ **Parameters**: | ||
```js | ||
notion.getPageById("pageID").then(page => { | ||
// Your Code here | ||
notion.getPageById("pageId").then(page => { | ||
// Your code here | ||
}); | ||
@@ -164,3 +164,3 @@ ``` | ||
Gets a Notion page by the given Id and all subpages of that page. Useful if you want to use a homepage. | ||
Gets a Notion page by the given pageId and all subpages of that page. Useful if you want to use a homepage. | ||
@@ -176,4 +176,4 @@ **Parameters**: | ||
```js | ||
notion.getPagesByIndexId("pageID").then(page => { | ||
// Your Code here | ||
notion.getPagesByIndexId("pageId").then(page => { | ||
// Your code here | ||
}); | ||
@@ -188,3 +188,3 @@ ``` | ||
notion.getAllHTML().then(html => { | ||
// Your Code here | ||
// Your Ccode here | ||
}); | ||
@@ -195,2 +195,2 @@ ``` | ||
It's really WIP right now but I would highly appreciate if you would like to contribute to the project. Just fork this repo and create a PR 😄 | ||
It's really WIP right now but I would highly appreciate if you would like to contribute to the project. Just fork this repository and create a PR 😄 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18847
387