11// [ Modules ] ///////////////////////////////////////////////////////////////////
2- import { config } from "../../config" ;
2+ import { defaultOpenbloxConfig , OpenbloxConfig } from "../../config" ;
33import { HttpHandler , isOpenCloudUrl } from "../../http/httpHandler" ;
44import { isObject , objectToFieldMask } from "../../utils/utils" ;
55import { HttpResponse } from "../../http/http.utils" ;
@@ -68,7 +68,7 @@ const isCursorEmpty = (cursor: Cursor) => (!cursor || (typeof cursor == "string"
6868const paginate = (
6969 initialResponse : ApiMethodResponse ,
7070 callApiMethod : CallApiMethod < any , any , true > ,
71- args : Record < any , any > , overrides : any ,
71+ args : Record < any , any > , config : any ,
7272 handlerFnCursorArg : "cursor" | "startRowIndex" | "pageNumber"
7373) => (
7474 async function * ( ) {
@@ -79,7 +79,7 @@ const paginate = (
7979 if ( isCursorEmpty ( nextCursor ) ) return
8080
8181 while ( true ) {
82- const newValue = await callApiMethod . call ( overrides , { ...args , [ handlerFnCursorArg ] : nextCursor } )
82+ const newValue = await callApiMethod . call ( config , { ...args , [ handlerFnCursorArg ] : nextCursor } )
8383 if ( isNoMoreData ( newValue . data ) ) return
8484 yield newValue
8585
@@ -89,7 +89,7 @@ const paginate = (
8989 }
9090)
9191
92- const pollForResponse = async ( url : string , operationPath : string , cloudKey : string ) => {
92+ const pollForResponse = async ( config : OpenbloxConfig , url : string , operationPath : string , cloudKey : string ) => {
9393 const operationPrefix = operationPath . match ( / ^ ( \/ ? ) c l o u d \/ v [ 1 - 9 ] + ( \/ ? ) / )
9494 ? operationPrefixRegexWithoutVersion . exec ( url ) ?. [ 1 ] as UrlSecure
9595 : operationPrefixRegexWithVersion . exec ( url ) ?. [ 1 ] as UrlSecure
@@ -121,11 +121,11 @@ export const createApiGroup: CreateApiGroupFn = ({ name:groupName, baseUrl, defa
121121
122122 const thisDefaultGetCursors = groupDefaultGetCursors ?? defaultGetCursors
123123
124- const createCallApiMethod = ( _baseUrl : UrlSecure = baseUrl ) : CallApiMethod < any , any , boolean > => async function ( args ) {
125- const overrides = this
126- const cookie = overrides ?. cookie || config ?. cookie
127- const cloudKey = overrides ?. cloudKey || config ?. cloudKey
128- const oauthToken = overrides ?. oauthToken
124+ const createCallApiMethod = ( _baseUrl : UrlSecure = baseUrl ) : CallApiMethod < any , any , boolean > => async function ( args ) {
125+ const config = this || defaultOpenbloxConfig
126+ const cookie = config ?. cookie
127+ const cloudKey = config ?. cloudKey
128+ const oauthToken = config ?. oauthToken
129129
130130 const handlerFnData = await handlerFn ( args as any )
131131 let { path, method, searchParams, applyFieldMask, body, formData, headers, getCursorsFn, pathToPoll, name } = handlerFnData
@@ -147,29 +147,37 @@ export const createApiGroup: CreateApiGroupFn = ({ name:groupName, baseUrl, defa
147147
148148 let main : ( ) => Promise < any >
149149 main = async ( ) => {
150- let response : HttpResponse = await HttpHandler ( { method, url, body, formData, headers } ) as any // TODO
150+ let response : HttpResponse = await HttpHandler ( config , { method, url, body, formData, headers } ) as any // TODO
151151 if ( ! ( response instanceof HttpResponse ) ) throw response
152152 let rawData = response . body
153153
154154 // Uncompleted long running operation.
155155 let opPath = rawData ?. path
156156 if ( opPath && rawData ?. done === false && isOpenCloudUrl ( url ) ) {
157157 console . warn ( `Polling '${ groupName } .${ name } ' (Please be patient)...` )
158- response = await pollForResponse ( url , pathToPoll ? pathToPoll ( rawData ) : opPath , cloudKey )
158+ response = await pollForResponse ( config , url , pathToPoll ? pathToPoll ( rawData ) : opPath , cloudKey )
159159 rawData = response . body
160160 }
161161
162+ let cachedData : any
163+
162164 let apiMethodResult : ApiMethodResponse < any , any > = formatRawDataFn
163- ? { response, again : main , get data ( ) { return formatRawDataFn ( rawData , response ) } }
164- : { response, again : main , data : rawData }
165+ ? { response, again : main , get data ( ) {
166+ if ( cachedData ) return cachedData
167+ else {
168+ cachedData = formatRawDataFn ( rawData , response )
169+ return cachedData
170+ }
171+ } , configUsed : config }
172+ : { response, again : main , data : rawData , configUsed : config }
165173
166174 // Applies async iterator if method is paginated.
167175 if ( handlerFnCursorArg ) {
168176 let [ previousCursor , nextCursor ] = ( getCursorsFn ?? thisDefaultGetCursors ) ( rawData ) ;
169177 apiMethodResult . cursors = { previous : previousCursor , next : nextCursor }
170178 if ( args && ! ( "__notRoot" in args ) ) {
171179 ( apiMethodResult as any as ApiMethodResponse < any , any , true > ) [ Symbol . asyncIterator ] = paginate (
172- apiMethodResult , callApiMethod as CallApiMethod < any , any , true > , args as Record < any , any > , overrides , handlerFnCursorArg
180+ apiMethodResult , callApiMethod as CallApiMethod < any , any , true > , args as Record < any , any > , config , handlerFnCursorArg
173181 ) as any
174182 }
175183 }
@@ -180,7 +188,7 @@ export const createApiGroup: CreateApiGroupFn = ({ name:groupName, baseUrl, defa
180188 return await main ( )
181189 }
182190
183- const callApiMethod = createCallApiMethod ( ) ;
191+ const callApiMethod = createCallApiMethod ( ) . bind ( undefined ) ;
184192
185193 // To handle legacy open cloud endpoints which use classic endpoints but with different base urls.
186194 ( callApiMethod as any ) . _deriveWithDifferentBaseUrl = ( baseUrl : UrlSecure ) => createCallApiMethod ( baseUrl )
0 commit comments