2020-12-07 23:40:29 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-12-07 23:40:29 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import * as jwt from 'jsonwebtoken';
|
|
|
|
|
|
|
|
export async function ghostApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const source = this.getNodeParameter('source', 0) as string;
|
|
|
|
|
|
|
|
let credentials;
|
|
|
|
let version;
|
|
|
|
let token;
|
|
|
|
|
|
|
|
if (source === 'contentApi') {
|
|
|
|
//https://ghost.org/faq/api-versioning/
|
|
|
|
version = 'v3';
|
2021-08-20 09:57:30 -07:00
|
|
|
credentials = await this.getCredentials('ghostContentApi') as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
query.key = credentials.apiKey as string;
|
|
|
|
} else {
|
|
|
|
version = 'v2';
|
2021-08-20 09:57:30 -07:00
|
|
|
credentials = await this.getCredentials('ghostAdminApi') as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
// Create the token (including decoding secret)
|
|
|
|
const [id, secret] = (credentials.apiKey as string).split(':');
|
|
|
|
|
|
|
|
token = jwt.sign({}, Buffer.from(secret, 'hex'), {
|
|
|
|
keyid: id,
|
|
|
|
algorithm: 'HS256',
|
|
|
|
expiresIn: '5m',
|
|
|
|
audience: `/${version}/admin/`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs: query,
|
|
|
|
uri: uri || `${credentials.url}/ghost/api/${version}${endpoint}`,
|
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
options.headers = {
|
|
|
|
Authorization: `Ghost ${token}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function ghostApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
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]);
|
|
|
|
} while (
|
2021-06-25 05:03:50 -07:00
|
|
|
query.page !== null
|
2020-12-07 23:40:29 -08:00
|
|
|
);
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|