diff --git a/src/common/lib/client/auth.ts b/src/common/lib/client/auth.ts index 721d23e394..1a47291c08 100644 --- a/src/common/lib/client/auth.ts +++ b/src/common/lib/client/auth.ts @@ -12,6 +12,7 @@ import HttpMethods from '../../constants/HttpMethods'; import HttpStatusCodes from 'common/constants/HttpStatusCodes'; import Platform from '../../platform'; import Resource from './resource'; +import Defaults from '../util/defaults'; type BatchResult = API.Types.BatchResult; type TokenRevocationTargetSpecifier = API.Types.TokenRevocationTargetSpecifier; @@ -584,7 +585,7 @@ class Auth { return client.baseUri(host) + path; }; - const requestHeaders = Utils.defaultPostHeaders(this.client.options); + const requestHeaders = Defaults.defaultPostHeaders(this.client.options); if (authOptions.requestHeaders) Utils.mixin(requestHeaders, authOptions.requestHeaders); Logger.logAction( Logger.LOG_MICRO, @@ -1073,7 +1074,7 @@ class Auth { }; const format = this.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(this.client.options, { format }); + headers = Defaults.defaultPostHeaders(this.client.options, { format }); if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers); diff --git a/src/common/lib/client/baseclient.ts b/src/common/lib/client/baseclient.ts index f81d69f4e2..1486df07b5 100644 --- a/src/common/lib/client/baseclient.ts +++ b/src/common/lib/client/baseclient.ts @@ -118,7 +118,7 @@ class BaseClient { return Utils.promisify(this, 'stats', [params]) as Promise>; } } - const headers = Utils.defaultGetHeaders(this.options), + const headers = Defaults.defaultGetHeaders(this.options), format = this.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.http.supportsLinkHeaders ? undefined : format; @@ -148,7 +148,7 @@ class BaseClient { const _callback = callback || noop; - const headers = Utils.defaultGetHeaders(this.options); + const headers = Defaults.defaultGetHeaders(this.options); if (this.options.headers) Utils.mixin(headers, this.options.headers); const timeUri = (host: string) => { return this.baseUri(host) + '/time'; @@ -201,8 +201,8 @@ class BaseClient { const _method = method.toLowerCase() as HttpMethods; const headers = _method == 'get' - ? Utils.defaultGetHeaders(this.options, { format, protocolVersion: version }) - : Utils.defaultPostHeaders(this.options, { format, protocolVersion: version }); + ? Defaults.defaultGetHeaders(this.options, { format, protocolVersion: version }) + : Defaults.defaultPostHeaders(this.options, { format, protocolVersion: version }); if (callback === undefined) { return Utils.promisify(this, 'request', [method, path, version, params, body, customHeaders]) as Promise< @@ -264,7 +264,7 @@ class BaseClient { } const format = this.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(this.options, { format }); + headers = Defaults.defaultPostHeaders(this.options, { format }); if (this.options.headers) Utils.mixin(headers, this.options.headers); @@ -304,7 +304,7 @@ class BaseClient { } const format = this.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(this.options, { format }); + headers = Defaults.defaultPostHeaders(this.options, { format }); if (this.options.headers) Utils.mixin(headers, this.options.headers); diff --git a/src/common/lib/client/channel.ts b/src/common/lib/client/channel.ts index b195ecf370..d14f4c9752 100644 --- a/src/common/lib/client/channel.ts +++ b/src/common/lib/client/channel.ts @@ -11,6 +11,7 @@ import { PaginatedResultCallback, StandardCallback } from '../../types/utils'; import BaseClient from './baseclient'; import * as API from '../../../../ably'; import Platform from 'common/platform'; +import Defaults from '../util/defaults'; interface RestHistoryParams { start?: number; @@ -88,7 +89,7 @@ class Channel extends EventEmitter { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); Utils.mixin(headers, client.options.headers); @@ -141,7 +142,7 @@ class Channel extends EventEmitter { options = client.options, format = options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, idempotentRestPublishing = client.options.idempotentRestPublishing, - headers = Utils.defaultPostHeaders(client.options, { format }); + headers = Defaults.defaultPostHeaders(client.options, { format }); Utils.mixin(headers, options.headers); @@ -190,7 +191,7 @@ class Channel extends EventEmitter { } const format = this.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json; - const headers = Utils.defaultPostHeaders(this.client.options, { format }); + const headers = Defaults.defaultPostHeaders(this.client.options, { format }); Resource.get(this.client, this.basePath, headers, {}, format, callback || noop); } diff --git a/src/common/lib/client/presence.ts b/src/common/lib/client/presence.ts index 18cb5a2b97..ea64ad3cd7 100644 --- a/src/common/lib/client/presence.ts +++ b/src/common/lib/client/presence.ts @@ -7,6 +7,7 @@ import { CipherOptions } from '../types/message'; import { PaginatedResultCallback } from '../../types/utils'; import Channel from './channel'; import RealtimeChannel from './realtimechannel'; +import Defaults from '../util/defaults'; class Presence extends EventEmitter { channel: RealtimeChannel | Channel; @@ -32,7 +33,7 @@ class Presence extends EventEmitter { const client = this.channel.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.channel.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); Utils.mixin(headers, client.options.headers); @@ -71,7 +72,7 @@ class Presence extends EventEmitter { const client = this.channel.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.channel.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); Utils.mixin(headers, client.options.headers); diff --git a/src/common/lib/client/push.ts b/src/common/lib/client/push.ts index 0a60822cbd..2cf3e01d19 100644 --- a/src/common/lib/client/push.ts +++ b/src/common/lib/client/push.ts @@ -6,6 +6,7 @@ import ErrorInfo from '../types/errorinfo'; import PushChannelSubscription from '../types/pushchannelsubscription'; import { ErrCallback, PaginatedResultCallback, StandardCallback } from '../../types/utils'; import BaseClient from './baseclient'; +import Defaults from '../util/defaults'; class Push { client: BaseClient; @@ -31,7 +32,7 @@ class Admin { publish(recipient: any, payload: any, callback: ErrCallback) { const client = this.client; const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(client.options, { format }), + headers = Defaults.defaultPostHeaders(client.options, { format }), params = {}; const body = Utils.mixin({ recipient: recipient }, payload); @@ -59,7 +60,7 @@ class DeviceRegistrations { const client = this.client; const body = DeviceDetails.fromValues(device); const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(client.options, { format }), + headers = Defaults.defaultPostHeaders(client.options, { format }), params = {}; if (typeof callback !== 'function') { @@ -95,7 +96,7 @@ class DeviceRegistrations { get(deviceIdOrDetails: any, callback: StandardCallback) { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultGetHeaders(client.options, { format }), + headers = Defaults.defaultGetHeaders(client.options, { format }), deviceId = deviceIdOrDetails.id || deviceIdOrDetails; if (typeof callback !== 'function') { @@ -139,7 +140,7 @@ class DeviceRegistrations { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); if (typeof callback !== 'function') { return Utils.promisify(this, 'list', arguments); @@ -159,7 +160,7 @@ class DeviceRegistrations { remove(deviceIdOrDetails: any, callback: ErrCallback) { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultGetHeaders(client.options, { format }), + headers = Defaults.defaultGetHeaders(client.options, { format }), params = {}, deviceId = deviceIdOrDetails.id || deviceIdOrDetails; @@ -195,7 +196,7 @@ class DeviceRegistrations { removeWhere(params: any, callback: ErrCallback) { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); if (typeof callback !== 'function') { return Utils.promisify(this, 'removeWhere', arguments); @@ -220,7 +221,7 @@ class ChannelSubscriptions { const client = this.client; const body = PushChannelSubscription.fromValues(subscription); const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultPostHeaders(client.options, { format }), + headers = Defaults.defaultPostHeaders(client.options, { format }), params = {}; if (typeof callback !== 'function') { @@ -252,7 +253,7 @@ class ChannelSubscriptions { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); if (typeof callback !== 'function') { return Utils.promisify(this, 'list', arguments); @@ -272,7 +273,7 @@ class ChannelSubscriptions { removeWhere(params: any, callback: PaginatedResultCallback) { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); if (typeof callback !== 'function') { return Utils.promisify(this, 'removeWhere', arguments); @@ -292,7 +293,7 @@ class ChannelSubscriptions { const client = this.client, format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json, envelope = this.client.http.supportsLinkHeaders ? undefined : format, - headers = Utils.defaultGetHeaders(client.options, { format }); + headers = Defaults.defaultGetHeaders(client.options, { format }); if (typeof callback !== 'function') { return Utils.promisify(this, 'listChannels', arguments); diff --git a/src/common/lib/util/defaults.ts b/src/common/lib/util/defaults.ts index 1b1fd275fe..23d73aee98 100644 --- a/src/common/lib/util/defaults.ts +++ b/src/common/lib/util/defaults.ts @@ -42,6 +42,8 @@ type CompleteDefaults = IDefaults & { getRealtimeHost(options: ClientOptions, production: boolean, environment: string): string; objectifyOptions(options: ClientOptions | string): ClientOptions; normaliseOptions(options: InternalClientOptions): NormalisedClientOptions; + defaultGetHeaders(options: NormalisedClientOptions, headersOptions?: HeadersOptions): Record; + defaultPostHeaders(options: NormalisedClientOptions, headersOptions?: HeadersOptions): Record; }; const Defaults = { @@ -87,6 +89,8 @@ const Defaults = { checkHost, objectifyOptions, normaliseOptions, + defaultGetHeaders, + defaultPostHeaders, }; export function getHost(options: ClientOptions, host?: string | null, ws?: boolean): string { @@ -260,6 +264,56 @@ export function normaliseOptions(options: InternalClientOptions): NormalisedClie }; } +const contentTypes = { + json: 'application/json', + xml: 'application/xml', + html: 'text/html', + msgpack: 'application/x-msgpack', +}; + +export interface HeadersOptions { + format?: Utils.Format; + protocolVersion?: number; +} + +const defaultHeadersOptions: Required = { + format: Utils.Format.json, + protocolVersion: Defaults.protocolVersion, +}; + +export function defaultGetHeaders( + options: NormalisedClientOptions, + { + format = defaultHeadersOptions.format, + protocolVersion = defaultHeadersOptions.protocolVersion, + }: HeadersOptions = {} +): Record { + const accept = contentTypes[format]; + return { + accept: accept, + 'X-Ably-Version': protocolVersion.toString(), + 'Ably-Agent': getAgentString(options), + }; +} + +export function defaultPostHeaders( + options: NormalisedClientOptions, + { + format = defaultHeadersOptions.format, + protocolVersion = defaultHeadersOptions.protocolVersion, + }: HeadersOptions = {} +): Record { + let contentType; + const accept = (contentType = contentTypes[format]); + + return { + accept: accept, + 'content-type': contentType, + 'X-Ably-Version': protocolVersion.toString(), + 'Ably-Agent': getAgentString(options), + }; +} + export default Defaults as CompleteDefaults; export function getDefaults(platformDefaults: IDefaults) { diff --git a/src/common/lib/util/utils.ts b/src/common/lib/util/utils.ts index 8d8477a2ee..03d546b0a8 100644 --- a/src/common/lib/util/utils.ts +++ b/src/common/lib/util/utils.ts @@ -1,7 +1,5 @@ import Platform from 'common/platform'; -import Defaults, { getAgentString } from './defaults'; import ErrorInfo, { PartialErrorInfo } from 'common/lib/types/errorinfo'; -import { NormalisedClientOptions } from 'common/types/ClientOptions'; function randomPosn(arrOrStr: Array | string) { return Math.floor(Math.random() * arrOrStr.length); @@ -330,61 +328,11 @@ export function allSame(arr: Array>, prop: string): bool }); } -const contentTypes = { - json: 'application/json', - xml: 'application/xml', - html: 'text/html', - msgpack: 'application/x-msgpack', -}; - export enum Format { msgpack = 'msgpack', json = 'json', } -export interface HeadersOptions { - format?: Format; - protocolVersion?: number; -} - -const defaultHeadersOptions: Required = { - format: Format.json, - protocolVersion: Defaults.protocolVersion, -}; - -export function defaultGetHeaders( - options: NormalisedClientOptions, - { - format = defaultHeadersOptions.format, - protocolVersion = defaultHeadersOptions.protocolVersion, - }: HeadersOptions = {} -): Record { - const accept = contentTypes[format]; - return { - accept: accept, - 'X-Ably-Version': protocolVersion.toString(), - 'Ably-Agent': getAgentString(options), - }; -} - -export function defaultPostHeaders( - options: NormalisedClientOptions, - { - format = defaultHeadersOptions.format, - protocolVersion = defaultHeadersOptions.protocolVersion, - }: HeadersOptions = {} -): Record { - let contentType; - const accept = (contentType = contentTypes[format]); - - return { - accept: accept, - 'content-type': contentType, - 'X-Ably-Version': protocolVersion.toString(), - 'Ably-Agent': getAgentString(options), - }; -} - export function arrPopRandomElement(arr: Array): T { return arr.splice(randomPosn(arr), 1)[0]; }