packageDocumentation module import { AccessToken } from "@itwin/core-bentley" ; import { RequestGlobalOptions , RequestOptions , RequestQueryOptions } from "../itwin-client/Request" ; import { FileHandler } from "../itwin-client/FileHandler" ; import { ChunkedQueryContext } from "../wsg/ChunkedQueryContext" ; import { WsgInstance } from "../wsg/ECJsonTypeMap" ; import { DefaultWsgRequestOptionsProvider , HttpRequestOptions , WsgClient , WsgRequestOptions } from "../wsg/WsgClient" ; import { CustomRequestOptions } from "./CustomRequestOptions" ; import { IModelHubError } from "./Errors" ; const applicationVersionHeaderName = "Application-Version" ; /**
 * Provides default options for iModelHub requests.
 */ class DefaultIModelHubRequestOptionsProvider extends DefaultWsgRequestOptionsProvider { public constructor ( agent ) { super ( ) ; this . _defaultOptions . errorCallback = IModelHubError . parse ; this . _defaultOptions . retryCallback = IModelHubError . shouldRetry ; this . _defaultOptions . agent = agent ; } } internal export type HttpRequestOptionsTransformer = ( options : HttpRequestOptions ) => void ; internal export function addHeader ( name , valueFactory ) { return ( options ) => { if ( ! options . headers ) options . headers = { } ; options . headers [ name ] = valueFactory ( ) ; } ; } internal export function addApplicationVersion ( version ) { return addHeader ( applicationVersionHeaderName , ( ) => version ) ; } internal export function addCsrfHeader ( headerName = "X-XSRF-TOKEN" , cookieName = "XSRF-TOKEN" ) { return addHeader ( headerName , ( ) => { return document . cookie . split ( "; " ) . find ( ( r ) => r . startsWith ( `${ cookieName }=` ) ) ! . split ( "=" ) [ 1 ] ; } ) ; } internal export class IModelBaseHandler extends WsgClient { protected override _url ? : string ; private _defaultIModelHubOptionsProvider : DefaultIModelHubRequestOptionsProvider ; protected _agent : any ; protected _fileHandler : FileHandler | undefined ; private _customRequestOptions : CustomRequestOptions = new CustomRequestOptions ( ) ; private _httpRequestOptionsTransformers : HttpRequestOptionsTransformer [ ] = [ ] ; internal public constructor ( keepAliveDuration = 30000 , fileHandler ? ) { super ( "sv1.1" ) ; this . baseUrl = "https://api.bentley.com/imodelhub" ; this . _fileHandler = fileHandler ; const agentOptions = { keepAlive : keepAliveDuration > 0 , keepAliveMsecs : keepAliveDuration , secureProtocol : "TLSv1_2_method" } ; if ( RequestGlobalOptions . httpsProxy ) { this . _agent = RequestGlobalOptions . createHttpsProxy ( agentOptions ) ; } else { this . _agent = require ( "https" ) . Agent ( agentOptions ) ; } } internal public formatITwinIdForUrl ( iTwinId ) { return iTwinId ; } internal public getFileHandler ( ) { return this . _fileHandler ; } param options returns internal protected override async setupOptionDefaults ( options ) { if ( ! this . _defaultIModelHubOptionsProvider ) this . _defaultIModelHubOptionsProvider = new DefaultIModelHubRequestOptionsProvider ( this . _agent ) ; return this . _defaultIModelHubOptionsProvider . assignOptions ( options ) ; } param options returns internal protected setupHttpOptions ( options ? ) { const httpOptions = { ... options } ; for ( const transformer of this . _httpRequestOptionsTransformers ) { transformer ( httpOptions ) ; } return httpOptions ; } param func internal public use ( func ) { this . _httpRequestOptionsTransformers . push ( func ) ; } returns internal public getAgent ( ) { return this . _agent ; } returns internal public override async getUrl ( ) { return super . getUrl ( ) ; } param relativeUrlPath param httpRequestOptions returns public override async delete ( accessToken , relativeUrlPath , httpRequestOptions ? ) { return super . delete ( accessToken , relativeUrlPath , httpRequestOptions ) ; } param relativeUrlPath param instance param requestOptions returns public override async deleteInstance < T extends WsgInstance > ( accessToken , relativeUrlPath , instance ? , requestOptions ? ) { if ( this . _customRequestOptions . isSet ) { if ( ! requestOptions ) { requestOptions = { } ; } requestOptions . CustomOptions = this . _customRequestOptions . insertCustomOptions ( requestOptions . CustomOptions ) ; } return super . deleteInstance < T > ( accessToken , relativeUrlPath , instance , requestOptions ) ; } param typedConstructor param relativeUrlPath param instance param requestOptions param httpRequestOptions returns public override async postInstance < T extends WsgInstance > ( accessToken , typedConstructor , relativeUrlPath , instance , requestOptions ? , httpRequestOptions ? ) { if ( this . _customRequestOptions . isSet ) { if ( ! requestOptions ) { requestOptions = { } ; } requestOptions . CustomOptions = this . _customRequestOptions . insertCustomOptions ( requestOptions . CustomOptions ) ; } return super . postInstance < T > ( accessToken , typedConstructor , relativeUrlPath , instance , requestOptions , this . setupHttpOptions ( httpRequestOptions ) ) ; } param typedConstructor param relativeUrlPath param instances param requestOptions param httpRequestOptions returns public override async postInstances < T extends WsgInstance > ( accessToken , typedConstructor , relativeUrlPath , instances , requestOptions ? , httpRequestOptions ? ) { return super . postInstances ( accessToken , typedConstructor , relativeUrlPath , instances , requestOptions , this . setupHttpOptions ( httpRequestOptions ) ) ; } param typedConstructor param relativeUrlPath param queryOptions param httpRequestOptions returns public override async getInstances < T extends WsgInstance > ( accessToken , typedConstructor , relativeUrlPath , queryOptions ? , httpRequestOptions ? ) { return super . getInstances ( accessToken , typedConstructor , relativeUrlPath , queryOptions , this . setupHttpOptions ( httpRequestOptions ) ) ; } param url param chunkedQueryContext param typedConstructor param queryOptions param httpRequestOptions returns public override async getInstancesChunk < T extends WsgInstance > ( accessToken , url , chunkedQueryContext , typedConstructor , queryOptions ? , httpRequestOptions ? ) { return super . getInstancesChunk ( accessToken , url , chunkedQueryContext , typedConstructor , queryOptions , this . setupHttpOptions ( httpRequestOptions ) ) ; } param typedConstructor param relativeUrlPath param queryOptions param httpRequestOptions returns public override async postQuery < T extends WsgInstance > ( accessToken , typedConstructor , relativeUrlPath , queryOptions , httpRequestOptions ? ) { return super . postQuery ( accessToken , typedConstructor , relativeUrlPath , queryOptions , this . setupHttpOptions ( httpRequestOptions ) ) ; } internal public getCustomRequestOptions ( ) { return this . _customRequestOptions ; } }