mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54: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>
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
IWebhookFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const credentials = await this.getCredentials('strapiApi') as IDataObject;
|
|
|
|
try {
|
|
const options: OptionsWithUri = {
|
|
headers: {},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: uri || `${credentials.url}${resource}`,
|
|
json: true,
|
|
};
|
|
if (Object.keys(headers).length !== 0) {
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
}
|
|
if (Object.keys(body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
|
|
//@ts-ignore
|
|
return await this.helpers?.request(options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
|
|
const credentials = await this.getCredentials('strapiApi') as IDataObject;
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'content-type': `application/json`,
|
|
},
|
|
method: 'POST',
|
|
uri: `${credentials.url}/auth/local`,
|
|
body: {
|
|
identifier: credentials.email,
|
|
password: credentials.password,
|
|
},
|
|
json: true,
|
|
};
|
|
|
|
return this.helpers.request!(options);
|
|
}
|
|
|
|
export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
query._limit = 20;
|
|
|
|
query._start = 0;
|
|
|
|
do {
|
|
responseData = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers);
|
|
query._start += query._limit;
|
|
returnData.push.apply(returnData, responseData);
|
|
} while (
|
|
responseData.length !== 0
|
|
);
|
|
|
|
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;
|
|
}
|