Added API examples
This commit is contained in:
@ -2,12 +2,14 @@ package apidocs
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
"github.com/animenotifier/notify.moe/utils/routetests"
|
||||
)
|
||||
|
||||
// ByType renders the api docs page for the given type.
|
||||
@ -16,39 +18,44 @@ func ByType(typeName string) func(*aero.Context) string {
|
||||
t := arn.API.Type(typeName)
|
||||
fields := []*utils.APIField{}
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if t.Kind() == reflect.Struct {
|
||||
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()
|
||||
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
|
||||
continue
|
||||
}
|
||||
|
||||
typeName = sliceElementType.Name() + "[]"
|
||||
typeName := ""
|
||||
|
||||
default:
|
||||
typeName = field.Type.Name()
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
fields = append(fields, &utils.APIField{
|
||||
Name: field.Name,
|
||||
JSON: field.Tag.Get("json"),
|
||||
Type: typeName,
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.HTML(components.APIDocs(t, fields))
|
||||
route := "/api/" + strings.ToLower(typeName) + "/:id"
|
||||
examples := routetests.All()[route]
|
||||
|
||||
return ctx.HTML(components.APIDocs(t, examples, fields))
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,32 @@
|
||||
component APIDocs(t reflect.Type, fields []*utils.APIField)
|
||||
h1= "API: " + t.Name()
|
||||
component APIDocs(t reflect.Type, examples []string, fields []*utils.APIField)
|
||||
.api-docs-page
|
||||
h1= "API: " + t.Name()
|
||||
|
||||
table
|
||||
thead
|
||||
tr
|
||||
th Field name
|
||||
th JavaScript notation
|
||||
th Type
|
||||
tbody
|
||||
each field in fields
|
||||
h2 Examples
|
||||
|
||||
if len(examples) == 0
|
||||
p.no-data No examples have been added to this type yet.
|
||||
else
|
||||
.buttons
|
||||
each example in examples
|
||||
a.button(href="https://notify.moe" + example, target="_blank")
|
||||
Icon("external-link")
|
||||
span= example
|
||||
|
||||
h2 Fields
|
||||
|
||||
table
|
||||
thead
|
||||
tr
|
||||
td= field.Name
|
||||
td= field.JSON
|
||||
td= field.Type
|
||||
|
||||
th Field
|
||||
th Type
|
||||
tbody
|
||||
each field in fields
|
||||
tr.api-field
|
||||
td.api-field-json(title=field.Name + " (Go)")= field.JSON
|
||||
td.api-field-type= field.Type
|
||||
|
||||
.corner-buttons
|
||||
a.button(href="/api")
|
||||
Icon("code")
|
||||
span Overview
|
||||
.corner-buttons
|
||||
a.button(href="/api")
|
||||
Icon("code")
|
||||
span Overview
|
19
pages/apiview/apidocs/apidocs.scarlet
Normal file
19
pages/apiview/apidocs/apidocs.scarlet
Normal file
@ -0,0 +1,19 @@
|
||||
.api-docs-page
|
||||
h1,
|
||||
h2
|
||||
text-align left
|
||||
|
||||
table
|
||||
margin 0
|
||||
|
||||
.buttons
|
||||
justify-content flex-start
|
||||
|
||||
.api-field
|
||||
//
|
||||
|
||||
.api-field-json
|
||||
//
|
||||
|
||||
.api-field-type
|
||||
opacity 0.5
|
Reference in New Issue
Block a user