import Client from "../../client/Client" ; import { Cookie , CookieJar } from "tough-cookie" ; import { DefaultCreateCookieOptions , DefaultRESTControllerOptions , RESTControllerOptions , RESTCreateCookieOptions , RESTRequester , RESTRequestHandler , RESTRequestOptions , RESTResponseDataType , RESTResponseHandler } from "../../interfaces/RESTInterfaces" ; import updateXCSRFToken from "./lib/updateXCSRFToken" ; import RESTRequest from "./request" ; import responseHandlers from "./response/handlers" ; import getRequester from "./lib/getRequester" ; import { utilMergeDeep } from "../../util/utilFunctions" ; class RESTController { public client : Client ; public options : RESTControllerOptions ; public requester : RESTRequester ; public cookieJar : CookieJar ; public responseHandlers : RESTResponseHandler [ ] ; public requestHandlers : RESTRequestHandler [ ] ; constructor ( client , options ? ) { /**
         * The client
         */ this . client = client ; /**
         * The options for this RESTController
         */ this . options = this . setOptions ( options || this . client . options . rest ) ; /**
         * The cookie jar
         */ this . cookieJar = new CookieJar ( ) ; /**
         * Functions to go through to validate / modify the response
         */ this . responseHandlers = [ ... responseHandlers ] ; /**
         * Functions to go through to modify the request
         */ this . requestHandlers = [ ] ; /**
         * The function that's being used to perform the requests, can be modified
         */ this . requester = getRequester ( this , this . options . requester || undefined ) as RESTRequester ; } param { RequestOptions } options returns { Promise < RESTResponseDataType > } request ( options ) { const request = new RESTRequest ( this , options ) ; return request . send ( ) ; } /**
     * Fetches a new XCSRF token
     */ fetchXCSRFToken ( ) { return updateXCSRFToken ( this ) . then ( xcsrfToken => { this . setXCSRFToken ( xcsrfToken ) ; return xcsrfToken ; } ) ; } param { string } token setXCSRFToken ( token ) { this . options . xcsrf = token ; this . options . xcsrfSet = Date . now ( ) ; } /**
     * Gets the existing XCSRF token if it's not older than set refresh interval,
     * otherwise, fetch a new one
     */ async getXCSRFToken ( ) { if ( ! this . options . xcsrf || ( Date . now ( ) - ( this . options . xcsrfSet || 0 ) ) >= ( this . options . xcsrfRefreshInterval || DefaultRESTControllerOptions . xcsrfRefreshInterval ) ) { await this . fetchXCSRFToken ( ) . then ( token => { this . setXCSRFToken ( token ) ; } ) ; } return this . options . xcsrf ; } param { RESTCreateCookieOptions } cookieOptions returns { Cookie } createCookie ( cookieOptions ) { return new Cookie ( { ... DefaultCreateCookieOptions , ... cookieOptions } ) ; } param { Cookie } cookie param { ? string } domain param { Object } setCookieOptions returns { Cookie } addCookie ( cookie , domain ? , setCookieOptions ? ) { return this . cookieJar . setCookieSync ( cookie , domain || "https://roblox.com" , setCookieOptions || { } ) ; } param { string } domain returns { Cookie [ ] } getCookies ( domain ) { return this . cookieJar . getCookiesSync ( domain ) ; } param { Function } handler addResponseHandler ( handler ) { this . responseHandlers . push ( handler ) ; } param { Function } handler addRequestHandler ( handler ) { this . requestHandlers . push ( handler ) ; } param { string } proxyURL setProxy ( proxyURL ) { this . options . proxy = proxyURL ; } returns { string | undefined } getProxy ( ) { return this . options . proxy ; } param { string } userAgent setUserAgent ( userAgent ) { this . options . userAgent = userAgent ; } returns { string | undefined } getUserAgent ( ) { return this . options . userAgent ; } param { number } xcsrfRefreshInterval setXCSRFTokenRefreshInterval ( xcsrfRefreshInterval ) { this . options . xcsrfRefreshInterval = xcsrfRefreshInterval ; } returns { number | undefined } getXCSRFTokenRefreshInterval ( ) { return this . options . xcsrfRefreshInterval ; } param { number } xcsrfRefreshMaxRetries setXCSRFTokenRefreshMaxRetries ( xcsrfRefreshMaxRetries ) { this . options . xcsrfRefreshMaxRetries = xcsrfRefreshMaxRetries ; } returns { number | undefined } getXCSRFTokenRefreshMaxRetries ( ) { return this . options . xcsrfRefreshMaxRetries ; } param { RESTControllerOptions } options returns { RESTControllerOptions } setOptions ( options ? ) { this . options = utilMergeDeep ( DefaultRESTControllerOptions , options || { } ) as RESTControllerOptions ; return this . options ; } } export default RESTController ;