-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
42 lines (35 loc) · 1.12 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"strings"
)
type Api struct {
ResourceOptions
}
func (s *Api) GetDir() string {
return "webapi"
}
func (s *Api) GetFileName() string {
return s.ResourceOptions.Domain + "-api.js"
}
func (s *Api) GetFileContent() string {
domainFirstCharUpper := strings.ToUpper(s.Domain[:1]) + s.Domain[1:]
return fmt.Sprintf(s.getTemplate(), domainFirstCharUpper, s.Action, s.Domain)
}
func (s *Api) getTemplate() string {
return "var request = require('superagent');\n" +
"var ApiResponseHandler = require('./api-response-handler');\n\n" +
"var %[1]sApi = {\n" +
" %[2]s: function(successCallback) {\n" +
" var url = Routing.generate('%[3]s_list');\n\n" +
" request\n" +
" .get(url)\n" +
" .set('Accept', 'application/json')\n" +
" .end(function(err, res) {\n" +
" ApiResponseHandler.handle(err, res, successCallback);\n" +
" })\n" +
" ;\n" +
" }\n" +
"};\n\n" +
"module.exports = %[1]sApi;"
}