Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: move to tsdx #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
"extends": [
"react-app",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"settings": {
"react": {
"version": "999.999.999"
}
}
}
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI
on: [push]
jobs:
build:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: Install deps and build (with cache)
uses: bahmutov/npm-install@v1

- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: size
on: [pull_request]
jobs:
size:
runs-on: ubuntu-latest
env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.log
.DS_Store
node_modules
.env
dist
dist
coverage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Tim Raderschad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
195 changes: 58 additions & 137 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,182 +1,103 @@
# Unofficial Notion.so API Wrapper (WIP)
# TSDX User Guide

![npm](https://img.shields.io/npm/v/notion-api-js.svg)
![npm bundle size](https://img.shields.io/bundlephobia/min/notion-api-js.svg)
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.

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.
> This TSDX setup is meant for developing libraries (not apps!) that can be published to NPM. If you’re looking to build a Node app, you could use `ts-node-dev`, plain `ts-node`, or simple `tsc`.

**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.
> If you’re new to TypeScript, checkout [this handy cheatsheet](https://devhints.io/typescript)

# Documentation
## Commands

- [Installation](#Installation)
- [Dependencies](#Dependencies)
- [Usage](#Usage)
- [Obtaining Credentials](#Obtaining-Credentials)
- [Instance Methods](#Instance-Methods)
- [Disclaimer](#Disclaimer)
TSDX scaffolds your new library inside `/src`.

# Installation
To run TSDX, use:

You can either use `npm ` or `yarn ` to install it:

```
npm i --save notion-api-js
```

```
yarn add notion-api-js
```

# Usage

## Creating an instance

To create an instance, simply pass an object with the token you read from the cookie:

```js
// ES Modules syntax
import Notion from "notion-api-js";

// require syntax
const Notion = require("notion-api-js").default;

const notion = new Notion({
token: "YOUR_TOKEN_V2"
});
```

You can also provide options for the HTML parsing:

```js
const notion = new Notion({
token: "YOUR_TOKEN_V2",
options: {
colors: {
orange: 'CSS COLOR HERE'
},
pageUrl: 'ABSOLUTE PAGE URL (e.g. /posts/'),
}
});
```bash
npm start # or yarn start
```

# Obtaining Credentials
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.

Right now there is no official way of accessing the Notion API but there is a little work-around to get your credentials.
To do a one-off build, use `npm run build` or `yarn build`.

## Prerequisites
To run tests, use `npm test` or `yarn test`.

You need to have an account on [Notion.so](https://notion.so/) and need to be logged in.
## Configuration

## Getting your credentials
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.

Most of the modern web browsers support inspecting cookies visually using the browser's devtools.
You can read how to do it in your browser here:
### Jest

- [Chrome](https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies)
- [Firefox](https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector)
Jest tests are set up to run with `npm test` or `yarn test`.

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.
### Bundle Analysis

# Instance Options
[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of your library with `npm run size` and visualize the bundle with `npm run analyze`.

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.
#### Setup Files

### Colors (Object)
This is the folder structure we set up for you:

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:

```js
options: {
colors: {
red: 'tomato',
blue: 'rgb(100, 149, 237)',
purple: '#9933cc',
}
}
```txt
/src
index.tsx # EDIT THIS
/test
blah.test.tsx # EDIT THIS
.gitignore
package.json
README.md # EDIT THIS
tsconfig.json
```

Possible colors are:

- red
- brown
- orange
- yellow
- teal
- blue
- purple
- pink

### PageUrl (String)

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 in `<a href="/page?id=SOME_ID">Hello World</a>`

# Instance Methods

- [getPages](<#getPages()>)
- [getPageById](<#getPageById(pageId)>)
- [getPagesByIndexId](<#getPagesByIndexId(pageId)>)
- [getAllHTML](<#getAllHTML()>)
### Rollup

## getPages()
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.

Gets all pages of the user by the userId passed to the `Notion ` instance. All pages are parsed to HTML.
### TypeScript

**Example**
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.

```js
notion.getPages().then(pages => {
// Your Code here
});
```
## Continuous Integration

## getPageById(pageId)
### GitHub Actions

Gets a Notion page by the pageId and returns the parsed HTML.
Two actions are added by default:

**Parameters**:
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)

| **Parameter** | **Type** | **Opt/Required** |
| ------------- | -------- | ---------------- |
| pageId | string | Required |
## Optimizations

**Example**
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:

```js
notion.getPageById("pageId").then(page => {
// Your code here
});
// ./types/index.d.ts
declare var __DEV__: boolean;

// inside your code...
if (__DEV__) {
console.log('foo');
}
```

## getPagesByIndexId(pageId)
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.

Gets a Notion page by the given pageId and all subpages of that page. Useful if you want to use a homepage.
## Module Formats

**Parameters**:
CJS, ESModules, and UMD module formats are supported.

| **Parameter** | **Type** | **Opt/Required** |
| ------------- | -------- | ---------------- |
| pageId | string | Required |
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.

**Example**
## Named Exports

```js
notion.getPagesByIndexId("pageId").then(page => {
// Your code here
});
```
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.

## getAllHTML() [WIP]
## Including Styles

Gets the HTML for all pages.
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.

```js
notion.getAllHTML().then(html => {
// Your Ccode here
});
```
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.

# Disclaimer
## Publishing to NPM

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 😄
We recommend using [np](https://github.com/sindresorhus/np).
24 changes: 0 additions & 24 deletions index.ts

This file was deleted.

38 changes: 0 additions & 38 deletions package-lock.json

This file was deleted.

Loading