Skip to content

Commit

Permalink
fix(Strapi Node): Strapi credentials notice (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed May 23, 2023
1 parent ed7f3b8 commit bbe6d4c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/nodes-base/credentials/StrapiApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export class StrapiApi implements ICredentialType {
documentationUrl = 'strapi';

properties: INodeProperties[] = [
{
displayName: 'Make sure you are using a user account not an admin account',
name: 'notice',
type: 'notice',
default: '',
},
{
displayName: 'Email',
name: 'email',
Expand Down
23 changes: 14 additions & 9 deletions packages/nodes-base/nodes/Strapi/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@ import type {
} 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(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
method: string,
resource: string,

body: IDataObject = {},
qs: IDataObject = {},
uri?: string,
headers: IDataObject = {},
) {
const credentials = await this.getCredentials('strapiApi');

const url = removeTrailingSlash(credentials.url as string);

try {
const options: OptionsWithUri = {
headers: {},
method,
body,
qs,
uri:
uri || credentials.apiVersion === 'v4'
? `${credentials.url}/api${resource}`
: `${credentials.url}${resource}`,
uri: uri || credentials.apiVersion === 'v4' ? `${url}/api${resource}` : `${url}${resource}`,
json: true,
qsStringifyOptions: {
arrayFormat: 'indice',
Expand All @@ -54,6 +59,9 @@ export async function getToken(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
): Promise<any> {
const credentials = await this.getCredentials('strapiApi');

const url = removeTrailingSlash(credentials.url as string);

let options = {} as OptionsWithUri;
options = {
headers: {
Expand All @@ -64,10 +72,7 @@ export async function getToken(
identifier: credentials.email,
password: credentials.password,
},
uri:
credentials.apiVersion === 'v4'
? `${credentials.url}/api/auth/local`
: `${credentials.url}/auth/local`,
uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
json: true,
};
return this.helpers.request(options);
Expand Down
9 changes: 5 additions & 4 deletions packages/nodes-base/nodes/Strapi/Strapi.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { NodeOperationError } from 'n8n-workflow';

import {
getToken,
removeTrailingSlash,
strapiApiRequest,
strapiApiRequestAllItems,
validateJSON,
Expand Down Expand Up @@ -70,6 +71,8 @@ export class Strapi implements INodeType {
const credentials = credential.data as IDataObject;
let options = {} as OptionsWithUri;

const url = removeTrailingSlash(credentials.url as string);

options = {
headers: {
'content-type': 'application/json',
Expand All @@ -79,12 +82,10 @@ export class Strapi implements INodeType {
identifier: credentials.email,
password: credentials.password,
},
uri:
credentials.apiVersion === 'v4'
? `${credentials.url}/api/auth/local`
: `${credentials.url}/auth/local`,
uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
json: true,
};

try {
await this.helpers.request(options);
return {
Expand Down

0 comments on commit bbe6d4c

Please sign in to comment.