Skip to content

Commit

Permalink
Support Media Block Types, Page Icons, Page Covers, and File Properti…
Browse files Browse the repository at this point in the history
…es (#178)

* Add support for page icons and covers, page file properties, and new URL based block types.

* Bump version to 0.3.1
  • Loading branch information
rhart92 authored Aug 25, 2021
1 parent 3dfa495 commit 4e21745
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/database-email-update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@sendgrid/mail": "^7.4.5",
"dotenv": "^10.0.0",
"@notionhq/client": "^0.3.0"
"@notionhq/client": "^0.3.1"
},
"author": "Aman Gupta",
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion examples/notion-github-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Aman Gupta",
"license": "MIT",
"dependencies": {
"@notionhq/client": "^0.3.0",
"@notionhq/client": "^0.3.1",
"dotenv": "^10.0.0",
"lodash": "^4.17.21",
"octokit": "^1.1.0"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@notionhq/client",
"version": "0.3.0",
"version": "0.3.1",
"description": "A simple and easy to use client for the Notion API",
"engines": {
"node": ">=12"
Expand Down
41 changes: 35 additions & 6 deletions src/api-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {
RichTextInput,
UpdateBlock,
UpdatePropertySchema,
ExternalFileInput,
EmojiInput,
FileInput,
} from "./api-types"
import { DistributiveOmit } from "./type-utils"

// TODO: type assertions to verify that each interface is synchronized to the list of keys in the runtime value below.

Expand Down Expand Up @@ -228,10 +232,29 @@ export type PropertyValueMap = { [propertyName: string]: PropertyValue }
export type InputPropertyValueMap = {
[propertyName: string]: InputPropertyValue
}

export type PageIcon = FileInput | ExternalFileInput | EmojiInput

export type PageIconInput =
| (DistributiveOmit<Exclude<PageIcon, FileInput>, "type"> & {
type?: string
})
| null

export type PageCover = FileInput | ExternalFileInput

export type PageCoverInput =
| (DistributiveOmit<Exclude<PageCover, FileInput>, "type"> & {
type?: string
})
| null

interface PagesCreateBodyParameters {
parent: ParentInput
properties: InputPropertyValueMap
children?: Block[]
icon?: PageIconInput
cover?: PageCoverInput
}

export interface PagesCreateParameters
Expand All @@ -244,7 +267,7 @@ export const pagesCreate = {
method: "post",
pathParams: [],
queryParams: [],
bodyParams: ["parent", "properties", "children"],
bodyParams: ["parent", "properties", "children", "icon", "cover"],
path: () => `pages`,
} as const

Expand All @@ -262,6 +285,8 @@ interface DatabasesCreateBodyParameters {
parent: ParentPageInput
properties: InputPropertySchemaMap
title?: RichTextInput[]
icon?: PageIconInput
cover?: PageCoverInput
}

export interface DatabasesCreateParameters
Expand All @@ -274,7 +299,7 @@ export const databasesCreate = {
method: "post",
pathParams: [],
queryParams: [],
bodyParams: ["parent", "properties", "title"],
bodyParams: ["parent", "properties", "title", "icon", "cover"],
path: () => `databases`,
} as const

Expand All @@ -293,6 +318,8 @@ export type UpdatePropertySchemaMap = {
interface DatabasesUpdateBodyParameters {
properties?: UpdatePropertySchemaMap
title?: RichTextInput[]
icon?: PageIconInput
cover?: PageCoverInput
}

export interface DatabasesUpdateParameters
Expand All @@ -305,7 +332,7 @@ export const databasesUpdate = {
method: "patch",
pathParams: ["database_id"],
queryParams: [],
bodyParams: ["properties", "title"],
bodyParams: ["properties", "title", "icon", "cover"],
path: (d: DatabasesUpdatePathParameters) => `databases/${d.database_id}`,
} as const

Expand Down Expand Up @@ -345,22 +372,24 @@ interface PagesUpdateQueryParameters {}
interface PagesUpdateBodyArchiveParameter {
archived: boolean
}
interface PagesUpdateBodyPropertiesParameters {
interface PagesUpdateBodyParameters {
properties: InputPropertyValueMap
icon?: PageIconInput
cover?: PageCoverInput
}

export interface PagesUpdateParameters
extends PagesUpdatePathParameters,
PagesUpdateQueryParameters,
PagesUpdateBodyArchiveParameter,
PagesUpdateBodyPropertiesParameters {}
PagesUpdateBodyParameters {}
export interface PagesUpdateResponse extends Page {}

export const pagesUpdate = {
method: "patch",
pathParams: ["page_id"],
queryParams: [],
bodyParams: ["archived", "properties"],
bodyParams: ["archived", "properties", "cover", "icon"],
path: (p: PagesUpdatePathParameters) => `pages/${p.page_id}`,
} as const

Expand Down
107 changes: 104 additions & 3 deletions src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

import { PropertyValueMap } from "./api-endpoints"
import { DistributiveExtend, DistributiveOmit, RequiredBy } from "./type-utils"
import {
DistributiveExtend,
DistributiveOmit,
PartialBy,
RequiredBy,
} from "./type-utils"

/*
* Pagination
Expand Down Expand Up @@ -49,6 +54,12 @@ export type Block =
| ToDoBlock
| ToggleBlock
| ChildPageBlock
| EmbedBlock
| ImageBlock
| VideoBlock
| FileBlock
| PDFBlock
| AudioBlock
| UnsupportedBlock

export interface BlockBase {
Expand Down Expand Up @@ -124,6 +135,47 @@ export interface ChildPageBlock extends BlockBase {
child_page: { title: string }
}

export interface EmbedBlock extends BlockBase {
type: "embed"
embed: {
url: string
caption?: RichText[]
}
}

export interface ExternalFileWithCaption extends ExternalFile {
caption?: RichText[]
}

export interface FileWithCaption extends File {
caption?: RichText[]
}

export interface ImageBlock extends BlockBase {
type: "image"
image: ExternalFileWithCaption | FileWithCaption
}

export interface VideoBlock extends BlockBase {
type: "video"
video: ExternalFileWithCaption | FileWithCaption
}

export interface FileBlock extends BlockBase {
type: "file"
file: ExternalFileWithCaption | FileWithCaption
}

export interface PDFBlock extends BlockBase {
type: "pdf"
pdf: ExternalFileWithCaption | FileWithCaption
}

export interface AudioBlock extends BlockBase {
type: "audio"
audio: ExternalFileWithCaption | FileWithCaption
}

export interface UnsupportedBlock extends BlockBase {
type: "unsupported"
}
Expand All @@ -139,6 +191,8 @@ export interface Database {
created_time: string
last_edited_time: string
title: RichText[]
icon: File | ExternalFile | Emoji | null
cover: File | ExternalFile | null
properties: { [propertyName: string]: Property }
}

Expand Down Expand Up @@ -565,6 +619,8 @@ export interface Page {
created_time: string
last_edited_time: string
archived: boolean
icon: File | ExternalFile | Emoji | null
cover: File | ExternalFile | null
properties: PropertyValueMap
url: string
}
Expand Down Expand Up @@ -686,7 +742,7 @@ export type InputPropertyValueWithRequiredId =
| FormulaPropertyValue
| RollupPropertyValue
| PeoplePropertyValue
| FilesPropertyValue
| FilesPropertyInputValue
| CheckboxPropertyValue
| URLPropertyValue
| EmailPropertyValue
Expand Down Expand Up @@ -793,9 +849,23 @@ export interface PeoplePropertyValue extends PropertyValueBase {
people: User[]
}

type FileWithName = {
name: string
} & (File | ExternalFile)

type ExternalFileWithName = {
name: string
} & ExternalFile

export interface FilesPropertyValue extends PropertyValueBase {
type: "files"
files: { name: string }[]
files: FileWithName[]
}

// When writing, we don't allow S3 hosted files
export interface FilesPropertyInputValue extends PropertyValueBase {
type: "files"
files: ExternalFileWithName[]
}

export interface CheckboxPropertyValue extends PropertyValueBase {
Expand Down Expand Up @@ -1064,3 +1134,34 @@ export type UpdatePropertySchema =
| UpdateMultiSelectPropertySchema
| RenamePropertySchema
| null

/*
* Files
*/

interface FileBase {
type: "file"
file: {
url: string
}
}

export type File = FileBase & { file: { expiry_time: string } }

export type FileInput = PartialBy<FileBase, "type">

export interface ExternalFile {
type: "external"
external: {
url: string
}
}

export type ExternalFileInput = PartialBy<ExternalFile, "type">

export interface Emoji {
type: "emoji"
emoji: string
}

export type EmojiInput = PartialBy<Emoji, "type">

0 comments on commit 4e21745

Please sign in to comment.