Merge pull request #648 from n8n-io/Typeform-OAuth2-support

Typeform OAuth2 support
This commit is contained in:
Ricardo Espinoza 2020-06-14 19:31:01 -04:00 committed by GitHub
commit 35e838800b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 12 deletions

View file

@ -0,0 +1,47 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class TypeformOAuth2Api implements ICredentialType {
name = 'typeformOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Typeform OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://api.typeform.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://api.typeform.com/oauth/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: 'webhooks:write,webhooks:read,forms:read,',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'header',
},
];
}

View file

@ -45,18 +45,10 @@ export interface ITypeformAnswerField {
* @returns {Promise<any>}
*/
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('typeformApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
query = query || {};
const authenticationMethod = this.getNodeParameter('authentication', 0);
const options: OptionsWithUri = {
headers: {
'Authorization': `bearer ${credentials.accessToken}`,
},
headers: {},
method,
body,
qs: query,
@ -64,8 +56,23 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
json: true,
};
query = query || {};
try {
return await this.helpers.request!(options);
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('typeformApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.headers!['Authorization'] = `bearer ${credentials.accessToken}`;
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'typeformOAuth2Api', options);
}
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error

View file

@ -37,7 +37,25 @@ export class TypeformTrigger implements INodeType {
{
name: 'typeformApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'typeformOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
{
@ -48,6 +66,23 @@ export class TypeformTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.',
},
{
displayName: 'Form',
name: 'formId',

View file

@ -125,6 +125,7 @@
"dist/credentials/TwilioApi.credentials.js",
"dist/credentials/TwitterOAuth1Api.credentials.js",
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/TypeformOAuth2Api.credentials.js",
"dist/credentials/TogglApi.credentials.js",
"dist/credentials/UpleadApi.credentials.js",
"dist/credentials/VeroApi.credentials.js",