@@ -16,6 +16,10 @@ export interface RegisterRequest {
1616 organization : string
1717}
1818
19+ interface ForgotPasswordReguest {
20+ username : string ;
21+ }
22+
1923type LabelType =
2024 | string
2125 | { '@value' : string ; '@language' ?: string }
@@ -56,9 +60,9 @@ export const userLogout = (group: string) => {
5660 return createGetRequest < any , any > ( endpoint , "application/json" ) ( ) ;
5761} ;
5862
59- export const getSelectedTermLabel = async ( searchTerm : string ) : Promise < string | undefined > => {
63+ export const getSelectedTermLabel = async ( searchTerm : string , group : string = 'base' ) : Promise < { label : string | undefined ; actualGroup : string } > => {
6064 try {
61- const response = await createGetRequest < JsonLdResponse , any > ( `/base /${ searchTerm } .jsonld` ) ( ) ;
65+ const response = await createGetRequest < JsonLdResponse , any > ( `/${ group } /${ searchTerm } .jsonld` ) ( ) ;
6266
6367 const label = response [ '@graph' ] ?. [ 0 ] ?. [ 'rdfs:label' ] ;
6468
@@ -73,10 +77,39 @@ export const getSelectedTermLabel = async (searchTerm: string): Promise<string |
7377 return label ?. [ '@value' ] || '' ;
7478 } ;
7579
76- return label ? getLabelValue ( label ) : undefined
80+ return {
81+ label : label ? getLabelValue ( label ) : undefined ,
82+ actualGroup : group
83+ } ;
7784 } catch ( err : any ) {
7885 console . error ( err . message ) ;
79- return undefined ;
86+ // If the request fails and we're not already trying 'base', try with 'base' as fallback
87+ if ( group !== 'base' ) {
88+ try {
89+ const fallbackResponse = await createGetRequest < JsonLdResponse , any > ( `/base/${ searchTerm } .jsonld` ) ( ) ;
90+ const fallbackLabel = fallbackResponse [ '@graph' ] ?. [ 0 ] ?. [ 'rdfs:label' ] ;
91+
92+ const getLabelValue = ( label : LabelType ) : string => {
93+ if ( typeof label === 'string' ) return label ;
94+ if ( Array . isArray ( label ) ) {
95+ const en = label . find (
96+ l => typeof l === 'string' || ( typeof l === 'object' && l ?. [ '@language' ] === 'en' )
97+ ) ;
98+ return typeof en === 'string' ? en : en ?. [ '@value' ] || '' ;
99+ }
100+ return label ?. [ '@value' ] || '' ;
101+ } ;
102+
103+ return {
104+ label : fallbackLabel ? getLabelValue ( fallbackLabel ) : undefined ,
105+ actualGroup : 'base'
106+ } ;
107+ } catch ( fallbackErr : any ) {
108+ console . error ( 'Fallback request also failed:' , fallbackErr . message ) ;
109+ return { label : undefined , actualGroup : group } ;
110+ }
111+ }
112+ return { label : undefined , actualGroup : group } ;
80113 }
81114} ;
82115
@@ -148,43 +181,54 @@ export const createNewOntology = async ({
148181
149182 const headers = {
150183 'Content-Type' : 'application/json' ,
151- 'Authorization' : `Bearer ${ token } `
184+ 'Authorization' : `Bearer ${ token } ` ,
152185 } ;
153186
154187 try {
155- const postResponse = await createPostRequest < any , any > ( endpoint , headers ) ( data ) ;
156-
157- // If the POST creates a new location, try fetching it (simulate follow-up GETs from the test)
158- if ( postResponse ?. location ) {
159- const getResponse = await fetch ( postResponse . location , {
160- headers : {
161- Accept : 'application/json' ,
162- } ,
163- } ) ;
164-
165- // Optionally fetch HTML if needed (like the .html equivalent in the Python test)
166- const htmlResponse = await fetch ( endpoint , {
167- headers : {
168- Accept : 'text/html' ,
169- } ,
170- } ) ;
188+ const postResponse = await fetch ( endpoint , {
189+ method : 'POST' ,
190+ headers,
191+ credentials : "include" ,
192+ body : JSON . stringify ( data ) ,
193+ redirect : 'manual' ,
194+ } ) ;
195+
196+ // Check for custom redirect header (all lowercase in fetch)
197+ const redirectLocation = postResponse . headers . get ( 'x-redirect-location' ) ;
171198
199+ if ( redirectLocation ) {
200+ const olympianRedirectLocation = redirectLocation . replace ( 'http://uri.interlex.org' , '' ) . replace ( / \. h t m l $ / , '.jsonld' ) ;
201+
202+ const getResponse = await fetch ( olympianRedirectLocation , { headers : { Authorization : `Bearer ${ token } ` } } ) ;
203+ const jsonResponse = await getResponse . json ( ) ;
204+
205+ const newOntologyID = jsonResponse ?. [ "@graph" ] ?. find ( ( object ) => object [ "@type" ] === "owl:Ontology" ) ?. [ "@id" ] || null ;
206+
172207 return {
173208 created : true ,
174- data : postResponse ,
175- jsonResponse : await getResponse . json ( ) ,
176- htmlAvailable : htmlResponse . ok ,
209+ location : olympianRedirectLocation ,
210+ newOntologyID : newOntologyID
177211 } ;
178212 }
179213
214+ // Try to parse the response as JSON (if present)
215+ let jsonResponse : any = null ;
216+ try {
217+ jsonResponse = await postResponse . json ( ) ;
218+ } catch ( e ) {
219+ // No JSON body, ignore
220+ }
221+
180222 return {
181- created : true ,
182- data : postResponse ,
223+ created : postResponse . ok ,
224+ location : endpoint ,
225+ jsonResponse,
183226 } ;
184227 } catch ( error : any ) {
228+ let errMsg = error ?. message ?? String ( error ) ;
185229 return {
186230 created : false ,
187- error : error ?. response ?. data || error . message ,
231+ error : errMsg ,
188232 } ;
189233 }
190234} ;
@@ -199,12 +243,24 @@ export const retrieveTokenApi = ({ groupname }: { groupname: string }) => {
199243 return createGetRequest < any , any > ( endpoint , "application/json" ) ( ) ;
200244} ;
201245
246+ export const forgotPassword = createPostRequest < any , ForgotPasswordReguest > ( API_CONFIG . REAL_API . USER_RECOVER , { "Content-Type" : "application/x-www-form-urlencoded" } )
247+
202248export const getMatchTerms = async ( group : string , term : string , filters = { } ) => {
203249 try {
204250 const response = await createGetRequest < any , any > ( `/${ group } /${ term } .${ BASE_EXTENSION } ` , "application/json" ) ( ) ;
205251 return termParser ( response , term ) ;
206252 } catch ( err : any ) {
207253 console . error ( err . message ) ;
254+ // If the request fails and we're not already trying 'base', try with 'base' as fallback
255+ if ( group !== 'base' ) {
256+ try {
257+ const fallbackResponse = await createGetRequest < any , any > ( `/base/${ term } .${ BASE_EXTENSION } ` , "application/json" ) ( ) ;
258+ return termParser ( fallbackResponse , term ) ;
259+ } catch ( fallbackErr : any ) {
260+ console . error ( 'Fallback request also failed:' , fallbackErr . message ) ;
261+ return undefined ;
262+ }
263+ }
208264 return undefined ;
209265 }
210266} ;
@@ -215,6 +271,16 @@ export const getRawData = async (group: string, termID: string, format: string)
215271 return response ;
216272 } catch ( err : any ) {
217273 console . error ( err . message ) ;
274+ // If the request fails and we're not already trying 'base', try with 'base' as fallback
275+ if ( group !== 'base' ) {
276+ try {
277+ const fallbackResponse = await createGetRequest < any , any > ( `/base/${ termID } .${ format } ` , "application/json" ) ( ) ;
278+ return fallbackResponse ;
279+ } catch ( fallbackErr : any ) {
280+ console . error ( 'Fallback request also failed:' , fallbackErr . message ) ;
281+ return undefined ;
282+ }
283+ }
218284 return undefined ;
219285 }
220286} ;
@@ -233,4 +299,4 @@ export const getTermDiscussions = async (group: string, variantID: string) => {
233299
234300export const getVariant = ( group : string , term : string ) => {
235301 return createGetRequest < any , any > ( `/${ group } /variant/${ term } ` , "application/json" ) ( ) ;
236- } ;
302+ } ;
0 commit comments