2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-05-27 09:04:56 -07:00
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHttpRequestOptions,
|
|
|
|
IN8nHttpFullResponse,
|
|
|
|
INodeExecutionData,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-05-27 09:04:56 -07:00
|
|
|
|
|
|
|
// Regular expressions used to extract binId from parameter value
|
|
|
|
const BIN_ID_REGEX = /\b\d{13}-\d{13}\b/g;
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
/**
|
|
|
|
* Extracts the PostBin Bin Id from the specified string.
|
|
|
|
* This method should be able to extract bin Id from the
|
|
|
|
* PostBin URL or from the string in the following format:
|
|
|
|
* `Bin '<binId>'.`
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function parseBinId(context: IExecuteSingleFunctions) {
|
|
|
|
const binId = context.getNodeParameter('binId') as string;
|
|
|
|
// Test if the Bin id is in the expected format
|
|
|
|
BIN_ID_REGEX.lastIndex = 0;
|
|
|
|
const idMatch = BIN_ID_REGEX.exec(binId);
|
|
|
|
|
|
|
|
// Return what is matched
|
|
|
|
if (idMatch) {
|
|
|
|
return idMatch[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not recognized, error out
|
|
|
|
throw new NodeApiError(
|
|
|
|
context.getNode(),
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
message: 'Bin ID format is not valid',
|
|
|
|
description: 'Please check the provided Bin ID and try again.',
|
|
|
|
parseXml: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-27 09:04:56 -07:00
|
|
|
/**
|
|
|
|
* Creates correctly-formatted PostBin API URL based on the entered binId.
|
|
|
|
* This function makes sure binId is in the expected format by parsing it
|
|
|
|
* from current node parameter value.
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function buildBinAPIURL(
|
|
|
|
this: IExecuteSingleFunctions,
|
|
|
|
requestOptions: IHttpRequestOptions,
|
|
|
|
): Promise<IHttpRequestOptions> {
|
2022-05-27 09:04:56 -07:00
|
|
|
const binId = parseBinId(this);
|
|
|
|
// Assemble the PostBin API URL and put it back to requestOptions
|
|
|
|
requestOptions.url = `/developers/postbin/api/bin/${binId}`;
|
|
|
|
|
|
|
|
return requestOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates correctly-formatted PostBin Bin test URL based on the entered binId.
|
|
|
|
* This function makes sure binId is in the expected format by parsing it
|
|
|
|
* from current node parameter value.
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function buildBinTestURL(
|
|
|
|
this: IExecuteSingleFunctions,
|
|
|
|
requestOptions: IHttpRequestOptions,
|
|
|
|
): Promise<IHttpRequestOptions> {
|
2022-05-27 09:04:56 -07:00
|
|
|
const binId = parseBinId(this);
|
|
|
|
|
|
|
|
// Assemble the PostBin API URL and put it back to requestOptions
|
|
|
|
requestOptions.url = `/developers/postbin/${binId}`;
|
|
|
|
return requestOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates correctly-formatted PostBin API URL based on the entered binId and reqId.
|
|
|
|
* This function makes sure binId is in the expected format by parsing it
|
|
|
|
* from current node parameter value.
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function buildRequestURL(
|
|
|
|
this: IExecuteSingleFunctions,
|
|
|
|
requestOptions: IHttpRequestOptions,
|
|
|
|
): Promise<IHttpRequestOptions> {
|
2022-05-27 09:04:56 -07:00
|
|
|
const reqId = this.getNodeParameter('requestId', 'shift') as string;
|
|
|
|
const binId = parseBinId(this);
|
|
|
|
|
|
|
|
requestOptions.url = `/developers/postbin/api/bin/${binId}/req/${reqId}`;
|
|
|
|
return requestOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the bin response data and adds additional properties
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function transformBinReponse(
|
|
|
|
this: IExecuteSingleFunctions,
|
|
|
|
items: INodeExecutionData[],
|
2022-11-08 06:28:21 -08:00
|
|
|
_response: IN8nHttpFullResponse,
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<INodeExecutionData[]> {
|
|
|
|
items.forEach(
|
|
|
|
(item) =>
|
|
|
|
(item.json = {
|
|
|
|
binId: item.json.binId,
|
|
|
|
nowTimestamp: item.json.now,
|
|
|
|
nowIso: new Date(item.json.now as string).toISOString(),
|
|
|
|
expiresTimestamp: item.json.expires,
|
|
|
|
expiresIso: new Date(item.json.expires as string).toISOString(),
|
2023-01-13 09:11:56 -08:00
|
|
|
requestUrl: 'https://www.toptal.com/developers/postbin/' + (item.json.binId as string),
|
|
|
|
viewUrl: 'https://www.toptal.com/developers/postbin/b/' + (item.json.binId as string),
|
2022-08-17 08:50:24 -07:00
|
|
|
}),
|
|
|
|
);
|
2022-05-27 09:04:56 -07:00
|
|
|
return items;
|
|
|
|
}
|