Node 빌드 방식 선택
n8n에는 선언형과 프로그래매틱 두 가지 node 빌드 스타일이 있습니다. 대부분의 node에는 선언형 스타일을 사용해야 합니다. 프로그래매틱 스타일은 더 장황합니다. 선언형 스타일과 프로그래매틱 스타일의 주요 차이점은 들어오는 데이터를 처리하고 API 요청을 빌드하는 방법입니다.
n8n에는 선언형과 프로그래매틱 두 가지 node 빌드 스타일이 있습니다.
대부분의 node에는 선언형 스타일을 사용해야 합니다. 이 스타일은:
- JSON 기반 구문을 사용하여 작성하기 더 간단하고 버그 도입 위험이 적습니다.
- 더 미래 지향적입니다.
- REST API와의 통합을 지원합니다.
프로그래매틱 스타일은 더 장황합니다. 다음의 경우에는 프로그래매틱 스타일을 사용해야 합니다:
- Trigger node
- REST 기반이 아닌 모든 node. GraphQL API를 호출해야 하는 node와 외부 종속성을 사용하는 node가 포함됩니다.
- 들어오는 데이터를 변환해야 하는 모든 node.
- 전체 버전 관리. 버전 관리 유형에 대한 자세한 내용은 Node 버전 관리를 참고하세요.
데이터 처리 차이점#
선언형 스타일과 프로그래매틱 스타일의 주요 차이점은 들어오는 데이터를 처리하고 API 요청을 빌드하는 방법입니다. 프로그래매틱 스타일은 들어오는 데이터와 파라미터를 읽은 다음 요청을 빌드하는 execute() 메서드가 필요합니다. 선언형 스타일은 operations 오브젝트의 routing 키를 사용하여 이를 처리합니다. node 파라미터와 execute() 메서드에 대한 자세한 내용은 Node 기본 파일을 참고하세요.
구문 차이점#
선언형 스타일과 프로그래매틱 스타일의 차이점을 이해하려면 아래 두 코드 스니펫을 비교하세요. 이 예시는 "FriendGrid"라는 단순화된 SendGrid 통합 버전을 생성합니다. 다음 코드 스니펫은 완전하지 않습니다: node 빌드 스타일의 차이점을 강조합니다.
프로그래매틱 스타일:
import {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
IRequestOptions,
} from 'n8n-workflow';
// FriendGrid 클래스 생성
export class FriendGrid implements INodeType {
description: INodeTypeDescription = {
displayName: 'FriendGrid',
name: 'friendGrid',
. . .
properties: [
{
displayName: 'Resource',
. . .
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'contact',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a contact',
},
],
default: 'create',
description: 'The operation to perform.',
},
{
displayName: 'Email',
name: 'email',
. . .
},
{
displayName: 'Additional Fields',
// 선택적 필드 설정
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
// 이 node에 대해 사용자가 제공한 자격 증명 가져오기
const credentials = await this.getCredentials('friendGridApi') as IDataObject;
if (resource === 'contact') {
if (operation === 'create') {
// 이메일 입력 가져오기
const email = this.getNodeParameter('email', 0) as string;
// 추가 필드 입력 가져오기
const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject;
const data: IDataObject = {
email,
};
Object.assign(data, additionalFields);
// https://sendgrid.com/docs/api-reference/에 정의된 대로 HTTP 요청 수행
const options: IRequestOptions = {
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${credentials.apiKey}`,
},
method: 'PUT',
body: {
contacts: [
data,
],
},
url: `https://api.sendgrid.com/v3/marketing/contacts`,
json: true,
};
responseData = await this.helpers.httpRequest(options);
}
}
// 데이터를 n8n 데이터로 매핑
return [this.helpers.returnJsonArray(responseData)];
}
}
선언형 스타일:
import { INodeType, INodeTypeDescription } from 'n8n-workflow';
// FriendGrid 클래스 생성
export class FriendGrid implements INodeType {
description: INodeTypeDescription = {
displayName: 'FriendGrid',
name: 'friendGrid',
. . .
// 기본 요청 구성 설정
requestDefaults: {
baseURL: 'https://api.sendgrid.com/v3/marketing'
},
properties: [
{
displayName: 'Resource',
. . .
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'contact',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a contact',
// routing 오브젝트 추가
routing: {
request: {
method: 'POST',
url: '=/contacts',
send: {
type: 'body',
properties: {
email: {{$parameter["email"]}}
}
}
}
},
// 연락처 생성 응답 처리
output: {
postReceive: [
{
type: 'set',
properties: {
value: '={{ { "success": $response } }}'
}
}
]
}
},
],
default: 'create',
description: 'The operation to perform.',
},
{
displayName: 'Email',
. . .
},
{
displayName: 'Additional Fields',
// 선택적 필드 설정
},
],
}
// execute 메서드 불필요
}
