88 warmEmbeddedParser as warmEmbeddedFdnextParser
99} from '@/services/fdnextApi' ;
1010import { FDNEXT_CAPABILITIES_SCHEMA_VERSIONS , summaryText } from '@/services/fdnextResultView' ;
11+ import { DEFAULT_HTTP_REQUEST_TIMEOUT_MS , runWithRequestTimeout } from '@/services/requestControl' ;
1112
1213const makeUrl = ( endpoint , params = { } ) => {
1314 const base = store . getServerAddress ( ) . replace ( / \/ + $ / , '' ) ;
@@ -38,18 +39,24 @@ const parseResponsePayload = async response => {
3839 }
3940} ;
4041
41- const request = async ( endpoint , params = { } , schemaVersion = 'fdnext.result.v1' ) => {
42- const response = await fetch ( makeUrl ( endpoint , params ) ) ;
43- const payload = await parseResponsePayload ( response ) ;
44- if ( payload ) {
45- try {
46- return assertFdnextPayload ( payload , schemaVersion , endpoint ) ;
47- } catch ( err ) {
48- if ( response . ok ) throw err ;
42+ const request = async ( endpoint , params = { } , schemaVersion = 'fdnext.result.v1' , options = { } ) => {
43+ return runWithRequestTimeout ( async signal => {
44+ const response = await fetch ( makeUrl ( endpoint , params ) , { signal } ) ;
45+ const payload = await parseResponsePayload ( response ) ;
46+ if ( payload ) {
47+ try {
48+ return assertFdnextPayload ( payload , schemaVersion , endpoint ) ;
49+ } catch ( err ) {
50+ if ( response . ok ) throw err ;
51+ }
4952 }
50- }
51- if ( ! response . ok ) throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
52- throw new Error ( `Unsupported fdnext response from ${ endpoint } ` ) ;
53+ if ( ! response . ok ) throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
54+ throw new Error ( `Unsupported fdnext response from ${ endpoint } ` ) ;
55+ } , {
56+ signal : options . signal ,
57+ timeoutMs : options . timeoutMs ?? DEFAULT_HTTP_REQUEST_TIMEOUT_MS ,
58+ timeoutMessage : `Request to ${ endpoint } timed out.`
59+ } ) ;
5360} ;
5461
5562const useEmbeddedParser = ( ) => store . isEmbeddedParser ( ) ;
@@ -67,51 +74,51 @@ const controllerGroupParams = () => ({
6774 controllerGroup : store . getControllerGroupParam ( )
6875} ) ;
6976
70- export const getServerInfo = async ( ) => useEmbeddedParser ( )
77+ export const getServerInfo = async ( options = { } ) => useEmbeddedParser ( )
7178 ? getEmbeddedInfo ( )
72- : request ( 'capabilities' , langParams ( ) , FDNEXT_CAPABILITIES_SCHEMA_VERSIONS ) ;
79+ : request ( 'capabilities' , langParams ( ) , FDNEXT_CAPABILITIES_SCHEMA_VERSIONS , options ) ;
7380
7481export const warmEmbeddedParser = ( ) => {
7582 if ( ! useEmbeddedParser ( ) ) return Promise . resolve ( ) ;
7683 return warmEmbeddedFdnextParser ( ) ;
7784} ;
7885
79- export const decodePartNumber = async pn => {
86+ export const decodePartNumber = async ( pn , options = { } ) => {
8087 return useEmbeddedParser ( ) ? decodeEmbeddedPartNumber ( pn ) : request ( 'parts/decode' , {
8188 ...langParams ( ) ,
8289 ...controllerGroupParams ( ) ,
8390 query : pn
84- } ) ;
91+ } , 'fdnext.result.v1' , options ) ;
8592} ;
8693
87- export const searchPartNumber = async ( pn , limit = 0 ) => {
94+ export const searchPartNumber = async ( pn , limit = 0 , options = { } ) => {
8895 return useEmbeddedParser ( ) ? searchEmbeddedPartNumber ( pn , limit ) : request ( 'parts/search' , {
8996 ...langParams ( ) ,
9097 query : pn ,
9198 ...limitParams ( limit )
92- } ) ;
99+ } , 'fdnext.result.v1' , options ) ;
93100} ;
94101
95- export const summarizePartNumber = async pn = > summaryText ( await decodePartNumber ( pn ) ) ;
102+ export const summarizePartNumber = async ( pn , options = { } ) = > summaryText ( await decodePartNumber ( pn , options ) ) ;
96103
97- export const decodeFlashId = async id => {
104+ export const decodeFlashId = async ( id , options = { } ) => {
98105 const input = { idScheme : 'nand.flash_id' } ;
99106 return useEmbeddedParser ( ) ? decodeEmbeddedFlashId ( id ) : request ( 'identifiers/decode' , {
100107 ...langParams ( ) ,
101108 query : id ,
102109 ...input ,
103110 ...controllerGroupParams ( )
104- } ) ;
111+ } , 'fdnext.result.v1' , options ) ;
105112} ;
106113
107- export const searchFlashId = async ( id , limit = 0 ) => {
114+ export const searchFlashId = async ( id , limit = 0 , options = { } ) => {
108115 const input = { idScheme : 'nand.flash_id' } ;
109116 return useEmbeddedParser ( ) ? searchEmbeddedFlashId ( id , limit ) : request ( 'identifiers/search' , {
110117 ...langParams ( ) ,
111118 query : id ,
112119 ...input ,
113120 ...limitParams ( limit )
114- } ) ;
121+ } , 'fdnext.result.v1' , options ) ;
115122} ;
116123
117- export const summarizeFlashId = async id = > summaryText ( await decodeFlashId ( id ) ) ;
124+ export const summarizeFlashId = async ( id , options = { } ) = > summaryText ( await decodeFlashId ( id , options ) ) ;
0 commit comments