fix(Strapi Node): Strapi credentials notice (#6289)

This commit is contained in:
Michael Kret
2023-05-23 12:00:02 +03:00
committed by GitHub
parent ed7f3b845f
commit bbe6d4c4db
3 changed files with 25 additions and 13 deletions

View File

@@ -8,6 +8,12 @@ export class StrapiApi implements ICredentialType {
documentationUrl = 'strapi'; documentationUrl = 'strapi';
properties: INodeProperties[] = [ properties: INodeProperties[] = [
{
displayName: 'Make sure you are using a user account not an admin account',
name: 'notice',
type: 'notice',
default: '',
},
{ {
displayName: 'Email', displayName: 'Email',
name: 'email', name: 'email',

View File

@@ -10,11 +10,17 @@ import type {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow'; import { NodeApiError } from 'n8n-workflow';
export const removeTrailingSlash = (url: string) => {
if (url.endsWith('/')) {
return url.slice(0, -1);
}
return url;
};
export async function strapiApiRequest( export async function strapiApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
method: string, method: string,
resource: string, resource: string,
body: IDataObject = {}, body: IDataObject = {},
qs: IDataObject = {}, qs: IDataObject = {},
uri?: string, uri?: string,
@@ -22,16 +28,15 @@ export async function strapiApiRequest(
) { ) {
const credentials = await this.getCredentials('strapiApi'); const credentials = await this.getCredentials('strapiApi');
const url = removeTrailingSlash(credentials.url as string);
try { try {
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: {}, headers: {},
method, method,
body, body,
qs, qs,
uri: uri: uri || credentials.apiVersion === 'v4' ? `${url}/api${resource}` : `${url}${resource}`,
uri || credentials.apiVersion === 'v4'
? `${credentials.url}/api${resource}`
: `${credentials.url}${resource}`,
json: true, json: true,
qsStringifyOptions: { qsStringifyOptions: {
arrayFormat: 'indice', arrayFormat: 'indice',
@@ -54,6 +59,9 @@ export async function getToken(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
): Promise<any> { ): Promise<any> {
const credentials = await this.getCredentials('strapiApi'); const credentials = await this.getCredentials('strapiApi');
const url = removeTrailingSlash(credentials.url as string);
let options = {} as OptionsWithUri; let options = {} as OptionsWithUri;
options = { options = {
headers: { headers: {
@@ -64,10 +72,7 @@ export async function getToken(
identifier: credentials.email, identifier: credentials.email,
password: credentials.password, password: credentials.password,
}, },
uri: uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
credentials.apiVersion === 'v4'
? `${credentials.url}/api/auth/local`
: `${credentials.url}/auth/local`,
json: true, json: true,
}; };
return this.helpers.request(options); return this.helpers.request(options);

View File

@@ -14,6 +14,7 @@ import { NodeOperationError } from 'n8n-workflow';
import { import {
getToken, getToken,
removeTrailingSlash,
strapiApiRequest, strapiApiRequest,
strapiApiRequestAllItems, strapiApiRequestAllItems,
validateJSON, validateJSON,
@@ -70,6 +71,8 @@ export class Strapi implements INodeType {
const credentials = credential.data as IDataObject; const credentials = credential.data as IDataObject;
let options = {} as OptionsWithUri; let options = {} as OptionsWithUri;
const url = removeTrailingSlash(credentials.url as string);
options = { options = {
headers: { headers: {
'content-type': 'application/json', 'content-type': 'application/json',
@@ -79,12 +82,10 @@ export class Strapi implements INodeType {
identifier: credentials.email, identifier: credentials.email,
password: credentials.password, password: credentials.password,
}, },
uri: uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
credentials.apiVersion === 'v4'
? `${credentials.url}/api/auth/local`
: `${credentials.url}/auth/local`,
json: true, json: true,
}; };
try { try {
await this.helpers.request(options); await this.helpers.request(options);
return { return {