@@ -2,37 +2,83 @@ import { writable } from 'svelte/store';
22import { base } from '$app/paths' ;
33import * as config from '@setup' ;
44
5-
65export const db = writable ( [ ] ) ;
7- export async function loadDb ( ) {
8- try {
9- const url = config . local . includes ( 'http' ) ? config . local : `${ base } /${ config . local } ` ;
10- console . log ( `Fetching URL: ${ url } ` ) ;
11- const response = await fetch ( url ) ;
12-
13- if ( ! response . ok ) {
14- throw new Error ( `Failed to load db: ${ response . statusText } (Status: ${ response . status } )` ) ;
15- }
16-
17- const contentType = response . headers . get ( 'content-type' ) ;
18- if ( ! contentType || ! contentType . includes ( 'application/json' ) ) {
19- throw new Error ( 'Response is not valid JSON' ) ;
20- }
21-
22- const data = await response . json ( ) ;
23- db . set (
24- data . map ( ( d ) => ( {
25- ...d ,
26- "@id" : d [ "@id" ]
27- ?. replace ( / \/ i t e m s \/ / , '/resources/' )
28- ?. replace ( / \/ m e d i a \/ / , '/resources/' )
29- ?. replace ( / \/ i t e m _ s e t s \/ / , '/resources/' )
30- } ) )
31- ) ;
32- console . log ( 'Database loaded successfully.' ) ;
33-
34- } catch ( error ) {
35- console . error ( 'Error loading db.json:' , error ) ;
36- }
6+
7+ function normalizeOmekaId ( url ) {
8+ if ( ! url || ! config . api ) return url ;
9+
10+ try {
11+ const originalUrl = new URL ( url ) ;
12+ const configuredApiUrl = new URL ( config . api ) ;
13+
14+ const apiIndex = originalUrl . pathname . indexOf ( '/api' ) ;
15+ if ( apiIndex === - 1 ) return url ;
16+
17+ const pathAfterApi = originalUrl . pathname . slice ( apiIndex + 4 ) ;
18+
19+ const basePath = configuredApiUrl . pathname . replace ( / \/ $ / , '' ) ;
20+
21+ return (
22+ configuredApiUrl . origin +
23+ basePath +
24+ pathAfterApi +
25+ ( originalUrl . search || '' ) +
26+ ( originalUrl . hash || '' )
27+ ) ;
28+ } catch {
29+ return url ;
30+ }
31+ }
32+
33+ function normalizeIdsDeep ( obj ) {
34+ if ( ! obj || typeof obj !== 'object' ) return obj ;
35+
36+ for ( const key in obj ) {
37+ if ( key === '@id' && typeof obj [ key ] === 'string' ) {
38+ const original = obj [ key ] ;
39+ let replaced = original
40+ . replace ( / \/ i t e m s \/ / , '/resources/' )
41+ . replace ( / \/ m e d i a \/ / , '/resources/' )
42+ . replace ( / \/ i t e m _ s e t s \/ / , '/resources/' ) ;
43+ replaced = normalizeOmekaId ( replaced ) ;
44+
45+ // if (original.includes('67423') || replaced.includes('67423')) {
46+ // console.log(`Found specific @id:`, original);
47+ // console.log(`Normalized @id:`, original, '→', replaced);
48+ // }
49+
50+ if ( original !== replaced ) {
51+ obj [ key ] = replaced ;
52+ }
53+ } else if ( typeof obj [ key ] === 'object' ) {
54+ normalizeIdsDeep ( obj [ key ] ) ;
55+ }
56+ }
57+ return obj ;
3758}
3859
60+ export async function loadDb ( ) {
61+ try {
62+ const url = config . local . includes ( 'http' ) ? config . local : `${ base } /${ config . local } ` ;
63+ console . log ( `Fetching URL: ${ url } ` ) ;
64+ const response = await fetch ( url ) ;
65+
66+ if ( ! response . ok ) {
67+ throw new Error ( `Failed to load db: ${ response . statusText } (Status: ${ response . status } )` ) ;
68+ }
69+
70+ const contentType = response . headers . get ( 'content-type' ) ;
71+ if ( ! contentType || ! contentType . includes ( 'application/json' ) ) {
72+ throw new Error ( 'Response is not valid JSON' ) ;
73+ }
74+
75+ const data = await response . json ( ) ;
76+
77+ const normalizedData = data . map ( ( item ) => normalizeIdsDeep ( item ) ) ;
78+
79+ db . set ( normalizedData ) ;
80+ console . log ( 'Database loaded and all @id fields normalized.' ) ;
81+ } catch ( error ) {
82+ console . error ( 'Error loading db.json:' , error ) ;
83+ }
84+ }
0 commit comments