Skip to content

Commit 266dced

Browse files
authored
Web3 API added. Types generation script updated to handle Web3 API spec design. (#75)
1 parent bf751a7 commit 266dced

File tree

21 files changed

+1064
-157
lines changed

21 files changed

+1064
-157
lines changed

codegen/generate_types.sh

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ change_any_of_ref_to_ref() {
6666
local temp_file="${api_openapi_file_name}.tmp"
6767

6868
jq '
69-
def simplify_allOf:
69+
def simplify_allOf:
7070
if type == "object" and .allOf then
7171
.allOf |= map(
7272
if type == "object" and has("$ref") then . else empty end
@@ -81,24 +81,24 @@ change_any_of_ref_to_ref() {
8181
.paths |= map_values(
8282
. as $path |
8383
. | map_values(
84-
if .parameters then
84+
if .parameters? then
8585
.parameters |= map(
86-
if .schema then .schema |= simplify_allOf else . end
86+
if .schema? then .schema |= simplify_allOf else . end
8787
)
8888
else . end |
8989
if .requestBody? then
9090
.requestBody.content."application/json".schema |= simplify_allOf
9191
else . end
9292
)
93-
) |
93+
)? |
9494
9595
# Apply the simplification to the components schemas
9696
.components.schemas |= map_values(
9797
. |= simplify_allOf |
98-
if .properties then
98+
if .properties? then
9999
.properties |= map_values(simplify_allOf)
100100
else . end
101-
)
101+
)?
102102
' ${api_openapi_file_name} > ${temp_file}
103103

104104
if [ $? -ne 0 ]; then
@@ -162,39 +162,48 @@ check_and_fix_incorrect_number_arrays() {
162162
# This is required to prevent the SDK from adding pointers to optional fields
163163
add_pointer_skip_field() {
164164
local api_openapi_file_name="$1"
165-
local temp_file="${api_openapi_file_name}.tmp" # Define the temporary file name
165+
local temp_file="${api_openapi_file_name}.tmp" # Define the temporary file name
166166

167167
jq '
168-
# Function to add x-go-type-skip-optional-pointer to schema objects if not already present
169-
def add_skip_pointer:
170-
if .type and (.["x-go-type-skip-optional-pointer"] // false) != true then
171-
. + {"x-go-type-skip-optional-pointer": true}
172-
else
173-
.
174-
end;
175-
176-
# Apply to path parameters
177-
.paths |= map_values(
178-
. as $path |
179-
. | map_values(
180-
if .parameters then
181-
.parameters |= map(
182-
if .required == false and .schema then .schema |= add_skip_pointer else . end
183-
)
184-
else . end |
185-
if .requestBody? then
186-
.requestBody.content."application/json".schema |= add_skip_pointer
187-
else . end
188-
)
189-
) |
190-
191-
# Apply to components schemas
192-
.components.schemas |= map_values(
193-
if .properties then
194-
.properties |= map_values(add_skip_pointer)
195-
else . end
196-
)
197-
' ${api_openapi_file_name} > ${temp_file}
168+
# Function to add x-go-type-skip-optional-pointer to schema objects if not already present
169+
def add_skip_pointer:
170+
if .type and (.["x-go-type-skip-optional-pointer"] // false) != true then
171+
. + {"x-go-type-skip-optional-pointer": true}
172+
elif .oneOf and (.["x-go-type-skip-optional-pointer"] // false) != true then
173+
. + {"x-go-type-skip-optional-pointer": true}
174+
else
175+
.
176+
end;
177+
178+
# Apply to path parameters and requestBody
179+
.paths |= map_values(
180+
. as $path |
181+
. | map_values(
182+
if .parameters? and (.parameters != null) then
183+
.parameters |= map(
184+
if .required == false and .schema? and (.schema != null) then
185+
.schema |= add_skip_pointer
186+
else . end
187+
)
188+
else . end |
189+
if .requestBody? and (.requestBody.content?["application/json"].schema != null) then
190+
.requestBody.content["application/json"].schema |= add_skip_pointer |
191+
if .requestBody.content["application/json"].schema.properties? then
192+
.requestBody.content["application/json"].schema.properties |= map_values(add_skip_pointer)
193+
else
194+
.
195+
end
196+
else . end
197+
)
198+
)? |
199+
200+
# Apply to components schemas
201+
.components.schemas |= map_values(
202+
if .properties? and (.properties != null) then
203+
.properties |= map_values(add_skip_pointer)
204+
else . end
205+
)?
206+
' ${api_openapi_file_name} > ${temp_file}
198207

199208
if [ $? -ne 0 ]; then
200209
echo "Error: Failed to add pointer skip fields with jq on $api_openapi_file_name."
@@ -341,4 +350,6 @@ for api_openapi_file_name in "$openapi_dir"/*-openapi.json; do
341350
echo "Error: Failed to move generated types to $new_dir."
342351
exit 1
343352
}
353+
354+
# Add logic to delete the temporary generatedtypes directory if everything succeeds
344355
done

codegen/openapi/web3-openapi.json

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Web3 RPC API",
5+
"description": "API to perform RPC calls on Web3 nodes based on blockchain chainId.",
6+
"version": "1.0.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "https://api.1inch.dev",
11+
"description": "Production server"
12+
}
13+
],
14+
"paths": {
15+
"/{chainId}": {
16+
"post": {
17+
"summary": "Perform RPC calls against full nodes",
18+
"description": "Performs JSON-RPC calls on the blockchain identified by the given chainId. The default nodeType will be used.",
19+
"parameters": [
20+
{
21+
"name": "chainId",
22+
"in": "path",
23+
"required": true,
24+
"description": "The unique identifier for the blockchain.",
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
],
30+
"requestBody": {
31+
"description": "JSON-RPC request payload",
32+
"required": true,
33+
"content": {
34+
"application/json": {
35+
"schema": {
36+
"type": "object",
37+
"properties": {
38+
"jsonrpc": {
39+
"type": "string",
40+
"description": "JSON-RPC version, typically \"2.0\"",
41+
"example": "2.0",
42+
"x-go-type-skip-optional-pointer": true
43+
},
44+
"method": {
45+
"type": "string",
46+
"description": "The name of the RPC method to be invoked",
47+
"example": "eth_blockNumber",
48+
"x-go-type-skip-optional-pointer": true
49+
},
50+
"params": {
51+
"type": "array",
52+
"items": {
53+
"type": "string"
54+
},
55+
"description": "Parameters for the RPC method",
56+
"example": [],
57+
"x-go-type-skip-optional-pointer": true
58+
},
59+
"id": {
60+
"type": "string",
61+
"description": "An identifier established by the client",
62+
"example": "1",
63+
"x-go-type-skip-optional-pointer": true
64+
}
65+
},
66+
"required": [
67+
"jsonrpc",
68+
"method"
69+
],
70+
"x-go-type-skip-optional-pointer": true
71+
}
72+
}
73+
}
74+
},
75+
"responses": {
76+
"200": {
77+
"description": "JSON-RPC response",
78+
"content": {
79+
"application/json": {
80+
"schema": {
81+
"type": "object",
82+
"properties": {
83+
"jsonrpc": {
84+
"type": "string",
85+
"description": "JSON-RPC version, typically \"2.0\"",
86+
"example": "2.0"
87+
},
88+
"result": {
89+
"type": "string",
90+
"description": "The result of the RPC method call",
91+
"example": "0x10"
92+
},
93+
"id": {
94+
"type": "string",
95+
"description": "An identifier established by the client",
96+
"example": "1"
97+
}
98+
},
99+
"required": [
100+
"jsonrpc",
101+
"id"
102+
]
103+
}
104+
}
105+
}
106+
},
107+
"400": {
108+
"description": "Invalid request format"
109+
},
110+
"404": {
111+
"description": "ChainId not found"
112+
}
113+
}
114+
}
115+
},
116+
"/{chainId}/{nodeType}": {
117+
"post": {
118+
"summary": "Perform RPC calls",
119+
"description": "Performs JSON-RPC calls on the blockchain identified by the given chainId. If nodeType is not provided, a default nodeType will be used.",
120+
"parameters": [
121+
{
122+
"name": "chainId",
123+
"in": "path",
124+
"required": true,
125+
"description": "The unique identifier for the blockchain.",
126+
"schema": {
127+
"type": "string"
128+
}
129+
},
130+
{
131+
"name": "nodeType",
132+
"in": "path",
133+
"required": true,
134+
"description": "The node type you require for your call. If not provided, 'full' will be used as the default value.",
135+
"schema": {
136+
"type": "string",
137+
"enum": [
138+
"full",
139+
"archive"
140+
]
141+
}
142+
}
143+
],
144+
"requestBody": {
145+
"description": "JSON-RPC request payload",
146+
"required": true,
147+
"content": {
148+
"application/json": {
149+
"schema": {
150+
"type": "object",
151+
"properties": {
152+
"jsonrpc": {
153+
"type": "string",
154+
"description": "JSON-RPC version, typically \"2.0\"",
155+
"example": "2.0",
156+
"x-go-type-skip-optional-pointer": true
157+
},
158+
"method": {
159+
"type": "string",
160+
"description": "The name of the RPC method to be invoked",
161+
"example": "eth_blockNumber",
162+
"x-go-type-skip-optional-pointer": true
163+
},
164+
"params": {
165+
"type": "array",
166+
"items": {
167+
"type": "string"
168+
},
169+
"description": "Parameters for the RPC method",
170+
"example": [],
171+
"x-go-type-skip-optional-pointer": true
172+
},
173+
"id": {
174+
"type": "integer",
175+
"description": "An identifier established by the client",
176+
"example": 1,
177+
"x-go-type-skip-optional-pointer": true
178+
}
179+
},
180+
"x-go-type-skip-optional-pointer": true
181+
}
182+
}
183+
}
184+
},
185+
"responses": {
186+
"200": {
187+
"description": "JSON-RPC response",
188+
"content": {
189+
"application/json": {
190+
"schema": {
191+
"type": "object",
192+
"properties": {
193+
"jsonrpc": {
194+
"type": "string",
195+
"description": "JSON-RPC version, typically \"2.0\"",
196+
"example": "2.0"
197+
},
198+
"result": {
199+
"type": "string",
200+
"description": "The result of the RPC method call",
201+
"example": "0x10"
202+
},
203+
"id": {
204+
"type": "integer",
205+
"description": "An identifier mirroring the one from the request",
206+
"example": 1
207+
}
208+
}
209+
}
210+
}
211+
}
212+
},
213+
"400": {
214+
"description": "Invalid request format"
215+
},
216+
"404": {
217+
"description": "ChainId not found"
218+
}
219+
}
220+
}
221+
}
222+
},
223+
"components": {
224+
"securitySchemes": {
225+
"ApiKeyAuth": {
226+
"type": "apiKey",
227+
"in": "header",
228+
"name": "X-API-KEY"
229+
}
230+
}
231+
},
232+
"security": [
233+
{
234+
"ApiKeyAuth": []
235+
}
236+
]
237+
}

0 commit comments

Comments
 (0)