2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function yourlsApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('yourlsApi');
|
2020-12-02 14:13:59 -08:00
|
|
|
|
|
|
|
qs.signature = credentials.signature as string;
|
|
|
|
qs.format = 'json';
|
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2020-12-02 14:13:59 -08:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `${credentials.url}/yourls-api.php`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request.call(this, options);
|
|
|
|
|
|
|
|
if (response.status === 'fail') {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2020-12-02 14:13:59 -08:00
|
|
|
`Yourls error response [400]: ${response.message}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-05 02:13:29 -08:00
|
|
|
if (typeof response === 'string' && response.includes('<b>Fatal error</b>')) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
"Yourls responded with a 'Fatal error', check description for more details",
|
|
|
|
{
|
|
|
|
description: `Server response:\n${response}`,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:13:59 -08:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-12-02 14:13:59 -08:00
|
|
|
}
|
|
|
|
}
|