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 { export class StrapiApi implements ICredentialType {
name = 'strapiApi'; name = 'strapiApi';
@ -7,54 +36,5 @@ export class StrapiApi implements ICredentialType {
documentationUrl = 'strapi'; documentationUrl = 'strapi';
properties: INodeProperties[] = [ properties = strapiApiCredentialSchema.toNodeProperties();
{
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',
},
],
},
];
} }

View file

@ -1,9 +1,29 @@
import type { import type { IAuthenticateGeneric, ICredentialTestRequest, ICredentialType } from 'n8n-workflow';
IAuthenticateGeneric, import { CredentialSchema, type InferCredentialSchema } from '../utils/CredentialSchema';
ICredentialTestRequest,
ICredentialType, export const strapiTokenApiCredential = CredentialSchema.create({
INodeProperties, apiToken: CredentialSchema.password({ label: 'API Token' }),
} from 'n8n-workflow'; 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 { export class StrapiTokenApi implements ICredentialType {
name = 'strapiTokenApi'; name = 'strapiTokenApi';
@ -12,41 +32,7 @@ export class StrapiTokenApi implements ICredentialType {
documentationUrl = 'strapi'; documentationUrl = 'strapi';
properties: INodeProperties[] = [ properties = strapiTokenApiCredential.toNodeProperties();
{
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',
},
],
},
];
authenticate: IAuthenticateGeneric = { authenticate: IAuthenticateGeneric = {
type: 'generic', type: 'generic',

View file

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

View file

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

View file

@ -897,6 +897,7 @@
"typedi": "catalog:", "typedi": "catalog:",
"uuid": "catalog:", "uuid": "catalog:",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz", "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, "noImplicitReturns": false,
"useUnknownInCatchVariables": 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": [ "references": [
{ "path": "../@n8n/imap/tsconfig.build.json" }, { "path": "../@n8n/imap/tsconfig.build.json" },
{ "path": "../workflow/tsconfig.build.json" }, { "path": "../workflow/tsconfig.build.json" },

View file

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