Add typing for getCredentials, implement for Strapi as POC

This commit is contained in:
Elias Meire 2024-08-28 20:19:40 +02:00
parent 96e69ad5f2
commit c164c0d0ce
No known key found for this signature in database
7 changed files with 77 additions and 100 deletions

View file

@ -1,4 +1,33 @@
import type { ICredentialType, INodeProperties } from 'n8n-workflow';
import type { ICredentialType } from 'n8n-workflow';
import { CredentialSchema, type InferCredentialSchema } from '../utils/CredentialSchema';
export const strapiApiCredentialSchema = CredentialSchema.create({
notice: CredentialSchema.notice('Make sure you are using a user account not an admin account'),
email: CredentialSchema.email({ placeholder: 'name@email.com' }),
password: CredentialSchema.password(),
url: CredentialSchema.url({
placeholder: 'https://api.example.com',
}),
apiVersion: CredentialSchema.options({
label: 'API Version',
description: 'The version of api to be used',
options: [
{
label: 'Version 4',
value: 'v4',
description: 'API version supported by Strapi 4',
},
{
label: 'Version 3',
value: 'v3',
default: true,
description: 'API version supported by Strapi 3',
},
],
}),
});
export type StrapiApiCredential = InferCredentialSchema<typeof strapiApiCredentialSchema>;
export class StrapiApi implements ICredentialType {
name = 'strapiApi';
@ -7,54 +36,5 @@ 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',
type: 'string',
placeholder: 'name@email.com',
default: '',
},
{
displayName: 'Password',
name: 'password',
type: 'string',
typeOptions: {
password: true,
},
default: '',
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
placeholder: 'https://api.example.com',
},
{
displayName: 'API Version',
name: 'apiVersion',
default: 'v3',
type: 'options',
description: 'The version of api to be used',
options: [
{
name: 'Version 4',
value: 'v4',
description: 'API version supported by Strapi 4',
},
{
name: 'Version 3',
value: 'v3',
description: 'API version supported by Strapi 3',
},
],
},
];
properties = strapiApiCredentialSchema.toNodeProperties();
}

View file

@ -1,9 +1,29 @@
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
import type { IAuthenticateGeneric, ICredentialTestRequest, ICredentialType } from 'n8n-workflow';
import { CredentialSchema, type InferCredentialSchema } from '../utils/CredentialSchema';
export const strapiTokenApiCredential = CredentialSchema.create({
apiToken: CredentialSchema.password({ label: 'API Token' }),
url: CredentialSchema.url({ placeholder: 'https://api.example.com' }),
apiVersion: CredentialSchema.options({
label: 'API Version',
description: 'The version of api to be used',
options: [
{
label: 'Version 4',
value: 'v4',
description: 'API version supported by Strapi 4',
},
{
label: 'Version 3',
value: 'v3',
default: true,
description: 'API version supported by Strapi 3',
},
],
}),
});
export type StrapiTokenApiCredential = InferCredentialSchema<typeof strapiTokenApiCredential>;
export class StrapiTokenApi implements ICredentialType {
name = 'strapiTokenApi';
@ -12,41 +32,7 @@ export class StrapiTokenApi implements ICredentialType {
documentationUrl = 'strapi';
properties: INodeProperties[] = [
{
displayName: 'API Token',
name: 'apiToken',
type: 'string',
typeOptions: { password: true },
default: '',
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
placeholder: 'https://api.example.com',
},
{
displayName: 'API Version',
name: 'apiVersion',
default: 'v3',
type: 'options',
description: 'The version of api to be used',
options: [
{
name: 'Version 4',
value: 'v4',
description: 'API version supported by Strapi 4',
},
{
name: 'Version 3',
value: 'v3',
description: 'API version supported by Strapi 3',
},
],
},
];
properties = strapiTokenApiCredential.toNodeProperties();
authenticate: IAuthenticateGeneric = {
type: 'generic',

View file

@ -1,5 +1,4 @@
import type {
ICredentialDataDecryptedObject,
IDataObject,
IExecuteFunctions,
IHookFunctions,
@ -10,6 +9,7 @@ import type {
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import type { StrapiApiCredential } from '../../credentials/StrapiApi.credentials';
export const removeTrailingSlash = (url: string) => {
if (url.endsWith('/')) {
@ -28,7 +28,7 @@ export async function strapiApiRequest(
headers: IDataObject = {},
) {
const authenticationMethod = this.getNodeParameter('authentication', 0);
let credentials: ICredentialDataDecryptedObject;
let credentials: Pick<StrapiApiCredential, 'apiVersion' | 'url'>;
if (authenticationMethod === 'password') {
credentials = await this.getCredentials('strapiApi');
@ -36,7 +36,7 @@ export async function strapiApiRequest(
credentials = await this.getCredentials('strapiTokenApi');
}
const url = removeTrailingSlash(credentials.url as string);
const url = removeTrailingSlash(credentials.url);
try {
const options: IRequestOptions = {

View file

@ -11,6 +11,7 @@ import type {
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import type { StrapiApiCredential } from '../../credentials/StrapiApi.credentials';
import {
getToken,
removeTrailingSlash,
@ -143,14 +144,14 @@ export class Strapi implements INodeType {
const authenticationMethod = this.getNodeParameter('authentication', 0);
let apiVersion: string;
let apiVersion: StrapiApiCredential['apiVersion'];
if (authenticationMethod === 'password') {
const { jwt } = await getToken.call(this);
apiVersion = (await this.getCredentials('strapiApi')).apiVersion as string;
apiVersion = (await this.getCredentials('strapiApi')).apiVersion;
headers.Authorization = `Bearer ${jwt}`;
} else {
apiVersion = (await this.getCredentials('strapiTokenApi')).apiVersion as string;
apiVersion = (await this.getCredentials('strapiTokenApi')).apiVersion;
}
for (let i = 0; i < length; i++) {

View file

@ -897,6 +897,7 @@
"typedi": "catalog:",
"uuid": "catalog:",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
"xml2js": "catalog:"
"xml2js": "catalog:",
"zod": "catalog:"
}
}

View file

@ -11,7 +11,13 @@
"noImplicitReturns": false,
"useUnknownInCatchVariables": false
},
"include": ["credentials/**/*.ts", "nodes/**/*.ts", "test/**/*.ts", "utils/**/*.ts"],
"include": [
"types.d.ts",
"credentials/**/*.ts",
"nodes/**/*.ts",
"test/**/*.ts",
"utils/**/*.ts"
],
"references": [
{ "path": "../@n8n/imap/tsconfig.build.json" },
{ "path": "../workflow/tsconfig.build.json" },

View file

@ -1681,6 +1681,9 @@ importers:
xml2js:
specifier: 'catalog:'
version: 0.6.2
zod:
specifier: 'catalog:'
version: 3.23.8
devDependencies:
'@types/amqplib':
specifier: ^0.10.1