Move CredentialSchema code to workflow package

This commit is contained in:
Elias Meire 2024-09-25 11:30:18 +02:00
parent 28e36e002d
commit df27da07dd
No known key found for this signature in database
6 changed files with 643 additions and 416 deletions

View file

@ -1,40 +1,49 @@
import type { ICredentialType } from 'n8n-workflow';
import { CredentialSchema, type InferCredentialSchema } from '../utils/CredentialSchema';
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' }),
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',
placeholder: "https://api.example.com",
}),
apiVersion: CredentialSchema.options({
label: 'API Version',
description: 'The version of api to be used',
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 4",
value: "v4",
description: "API version supported by Strapi 4",
},
{
label: 'Version 3',
value: 'v3',
label: "Version 3",
value: "v3",
default: true,
description: 'API version supported by Strapi 3',
description: "API version supported by Strapi 3",
},
],
}),
});
export type StrapiApiCredential = InferCredentialSchema<typeof strapiApiCredentialSchema>;
export type StrapiApiCredential = InferCredentialSchema<
typeof strapiApiCredentialSchema
>;
export class StrapiApi implements ICredentialType {
name = 'strapiApi';
name = "strapiApi";
displayName = 'Strapi API';
displayName = "Strapi API";
documentationUrl = 'strapi';
documentationUrl = "strapi";
properties = strapiApiCredentialSchema.toNodeProperties();
schema = strapiApiCredentialSchema;
}

View file

@ -1,61 +1,60 @@
{
"name": "n8n-workflow",
"version": "1.56.0",
"description": "Workflow base code of n8n",
"main": "dist/index.js",
"module": "src/index.ts",
"types": "dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./src/index.ts",
"types": "./dist/index.d.ts"
},
"./*": "./*"
},
"scripts": {
"clean": "rimraf dist .turbo",
"dev": "pnpm watch",
"typecheck": "tsc --noEmit",
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write . --ignore-path ../../.prettierignore",
"lint": "eslint . --quiet",
"lintfix": "eslint . --fix",
"watch": "tsc -p tsconfig.build.json --watch",
"test": "jest",
"test:dev": "jest --watch"
},
"files": [
"dist/**/*"
],
"devDependencies": {
"@langchain/core": "^0.2.18",
"@types/deep-equal": "^1.0.1",
"@types/express": "catalog:",
"@types/jmespath": "^0.15.0",
"@types/lodash": "catalog:",
"@types/luxon": "^3.2.0",
"@types/md5": "^2.3.5",
"@types/xml2js": "catalog:"
},
"dependencies": {
"@n8n/tournament": "1.0.5",
"@n8n_io/riot-tmpl": "4.0.0",
"ast-types": "0.15.2",
"axios": "catalog:",
"callsites": "3.1.0",
"deep-equal": "2.2.0",
"esprima-next": "5.8.4",
"form-data": "catalog:",
"jmespath": "0.16.0",
"js-base64": "3.7.2",
"jssha": "3.3.1",
"lodash": "catalog:",
"luxon": "catalog:",
"md5": "2.3.0",
"recast": "0.21.5",
"title-case": "3.0.3",
"transliteration": "2.3.5",
"xml2js": "catalog:"
}
"name": "n8n-workflow",
"version": "1.56.0",
"description": "Workflow base code of n8n",
"main": "dist/index.js",
"module": "src/index.ts",
"types": "dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./src/index.ts",
"types": "./dist/index.d.ts"
},
"./*": "./*"
},
"scripts": {
"clean": "rimraf dist .turbo",
"dev": "pnpm watch",
"typecheck": "tsc --noEmit",
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write . --ignore-path ../../.prettierignore",
"lint": "eslint . --quiet",
"lintfix": "eslint . --fix",
"watch": "tsc -p tsconfig.build.json --watch",
"test": "jest",
"test:dev": "jest --watch"
},
"files": ["dist/**/*"],
"devDependencies": {
"@langchain/core": "^0.2.18",
"@types/deep-equal": "^1.0.1",
"@types/express": "catalog:",
"@types/jmespath": "^0.15.0",
"@types/lodash": "catalog:",
"@types/luxon": "^3.2.0",
"@types/md5": "^2.3.5",
"@types/xml2js": "catalog:"
},
"dependencies": {
"@n8n/tournament": "1.0.5",
"@n8n_io/riot-tmpl": "4.0.0",
"ast-types": "0.15.2",
"axios": "catalog:",
"callsites": "3.1.0",
"deep-equal": "2.2.0",
"esprima-next": "5.8.4",
"form-data": "catalog:",
"jmespath": "0.16.0",
"js-base64": "3.7.2",
"jssha": "3.3.1",
"lodash": "catalog:",
"luxon": "catalog:",
"md5": "2.3.0",
"recast": "0.21.5",
"title-case": "3.0.3",
"transliteration": "2.3.5",
"xml2js": "catalog:",
"zod": "catalog:"
}
}

View file

@ -1,8 +1,8 @@
import type { INodeProperties } from 'n8n-workflow';
import z, { type ZodOptional, type ZodType } from 'zod';
import z, { type ZodOptional, type ZodType } from "zod";
import type { INodeProperties } from "@/Interfaces";
function isObject(value: unknown): value is object {
return typeof value === 'object' && value !== null && !Array.isArray(value);
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function removeUndefinedProperties<T extends object>(obj: T): T {
@ -40,19 +40,25 @@ class CredentialSchemaRootObject<
);
}
getProperty<K extends keyof T>(key: K): T[K]['metadata'] {
getProperty<K extends keyof T>(key: K): T[K]["metadata"] {
return this.shape[key].metadata;
}
toNodeProperties() {
return Object.entries(this.shape).map(([key, schema]) => schema.toNodeProperties(key));
return Object.entries(this.shape).map(([key, schema]) =>
schema.toNodeProperties(key),
);
}
}
type ToZodSchemaReturnType<
M extends BaseMetadata = BaseMetadata,
S extends ZodType | null = ZodType,
> = M['optional'] extends true ? (S extends null ? null : ZodOptional<NonNullable<S>>) : S;
> = M["optional"] extends true
? S extends null
? null
: ZodOptional<NonNullable<S>>
: S;
abstract class CredentialSchemaProperty<
M extends BaseMetadata = BaseMetadata,
@ -76,8 +82,8 @@ abstract class CredentialSchemaProperty<
name,
displayName: this.metadata.label,
description: this.metadata.description,
default: '',
type: 'string',
default: "",
type: "string",
});
}
}
@ -96,7 +102,7 @@ class CredentialSchemaString<
toNodeProperties(name: string): INodeProperties {
return removeUndefinedProperties({
...super.toNodeProperties(name),
type: 'string',
type: "string",
placeholder: this.metadata.placeholder,
typeOptions: { password: this.metadata.password },
});
@ -117,7 +123,7 @@ class CredentialSchemaNumber<
toNodeProperties(name: string): INodeProperties {
return removeUndefinedProperties({
...super.toNodeProperties(name),
type: 'number',
type: "number",
default: this.metadata.default,
});
}
@ -139,18 +145,22 @@ class CredentialSchemaOptions<
const { options } = this.metadata;
return removeUndefinedProperties({
...super.toNodeProperties(name),
type: 'options',
type: "options",
options: options.map((option) => ({
name: option.label,
value: option.value,
description: option.description,
})),
default: options.find((option) => option.default)?.value ?? options[0].value,
default:
options.find((option) => option.default)?.value ?? options[0].value,
});
}
}
class CredentialSchemaNotice extends CredentialSchemaProperty<BaseMetadata, null> {
class CredentialSchemaNotice extends CredentialSchemaProperty<
BaseMetadata,
null
> {
constructor(public notice: string) {
super({ label: notice }, null);
}
@ -158,7 +168,7 @@ class CredentialSchemaNotice extends CredentialSchemaProperty<BaseMetadata, null
toNodeProperties(name: string): INodeProperties {
return {
...super.toNodeProperties(name),
type: 'notice',
type: "notice",
};
}
}
@ -190,24 +200,26 @@ type Option<V extends string> = {
};
type NonEmptyArray<T> = [T, ...T[]];
type OptionsMetadata<V extends string> = BaseMetadata & { options: NonEmptyArray<Option<V>> };
type OptionsMetadata<V extends string> = BaseMetadata & {
options: NonEmptyArray<Option<V>>;
};
type Zodify<
M extends BaseMetadata,
S extends ZodType | null,
T extends CredentialSchemaProperty<M, S>,
> = ReturnType<T['toZodSchema']> extends z.ZodType ? ReturnType<T['toZodSchema']> : never;
> = ReturnType<T["toZodSchema"]> extends z.ZodType
? ReturnType<T["toZodSchema"]>
: never;
type ZodifyObject<
M extends BaseMetadata,
S extends ZodType | null,
T extends { [k: string]: CredentialSchemaProperty<M, S> },
> = {
[K in keyof T as ReturnType<T[K]['toZodSchema']> extends z.ZodType ? K : never]: Zodify<
M,
S,
T[K]
>;
[K in keyof T as ReturnType<T[K]["toZodSchema"]> extends z.ZodType
? K
: never]: Zodify<M, S, T[K]>;
};
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@ -221,11 +233,11 @@ export const CredentialSchema = {
return new CredentialSchemaRootObject(shape);
},
password(options: Omit<Optional<StringMetadata, 'label'>, 'password'> = {}) {
password(options: Omit<Optional<StringMetadata, "label">, "password"> = {}) {
return new CredentialSchemaString(
{
password: true,
label: 'Password',
label: "Password",
...options,
},
z.string(),
@ -239,18 +251,24 @@ export const CredentialSchema = {
number<M extends NumberMetadata>(options: M) {
return new CredentialSchemaNumber(options, z.number());
},
url(options: Optional<StringMetadata, 'label'> = {}) {
return new CredentialSchemaString({ label: 'URL', ...options }, z.string().url());
url(options: Optional<StringMetadata, "label"> = {}) {
return new CredentialSchemaString(
{ label: "URL", ...options },
z.string().url(),
);
},
email(options: Optional<StringMetadata, 'label'> = {}) {
return new CredentialSchemaString({ label: 'Email', ...options }, z.string().email());
email(options: Optional<StringMetadata, "label"> = {}) {
return new CredentialSchemaString(
{ label: "Email", ...options },
z.string().email(),
);
},
options<V extends string, M extends OptionsMetadata<V>>(options: M) {
return new CredentialSchemaOptions(
options,
z.enum(
options.options.map((option) => option.value) as NonEmptyArray<
M['options'][number]['value']
M["options"][number]["value"]
>,
),
);
@ -260,6 +278,11 @@ export const CredentialSchema = {
},
};
export type InferCredentialSchema<
T extends CredentialSchemaRootObject<BaseMetadata, ZodType | null>,
> = z.infer<ReturnType<T['toZodSchema']>>;
export type TCredentialSchema = CredentialSchemaRootObject<
BaseMetadata,
ZodType | null
>;
export type InferCredentialSchema<T extends TCredentialSchema> = z.infer<
ReturnType<T["toZodSchema"]>
>;

File diff suppressed because it is too large Load diff

View file

@ -1,27 +1,27 @@
import * as LoggerProxy from './LoggerProxy';
export * as ErrorReporterProxy from './ErrorReporterProxy';
export * as ExpressionEvaluatorProxy from './ExpressionEvaluatorProxy';
import * as NodeHelpers from './NodeHelpers';
import * as ObservableObject from './ObservableObject';
import * as TelemetryHelpers from './TelemetryHelpers';
import * as LoggerProxy from "./LoggerProxy";
export * as ErrorReporterProxy from "./ErrorReporterProxy";
export * as ExpressionEvaluatorProxy from "./ExpressionEvaluatorProxy";
import * as NodeHelpers from "./NodeHelpers";
import * as ObservableObject from "./ObservableObject";
import * as TelemetryHelpers from "./TelemetryHelpers";
export * from './errors';
export * from './Authentication';
export * from './Constants';
export * from './Cron';
export * from './DeferredPromise';
export * from './GlobalState';
export * from './Interfaces';
export * from './MessageEventBus';
export * from './ExecutionStatus';
export * from './Expression';
export * from './NodeHelpers';
export * from './RoutingNode';
export * from './Workflow';
export * from './WorkflowDataProxy';
export * from './WorkflowHooks';
export * from './VersionedNodeType';
export * from './TypeValidation';
export * from "./errors";
export * from "./Authentication";
export * from "./Constants";
export * from "./Cron";
export * from "./DeferredPromise";
export * from "./GlobalState";
export * from "./Interfaces";
export * from "./MessageEventBus";
export * from "./ExecutionStatus";
export * from "./Expression";
export * from "./NodeHelpers";
export * from "./RoutingNode";
export * from "./Workflow";
export * from "./WorkflowDataProxy";
export * from "./WorkflowHooks";
export * from "./VersionedNodeType";
export * from "./TypeValidation";
export { LoggerProxy, NodeHelpers, ObservableObject, TelemetryHelpers };
export {
isObjectEmpty,
@ -36,7 +36,7 @@ export {
updateDisplayOptions,
randomInt,
randomString,
} from './utils';
} from "./utils";
export {
isINodeProperties,
isINodePropertyOptions,
@ -46,12 +46,16 @@ export {
isINodePropertyOptionsList,
isResourceMapperValue,
isFilterValue,
} from './type-guards';
} from "./type-guards";
export { ExpressionExtensions } from './Extensions';
export * as ExpressionParser from './Extensions/ExpressionParser';
export { NativeMethods } from './NativeMethods';
export * from './NodeParameters/FilterParameter';
export { ExpressionExtensions } from "./Extensions";
export * as ExpressionParser from "./Extensions/ExpressionParser";
export { NativeMethods } from "./NativeMethods";
export * from "./NodeParameters/FilterParameter";
export {
CredentialSchema,
InferCredentialSchema,
} from "./CredentialSchema/CredentialSchema";
export type {
DocMetadata,
@ -59,9 +63,9 @@ export type {
DocMetadataArgument,
DocMetadataExample,
Extension,
} from './Extensions';
} from "./Extensions";
declare module 'http' {
declare module "http" {
export interface IncomingMessage {
contentType?: string;
encoding: BufferEncoding;