feat(Strapi Node): Add token credentials (#7048)

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Elias Meire
2023-08-31 12:46:28 +02:00
committed by GitHub
parent d72f79ffb3
commit c01bca562b
4 changed files with 138 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
import type { OptionsWithUri } from 'request';
import type {
ICredentialDataDecryptedObject,
IDataObject,
IExecuteFunctions,
IHookFunctions,
@@ -26,7 +27,14 @@ export async function strapiApiRequest(
uri?: string,
headers: IDataObject = {},
) {
const credentials = await this.getCredentials('strapiApi');
const authenticationMethod = this.getNodeParameter('authentication', 0);
let credentials: ICredentialDataDecryptedObject;
if (authenticationMethod === 'password') {
credentials = await this.getCredentials('strapiApi');
} else {
credentials = await this.getCredentials('strapiTokenApi');
}
const url = removeTrailingSlash(credentials.url as string);
@@ -49,7 +57,11 @@ export async function strapiApiRequest(
delete options.body;
}
return await this.helpers?.request(options);
return await this.helpers.requestWithAuthentication.call(
this,
authenticationMethod === 'password' ? 'strapiApi' : 'strapiTokenApi',
options,
);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
@@ -57,7 +69,7 @@ export async function strapiApiRequest(
export async function getToken(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
): Promise<any> {
): Promise<{ jwt: string }> {
const credentials = await this.getCredentials('strapiApi');
const url = removeTrailingSlash(credentials.url as string);
@@ -75,7 +87,7 @@ export async function getToken(
uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
json: true,
};
return this.helpers.request(options);
return this.helpers.request(options) as Promise<{ jwt: string }>;
}
export async function strapiApiRequestAllItems(
@@ -85,9 +97,9 @@ export async function strapiApiRequestAllItems(
body: IDataObject = {},
query: IDataObject = {},
headers: IDataObject = {},
apiVersion: string = 'v3',
) {
const returnData: IDataObject[] = [];
const { apiVersion } = await this.getCredentials('strapiApi');
let responseData;
if (apiVersion === 'v4') {