box-node-sdk
Version:
Official SDK for Box Platform APIs
223 lines • 9.13 kB
JavaScript
import { BaseUrls } from './baseUrls.js';
import { createAgent } from '../internal/utils.js';
import { BoxNetworkClient } from './boxNetworkClient.js';
import { BoxRetryStrategy } from './retries.js';
import { DataSanitizer } from '../internal/logging.js';
export class NetworkSession {
additionalHeaders = {};
baseUrls = new BaseUrls({});
interceptors = [];
agent = createAgent(void 0, void 0);
agentOptions;
proxyConfig;
networkClient = new BoxNetworkClient({});
retryStrategy = new BoxRetryStrategy({});
dataSanitizer = new DataSanitizer({});
timeoutConfig;
constructor(fields) {
if (fields.additionalHeaders !== undefined) {
this.additionalHeaders = fields.additionalHeaders;
}
if (fields.baseUrls !== undefined) {
this.baseUrls = fields.baseUrls;
}
if (fields.interceptors !== undefined) {
this.interceptors = fields.interceptors;
}
if (fields.agent !== undefined) {
this.agent = fields.agent;
}
if (fields.agentOptions !== undefined) {
this.agentOptions = fields.agentOptions;
}
if (fields.proxyConfig !== undefined) {
this.proxyConfig = fields.proxyConfig;
}
if (fields.networkClient !== undefined) {
this.networkClient = fields.networkClient;
}
if (fields.retryStrategy !== undefined) {
this.retryStrategy = fields.retryStrategy;
}
if (fields.dataSanitizer !== undefined) {
this.dataSanitizer = fields.dataSanitizer;
}
if (fields.timeoutConfig !== undefined) {
this.timeoutConfig = fields.timeoutConfig;
}
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also including additional headers to be attached to every API call.
* @param {{
readonly [key: string]: string;
}} additionalHeaders Headers, which are appended to each API request
* @returns {NetworkSession}
*/
withAdditionalHeaders(additionalHeaders = {}) {
return new NetworkSession({
additionalHeaders: { ...this.additionalHeaders, ...additionalHeaders },
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also including custom base urls to be used for every API call.
* @param {BaseUrls} baseUrls Custom base urls
* @returns {NetworkSession}
*/
withCustomBaseUrls(baseUrls) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also including custom agent options to be used for every API call.
* @param {AgentOptions} agentOptions Custom agent options
* @returns {NetworkSession}
*/
withCustomAgentOptions(agentOptions) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: createAgent(agentOptions, this.proxyConfig),
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also additional including custom interceptors.
* @param {readonly Interceptor[]} interceptors Custom base urls
* @returns {NetworkSession}
*/
withInterceptors(interceptors) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors.concat(interceptors),
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also including a custom proxy configuration.
* @param {ProxyConfig} proxyConfig
* @returns {NetworkSession}
*/
withProxy(proxyConfig) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: createAgent(this.agentOptions, proxyConfig),
agentOptions: this.agentOptions,
proxyConfig: proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also including a custom network client.
* @param {NetworkClient} networkClient
* @returns {NetworkSession}
*/
withNetworkClient(networkClient) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also applying retry strategy
* @param {RetryStrategy} retryStrategy
* @returns {NetworkSession}
*/
withRetryStrategy(retryStrategy) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also applying data sanitizer
* @param {DataSanitizerInput} dataSanitizerInput
* @returns {NetworkSession}
*/
withDataSanitizer(dataSanitizerInput) {
const dataSanitizer = new DataSanitizer({});
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: dataSanitizer,
timeoutConfig: this.timeoutConfig,
});
}
/**
* Generate a fresh network session by duplicating the existing configuration and network parameters, while also applying timeout config
* @param {TimeoutConfig} timeoutConfig
* @returns {NetworkSession}
*/
withTimeoutConfig(timeoutConfig) {
return new NetworkSession({
additionalHeaders: this.additionalHeaders,
baseUrls: this.baseUrls,
interceptors: this.interceptors,
agent: this.agent,
agentOptions: this.agentOptions,
proxyConfig: this.proxyConfig,
networkClient: this.networkClient,
retryStrategy: this.retryStrategy,
dataSanitizer: this.dataSanitizer,
timeoutConfig: timeoutConfig,
});
}
}
//# sourceMappingURL=network.js.map