Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Yourls Node * ⚡ Minor improvements to Yourls-Node Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
- Loading branch information
1 parent
bea7469
commit e770fa3
Showing
6 changed files
with
319 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
ICredentialType, | ||
NodePropertyTypes, | ||
} from 'n8n-workflow'; | ||
|
||
export class YourlsApi implements ICredentialType { | ||
name = 'yourlsApi'; | ||
displayName = 'Yourls API'; | ||
documentationUrl = 'yourls'; | ||
properties = [ | ||
{ | ||
displayName: 'Signature', | ||
name: 'signature', | ||
type: 'string' as NodePropertyTypes, | ||
default: '', | ||
}, | ||
{ | ||
displayName: 'URL', | ||
name: 'url', | ||
type: 'string' as NodePropertyTypes, | ||
default: '', | ||
placeholder: 'http://localhost:8080', | ||
}, | ||
]; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
OptionsWithUri, | ||
} from 'request'; | ||
|
||
import { | ||
IExecuteFunctions, | ||
IExecuteSingleFunctions, | ||
ILoadOptionsFunctions, | ||
} from 'n8n-core'; | ||
|
||
import { | ||
IDataObject, | ||
} from 'n8n-workflow'; | ||
|
||
export async function yourlsApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, body: any = {}, qs: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any | ||
|
||
const credentials = this.getCredentials('yourlsApi') as IDataObject; | ||
|
||
qs.signature = credentials.signature as string; | ||
qs.format = 'json'; | ||
|
||
const options: OptionsWithUri = { | ||
method, | ||
body, | ||
qs, | ||
uri: `${credentials.url}/yourls-api.php`, | ||
json: true, | ||
}; | ||
try { | ||
//@ts-ignore | ||
const response = await this.helpers.request.call(this, options); | ||
|
||
if (response.status === 'fail') { | ||
throw new Error( | ||
`Yourls error response [400]: ${response.message}`, | ||
); | ||
} | ||
|
||
return response; | ||
} catch (error) { | ||
if (error.response && error.response.body && error.response.body.msg) { | ||
|
||
const message = error.response.body.msg; | ||
|
||
// Try to return the error prettier | ||
throw new Error( | ||
`Yourls error response [${error.statusCode}]: ${message}`, | ||
); | ||
} | ||
throw error; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
INodeProperties, | ||
} from 'n8n-workflow'; | ||
|
||
export const urlOperations = [ | ||
{ | ||
displayName: 'Operation', | ||
name: 'operation', | ||
type: 'options', | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'url', | ||
], | ||
}, | ||
}, | ||
options: [ | ||
{ | ||
name: 'Expand', | ||
value: 'expand', | ||
description: 'Expand a URL', | ||
}, | ||
{ | ||
name: 'Shorten', | ||
value: 'shorten', | ||
description: 'Shorten a URL', | ||
}, | ||
{ | ||
name: 'Stats', | ||
value: 'stats', | ||
description: 'Get stats about one short URL', | ||
}, | ||
], | ||
default: 'shorten', | ||
description: 'The operation to perform.', | ||
}, | ||
] as INodeProperties[]; | ||
|
||
export const urlFields = [ | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* url:shorten */ | ||
/* -------------------------------------------------------------------------- */ | ||
{ | ||
displayName: 'URL', | ||
name: 'url', | ||
type: 'string', | ||
required: true, | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'url', | ||
], | ||
operation: [ | ||
'shorten', | ||
], | ||
}, | ||
}, | ||
default: '', | ||
description: 'The URL to shorten.', | ||
}, | ||
{ | ||
displayName: 'Additional Fields', | ||
name: 'additionalFields', | ||
type: 'collection', | ||
placeholder: 'Add Field', | ||
default: {}, | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'url', | ||
], | ||
operation: [ | ||
'shorten', | ||
], | ||
}, | ||
}, | ||
options: [ | ||
{ | ||
displayName: 'Keyword', | ||
name: 'keyword', | ||
type: 'string', | ||
default: '', | ||
}, | ||
{ | ||
displayName: 'Title', | ||
name: 'title', | ||
type: 'string', | ||
default: '', | ||
description: 'Title for custom short URLs', | ||
}, | ||
], | ||
}, | ||
/* -------------------------------------------------------------------------- */ | ||
/* url:expand */ | ||
/* -------------------------------------------------------------------------- */ | ||
{ | ||
displayName: 'Short URL', | ||
name: 'shortUrl', | ||
type: 'string', | ||
required: true, | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'url', | ||
], | ||
operation: [ | ||
'expand', | ||
], | ||
}, | ||
}, | ||
default: '', | ||
description: 'The short URL to expand.', | ||
}, | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* url:stats */ | ||
/* -------------------------------------------------------------------------- */ | ||
{ | ||
displayName: 'Short URL', | ||
name: 'shortUrl', | ||
type: 'string', | ||
required: true, | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'url', | ||
], | ||
operation: [ | ||
'stats', | ||
], | ||
}, | ||
}, | ||
default: '', | ||
description: 'The short URL for which to get stats.', | ||
}, | ||
] as INodeProperties[]; |
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
IExecuteFunctions, | ||
} from 'n8n-core'; | ||
|
||
import { | ||
IDataObject, | ||
INodeExecutionData, | ||
INodeType, | ||
INodeTypeDescription, | ||
} from 'n8n-workflow'; | ||
|
||
import { | ||
yourlsApiRequest, | ||
} from './GenericFunctions'; | ||
|
||
import { | ||
urlFields, | ||
urlOperations, | ||
} from './UrlDescription'; | ||
|
||
export class Yourls implements INodeType { | ||
description: INodeTypeDescription = { | ||
displayName: 'Yourls', | ||
name: 'yourls', | ||
icon: 'file:yourls.png', | ||
group: ['input'], | ||
version: 1, | ||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', | ||
description: 'Consume Yourls API', | ||
defaults: { | ||
name: 'Yourls', | ||
color: '#336498', | ||
}, | ||
inputs: ['main'], | ||
outputs: ['main'], | ||
credentials: [ | ||
{ | ||
name: 'yourlsApi', | ||
required: true, | ||
}, | ||
], | ||
properties: [ | ||
{ | ||
displayName: 'Resource', | ||
name: 'resource', | ||
type: 'options', | ||
options: [ | ||
{ | ||
name: 'URL', | ||
value: 'url', | ||
}, | ||
], | ||
default: 'url', | ||
description: 'The resource to operate on.', | ||
}, | ||
...urlOperations, | ||
...urlFields, | ||
], | ||
}; | ||
|
||
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; | ||
const resource = this.getNodeParameter('resource', 0) as string; | ||
const operation = this.getNodeParameter('operation', 0) as string; | ||
for (let i = 0; i < length; i++) { | ||
if (resource === 'url') { | ||
if (operation === 'shorten') { | ||
const url = this.getNodeParameter('url', i) as string; | ||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; | ||
qs.url = url; | ||
qs.action = 'shorturl'; | ||
Object.assign(qs, additionalFields); | ||
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs); | ||
} | ||
|
||
if (operation === 'expand') { | ||
const shortUrl = this.getNodeParameter('shortUrl', i) as string; | ||
qs.shorturl = shortUrl; | ||
qs.action = 'expand'; | ||
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs); | ||
} | ||
|
||
if (operation === 'stats') { | ||
const shortUrl = this.getNodeParameter('shortUrl', i) as string; | ||
qs.shorturl = shortUrl; | ||
qs.action = 'url-stats'; | ||
responseData = await yourlsApiRequest.call(this, 'GET', {}, qs); | ||
responseData = responseData.link; | ||
} | ||
} | ||
if (Array.isArray(responseData)) { | ||
returnData.push.apply(returnData, responseData as IDataObject[]); | ||
} else { | ||
returnData.push(responseData as IDataObject); | ||
} | ||
} | ||
return [this.helpers.returnJsonArray(returnData)]; | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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