1313 * -------------------------------------------------------------------------
1414 */
1515
16- import { Client } from '@notionhq/client' ;
16+ import { APIErrorCode , Client , isNotionClientError } from '@notionhq/client' ;
1717import { caching } from 'cache-manager' ;
1818import { dump } from 'js-yaml' ;
1919
@@ -27,18 +27,19 @@ import {
2727 normalizeProperties ,
2828} from '#property' ;
2929
30+ import type { Cache } from 'cache-manager' ;
31+
3032import type {
3133 Block ,
3234 Database ,
3335 NotionAPIDatabase ,
3436 NotionAPIList ,
3537 NotionAPIPage ,
3638 NotionAPITitle ,
39+ NotionAPIUser ,
3740 Page ,
3841} from './types' ;
3942
40- import type { Cache } from 'cache-manager' ;
41-
4243export interface NotionTTL {
4344 /** the number of seconds in which a database metadata will be cached */
4445 databaseMeta : number ;
@@ -142,7 +143,13 @@ export class Notion {
142143 object : 'database' ,
143144 parent : database . parent ,
144145 title : getPropertyContentFromRichText ( database . title ) ,
145- metadata : getMetadata ( database ) ,
146+ metadata : getMetadata ( {
147+ ...database ,
148+ /* eslint-disable @typescript-eslint/naming-convention */
149+ created_by : await this . getUser ( database . created_by . id ) ,
150+ last_edited_by : await this . getUser ( database . last_edited_by . id ) ,
151+ /* eslint-enable @typescript-eslint/naming-convention */
152+ } ) ,
146153 pages : normalizedPages ,
147154 } ;
148155 }
@@ -168,6 +175,32 @@ export class Notion {
168175 return this . normalizePageAndCache ( page ) ;
169176 }
170177
178+ /**
179+ * get user detail with cache
180+ * @param id the uuid of a Notion user to be queried
181+ * @returns user object returned from Notion's API
182+ */
183+ public async getUser ( id : string ) : Promise < NotionAPIUser | null > {
184+ try {
185+ return await this . cache . wrap (
186+ `user:${ id } ` ,
187+ /* eslint-disable @typescript-eslint/naming-convention */
188+ async ( ) => this . client . users . retrieve ( { user_id : id } ) ,
189+ /* eslint-enable @typescript-eslint/naming-convention */
190+ ) ;
191+ } catch ( error ) {
192+ if (
193+ isNotionClientError ( error ) &&
194+ error . code === APIErrorCode . RestrictedResource
195+ ) {
196+ // NOTE: not throwing an error here because users may still want other data
197+ return null ;
198+ } else {
199+ throw error ;
200+ }
201+ }
202+ }
203+
171204 /**
172205 * get all block related to a collection
173206 * @param id the uuid of the collection, either a database or page or a parent block
@@ -193,7 +226,7 @@ export class Notion {
193226 ...( block . has_children
194227 ? { has_children : true , children : await this . getBlocks ( block . id ) }
195228 : { has_children : false } ) ,
196- /* eslint-enable */
229+ /* eslint-enable @typescript-eslint/naming-convention */
197230 } ) ,
198231 ) ,
199232 ) ;
@@ -213,7 +246,13 @@ export class Notion {
213246 ) [ 0 ] ;
214247 const title = getPropertyContentFromRichText ( titleProperty . title ) ;
215248
216- const metadata = getMetadata ( page ) ;
249+ const metadata = getMetadata ( {
250+ ...page ,
251+ /* eslint-disable @typescript-eslint/naming-convention */
252+ created_by : await this . getUser ( page . created_by . id ) ,
253+ last_edited_by : await this . getUser ( page . last_edited_by . id ) ,
254+ /* eslint-enable @typescript-eslint/naming-convention */
255+ } ) ;
217256
218257 return {
219258 id : page . id ,
0 commit comments