mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
7ce7285f7a
* Changes to types so that credentials can be always loaded from DB This first commit changes all return types from the execute functions and calls to get credentials to be async so we can use await. This is a first step as previously credentials were loaded in memory and always available. We will now be loading them from the DB which requires turning the whole call chain async. * Fix updated files * Removed unnecessary credential loading to improve performance * Fix typo * ⚡ Fix issue * Updated new nodes to load credentials async * ⚡ Remove not needed comment Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError,
|
|
} 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';
|
|
credentials = await this.getCredentials('ghostContentApi') as IDataObject;
|
|
query.key = credentials.apiKey as string;
|
|
} else {
|
|
version = 'v2';
|
|
credentials = await this.getCredentials('ghostAdminApi') as IDataObject;
|
|
// 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) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
query.limit = 50;
|
|
query.page = 1;
|
|
|
|
do {
|
|
responseData = await ghostApiRequest.call(this, method, endpoint, body, query);
|
|
query.page = responseData.meta.pagination.next;
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
} while (
|
|
query.page !== null
|
|
);
|
|
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;
|
|
}
|