2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-12-07 23:40:29 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function ghostApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2020-12-07 23:40:29 -08:00
|
|
|
const source = this.getNodeParameter('source', 0) as string;
|
|
|
|
|
|
|
|
let version;
|
2022-05-14 01:39:28 -07:00
|
|
|
let credentialType;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
if (source === 'contentApi') {
|
|
|
|
//https://ghost.org/faq/api-versioning/
|
|
|
|
version = 'v3';
|
2022-05-14 01:39:28 -07:00
|
|
|
credentialType = 'ghostContentApi';
|
2020-12-07 23:40:29 -08:00
|
|
|
} else {
|
|
|
|
version = 'v2';
|
2022-05-14 01:39:28 -07:00
|
|
|
credentialType = 'ghostAdminApi';
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const credentials = await this.getCredentials(credentialType);
|
2022-05-14 01:39:28 -07:00
|
|
|
|
2020-12-07 23:40:29 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${credentials.url}/ghost/api/${version}${endpoint}`,
|
2020-12-07 23:40:29 -08:00
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function ghostApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-12-07 23:40:29 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
2021-06-25 05:03:50 -07:00
|
|
|
query.limit = 50;
|
|
|
|
query.page = 1;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
do {
|
2021-06-25 06:52:57 -07:00
|
|
|
responseData = await ghostApiRequest.call(this, method, endpoint, body, query);
|
2021-06-25 05:03:50 -07:00
|
|
|
query.page = responseData.meta.pagination.next;
|
2020-12-07 23:40:29 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (query.page !== null);
|
2020-12-07 23:40:29 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2020-12-07 23:40:29 -08:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|