Skip to content

Commit 6575100

Browse files
committed
feat: add support of passing custom headers
1 parent 49f1b51 commit 6575100

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

src/Api.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,34 @@ export interface IHttpOptions {
3131
url: string
3232
alwaysUseGet?: boolean
3333
headers: {
34-
sessionID?: string,
34+
sessionID?: string
3535
apiKey?: string
3636
}
3737
}
38+
export interface IApiConfig {
39+
url: string
40+
version?: string
41+
alwaysUseGet?: boolean
42+
apiKey?: string
43+
headers?: Record<string, string>
44+
}
3845
export type TFields = string | string[]
3946

4047
const GlobalScope = Function('return this')()
4148

49+
/**
50+
* Configuration for the Api constructor
51+
* @typedef {Object} config
52+
* @property {String} url - Required. A url to Workfront server (for example: http://localhost:8080)
53+
* @property {String} [version=internal] - Which version of api to use. At the moment of writing can be 2.0, 3.0, ..., 8.0. Pass 'unsupported' to use Workfront latest API (maybe unstable)
54+
* @property {Boolean} [alwaysUseGet=false] - Will cause the api to make every request as a GET with params in the query string and add method=DESIRED_METHOD_TYPE in the query string. Some Workfront urls will have issues with PUT and DELETE calls if this value is false
55+
* @property {String} [apiKey] - It is used to pass apiKey with every api request headers
56+
* @property {Object} [headers] - An key-value object that sets custom headers (for example: {'user-agent': DESIRED_USER_AGENT_NAME})
57+
*/
58+
4259
/**
4360
* Creates new Api instance.
4461
* @param {Object} config An object with the following keys:<br/>
45-
* <code>url</code> {String} - Required. A url to Workfront server (for example: http://localhost:8080)<br/>
46-
* <code>version</code> {String} - Optional. Which version of api to use. At the moment of writing can be 1.0, 2.0, 3.0, 4.0. Pass 'unsupported' to use Workfront latest API (maybe unstable).<br/>
47-
* <code>alwaysUseGet</code> {Boolean} - Optional. Defaults to false. Will cause the api to make every request as a GET with params in the query string and add method=DESIRED_METHOD_TYPE in the query string. Some Workfront urls will have issues with PUT and DELETE calls if this value is false.<br/>
48-
* <code>secureProtocol</code> {String} - Optional. Used only in https. The SSL method to use, e.g. TLSv1_method to force TLS version 1. The possible values depend on your installation of OpenSSL and are defined in the constant {@link http://www.openssl.org/docs/ssl/ssl.html#DEALING_WITH_PROTOCOL_METHODS|SSL_METHODS}.
4962
* @constructor
5063
*/
5164
export class Api {
@@ -58,20 +71,20 @@ export class Api {
5871
_httpOptions: IHttpOptions
5972
serverAcceptsJSON: boolean
6073

61-
constructor(config) {
74+
constructor(config: IApiConfig) {
6275
this.serverAcceptsJSON = true
6376
this._httpOptions = {
6477
url: config.url,
6578
alwaysUseGet: config.alwaysUseGet,
66-
headers: {}
79+
headers: config.headers || {}
6780
}
6881
if (config.apiKey) {
6982
this._httpOptions.headers.apiKey = config.apiKey
7083
}
7184
// Append version to path if provided
7285
let path
7386
const {version = 'internal'}: {
74-
version: string
87+
version?: string
7588
} = config
7689
if (['internal', 'unsupported', 'asp'].indexOf(version) >= 0) {
7790
path = '/attask/api-' + version

0 commit comments

Comments
 (0)