Skip to content

Commit

Permalink
feat(notion utils): add support for notion relation fields
Browse files Browse the repository at this point in the history
enhancement #1
  • Loading branch information
andrewvo89 committed Jan 18, 2022
1 parent 9d56a0c commit a79825c
Show file tree
Hide file tree
Showing 40 changed files with 365 additions and 463 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
],
"no-duplicate-imports": "error",
"object-shorthand": "error",
"sort-imports": [
"error",
{
"allowSeparatedGroups": false,
"ignoreCase": false,
"ignoreDeclarationSort": false,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": [
"none",
"all",
"multiple",
"single"
]
}
],
"spaced-comment": "error"
}
}
9 changes: 5 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"printWidth": 120,
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"bracketSpacing": true
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

## 🧐 About

NotionDB is an object modelling tool designed to interface with [Notion](https://www.notion.so/). It is a tool to allow developers to easily spin up a cloud based database using [Notion Databases](https://www.notion.so/Intro-to-databases-fd8cd2d212f74c50954c11086d85997e) Notion Databases consist of a series of Notion Pages (database rows) with various Properties (database columns).
NotionDB is an object modelling tool designed to interface with [Notion](https://www.notion.so/). It is a tool to allow developers to easily spin up a cloud based database using [Notion Databases](https://www.notion.so/Intro-to-databases-fd8cd2d212f74c50954c11086d85997e). Notion Databases consist of a series of Notion Pages (database rows) with various Properties (database columns).

NotionDB leverages off the official [Notion API](https://developers.notion.com/) and provides developers with easy to use classes and helper functions to easily retrieve, write, update and delete data.

Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "notiondb",
"license": "MIT",
"author": "Andrew Vo-Nguyen <hello@andrewvo.co> (https://andrewvo.co)",
"version": "1.0.10",
"version": "1.0.11",
"description": "NotionDB is an object modelling tool designed to interface with Notion Databases. It is a tool to allow developers to easily spin up a cloud based database using Notion Databases, consisting of a series of Notion Pages (database rows) with various Properties (database columns).",
"keywords": [
"notion",
Expand All @@ -17,6 +17,7 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc",
"commit": "cz",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"prepare": "yarn run build",
Expand All @@ -31,10 +32,12 @@
"devDependencies": {
"@types/node": "^16.4.1",
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.3.2",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.3.5"
},
"files": [
Expand Down
16 changes: 16 additions & 0 deletions src/axios/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AxiosInstance } from 'axios';

let axios: AxiosInstance | undefined;

function setAxiosInstance(instance: AxiosInstance): void {
axios = instance;
}

function getAxiosInstance(): AxiosInstance {
if (!axios) {
throw new Error('Axios instance has not been instantiated yet');
}
return axios;
}

export { getAxiosInstance, setAxiosInstance };
27 changes: 9 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import axios, { AxiosInstance } from 'axios';
import { Database, NotionId, NotionUrl } from './models';

import { PropertySchema } from './schema';
import axios from 'axios';
import { setAxiosInstance } from './axios';

/**
* Class representing a NotionDB.
* @class NotionDB
*/
class NotionDB {
#axiosInstance: AxiosInstance;
/**
* Creates an instance of NotionDB with an integration key.
* An Integration needs to be created at https://www.notion.so/my-integrations.
* @param {string} integrationToken
* @memberof NotionDB
*/
constructor(integrationToken: string) {
const axiosInstance = axios.create({
const instance = axios.create({
baseURL: 'https://api.notion.com/v1',
headers: {
'Notion-Version': '2021-05-13',
Authorization: integrationToken,
},
});
this.#axiosInstance = axiosInstance;
setAxiosInstance(instance);
}

/**
Expand All @@ -32,20 +33,10 @@ class NotionDB {
*/
get databases() {
return {
get: (identifier: NotionUrl | NotionId) =>
Database.get(identifier, this.#axiosInstance),
getAll: () => Database.getAll(this.#axiosInstance),
create: (
parentPageIdentifier: NotionUrl | NotionId,
title: string,
schema: PropertySchema,
) =>
Database.create(
parentPageIdentifier,
title,
schema,
this.#axiosInstance,
),
get: (identifier: NotionUrl | NotionId) => Database.get(identifier),
getAll: () => Database.getAll(),
create: (parentPageIdentifier: NotionUrl | NotionId, title: string, schema: PropertySchema) =>
Database.create(parentPageIdentifier, title, schema),
};
}
}
Expand Down
29 changes: 10 additions & 19 deletions src/models/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AxiosInstance } from 'axios';
import { BACK_OFF_TIME, MAX_RETRIES } from '../../utils/api';
import { BlockObject, BlockResponse, BlockTypes } from '.';
import { NotionId, NotionUrl } from '../notion';

import { getAxiosInstance } from '../../axios';

/**
* Class representing a Block.
* @class Block
Expand All @@ -14,7 +15,6 @@ class Block {
#lastEditedTime: globalThis.Date;
#hasChildren: boolean;
#data: Record<string, any>;
#axiosInstance: AxiosInstance;

/**
* Creates an instance of Block.
Expand All @@ -24,7 +24,6 @@ class Block {
* @param {globalThis.Date} lastEditedTime
* @param {boolean} hasChildren
* @param {Record<string, any>} data
* @param {AxiosInstance} axiosInstance
* @memberof Block
*/
constructor(
Expand All @@ -34,15 +33,13 @@ class Block {
lastEditedTime: globalThis.Date,
hasChildren: boolean,
data: Record<string, any>,
axiosInstance: AxiosInstance,
) {
this.#id = id;
this.#type = type;
this.#createdTime = createdTime;
this.#lastEditedTime = lastEditedTime;
this.#hasChildren = hasChildren;
this.#data = data;
this.#axiosInstance = axiosInstance;
}

/**
Expand All @@ -66,27 +63,20 @@ class Block {
* Gets all Blocks from a parent Block Notion ID or Notion URL.
* @static
* @param {(NotionId | NotionUrl)} identifier
* @param {AxiosInstance} axiosInstance
* @return {*} {Promise<Block[]>}
* @memberof Block
*/
static async getAll(
identifier: NotionId | NotionUrl,
axiosInstance: AxiosInstance,
): Promise<Block[]> {
static async getAll(identifier: NotionId | NotionUrl): Promise<Block[]> {
const axiosInstance = getAxiosInstance();
const blockId = identifier.getId();
const blocks: Block[] = [];
let hasMore: boolean = false;
let nextCursor: string = '';
const nextCursorParam: string = hasMore
? `?start_cursor=${nextCursor}`
: '';
let hasMore = false;
let nextCursor = '';
const nextCursorParam: string = hasMore ? `?start_cursor=${nextCursor}` : '';
do {
let retries = 0;
try {
const response = await axiosInstance.get(
`/blocks/${blockId}/children${nextCursorParam}`,
);
const response = await axiosInstance.get(`/blocks/${blockId}/children${nextCursorParam}`);
const results = response.data.results as BlockResponse[];
blocks.push(
...results.map(
Expand All @@ -98,7 +88,6 @@ class Block {
new globalThis.Date(result.last_edited_time),
result.has_children,
result[result.type],
axiosInstance,
),
),
);
Expand All @@ -107,7 +96,9 @@ class Block {
nextCursor = response.data.next_cursor;
}
} catch (error) {
// @ts-ignore
if (!error.isAxiosError) {
// @ts-ignore
throw new Error(error);
}
if (retries === MAX_RETRIES) {
Expand Down
Loading

0 comments on commit a79825c

Please sign in to comment.