mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Add API key to Twilio credentials (#1797)
* ⚡ Add API key to Twilio credentials * ⚡ Minor simplification Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
@@ -9,6 +9,22 @@ export class TwilioApi implements ICredentialType {
|
|||||||
displayName = 'Twilio API';
|
displayName = 'Twilio API';
|
||||||
documentationUrl = 'twilio';
|
documentationUrl = 'twilio';
|
||||||
properties = [
|
properties = [
|
||||||
|
{
|
||||||
|
displayName: 'Auth Type',
|
||||||
|
name: 'authType',
|
||||||
|
type: 'options' as NodePropertyTypes,
|
||||||
|
default: 'authToken',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'Auth Token',
|
||||||
|
value: 'authToken',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'API Key',
|
||||||
|
value: 'apiKey',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Account SID',
|
displayName: 'Account SID',
|
||||||
name: 'accountSid',
|
name: 'accountSid',
|
||||||
@@ -20,6 +36,42 @@ export class TwilioApi implements ICredentialType {
|
|||||||
name: 'authToken',
|
name: 'authToken',
|
||||||
type: 'string' as NodePropertyTypes,
|
type: 'string' as NodePropertyTypes,
|
||||||
default: '',
|
default: '',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authType: [
|
||||||
|
'authToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'API Key SID',
|
||||||
|
name: 'apiKeySid',
|
||||||
|
type: 'string' as NodePropertyTypes,
|
||||||
|
default: '',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authType: [
|
||||||
|
'apiKey',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'API Key Secret',
|
||||||
|
name: 'apiKeySecret',
|
||||||
|
type: 'string' as NodePropertyTypes,
|
||||||
|
typeOptions: {
|
||||||
|
password: true,
|
||||||
|
},
|
||||||
|
default: '',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authType: [
|
||||||
|
'apiKey',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import {
|
|||||||
IDataObject, NodeApiError, NodeOperationError,
|
IDataObject, NodeApiError, NodeOperationError,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
import {
|
||||||
|
OptionsWithUri,
|
||||||
|
} from 'request';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make an API request to Twilio
|
* Make an API request to Twilio
|
||||||
*
|
*
|
||||||
@@ -17,7 +21,14 @@ import {
|
|||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
||||||
const credentials = this.getCredentials('twilioApi');
|
const credentials = this.getCredentials('twilioApi') as {
|
||||||
|
accountSid: string;
|
||||||
|
authType: 'authToken' | 'apiKey';
|
||||||
|
authToken: string;
|
||||||
|
apiKeySid: string;
|
||||||
|
apiKeySecret: string;
|
||||||
|
};
|
||||||
|
|
||||||
if (credentials === undefined) {
|
if (credentials === undefined) {
|
||||||
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
||||||
}
|
}
|
||||||
@@ -26,18 +37,26 @@ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions,
|
|||||||
query = {};
|
query = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = {
|
const options: OptionsWithUri = {
|
||||||
method,
|
method,
|
||||||
form: body,
|
form: body,
|
||||||
qs: query,
|
qs: query,
|
||||||
uri: `https://api.twilio.com/2010-04-01/Accounts/${credentials.accountSid}${endpoint}`,
|
uri: `https://api.twilio.com/2010-04-01/Accounts/${credentials.accountSid}${endpoint}`,
|
||||||
auth: {
|
|
||||||
user: credentials.accountSid as string,
|
|
||||||
pass: credentials.authToken as string,
|
|
||||||
},
|
|
||||||
json: true,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (credentials.authType === 'apiKey') {
|
||||||
|
options.auth = {
|
||||||
|
user: credentials.apiKeySid,
|
||||||
|
password: credentials.apiKeySecret,
|
||||||
|
};
|
||||||
|
} else if (credentials.authType === 'authToken') {
|
||||||
|
options.auth = {
|
||||||
|
user: credentials.accountSid,
|
||||||
|
pass: credentials.authToken,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request(options);
|
return await this.helpers.request(options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user