Skip to content

Commit 156f5ec

Browse files
committed
readme
1 parent 0da7f44 commit 156f5ec

File tree

1 file changed

+100
-15
lines changed

1 file changed

+100
-15
lines changed

README.md

Lines changed: 100 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ Turn Your **OpenAPI** (**Swagger**) spec into a **n8n node**!
99

1010
- [Installation](#installation)
1111
- [Usage](#usage)
12-
* [Overview](#overview)
13-
* [Customization](#customization)
14-
+ [Resource](#resource)
15-
+ [Operation](#operation)
16-
+ [Fields](#fields)
12+
- [How it works](#how-it-works)
13+
* [Request Body](#request-body)
14+
- [Customization](#customization)
15+
* [Resource](#resource)
16+
* [Operation](#operation)
17+
* [Fields](#fields)
1718
- [Use Cases](#use-cases)
1819
- [FAQ](#faq)
1920
* [I have only OpenAPI v2 spec, what can I do?](#i-have-only-openapi-v2-spec-what-can-i-do)
@@ -37,10 +38,10 @@ yarn add @devlikeapro/n8n-openapi-node
3738

3839
# Usage
3940

40-
Add your `openapi.json` to `src/{NodeName}` folder
41-
(use **OpenAPI v3** and **json**, see [FAQ](#faq) if you don't have it)
41+
1. Add your `openapi.json` to `src/{NodeName}` folder
42+
(use **OpenAPI v3** and **json**, see [FAQ](#faq) if you don't have it)
4243

43-
Get your `Node.properties` from OpenAPI v3 spec:
44+
2. Get your `Node.properties` from OpenAPI v3 spec:
4445

4546
```typescript
4647
import {INodeType, INodeTypeDescription} from 'n8n-workflow';
@@ -83,21 +84,105 @@ export class Petstore implements INodeType {
8384
}
8485
```
8586

86-
## Overview
87+
# How it works
8788

88-
tbd
89+
`N8NPropertiesBuilder` extracts few entities from OpenAPI v3 to your n8n community node:
8990

90-
## Customization
91+
1. **Resource** - a list of **Tags** from OpenAPI spec
92+
2. **Operation** - a list of **Operations** from OpenAPI spec (aka **Actions** in n8n)
93+
3. **Query Parameters** - a list of `operation.parameters` from OpenAPI spec
94+
4. **Request Body** - a list of `operation.requestBody.content` from OpenAPI spec (only for `application/json`)
95+
5. **Headers** - a list of `operation.parameters` from OpenAPI spec
9196

92-
### Resource
97+
## Request Body
9398

94-
tbd
99+
It doesn't create the full structure of the request body, only the first level of properties.
100+
So if you have request body as
101+
102+
```json
103+
{
104+
"name": "string",
105+
"config": {
106+
"id": 0,
107+
"name": "string"
108+
}
109+
}
110+
```
111+
112+
it creates 2 fields in n8n:
113+
114+
- `name` - with default value `string`
115+
- `config` - with default value `{"id": 0, "name": "string"}`
116+
117+
# Customization
118+
119+
## Resource
120+
121+
You can override the way how to extract **Resource** from **OpenAPI Tag** defining your custom `IResourceParser`:
122+
123+
```typescript
124+
import {IResourceParser} from '@devlikeapro/n8n-openapi-node';
125+
126+
export class CustomResourceParser {
127+
CUSTOM_DESCRIPTION = {
128+
"cats": "Cats are cute",
129+
}
130+
131+
name(tag: OpenAPIV3.TagObject): string {
132+
// Your custom logic here
133+
if (tag['X-Visible-Name']) {
134+
return tag['X-Visible-Name'];
135+
}
136+
return lodash.startCase(tag.name);
137+
}
138+
139+
value(tag: Pick<OpenAPIV3.TagObject, "name">): string {
140+
// Remove all non-alphanumeric characters
141+
const name = tag.name.replace(/[^a-zA-Z0-9_-]/g, '')
142+
return lodash.startCase(name)
143+
}
144+
145+
description(tag: OpenAPIV3.TagObject): string {
146+
// Your custom logic here
147+
return this.CUSTOM_DESCRIPTION[tag.name] || tag.description || '';
148+
}
149+
}
150+
```
151+
152+
```typescript
153+
import {N8NPropertiesBuilder, N8NPropertiesBuilderConfig} from '@devlikeapro/n8n-openapi-node';
154+
import * as doc from './openapi.json';
155+
156+
import {CustomResourceParser} from './CustomResourceParser';
157+
158+
const config: N8NPropertiesBuilderConfig = {
159+
resource: new CustomResourceParser()
160+
}
161+
const parser = new N8NPropertiesBuilder(doc, config);
162+
const properties = parser.build()
163+
```
164+
165+
Alternatively, you can use `DefaultResourceParser` and override only the methods you need:
166+
167+
```typescript
168+
import {OpenAPIV3} from 'openapi-types';
169+
import * as lodash from 'lodash';
170+
import {DefaultResourceParser} from '@devlikeapro/n8n-openapi-node';
171+
172+
export class CustomResourceParser extends DefaultResourceParser {
173+
value(tag: OpenAPIV3.TagObject): string {
174+
return lodash.startCase(tag.name.replace(/[^a-zA-Z0-9_-]/g, ''));
175+
}
176+
}
177+
```
178+
179+
The default implementation you can find in [src/ResourceParser.ts](src/ResourceParser.ts)
95180

96-
### Operation
181+
## Operation
97182

98183
tbd
99184

100-
### Fields
185+
## Fields
101186

102187
tbd
103188

0 commit comments

Comments
 (0)