11/* eslint-disable @typescript-eslint/no-explicit-any */
2+ import { NOTION_CLIENTS_DB_ID , NOTION_KEY } from '$env/static/private' ;
23import { Client } from '@notionhq/client' ;
3- import type { PageServerLoad } from './$types' ;
4- import { NOTION_KEY , NOTION_CLIENTS_DB_ID } from '$env/static/private' ;
54import type { PageObjectResponse } from '@notionhq/client/build/src/api-endpoints' ;
5+ import { convertNotionDates , convertRichText } from '../utils/notion' ;
6+ import type { PageServerLoad } from './$types' ;
67
78const notion = new Client ( { auth : NOTION_KEY } ) ;
89
@@ -20,19 +21,37 @@ export const load: PageServerLoad = async ({ fetch }) => {
2021 }
2122 } ) ;
2223
23- const clients : ClientItem [ ] = pages . results . map ( ( _page ) => {
24- const page = _page as PageObjectResponse ;
25- const properties = page . properties ;
24+ const clients : ClientItem [ ] = pages . results
25+ . map ( ( _page ) => {
26+ const page = _page as PageObjectResponse ;
27+ const properties = page . properties ;
2628
27- return {
28- name : ( properties . Name as any ) . title [ 0 ] . plain_text ,
29- description : ( properties . Description as any ) . rich_text [ 0 ] ?. plain_text ?? "Very mysterious!" ,
30- website : ( properties . Website as any ) . url ,
31-
32- logo : ( page . icon as any ) . file . url ,
33- cover : ( page . cover as any ) . file . url ,
34- }
35- } ) ;
29+ // Type assertion
30+ if ( properties . Name . type !== 'title' ) throw new Error ( 'Name is not a title' ) ;
31+ if ( properties . Description . type !== 'rich_text' )
32+ throw new Error ( 'Description is not rich text' ) ;
33+ if ( properties . Website . type !== 'url' ) throw new Error ( 'Website is not a URL' ) ;
34+ if ( properties [ 'Working Since' ] . type !== 'date' )
35+ throw new Error ( 'Working Since is not a date' ) ;
36+
37+ if ( page . icon ?. type !== 'file' ) throw new Error ( 'Logo is not a file' ) ;
38+ if ( page . cover ?. type !== 'file' ) throw new Error ( 'Cover is not a file' ) ;
39+
40+ // Convert dates
41+ const dates = properties [ 'Working Since' ] . date
42+ ? convertNotionDates ( properties [ 'Working Since' ] . date )
43+ : { start : new Date ( ) , end : null } ;
44+
45+ return {
46+ name : convertRichText ( properties . Name . title ) || '' ,
47+ description : convertRichText ( properties . Description . rich_text ) || 'Very mysterious!' ,
48+ website : properties . Website . url || undefined ,
49+ dates,
50+ logo : page . icon . file . url ,
51+ cover : page . cover . file . url
52+ } ;
53+ } )
54+ . sort ( ( a , b ) => ( b . dates . end ?? new Date ( ) ) . getTime ( ) - ( a . dates . end ?? new Date ( ) ) . getTime ( ) ) ;
3655
3756 return { pinned, clients } ;
3857} ;
@@ -65,6 +84,7 @@ export interface ClientItem {
6584 name : string ;
6685 description : string ;
6786 website ?: string ;
87+ dates : ReturnType < typeof convertNotionDates > ;
6888
6989 logo ?: string ;
7090 cover ?: string ;
0 commit comments