-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add support for setting additional headers and strictApi #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jimmycallin
merged 3 commits into
main
from
backlog/add-support-for-strict-api-calls-in-js-client
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,8 @@ export interface SessionOptions { | |
eventHubOptions?: EventHubOptions; | ||
clientToken?: string; | ||
apiEndpoint?: string; | ||
additionalHeaders?: Data; | ||
strictApi?: boolean; | ||
} | ||
|
||
export interface CreateComponentOptions { | ||
|
@@ -95,11 +97,13 @@ export interface ResponseError { | |
|
||
export interface MutatationOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MutatationOptions ? ;) |
||
pushToken?: string; | ||
additionalHeaders?: Data; | ||
} | ||
|
||
export interface QueryOptions { | ||
abortController?: AbortController; | ||
signal?: AbortSignal; | ||
additionalHeaders?: Data; | ||
} | ||
|
||
export interface CallOptions extends MutatationOptions, QueryOptions {} | ||
|
@@ -121,6 +125,7 @@ export class Session { | |
serverInformation?: Data; | ||
schemas?: Data; | ||
serverVersion?: string; | ||
additionalHeaders: Data; | ||
|
||
/** | ||
* Construct Session instance with API credentials. | ||
|
@@ -134,6 +139,8 @@ export class Session { | |
* @param {Object} [options.eventHubOptions={}] - Options to configure event hub with. | ||
* @param {string} [options.clientToken] - Client token for update events. | ||
* @param {string} [options.apiEndpoint=/api] - API endpoint. | ||
* @param {object} [options.headers] - Additional headers to send with the request | ||
* @param {object} [options.strictApi] - Turn on strict API mode | ||
* | ||
* @constructs Session | ||
*/ | ||
|
@@ -147,6 +154,8 @@ export class Session { | |
eventHubOptions = {}, | ||
clientToken, | ||
apiEndpoint = "/api", | ||
additionalHeaders = {}, | ||
strictApi = false, | ||
}: SessionOptions = {} | ||
) { | ||
if (!serverUrl || !apiUser || !apiKey) { | ||
|
@@ -188,6 +197,21 @@ export class Session { | |
*/ | ||
this.apiEndpoint = apiEndpoint; | ||
|
||
/** | ||
* allows setting additional headers to be sent with each request | ||
* @memberof Session | ||
* @instance | ||
* @type {Data} | ||
*/ | ||
this.additionalHeaders = additionalHeaders; | ||
|
||
if (strictApi) { | ||
this.additionalHeaders = { | ||
...additionalHeaders, | ||
"ftrack-strict-api": "true", | ||
}; | ||
} | ||
|
||
/** | ||
* session event hub | ||
* @memberof Session | ||
|
@@ -486,11 +510,17 @@ export class Session { | |
* @param {AbortController} options.abortController - Abort controller, deprecated in favor of options.signal | ||
* @param {AbortSignal} options.signal - Abort signal | ||
* @param {string} options.pushToken - push token to associate with the request | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* | ||
*/ | ||
call( | ||
operations: operation.Operation[], | ||
{ abortController, pushToken, signal }: CallOptions = {} | ||
{ | ||
abortController, | ||
pushToken, | ||
signal, | ||
additionalHeaders = {}, | ||
}: CallOptions = {} | ||
): Promise<Response<Data>[]> { | ||
const url = `${this.serverUrl}${this.apiEndpoint}`; | ||
|
||
|
@@ -516,6 +546,8 @@ export class Session { | |
"ftrack-user": this.apiUser, | ||
"ftrack-Clienttoken": this.clientToken, | ||
"ftrack-pushtoken": pushToken, | ||
...this.additionalHeaders, | ||
...additionalHeaders, | ||
} as HeadersInit, | ||
body: this.encodeOperations(operations), | ||
signal: abortController ? abortController.signal : signal, | ||
|
@@ -701,12 +733,12 @@ export class Session { | |
* @param {object} options | ||
* @param {object} options.abortController - Deprecated in favour of options.signal | ||
* @param {object} options.signal - Abort signal user for aborting requests prematurely | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* @return {Promise} Promise which will be resolved with an object | ||
* containing action, data and metadata | ||
*/ | ||
query(expression: string, options: QueryOptions = {}) { | ||
logger.debug("Query", expression); | ||
|
||
const queryOperation = operation.query(expression); | ||
let request = this.call([queryOperation], options).then((responses) => { | ||
const response = responses[0]; | ||
|
@@ -728,6 +760,7 @@ export class Session { | |
* @param {object} additionalOptions | ||
* @param {object} options.abortController - Deprecated in favour of options.signal | ||
* @param {object} options.signal - Abort signal user for aborting requests prematurely | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* @return {Promise} Promise which will be resolved with an object | ||
* containing data and metadata | ||
*/ | ||
|
@@ -771,6 +804,7 @@ export class Session { | |
* @param {Object} data data which should be used to populate attributes on the entity. | ||
* @param {Object} options | ||
* @param {string} options.pushToken - push token to associate with the request | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* @return {Promise} Promise which will be resolved with the response. | ||
*/ | ||
create(entityType: string, data: Data, { pushToken }: CallOptions = {}) { | ||
|
@@ -794,6 +828,7 @@ export class Session { | |
* @param {Object} data | ||
* @param {Object} options | ||
* @param {string} options.pushToken - push token to associate with the request | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* @return {Promise} Promise resolved with the response. | ||
*/ | ||
update( | ||
|
@@ -821,6 +856,7 @@ export class Session { | |
* @param {Array} keys Identifying keys, typically [<entity id>] | ||
* @param {Object} options | ||
* @param {string} options.pushToken - push token to associate with the request | ||
* @param {object} options.headers - Additional headers to send with the request | ||
* @return {Promise} Promise resolved with the response. | ||
*/ | ||
delete(type: string, keys: string[], { pushToken }: MutatationOptions = {}) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
options.additionalHeaders
?