added array to params
This commit is contained in:
+4
-4
@@ -27,7 +27,7 @@ func Init() API {
|
||||
mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
h_SomeAPI(w, r)
|
||||
h_SomeAPI2(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", 500)
|
||||
}
|
||||
@@ -35,12 +35,12 @@ func Init() API {
|
||||
return ret
|
||||
}
|
||||
|
||||
func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
func h_SomeAPI2(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(r.Context(), "REQ", r)
|
||||
ctx = context.WithValue(ctx, "RES", w)
|
||||
req := &ARequestStruct{}
|
||||
req := &AStr{}
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
@@ -49,7 +49,7 @@ func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
res, err := SomeAPI(ctx, req)
|
||||
res, err := SomeAPI2(ctx, req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
|
||||
+26
-13
@@ -2,29 +2,42 @@ package goapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*@API*/
|
||||
type ARequestStruct struct {
|
||||
A *string `json:"a"`
|
||||
B int64
|
||||
C time.Time
|
||||
D *string
|
||||
}
|
||||
//
|
||||
///*@API*/
|
||||
//type ARequestStruct struct {
|
||||
// A *string `json:"a"`
|
||||
// B int64
|
||||
// C time.Time
|
||||
// D *string
|
||||
//}
|
||||
//
|
||||
///*@API*/
|
||||
//type AResponseStruct struct {
|
||||
// A string
|
||||
// B int64
|
||||
// C time.Time
|
||||
// D *string
|
||||
//}
|
||||
//
|
||||
///*
|
||||
//@API
|
||||
//@PATH: /someapi
|
||||
//*/
|
||||
//func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) {
|
||||
// return nil, nil
|
||||
//}
|
||||
|
||||
/*@API*/
|
||||
type AResponseStruct struct {
|
||||
type AStr struct {
|
||||
A string
|
||||
B int64
|
||||
C time.Time
|
||||
D *string
|
||||
}
|
||||
|
||||
/*
|
||||
@API
|
||||
@PATH: /someapi
|
||||
*/
|
||||
func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) {
|
||||
func SomeAPI2(ctx context.Context, a *AStr) ([]*AStr, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
+4
-23
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Basepath string = ""
|
||||
@@ -38,31 +37,13 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func Some() (*struct{}, error) {
|
||||
dec, err := invoke("a", "/c/d", struct{}{})
|
||||
ret := &struct{}{}
|
||||
dec.Decode(ret)
|
||||
return ret, err
|
||||
|
||||
type AStr struct {
|
||||
A string `json:"a"`
|
||||
}
|
||||
|
||||
type ARequestStruct struct {
|
||||
A string `json:"a"`
|
||||
B int64 `json:"b"`
|
||||
C time.Time `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
|
||||
type AResponseStruct struct {
|
||||
A string `json:"a"`
|
||||
B int64 `json:"b"`
|
||||
C time.Time `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
|
||||
func SomeAPI(req *ARequestStruct) (res *AResponseStruct, err error) {
|
||||
func SomeAPI2(req *AStr) (res []*AStr, err error) {
|
||||
dec, err := invoke("POST", "/someapi", req)
|
||||
ret := &AResponseStruct{}
|
||||
ret := []*AStr{}
|
||||
err = dec.Decode(ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
+3
-13
@@ -81,26 +81,16 @@ async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise<b
|
||||
//#endregion
|
||||
|
||||
//#region Types
|
||||
export interface ARequestStruct {
|
||||
export interface AStr {
|
||||
a:string
|
||||
b:number
|
||||
c:Date
|
||||
d:string
|
||||
}
|
||||
|
||||
export interface AResponseStruct {
|
||||
a:string
|
||||
b:number
|
||||
c:Date
|
||||
d:string
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Methods
|
||||
/**
|
||||
SomeAPI*/
|
||||
export async function SomeAPI(req:ARequestStruct):Promise<AResponseStruct>{
|
||||
SomeAPI2*/
|
||||
export async function SomeAPI2(req:AStr):Promise<AStr[]>{
|
||||
return InvokeJSON("/someapi","POST",req)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user