2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2020-12-02 14:13:59 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } 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(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
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';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
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}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|