From c164c0d0ceccdd03639041a6abfd56d321569ae1 Mon Sep 17 00:00:00 2001 From: Elias Meire Date: Wed, 28 Aug 2024 20:19:40 +0200 Subject: [PATCH] Add typing for getCredentials, implement for Strapi as POC --- .../credentials/StrapiApi.credentials.ts | 82 +++++++------------ .../credentials/StrapiTokenApi.credentials.ts | 68 ++++++--------- .../nodes/Strapi/GenericFunctions.ts | 6 +- .../nodes-base/nodes/Strapi/Strapi.node.ts | 7 +- packages/nodes-base/package.json | 3 +- packages/nodes-base/tsconfig.json | 8 +- pnpm-lock.yaml | 3 + 7 files changed, 77 insertions(+), 100 deletions(-) diff --git a/packages/nodes-base/credentials/StrapiApi.credentials.ts b/packages/nodes-base/credentials/StrapiApi.credentials.ts index 1c8ef69632..d5193f5acf 100644 --- a/packages/nodes-base/credentials/StrapiApi.credentials.ts +++ b/packages/nodes-base/credentials/StrapiApi.credentials.ts @@ -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; 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(); } diff --git a/packages/nodes-base/credentials/StrapiTokenApi.credentials.ts b/packages/nodes-base/credentials/StrapiTokenApi.credentials.ts index 07b689884b..52ee5dde44 100644 --- a/packages/nodes-base/credentials/StrapiTokenApi.credentials.ts +++ b/packages/nodes-base/credentials/StrapiTokenApi.credentials.ts @@ -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; 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', diff --git a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts index b687766037..03d0912860 100644 --- a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts @@ -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; 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 = { diff --git a/packages/nodes-base/nodes/Strapi/Strapi.node.ts b/packages/nodes-base/nodes/Strapi/Strapi.node.ts index 277f67e0d8..3634b4e674 100644 --- a/packages/nodes-base/nodes/Strapi/Strapi.node.ts +++ b/packages/nodes-base/nodes/Strapi/Strapi.node.ts @@ -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++) { diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 4239424b67..5e25b31916 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -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:" } } diff --git a/packages/nodes-base/tsconfig.json b/packages/nodes-base/tsconfig.json index 7e4ed688fb..741d9491ea 100644 --- a/packages/nodes-base/tsconfig.json +++ b/packages/nodes-base/tsconfig.json @@ -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" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a826d9b452..936ae8a657 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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