Added listing of API fields
This commit is contained in:
54
pages/apiview/apidocs/apidocs.go
Normal file
54
pages/apiview/apidocs/apidocs.go
Normal file
@ -0,0 +1,54 @@
|
||||
package apidocs
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unicode"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
// ByType renders the api docs page for the given type.
|
||||
func ByType(typeName string) func(*aero.Context) string {
|
||||
return func(ctx *aero.Context) string {
|
||||
t := arn.API.Type(typeName)
|
||||
fields := []*utils.APIField{}
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
|
||||
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
|
||||
continue
|
||||
}
|
||||
|
||||
typeName := ""
|
||||
|
||||
switch field.Type.Kind() {
|
||||
case reflect.Ptr:
|
||||
typeName = field.Type.Elem().Name()
|
||||
|
||||
case reflect.Slice:
|
||||
sliceElementType := field.Type.Elem()
|
||||
|
||||
if sliceElementType.Kind() == reflect.Ptr {
|
||||
sliceElementType = sliceElementType.Elem()
|
||||
}
|
||||
|
||||
typeName = sliceElementType.Name() + "[]"
|
||||
|
||||
default:
|
||||
typeName = field.Type.Name()
|
||||
}
|
||||
|
||||
fields = append(fields, &utils.APIField{
|
||||
Name: field.Name,
|
||||
JSON: field.Tag.Get("json"),
|
||||
Type: typeName,
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.HTML(components.APIDocs(t, fields))
|
||||
}
|
||||
}
|
21
pages/apiview/apidocs/apidocs.pixy
Normal file
21
pages/apiview/apidocs/apidocs.pixy
Normal file
@ -0,0 +1,21 @@
|
||||
component APIDocs(t reflect.Type, fields []*utils.APIField)
|
||||
h1= "API: " + t.Name()
|
||||
|
||||
table
|
||||
thead
|
||||
tr
|
||||
th Field name
|
||||
th JavaScript notation
|
||||
th Type
|
||||
tbody
|
||||
each field in fields
|
||||
tr
|
||||
td= field.Name
|
||||
td= field.JSON
|
||||
td= field.Type
|
||||
|
||||
|
||||
.corner-buttons
|
||||
a.button(href="/api")
|
||||
Icon("code")
|
||||
span Overview
|
Reference in New Issue
Block a user