11/* eslint-disable @typescript-eslint/no-explicit-any */
2- import { NOTION_CLIENTS_DB_ID , NOTION_KEY } from '$env/static/private' ;
3- import { Client } from '@notionhq/client' ;
4- import type { PageObjectResponse } from '@notionhq/client/build/src/api-endpoints' ;
5- import { convertNotionDates , convertRichText } from '$lib/util/notion' ;
2+ import { NOTION_CLIENTS_DB_ID } from '$env/static/private' ;
3+ import type { DateProperty } from '$lib/notion' ;
4+ import { queryDatabase } from '$lib/notion/notion.server' ;
65import type { ServerLoadEvent } from '@sveltejs/kit' ;
76
8- const notion = new Client ( { auth : NOTION_KEY } ) ;
9-
107// noinspection JSUnusedGlobalSymbols
118export const load = async ( { fetch } : ServerLoadEvent ) => {
9+ // Fetch pinned repositories
1210 const req = await fetch ( 'https://gh-pinned-repos.egoist.dev/?username=KodingDev' ) ;
1311 const pinned : Repository [ ] = await req . json ( ) ;
1412
15- const pages = await notion . databases
16- . query ( {
17- database_id : NOTION_CLIENTS_DB_ID ,
18- filter : {
19- property : 'Public' ,
20- checkbox : {
21- equals : true
22- }
23- }
24- } )
25- . catch ( ( err ) => {
26- console . error ( 'Failed to fetch clients from Notion' , err ) ;
27- return { results : [ ] } ;
28- } ) ;
29-
30- const clients : ClientItem [ ] = pages . results
31- . map ( ( _page ) => {
32- const page = _page as PageObjectResponse ;
33- const properties = page . properties ;
34-
35- // Type assertion
36- if ( properties . Name . type !== 'title' ) throw new Error ( 'Name is not a title' ) ;
37- if ( properties . Description . type !== 'rich_text' )
38- throw new Error ( 'Description is not rich text' ) ;
39- if ( properties . Website . type !== 'url' ) throw new Error ( 'Website is not a URL' ) ;
40- if ( properties [ 'Working Since' ] . type !== 'date' )
41- throw new Error ( 'Working Since is not a date' ) ;
42-
43- if ( page . icon ?. type !== 'file' ) throw new Error ( 'Logo is not a file' ) ;
44- if ( page . cover ?. type !== 'file' ) throw new Error ( 'Cover is not a file' ) ;
45-
46- // Convert dates
47- const dates = properties [ 'Working Since' ] . date
48- ? convertNotionDates ( properties [ 'Working Since' ] . date )
49- : { start : new Date ( ) , end : null } ;
13+ // Fetch clients
14+ const pages = await queryDatabase ( NOTION_CLIENTS_DB_ID , {
15+ property : 'Public' ,
16+ checkbox : {
17+ equals : true
18+ }
19+ } ) . catch ( ( err ) => {
20+ console . error ( 'Failed to fetch clients from Notion' , err ) ;
21+ return [ ] ;
22+ } ) ;
23+
24+ // Type the clients
25+ const clients = pages
26+ . map ( ( page ) => {
27+ // Dates
28+ const prop = page . properties [ 'Working Since' ] as DateProperty ;
29+
30+ // Convert date
31+ const date = prop . date . end ? new Date ( prop . date . end ) : new Date ( ) ;
5032
5133 return {
52- dates,
53- properties,
54-
55- name : convertRichText ( properties . Name . title ) || '' ,
56- description : convertRichText ( properties . Description . rich_text ) || 'Very mysterious!' ,
57- website : properties . Website . url || undefined ,
58- logo : page . icon . file . url ,
59- cover : page . cover . file . url
34+ page,
35+ date
6036 } ;
6137 } )
62- . sort ( ( a , b ) => ( b . dates . end ?? new Date ( ) ) . getTime ( ) - ( a . dates . end ?? new Date ( ) ) . getTime ( ) ) ;
38+ . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) )
39+ . map ( ( item ) => item . page ) ;
6340
6441 return { pinned, clients } ;
6542} ;
@@ -78,13 +55,3 @@ export interface Skill {
7855 color : string ;
7956 link ?: string ;
8057}
81-
82- export interface ClientItem {
83- name : string ;
84- description : string ;
85- website ?: string ;
86- dates : ReturnType < typeof convertNotionDates > ;
87-
88- logo ?: string ;
89- cover ?: string ;
90- }
0 commit comments