@@ -2,44 +2,70 @@ const fetch = require('node-fetch');
2
2
const fs = require ( 'fs' ) ;
3
3
const jsonToGo = require ( '../vendor/json-to-go.js' ) ;
4
4
const buildPath = require ( './buildPath' ) ;
5
- const buildContent = require ( './buildContent' ) ;
6
- const { loadJsonOrYaml, isJsonString} = require ( "./util" ) ;
5
+ const { loadJsonOrYaml, isJsonString, loadConfig} = require ( "./common" ) ;
6
+
7
+
8
+ let configFile = "./.api-to-go.yml"
9
+
10
+ function run ( urlStr , body , cliOpts ) {
11
+ let comment
12
+ let url
13
+ let path
14
+ let cfg
7
15
8
- function run ( url , body , cliOpts ) {
9
- const apiUrl = url . replace ( / \/ $ / , '' )
10
16
let opts = { }
11
17
12
18
try {
13
- opts = _buildOpts ( body , cliOpts )
19
+ opts = buildOpts ( body , cliOpts )
14
20
} catch ( e ) {
15
21
console . log ( e . message ) ;
16
22
return
17
23
}
18
24
19
- // See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
20
- fetch ( apiUrl , opts )
21
- . then ( res => {
22
- console . log ( `Status: ${ res . status } ${ res . statusText } ` )
23
- return res . json ( )
24
- } )
25
- . then ( json => {
26
- const url = new URL ( apiUrl ) ;
27
- const path = buildPath ( url )
28
- const res = jsonToGo ( JSON . stringify ( json ) , path . struct ) ;
29
- const content = buildContent ( res . go , path , url )
30
- fs . mkdirSync ( path . dir , { recursive : true } )
31
- fs . writeFile ( path . jsonFilePath , JSON . stringify ( json , null , "\t" ) , ( err ) => {
32
- if ( err ) throw err ;
33
- console . log ( `Saved: ${ path . jsonFilePath } ` )
34
- } ) ;
35
- fs . writeFile ( path . goFilePath , content , ( err ) => {
36
- if ( err ) throw err ;
37
- console . log ( `Generated: ${ path . goFilePath } ` )
38
- } ) ;
39
- } ) ;
25
+ try {
26
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
27
+ fetch ( urlStr , opts )
28
+ . then ( res => {
29
+ console . log ( )
30
+ const apiUrl = urlStr . replace ( / \/ $ / , '' )
31
+ url = new URL ( apiUrl ) ;
32
+ cfg = loadConfig ( url , configFile )
33
+ if ( cfg ?. [ "docs" ] !== undefined ) {
34
+ console . log ( `Docs: ${ cfg ?. [ "docs" ] } ` )
35
+ }
36
+ path = buildPath ( url )
37
+ console . log ( `Request: (${ opts . method } ) ${ url } ` )
38
+ console . log ( `Response: ${ res . status } ${ res . statusText } ` )
39
+
40
+ comment = buildComment ( url , path , opts . method , res )
41
+ return res . json ( )
42
+ } )
43
+ . then ( json => {
44
+ const struct = jsonToGo ( JSON . stringify ( json ) , path . struct ) ;
45
+ const content = buildContent ( struct . go , path , comment )
46
+ write ( json , path , content )
47
+ } )
48
+ ;
49
+ } catch ( e ) {
50
+ console . log ( e . message ) ;
51
+ }
40
52
}
41
53
42
- function _buildOpts ( body , cliOpts ) {
54
+ function write ( json , path , content ) {
55
+ fs . mkdirSync ( path . dir , { recursive : true } )
56
+ fs . writeFile ( path . jsonFilePath , JSON . stringify ( json , null , "\t" ) , ( err ) => {
57
+ if ( err ) throw err ;
58
+ } ) ;
59
+ fs . writeFile ( path . goFilePath , content , ( err ) => {
60
+ if ( err ) throw err ;
61
+ } ) ;
62
+ console . log ( )
63
+ console . log ( "Saved:" )
64
+ console . log ( ` ${ path . jsonFilePath } ` )
65
+ console . log ( ` ${ path . goFilePath } ` )
66
+ }
67
+
68
+ function buildOpts ( body , cliOpts ) {
43
69
const opts = { }
44
70
if ( cliOpts ?. method ) opts . method = cliOpts ?. method
45
71
if ( cliOpts ?. headers ) {
@@ -65,7 +91,33 @@ function _buildOpts(body, cliOpts) {
65
91
console . error ( )
66
92
}
67
93
}
94
+ if ( ! opts ?. method ) opts . method = "GET"
68
95
return opts
69
96
}
70
97
98
+ function buildContent ( struct , path , comment ) {
99
+ let content = `// Code generated by api-to-go (https://github.com/nkmr-jp/api-to-go).\n\n`
100
+ content += `package ${ path . pkg } \n\n`
101
+ if ( struct . indexOf ( 'time.' ) !== - 1 ) {
102
+ content += `${ content } import "time"\n\n`
103
+ }
104
+ content += comment
105
+ content += struct
106
+ return content
107
+ }
108
+
109
+ function buildComment ( url , path , method , res ) {
110
+ let comment = `// ${ path . struct } is the go struct of api's payload.\n//`
111
+ const cfg = loadConfig ( url , configFile )
112
+ if ( cfg ?. [ "docs" ] !== undefined ) {
113
+ comment += `\n// Docs: ${ cfg ?. [ "docs" ] } `
114
+ }
115
+ if ( path . path . pathFormat ) {
116
+ comment += `\n// Format: ${ path . path . pathFormat } `
117
+ }
118
+ comment += `\n// Request: (${ method } ) ${ url . href } `
119
+ comment += `\n// Response: ${ res . status } ${ res . statusText } `
120
+ return `${ comment } \n//\n`
121
+ }
122
+
71
123
module . exports = run ;
0 commit comments