npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

notion-toolbox

v0.0.1

Published

Various tools to easily manipulate Notion public API in Typescript

Downloads

3

Readme

Notion toolbox

Various tools to easily manipulate Notion's API, to be used with Notion JS SDK, in Typescript.

🔥 Not yet production-ready 🔥

Payload builders

Easily create type coherent payloads for the complex queries.
First, configure your database schema by associating column names with their types.

const MY_DB_SCHEMA = {
  Name: 'title',
  Tags: 'multi_select',
  'Other column': 'select',
};

You can then pass it to the different builders depending on your use case:

import { buildPageCreate } from 'notion-toolbox';

// `payload` can be used to call Notion SDK's `client.pages.create()`
const payload = buildPageCreate(MY_DB_SCHEMA, 'my-database-id', {
  Name: 'My new page',
  Tags: ['Tag 1', 'Tag 2'],
  'Other column': 'Other value',
});

Or even using the full power of Typescript:

import { buildPageCreate, PropertyType } from 'notion-toolbox';

enum MyDbColumn {
  Name = 'Name',
  Tags = 'Tags',
  OtherColumn = 'Other column',
}

const MY_DB_SCHEMA: Record<MyDbColumn, PropertyType> = {
  [MyDbColumn.Name]: 'title',
  [MyDbColumn.Tags]: 'multi_select',
  [MyDbColumn.OtherColumn]: 'select',
};

const payload = buildPageCreate<MyDbColumn>(MY_DB_SCHEMA, 'my-database-id', {
  [MyDbColumn.Name]: 'My new page',
  [MyDbColumn.Tags]: ['Tag 1', 'Tag 2'],
  [MyDbColumn.OtherColumn]: 'Other value',
});

buildDatabaseQuery

Build the payload for Notion's Database Query endpoint

const payload = buildDatabaseQuery<MyDbColumn>(MY_DB_SCHEMA, {
  [MyDbColumn.Tags]: 'Tag 1',
});
await notion.databases.query(payload);

The builder is going to use the condition operator that makes more sense by default, but you can supply the one you want.

const payload = buildDatabaseQuery<MyDbColumn>(MY_DB_SCHEMA, {
  [MyDbColumn.Tags]: { is_empty: true },
});
await notion.databases.query(payload);

You can also filter with the or operand, and combine complex queries.

const payload = buildDatabaseQuery<MyDbColumn>(MY_DB_SCHEMA, {
  or: [
    { [MyDbColumn.Tags]: { is_empty: true } },
    {
      and: [{ [MyDbColumn.Tags]: 'Tag 1' }, { [MyDbColumn.CreatedBy]: 'me' }],
    },
  ],
});
await notion.databases.query(payload);

buildPageCreate

Build the payload for Notion's Page Creation endpoint

const payload = buildPageCreate<MyDbColumn>(MY_DB_SCHEMA, MY_DB_ID, {
  [MyDbColumn.Name]: 'My new page',
  [MyDbColumn.Tags]: ['Tag 1', 'Tag 2'],
  [MyDbColumn.OtherColumn]: 'Other value',
});
await notionClient.pages.create(payload);

buildPageUpdate

Build the payload for Notion's Page Update endpoint

const payload = buildPageUpdate<MyDbColumn>(MY_DB_SCHEMA, MY_PAGE_ID, {
  [MyDbColumn.Name]: 'My other page',
});
await notionClient.pages.update(payload);

Tools

readPageProperty

When a page is retrieved, use this tool to read one of it's property.

// `myPage` was retrieved from a previous API call
const propertyValues: string[] = readPageProperty<MyDbColumn>(
  myPage,
  MyDbColumn.Tags,
  MY_DB_SCHEMA
);

To do

Builders

  • [ ] sorting for the database_query builder
  • [ ] possibility to provide content to the page_create builder
  • [ ] new block_create_children builder
  • [ ] new block_delete builder
  • [ ] new block_update builder
  • [ ] new database_create builder
  • [ ] new database_update builder
  • [ ] new search builder

Tools

  • [ ] tests for the read_page_property tool
  • [ ] new duplicate_pages tool that duplicates a set of pages, their attributes and content

Wrappers

Instead of always providing builders with the schema of the databases we manipulate and the available properties, why not configuring an object that would provide all the builders from the get go? Something with an API along the lines of

const myDbWrapper = new DbWrapper<MyDbColumn>('my-database-id', MY_DB_SCHEMA);
const payload = myDbWrapper.buildPageCreate({
  [MyDbColumn.Name]: 'My new page',
  [MyDbColumn.Tags]: ['Tag 1', 'Tag 2'],
  [MyDbColumn.OtherColumn]: 'Other value',
});

and even

// `myPage` was retrieved from a previous API call
const myPageWrapper = myDbWrapper.page(myPage);
const propertyValues = myPageWrapper.getProperty(MyDbColumn.Tags);

License

MIT