-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth,messages,server-client): Made auth handlers async (#801)
- Loading branch information
1 parent
141c4b8
commit 0fa9d7f
Showing
6 changed files
with
118 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,51 +1,54 @@ | ||
import { Client } from '@vonage/server-client' | ||
import { MessageObject } from './interfaces/MessageObject' | ||
import { MessagesSendResponse } from './types' | ||
import { Client } from '@vonage/server-client'; | ||
import { MessageObject } from './interfaces/MessageObject'; | ||
import { MessagesSendResponse } from './types'; | ||
|
||
const stripUndefined = (obj) => { | ||
for (const key in obj) { | ||
if (typeof obj[key] === 'object') { | ||
obj[key] = stripUndefined(obj[key]) | ||
} else if (!obj[key]) { | ||
delete obj[key] | ||
} | ||
for (const key in obj) { | ||
if (typeof obj[key] === 'object') { | ||
obj[key] = stripUndefined(obj[key]); | ||
} else if (!obj[key]) { | ||
delete obj[key]; | ||
} | ||
} | ||
|
||
return obj | ||
} | ||
return obj; | ||
}; | ||
|
||
export class Messages extends Client { | ||
/** | ||
/** | ||
* Handle various ways the Messages API handles auth | ||
* The Messages API handles both JWT (preferred) as well as Basic so we | ||
* cannot just set a local authType | ||
* | ||
* @param {any} request - Object containing request data | ||
*/ | ||
public addAuthenticationToRequest(request: any) { | ||
if (this.auth.applicationId && this.auth.privateKey) { | ||
request.headers = Object.assign({}, request.headers, { | ||
Authorization: this.auth.createBearerHeader(), | ||
}) | ||
} else if (this.auth.signature) { | ||
request.data = Object.assign( | ||
request.data, | ||
this.auth.createSignatureHash(request.data) | ||
) | ||
} else { | ||
request.data = Object.assign( | ||
request.data, | ||
this.auth.getQueryParams(request.data) | ||
) | ||
} | ||
|
||
return request | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public async addAuthenticationToRequest(request: any) { | ||
if (this.auth.applicationId && this.auth.privateKey) { | ||
request.headers = Object.assign({}, request.headers, { | ||
Authorization: await this.auth.createBearerHeader(), | ||
}); | ||
} else if (this.auth.signature) { | ||
request.data = Object.assign( | ||
request.data, | ||
await this.auth.createSignatureHash(request.data), | ||
); | ||
} else { | ||
request.data = Object.assign( | ||
request.data, | ||
await this.auth.getQueryParams(request.data), | ||
); | ||
} | ||
|
||
public async send(message: MessageObject) { | ||
const data = stripUndefined(message) | ||
const resp = await this.sendPostRequest<MessagesSendResponse>( | ||
`${this.config.apiHost}/v1/messages`, | ||
data | ||
) | ||
return resp.data | ||
} | ||
return request; | ||
} | ||
|
||
public async send(message: MessageObject) { | ||
const data = stripUndefined(message); | ||
const resp = await this.sendPostRequest<MessagesSendResponse>( | ||
`${this.config.apiHost}/v1/messages`, | ||
data, | ||
); | ||
return resp.data; | ||
} | ||
} |
This file contains 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 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