@@ -30,8 +30,11 @@ interface ResponseWithHeaders<T> extends Response<T> {
3030 headers : Record < string , string >
3131}
3232
33+ // Type alias for cached DOM results to improve maintainability
34+ type CachedDOMResult = cheerio . Root & { res : Response ; $ : cheerio . Root }
35+
3336// Cache to store DOM objects
34- const getDOMCache = new Map < string , cheerio . Root > ( )
37+ const getDOMCache = new Map < string , CachedDOMResult > ( )
3538
3639/**
3740 * Makes an HTTP request using the specified method and options.
@@ -119,10 +122,10 @@ export function post(
119122export async function getDOMCached (
120123 route : string ,
121124 options : GetDOMOptions = { } ,
122- ) : Promise < cheerio . Root > {
125+ ) : Promise < CachedDOMResult > {
123126 const key = `${ route } ::${ JSON . stringify ( options ) } `
124127 if ( ! getDOMCache . has ( key ) ) {
125- const { $ } = await getDOM ( route , options )
128+ const $ = await getDOM ( route , options )
126129 getDOMCache . set ( key , $ )
127130 }
128131 // The non-null assertion is safe here because we've just set the key if it didn't exist
@@ -134,12 +137,9 @@ export async function getDOMCached(
134137 *
135138 * @param route - The route to request.
136139 * @param options - Options for fetching the DOM.
137- * @returns A promise that resolves to the loaded DOM object.
140+ * @returns A promise that resolves to the loaded DOM object with res attached and destructurable .
138141 */
139- export async function getDOM (
140- route : string ,
141- options : GetDOMOptions = { } ,
142- ) : Promise < { $ : cheerio . Root ; res : Response } > {
142+ export async function getDOM ( route : string , options : GetDOMOptions = { } ) : Promise < CachedDOMResult > {
143143 const { headers, allow500s = false , allow404 = false , retries = 0 } = options
144144 const res = await get ( route , { followRedirects : true , headers, retries } )
145145
@@ -152,8 +152,13 @@ export async function getDOM(
152152 }
153153
154154 const $ = cheerio . load ( res . body || '' , { xmlMode : true } )
155+ const result = $ as CachedDOMResult
156+ // Attach res to the cheerio object for backward compatibility
157+ result . res = res
158+ // Attach $ to itself for destructuring compatibility
159+ result . $ = result
155160
156- return { $ , res }
161+ return result
157162}
158163
159164/**
0 commit comments