Add OneSimpleAPI Node (#2360)

* Start of OneSimpleAPI Node

* Node functionality is complete

*  Improvements to #2357

*  Add internal feedback

*  Minor improvements

Co-authored-by: Jonathan <jonathan.bennetts@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2021-11-10 16:48:20 -05:00 committed by GitHub
parent e8133d80f8
commit 3c6f38d045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 974 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class OneSimpleApi implements ICredentialType {
name = 'oneSimpleApi';
displayName = 'One Simple API';
documentationUrl = 'oneSimpleApi';
properties: INodeProperties[] = [
{
displayName: 'API Token',
name: 'apiToken',
type: 'string',
default: '',
},
];
}

View file

@ -0,0 +1,41 @@
import {
OptionsWithUri
} from 'request';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
export async function oneSimpleApiRequest(this: IExecuteFunctions, method: string, resource: string, body: IDataObject = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}) {
const credentials = await this.getCredentials('oneSimpleApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const outputFormat = 'json';
let options: OptionsWithUri = {
method,
body,
qs,
uri: uri || `https://onesimpleapi.com/api${resource}?token=${credentials.apiToken}&output=${outputFormat}`,
json: true,
};
options = Object.assign({}, options, option);
if (Object.keys(body).length === 0) {
delete options.body;
}
try {
const responseData = await this.helpers.request(options);
return responseData;
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}

View file

@ -0,0 +1,20 @@
{
"node": "n8n-nodes-base.oneSimpleApi",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": [
"Utility"
],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/credentials/OneSimpleAPI"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/nodes/n8n-nodes-base.oneSimpleApi/"
}
]
}
}

View file

@ -0,0 +1,867 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
oneSimpleApiRequest,
} from './GenericFunctions';
export class OneSimpleApi implements INodeType {
description: INodeTypeDescription = {
displayName: 'One Simple API',
name: 'oneSimpleApi',
icon: 'file:onesimpleapi.svg',
group: ['transform'],
version: 1,
description: 'A toolbox of no-code utilities',
defaults: {
name: 'One Simple API',
color: '#1A82e2',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'oneSimpleApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Information',
value: 'information',
},
{
name: 'Utility',
value: 'utility',
},
{
name: 'Website',
value: 'website',
},
],
default: 'website',
required: true,
},
// Generation
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'website',
],
},
},
options: [
{
name: 'Generate PDF',
value: 'pdf',
description: 'Generate a PDF from a webpage',
},
{
name: 'Get SEO Data',
value: 'seo',
description: 'Get SEO information from website',
},
{
name: 'Take Screenshot',
value: 'screenshot',
description: 'Create a screenshot from a webpage',
},
],
default: 'pdf',
},
// Information
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'information',
],
},
},
options: [
{
name: 'Exchange Rate',
value: 'exchangeRate',
description: 'Convert a value between currencies',
},
{
name: 'Image Metadata',
value: 'imageMetadata',
description: 'Retrieve image metadata from a URL',
},
],
default: 'exchangeRate',
description: 'The operation to perform.',
},
// Utiliy
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'utility',
],
},
},
options: [
{
name: 'Expand URL',
value: 'expandURL',
description: 'Expand a shortened url',
},
{
name: 'Generate QR Code',
value: 'qrCode',
description: 'Generate a QR Code',
},
{
name: 'Validate Email',
value: 'validateEmail',
description: 'Validate an email address',
},
],
default: 'validateEmail',
description: 'The operation to perform.',
},
// website: pdf
{
displayName: 'Webpage URL',
name: 'link',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'pdf',
],
resource: [
'website',
],
},
},
default: '',
description: 'Link to webpage to convert',
},
{
displayName: 'Download PDF?',
name: 'download',
type: 'boolean',
required: true,
displayOptions: {
show: {
operation: [
'pdf',
],
resource: [
'website',
],
},
},
default: false,
description: 'Whether to download the PDF or return a link to it',
},
{
displayName: 'Put Output In Field',
name: 'output',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'pdf',
],
resource: [
'website',
],
download: [
true,
],
},
},
default: 'data',
description: 'The name of the output field to put the binary file data in',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'website',
],
operation: [
'pdf',
],
},
},
options: [
{
displayName: 'Page Size',
name: 'page',
type: 'options',
options: [
{
name: 'A0',
value: 'A0',
},
{
name: 'A1',
value: 'A1',
},
{
name: 'A2',
value: 'A2',
},
{
name: 'A3',
value: 'A3',
},
{
name: 'A4',
value: 'A4',
},
{
name: 'A5',
value: 'A5',
},
{
name: 'A6',
value: 'A6',
},
{
name: 'Legal',
value: 'Legal',
},
{
name: 'Ledger',
value: 'Ledger',
},
{
name: 'Letter',
value: 'Letter',
},
{
name: 'Tabloid',
value: 'Tabloid',
},
],
default: '',
description: 'The page size',
},
{
displayName: 'Force Refresh',
name: 'force',
type: 'boolean',
default: false,
description: `Normally the API will reuse a previously taken screenshot of the URL to give a faster response.
This option allows you to retake the screenshot at that exact time, for those times when it's necessary`,
},
],
},
// website: qrCode
{
displayName: 'QR Content',
name: 'message',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'qrCode',
],
resource: [
'utility',
],
},
},
default: '',
description: 'The text that should be turned into a QR code - like a website URL',
},
{
displayName: 'Download Image?',
name: 'download',
type: 'boolean',
required: true,
displayOptions: {
show: {
operation: [
'qrCode',
],
resource: [
'utility',
],
},
},
default: false,
description: 'Whether to download the QR code or return a link to it',
},
{
displayName: 'Put Output In Field',
name: 'output',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'qrCode',
],
resource: [
'utility',
],
download: [
true,
],
},
},
default: 'data',
description: 'The name of the output field to put the binary file data in',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'utility',
],
operation: [
'qrCode',
],
},
},
options: [
{
displayName: 'Size',
name: 'size',
type: 'options',
options: [
{
name: 'Small',
value: 'Small',
},
{
name: 'Medium',
value: 'Medium',
},
{
name: 'Large',
value: 'Large',
},
],
default: 'Small',
description: 'The QR Code size',
},
{
displayName: 'Format',
name: 'format',
type: 'options',
options: [
{
name: 'PNG',
value: 'PNG',
},
{
name: 'SVG',
value: 'SVG',
},
],
default: 'PNG',
description: 'The QR Code format',
},
],
},
// website: screenshot
{
displayName: 'Webpage URL',
name: 'link',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'screenshot',
],
resource: [
'website',
],
},
},
default: '',
description: 'Link to webpage to convert',
},
{
displayName: 'Download Screenshot?',
name: 'download',
type: 'boolean',
required: true,
displayOptions: {
show: {
operation: [
'screenshot',
],
resource: [
'website',
],
},
},
default: false,
description: 'Whether to download the screenshot or return a link to it',
},
{
displayName: 'Put Output In Field',
name: 'output',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'screenshot',
],
resource: [
'website',
],
download: [
true,
],
},
},
default: 'data',
description: 'The name of the output field to put the binary file data in',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'website',
],
operation: [
'screenshot',
],
},
},
options: [
{
displayName: 'Screen Size',
name: 'screen',
type: 'options',
options: [
{
name: 'Phone',
value: 'phone',
},
{
name: 'Phone Landscape',
value: 'phone-landscape',
},
{
name: 'Retina',
value: 'retina',
},
{
name: 'Tablet',
value: 'tablet',
},
{
name: 'Tablet Landscape',
value: 'tablet-landscape',
},
],
default: '',
description: 'The screen size',
},
{
displayName: 'Force Refresh',
name: 'force',
type: 'boolean',
default: false,
description: `Normally the API will reuse a previously taken screenshot of the URL to give a faster response.
This option allows you to retake the screenshot at that exact time, for those times when it's necessary`,
},
{
displayName: 'Full Page',
name: 'fullpage',
type: 'boolean',
default: false,
description: 'The API takes a screenshot of the viewable area for the desired screen size. If you need a screenshot of the whole length of the page, use this option',
},
],
},
// information: exchangeRate
{
displayName: 'Value',
name: 'value',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'exchangeRate',
],
resource: [
'information',
],
},
},
default: '',
description: 'Value to convert',
},
{
displayName: 'From Currency',
name: 'fromCurrency',
type: 'string',
required: true,
placeholder: 'USD',
displayOptions: {
show: {
operation: [
'exchangeRate',
],
resource: [
'information',
],
},
},
default: '',
description: 'From Currency',
},
{
displayName: 'To Currency',
name: 'toCurrency',
type: 'string',
placeholder: 'EUR',
required: true,
displayOptions: {
show: {
operation: [
'exchangeRate',
],
resource: [
'information',
],
},
},
default: '',
description: 'To Currency',
},
// information: imageMetadata
{
displayName: 'Link To Image',
name: 'link',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'imageMetadata',
],
resource: [
'information',
],
},
},
default: '',
description: 'Image to get metadata from',
},
// website: seo
{
displayName: 'Webpage URL',
name: 'link',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'seo',
],
resource: [
'website',
],
},
},
default: '',
description: 'Webpage to get SEO information for',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'website',
],
operation: [
'seo',
],
},
},
options: [
{
displayName: 'Include Headers?',
name: 'headers',
type: 'boolean',
default: false,
description: '',
},
],
},
// utility: validateEmail
{
displayName: 'Email Address',
name: 'emailAddress',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'validateEmail',
],
resource: [
'utility',
],
},
},
default: '',
description: 'Email Address',
},
// utility: expandURL
{
displayName: 'URL',
name: 'link',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'expandURL',
],
resource: [
'utility',
],
},
},
default: '',
description: 'URL to unshorten',
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
const qs: IDataObject = {};
let responseData;
let download;
for (let i = 0; i < length; i++) {
try {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
if (resource === 'website') {
if (operation === 'pdf') {
const link = this.getNodeParameter('link', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
download = this.getNodeParameter('download', i) as boolean;
qs.url = link;
if (options.page) {
qs.page = options.page as string;
}
if (options.force) {
qs.force = 'yes';
} else {
qs.force = 'no';
}
const response = await oneSimpleApiRequest.call(this, 'GET', '/pdf', {}, qs);
if (download) {
const output = this.getNodeParameter('output', i) as string;
const buffer = await oneSimpleApiRequest.call(this, 'GET', '', {}, {}, response.url, { json: false, encoding: null }) as Buffer;
responseData = {
json: response,
binary: {
[output]: await this.helpers.prepareBinaryData(buffer),
},
};
} else {
responseData = response;
}
}
if (operation === 'screenshot') {
const link = this.getNodeParameter('link', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
download = this.getNodeParameter('download', i) as boolean;
qs.url = link;
if (options.screen) {
qs.screen = options.screen as string;
}
if (options.fullpage) {
qs.fullpage = 'yes';
} else {
qs.fullpage = 'no';
}
if (options.force) {
qs.force = 'yes';
} else {
qs.force = 'no';
}
const response = await oneSimpleApiRequest.call(this, 'GET', '/screenshot', {}, qs);
if (download) {
const output = this.getNodeParameter('output', i) as string;
const buffer = await oneSimpleApiRequest.call(this, 'GET', '', {}, {}, response.url, { json: false, encoding: null }) as Buffer;
responseData = {
json: response,
binary: {
[output]: await this.helpers.prepareBinaryData(buffer),
},
};
} else {
responseData = response;
}
}
if (operation === 'seo') {
const link = this.getNodeParameter('link', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
qs.url = link;
if (options.headers) {
qs.headers = 'yes';
}
responseData = await oneSimpleApiRequest.call(this, 'GET', '/page_info', {}, qs);
}
}
if (resource === 'information') {
if (operation === 'exchangeRate') {
const value = this.getNodeParameter('value', i) as string;
const fromCurrency = this.getNodeParameter('fromCurrency', i) as string;
const toCurrency = this.getNodeParameter('toCurrency', i) as string;
qs.from_currency = fromCurrency;
qs.to_currency = toCurrency;
qs.from_value = value;
responseData = await oneSimpleApiRequest.call(this, 'GET', '/exchange_rate', {}, qs);
}
if (operation === 'imageMetadata') {
const link = this.getNodeParameter('link', i) as string;
qs.url = link;
qs.raw = true;
responseData = await oneSimpleApiRequest.call(this, 'GET', '/image_info', {}, qs);
}
}
if (resource === 'utility') {
// validateEmail
if (operation === 'validateEmail') {
const emailAddress = this.getNodeParameter('emailAddress', i) as string;
qs.email = emailAddress;
responseData = await oneSimpleApiRequest.call(this, 'GET', '/email', {}, qs);
}
// expandURL
if (operation === 'expandURL') {
const url = this.getNodeParameter('link', i) as string;
qs.url = url;
responseData = await oneSimpleApiRequest.call(this, 'GET', '/unshorten', {}, qs);
}
if (operation === 'qrCode') {
const message = this.getNodeParameter('message', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
download = this.getNodeParameter('download', i) as boolean;
qs.message = message;
if (options.size) {
qs.size = options.size as string;
}
if (options.format) {
qs.format = options.format as string;
}
const response = await oneSimpleApiRequest.call(this, 'GET', '/qr_code', {}, qs);
if (download) {
const output = this.getNodeParameter('output', i) as string;
const buffer = await oneSimpleApiRequest.call(this, 'GET', '', {}, {}, response.url, { json: false, encoding: null }) as Buffer;
responseData = {
json: response,
binary: {
[output]: await this.helpers.prepareBinaryData(buffer),
},
};
} else {
responseData = response;
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
}
if (download) {
return this.prepareOutputData(returnData as unknown as INodeExecutionData[]);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,25 @@
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 512.000000 512.000000" preserveAspectRatio="xMidYMid meet" class="block h-9 w-auto"><g transform="translate(0.000000,512.000000) scale(0.100000,-0.100000)" fill="#2563eb" stroke="none"><path d="M381 5100 c-91 -24 -149 -57 -221 -126 -72 -69 -115 -142 -140 -238
-19 -77 -20 -109 -20 -1052 l0 -973 37 5 c128 17 191 43 259 106 57 53 90 112
115 205 19 69 22 123 29 468 9 422 13 455 70 603 98 254 304 439 609 543 86
30 84 32 121 -88 18 -60 35 -113 37 -119 2 -7 -35 -31 -84 -54 -165 -80 -274
-220 -323 -415 -28 -110 -39 -273 -40 -585 -1 -287 -10 -367 -53 -482 -47
-123 -136 -235 -247 -308 -23 -15 -41 -31 -41 -34 0 -4 28 -25 62 -48 113 -75
203 -209 250 -368 20 -69 22 -108 30 -465 7 -332 11 -404 28 -485 44 -207 158
-367 319 -448 44 -22 85 -41 92 -44 10 -3 4 -32 -25 -118 -21 -63 -41 -119
-45 -123 -8 -9 -164 45 -245 85 -97 49 -158 92 -234 164 -87 82 -128 136 -179
239 -84 168 -102 280 -102 640 -1 309 -10 443 -39 535 -50 161 -167 256 -340
276 l-61 7 0 -970 c0 -940 1 -972 20 -1049 25 -96 68 -169 140 -238 73 -70
131 -102 225 -126 77 -20 111 -20 2175 -20 2070 0 2098 0 2176 20 96 25 169
68 238 140 70 73 102 131 126 225 19 76 20 111 20 1048 l0 969 -56 -7 c-186
-23 -302 -128 -350 -317 -12 -49 -18 -150 -24 -453 -5 -214 -13 -412 -19 -440
-77 -353 -286 -577 -664 -710 -43 -15 -79 -26 -81 -24 -11 13 -77 232 -73 242
3 7 23 19 44 25 107 32 247 162 307 284 72 146 78 188 86 618 9 447 17 504 92
657 40 83 146 192 237 245 l63 37 -49 25 c-107 54 -200 150 -258 265 -68 134
-85 271 -85 681 0 291 -9 377 -50 500 -30 89 -100 198 -165 259 -52 47 -181
121 -211 121 -20 0 -18 12 23 138 l38 113 34 -6 c19 -4 68 -20 110 -36 277
-110 459 -282 551 -521 57 -149 61 -181 70 -608 6 -308 11 -409 24 -458 48
-189 156 -284 354 -311 l52 -8 0 972 c0 939 -1 974 -20 1050 -24 94 -56 152
-126 225 -69 72 -142 115 -238 140 -78 20 -105 20 -2180 19 -2045 0 -2103 -1
-2175 -19z m2519 -2295 l0 -1575 -200 0 -200 0 0 1325 c0 729 -2 1325 -5 1325
-3 0 -141 -50 -307 -111 -167 -61 -345 -126 -396 -145 l-92 -33 2 181 3 181
560 213 c334 126 575 212 598 213 l37 1 0 -1575z"></path></g></svg>

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -201,6 +201,7 @@
"dist/credentials/NotionOAuth2Api.credentials.js", "dist/credentials/NotionOAuth2Api.credentials.js",
"dist/credentials/OAuth1Api.credentials.js", "dist/credentials/OAuth1Api.credentials.js",
"dist/credentials/OAuth2Api.credentials.js", "dist/credentials/OAuth2Api.credentials.js",
"dist/credentials/OneSimpleApi.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/OrbitApi.credentials.js", "dist/credentials/OrbitApi.credentials.js",
"dist/credentials/OuraApi.credentials.js", "dist/credentials/OuraApi.credentials.js",
@ -520,6 +521,7 @@
"dist/nodes/Notion/NotionTrigger.node.js", "dist/nodes/Notion/NotionTrigger.node.js",
"dist/nodes/N8nTrainingCustomerDatastore.node.js", "dist/nodes/N8nTrainingCustomerDatastore.node.js",
"dist/nodes/N8nTrainingCustomerMessenger.node.js", "dist/nodes/N8nTrainingCustomerMessenger.node.js",
"dist/nodes/OneSimpleApi/OneSimpleApi.node.js",
"dist/nodes/OpenThesaurus/OpenThesaurus.node.js", "dist/nodes/OpenThesaurus/OpenThesaurus.node.js",
"dist/nodes/OpenWeatherMap.node.js", "dist/nodes/OpenWeatherMap.node.js",
"dist/nodes/Orbit/Orbit.node.js", "dist/nodes/Orbit/Orbit.node.js",