Skip to content

Commit d0cc5ae

Browse files
authored
Update endpoints to be fully typed. (makenotion#198)
1 parent 9e3b128 commit d0cc5ae

File tree

6 files changed

+30864
-1816
lines changed

6 files changed

+30864
-1816
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ module.exports = {
2525
ignoreRestSiblings: true,
2626
},
2727
],
28+
"no-mixed-spaces-and-tabs": ["error", "smart-tabs"],
2829
},
2930
}

examples/generate-random-data/index.ts

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
// Find the official Notion API client @ https:// github.com/makenotion/notion-sdk-js/
22
// npm install @notionhq/client
33
import { Client } from "@notionhq/client"
4+
import {
5+
CreatePageBodyParameters,
6+
CreatePageParameters,
7+
GetDatabaseResponse,
8+
GetPageResponse,
9+
} from "@notionhq/client/build/src/api-endpoints"
410

511
import * as _ from "lodash"
612

713
import { config } from "dotenv"
814
config()
915

1016
import * as faker from "faker"
11-
import {
12-
InputPropertyValueMap,
13-
PropertyMap,
14-
} from "@notionhq/client/build/src/api-endpoints"
15-
import {
16-
PropertyValueWithoutId,
17-
SelectFilter,
18-
TextFilter,
19-
UserBase,
20-
} from "@notionhq/client/build/src/api-types"
2117

2218
const notion = new Client({ auth: process.env["NOTION_KEY"] })
2319

2420
const startTime = new Date()
21+
startTime.setSeconds(0, 0)
2522

2623
// Given the properties of a database, generate an object full of
2724
// random data that can be used to generate new rows in our Notion database.
2825
function makeFakePropertiesData(
29-
properties: PropertyMap
30-
): InputPropertyValueMap {
31-
const propertyValues: InputPropertyValueMap = {}
26+
properties: GetDatabaseResponse["properties"]
27+
): Record<string, CreatePageBodyParameters["properties"]> {
28+
const propertyValues: Record<string, CreatePageBodyParameters["properties"]> =
29+
{}
3230
Object.entries(properties).forEach(([name, property]) => {
3331
if (property.type === "date") {
3432
propertyValues[name] = {
@@ -117,15 +115,17 @@ function assertUnreachable(_x: never): never {
117115
throw new Error("Didn't expect to get here")
118116
}
119117

120-
function userToString(userBase: UserBase) {
118+
function userToString(userBase: { id: string; name?: string | null }) {
121119
return `${userBase.id}: ${userBase.name || "Unknown Name"}`
122120
}
123121

124-
function findRandomSelectColumnNameAndValue(properties: PropertyMap): {
122+
function findRandomSelectColumnNameAndValue(
123+
properties: GetDatabaseResponse["properties"]
124+
): {
125125
name: string
126126
value: string | undefined
127127
} {
128-
const options = Object.entries(properties).flatMap(([name, property]) => {
128+
const options = _.flatMap(Object.entries(properties), ([name, property]) => {
129129
if (property.type === "select") {
130130
return [
131131
{ name, value: _.sample(property.select.options.map(o => o.name)) },
@@ -141,7 +141,9 @@ function findRandomSelectColumnNameAndValue(properties: PropertyMap): {
141141
return { name: "", value: undefined }
142142
}
143143

144-
function extractValueToString(property: PropertyValueWithoutId): string {
144+
function extractValueToString(
145+
property: GetPageResponse["properties"][string]
146+
): string {
145147
switch (property.type) {
146148
case "checkbox":
147149
return property.checkbox.toString()
@@ -160,8 +162,14 @@ function extractValueToString(property: PropertyValueWithoutId): string {
160162
case "phone_number":
161163
return property.phone_number
162164
case "select":
165+
if (!property.select) {
166+
return ""
167+
}
163168
return `${property.select.id} ${property.select.name}`
164169
case "multi_select":
170+
if (!property.multi_select) {
171+
return ""
172+
}
165173
return property.multi_select
166174
.map(select => `${select.id} ${select.name}`)
167175
.join(", ")
@@ -183,27 +191,43 @@ function extractValueToString(property: PropertyValueWithoutId): string {
183191
} else if (property.formula.type === "number") {
184192
return property.formula.number?.toString() || "???"
185193
} else if (property.formula.type === "boolean") {
186-
return property.formula.boolean.toString()
194+
return property.formula.boolean?.toString() || "???"
187195
} else if (property.formula.type === "date") {
188-
return new Date(property.formula.date.date.start).toISOString()
196+
return (
197+
(property.formula.date?.start &&
198+
new Date(property.formula.date.start).toISOString()) ||
199+
"???"
200+
)
189201
} else {
190202
return assertUnreachable(property.formula)
191203
}
192204
case "rollup":
193205
if (property.rollup.type === "number") {
194-
return property.rollup.number.toString()
206+
return property.rollup.number?.toString() || "???"
195207
} else if (property.rollup.type === "date") {
196-
return new Date(property.rollup.date.date.start).toISOString()
208+
return (
209+
(property.rollup.date?.start &&
210+
new Date(property.rollup.date?.start).toISOString()) ||
211+
"???"
212+
)
197213
} else if (property.rollup.type === "array") {
198214
return JSON.stringify(property.rollup.array)
199215
} else {
200216
return assertUnreachable(property.rollup)
201217
}
218+
case "relation":
219+
if (property.relation) {
220+
return property.relation.join(",")
221+
}
222+
return "???"
202223
}
203224
return assertUnreachable(property)
204225
}
205226

206-
async function exerciseWriting(databaseId: string, properties: PropertyMap) {
227+
async function exerciseWriting(
228+
databaseId: string,
229+
properties: GetDatabaseResponse["properties"]
230+
) {
207231
console.log("\n\n********* Exercising Writing *********\n\n")
208232

209233
const RowsToWrite = 10
@@ -212,18 +236,23 @@ async function exerciseWriting(databaseId: string, properties: PropertyMap) {
212236
for (let i = 0; i < RowsToWrite; i++) {
213237
const propertiesData = makeFakePropertiesData(properties)
214238

215-
await notion.pages.create({
239+
const parameters: CreatePageParameters = {
216240
parent: {
217241
database_id: databaseId,
218242
},
219243
properties: propertiesData,
220-
})
244+
} as CreatePageParameters
245+
246+
await notion.pages.create(parameters)
221247
}
222248

223249
console.log(`Wrote ${RowsToWrite} rows after ${startTime}`)
224250
}
225251

226-
async function exerciseReading(databaseId: string, _properties: PropertyMap) {
252+
async function exerciseReading(
253+
databaseId: string,
254+
_properties: GetDatabaseResponse["properties"]
255+
) {
227256
console.log("\n\n********* Exercising Reading *********\n\n")
228257
// and read back what we just did
229258
const queryResponse = await notion.databases.query({
@@ -232,7 +261,7 @@ async function exerciseReading(databaseId: string, _properties: PropertyMap) {
232261
let numOldRows = 0
233262
queryResponse.results.map(page => {
234263
const createdTime = new Date(page.created_time)
235-
if (createdTime <= startTime) {
264+
if (startTime > createdTime) {
236265
numOldRows++
237266
return
238267
}
@@ -246,11 +275,14 @@ async function exerciseReading(databaseId: string, _properties: PropertyMap) {
246275
})
247276
})
248277
console.log(
249-
`Skipped printing ${numOldRows} rows that were written before this test`
278+
`Skipped printing ${numOldRows} rows that were written before ${startTime}`
250279
)
251280
}
252281

253-
async function exerciseFilters(databaseId: string, properties: PropertyMap) {
282+
async function exerciseFilters(
283+
databaseId: string,
284+
properties: GetDatabaseResponse["properties"]
285+
) {
254286
console.log("\n\n********* Exercising Filters *********\n\n")
255287

256288
// get a random select or multi-select column from the collection with a random value for it
@@ -264,7 +296,7 @@ async function exerciseFilters(databaseId: string, properties: PropertyMap) {
264296
console.log(`Looking for ${selectColumnName}=${selectColumnValue}`)
265297

266298
// Check we can search by name
267-
const queryFilterSelectFilterTypeBased: SelectFilter = {
299+
const queryFilterSelectFilterTypeBased = {
268300
property: selectColumnName,
269301
select: { equals: selectColumnValue },
270302
}
@@ -288,14 +320,14 @@ async function exerciseFilters(databaseId: string, properties: PropertyMap) {
288320
"Need a rich_text column for this part of the test, could not find one"
289321
)
290322
}
291-
const textColumnId = textColumn.id
323+
const textColumnId = decodeURIComponent(textColumn.id)
292324
const letterToFind = faker.lorem.word(1)
293325

294326
console.log(
295-
`\n\nLooking for text column with id ${textColumnId} contains letter ${letterToFind}`
327+
`\n\nLooking for text column with id "${textColumnId}" contains letter "${letterToFind}"`
296328
)
297329

298-
const textFilter: TextFilter = {
330+
const textFilter = {
299331
property: textColumnId,
300332
text: { contains: letterToFind },
301333
}
@@ -307,7 +339,7 @@ async function exerciseFilters(databaseId: string, properties: PropertyMap) {
307339
})
308340

309341
console.log(
310-
`Had ${matchingTextResults.results.length} matching rows in column with ID ${textColumnId} containing letter ${letterToFind}`
342+
`Had ${matchingTextResults.results.length} matching rows in column with ID "${textColumnId}" containing letter "${letterToFind}"`
311343
)
312344
}
313345

0 commit comments

Comments
 (0)