Move "organizationId" in credentials and fix error messages

This commit is contained in:
Jan Oberhauser 2019-12-09 12:07:48 +01:00
parent 9250163c8c
commit b36efd4c9d
5 changed files with 47 additions and 99 deletions

View file

@ -8,6 +8,12 @@ export class FlowApi implements ICredentialType {
name = 'flowApi'; name = 'flowApi';
displayName = 'Flow API'; displayName = 'Flow API';
properties = [ properties = [
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'number' as NodePropertyTypes,
default: 0,
},
{ {
displayName: 'Access Token', displayName: 'Access Token',
name: 'accessToken', name: 'accessToken',

View file

@ -64,6 +64,12 @@ export class Flow implements INodeType {
}; };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('flowApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = items.length as unknown as number; const length = items.length as unknown as number;
@ -76,12 +82,11 @@ export class Flow implements INodeType {
if (resource === 'task') { if (resource === 'task') {
//https://developer.getflow.com/api/#tasks_create-task //https://developer.getflow.com/api/#tasks_create-task
if (operation === 'create') { if (operation === 'create') {
const organizationId = this.getNodeParameter('organizationId', i) as string;
const workspaceId = this.getNodeParameter('workspaceId', i) as string; const workspaceId = this.getNodeParameter('workspaceId', i) as string;
const name = this.getNodeParameter('name', i) as string; const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: ITask = { const body: ITask = {
organization_id: parseInt(organizationId, 10), organization_id: credentials.organizationId as number,
}; };
const task: TaskInfo = { const task: TaskInfo = {
name, name,
@ -134,17 +139,16 @@ export class Flow implements INodeType {
responseData = await flowApiRequest.call(this, 'POST', '/tasks', body); responseData = await flowApiRequest.call(this, 'POST', '/tasks', body);
responseData = responseData.task; responseData = responseData.task;
} catch (err) { } catch (err) {
throw new Error(`Flow Error: ${JSON.stringify(err)}`); throw new Error(`Flow Error: ${err.message}`);
} }
} }
//https://developer.getflow.com/api/#tasks_update-a-task //https://developer.getflow.com/api/#tasks_update-a-task
if (operation === 'update') { if (operation === 'update') {
const organizationId = this.getNodeParameter('organizationId', i) as string;
const workspaceId = this.getNodeParameter('workspaceId', i) as string; const workspaceId = this.getNodeParameter('workspaceId', i) as string;
const taskId = this.getNodeParameter('taskId', i) as string; const taskId = this.getNodeParameter('taskId', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject; const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: ITask = { const body: ITask = {
organization_id: parseInt(organizationId, 10), organization_id: credentials.organizationId as number,
}; };
const task: TaskInfo = { const task: TaskInfo = {
workspace_id: parseInt(workspaceId, 10), workspace_id: parseInt(workspaceId, 10),
@ -203,30 +207,28 @@ export class Flow implements INodeType {
responseData = await flowApiRequest.call(this, 'PUT', `/tasks/${taskId}`, body); responseData = await flowApiRequest.call(this, 'PUT', `/tasks/${taskId}`, body);
responseData = responseData.task; responseData = responseData.task;
} catch (err) { } catch (err) {
throw new Error(`Flow Error: ${JSON.stringify(err)}`); throw new Error(`Flow Error: ${err.message}`);
} }
} }
//https://developer.getflow.com/api/#tasks_get-task //https://developer.getflow.com/api/#tasks_get-task
if (operation === 'get') { if (operation === 'get') {
const organizationId = this.getNodeParameter('organizationId', i) as string;
const taskId = this.getNodeParameter('taskId', i) as string; const taskId = this.getNodeParameter('taskId', i) as string;
const filters = this.getNodeParameter('filters', i) as IDataObject; const filters = this.getNodeParameter('filters', i) as IDataObject;
qs.organization_id = organizationId; qs.organization_id = credentials.organizationId as number;
if (filters.include) { if (filters.include) {
qs.include = (filters.include as string[]).join(','); qs.include = (filters.include as string[]).join(',');
} }
try { try {
responseData = await flowApiRequest.call(this,'GET', `/tasks/${taskId}`, {}, qs); responseData = await flowApiRequest.call(this,'GET', `/tasks/${taskId}`, {}, qs);
} catch (err) { } catch (err) {
throw new Error(`Flow Error: ${JSON.stringify(err)}`); throw new Error(`Flow Error: ${err.message}`);
} }
} }
//https://developer.getflow.com/api/#tasks_get-tasks //https://developer.getflow.com/api/#tasks_get-tasks
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const organizationId = this.getNodeParameter('organizationId', i) as string;
const filters = this.getNodeParameter('filters', i) as IDataObject; const filters = this.getNodeParameter('filters', i) as IDataObject;
qs.organization_id = organizationId; qs.organization_id = credentials.organizationId as number;
if (filters.include) { if (filters.include) {
qs.include = (filters.include as string[]).join(','); qs.include = (filters.include as string[]).join(',');
} }
@ -263,7 +265,7 @@ export class Flow implements INodeType {
responseData = responseData.tasks; responseData = responseData.tasks;
} }
} catch (err) { } catch (err) {
throw new Error(`Flow Error: ${JSON.stringify(err)}`); throw new Error(`Flow Error: ${err.message}`);
} }
} }
} }

View file

@ -43,14 +43,6 @@ export class FlowTrigger implements INodeType {
}, },
], ],
properties: [ properties: [
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
required: true,
default: '',
description: 'Organization',
},
{ {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
@ -116,6 +108,12 @@ export class FlowTrigger implements INodeType {
webhookMethods = { webhookMethods = {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('flowApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let webhooks; let webhooks;
const qs: IDataObject = {}; const qs: IDataObject = {};
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
@ -125,7 +123,7 @@ export class FlowTrigger implements INodeType {
if (!(webhookData.webhookIds as [number]).length) { if (!(webhookData.webhookIds as [number]).length) {
return false; return false;
} }
qs.organization_id = this.getNodeParameter('organizationId') as string; qs.organization_id = credentials.organizationId as number;
const endpoint = `/integration_webhooks`; const endpoint = `/integration_webhooks`;
try { try {
webhooks = await flowApiRequest.call(this, 'GET', endpoint, {}, qs); webhooks = await flowApiRequest.call(this, 'GET', endpoint, {}, qs);
@ -144,10 +142,15 @@ export class FlowTrigger implements INodeType {
return true; return true;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('flowApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let resourceIds, body, responseData; let resourceIds, body, responseData;
const webhookUrl = this.getNodeWebhookUrl('default'); const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const organizationId = this.getNodeParameter('organizationId') as string;
const resource = this.getNodeParameter('resource') as string; const resource = this.getNodeParameter('resource') as string;
const endpoint = `/integration_webhooks`; const endpoint = `/integration_webhooks`;
if (resource === 'list') { if (resource === 'list') {
@ -159,7 +162,7 @@ export class FlowTrigger implements INodeType {
// @ts-ignore // @ts-ignore
for (const resourceId of resourceIds ) { for (const resourceId of resourceIds ) {
body = { body = {
organization_id: organizationId, organization_id: credentials.organizationId as number,
integration_webhook: { integration_webhook: {
name: 'n8n-trigger', name: 'n8n-trigger',
url: webhookUrl, url: webhookUrl,
@ -183,9 +186,15 @@ export class FlowTrigger implements INodeType {
return true; return true;
}, },
async delete(this: IHookFunctions): Promise<boolean> { async delete(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('flowApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const qs: IDataObject = {}; const qs: IDataObject = {};
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
qs.organization_id = this.getNodeParameter('organizationId') as string; qs.organization_id = credentials.organizationId as number;
// @ts-ignore // @ts-ignore
if (webhookData.webhookIds.length > 0) { if (webhookData.webhookIds.length > 0) {
// @ts-ignore // @ts-ignore

View file

@ -25,17 +25,16 @@ export async function flowApiRequest(this: IHookFunctions | IExecuteFunctions |
if (Object.keys(options.body).length === 0) { if (Object.keys(options.body).length === 0) {
delete options.body; delete options.body;
} }
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
console.error(error); let errorMessage = error.message;
if (error.response.body) {
const errorMessage = error.response.body.message || error.response.body.Message; errorMessage = error.response.body.message || error.response.body.Message || error.message;
if (errorMessage !== undefined) {
throw errorMessage;
} }
throw error.response.body;
throw new Error(errorMessage);
} }
} }

View file

@ -44,23 +44,6 @@ export const taskFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* task:create */ /* task:create */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create'
]
},
},
description: 'Select resources belonging to an organization.',
},
{ {
displayName: 'Workspace ID', displayName: 'Workspace ID',
name: 'workspaceId', name: 'workspaceId',
@ -246,23 +229,6 @@ export const taskFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* task:update */ /* task:update */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'update'
]
},
},
description: 'Select resources belonging to an organization.',
},
{ {
displayName: 'Workspace ID', displayName: 'Workspace ID',
name: 'workspaceId', name: 'workspaceId',
@ -449,23 +415,6 @@ export const taskFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* task:get */ /* task:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'get'
]
},
},
description: 'Select resources belonging to an organization.',
},
{ {
displayName: 'Task ID', displayName: 'Task ID',
name: 'taskId', name: 'taskId',
@ -529,23 +478,6 @@ export const taskFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* task:getAll */ /* task:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'getAll'
]
},
},
description: 'Select resources belonging to an organization.',
},
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',