This commit is contained in:
yanweidong 2024-02-27 18:39:15 +08:00
parent 8ef319ad48
commit f26648abfd
7 changed files with 355 additions and 18 deletions

View File

@ -1,14 +1,13 @@
import axios, { InternalAxiosRequestConfig, AxiosResponse } from "axios";
import { v4 as uuidv4 } from 'uuid';
import { Config } from "./sdk";
// 创建 axios 实例
const service = axios.create({
baseURL: Config.BaseUrl,
baseURL: import.meta.env.VITE_APP_BASE_API,
timeout: 50000,
headers: {
"Request-ID":uuidv4(),
"Workspace":Config.Workspace,
"Workspace":import.meta.env.VITE_Workspace,
"Content-Type": "application/json;charset=utf-8"
},
});

View File

@ -1,10 +0,0 @@
export class Config {
static BaseUrl: string; // 服务器地址
static Workspace: string; // 工作空间
constructor(url:string,ws:string){
Config.BaseUrl = url;
Config.Workspace = ws;
}
}

View File

@ -1,10 +1,86 @@
export interface Crc {
code?: string; // ping码
code?: string;
}
export interface StatusReply {
status?: number;// 状态码
identity?: string; // 标识码
message?: string; //状态说明
timeseq?: number; // 响应时间序列
status?: number;
identity?: string;
message?: string;
timeseq?: number;
}
export interface CheckForUpdatesRequest {
app?: string;
os?: string;
arch?: string;
version?: string;
}
export interface CheckForUpdatesReply {
identity?: string;
version?: string;
summary?: string;
files?: string;
pubdate?: string;
}
export interface Empty {
}
export interface ConfigureRequest {
app?: string;
os?: string;
}
export interface ConfigureReply {
data?: ConfigureItem[];
}
export interface ConfigureItem {
Identity?: string;
key?: string;
value?: string;
version?: number;
}
export interface AreasReply {
areas?: AreasItem[];
}
export interface AreasItem {
id?: string;
pid?: string;
deep?: string;
name?: string;
pinyin_prefix?: string;
pinyin?: string;
ext_id?: string;
ext_name?: string;
}
export interface TagsReply {
tags?: TagsItem[];
}
export interface TagsItem {
id?: number;
type?: number;
key?: string;
title?: string;
remark?: string;
icon?: string;
}

142
cli/main.go Normal file
View File

@ -0,0 +1,142 @@
package main
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"git.apinb.com/bsm-sdk/engine/utils"
"github.com/tallstoat/pbparser"
)
var (
protoPath string = "./proto"
jsPath string = "./axios/service"
)
func main() {
fmt.Println("Conv poroto file to js file.")
srvPaths := getSubDirs(protoPath)
fmt.Println("Root:", protoPath, srvPaths, "JS Out:", jsPath)
parseProtos(protoPath, srvPaths[1])
}
func getSubDirs(root string) []string {
var paths []string
files, _ := os.ReadDir(root)
for _, file := range files {
if file.IsDir() {
paths = append(paths, file.Name())
}
}
return paths
}
func parseProtos(root, dir string) {
var msg []pbparser.MessageElement
fPath := path.Join(root, dir)
tsPath := path.Join(jsPath, dir)
files, _ := os.ReadDir(fPath)
for _, file := range files {
if file.IsDir() {
continue
} else {
pfPath := filepath.Join(fPath, file.Name())
pb, err := pbparser.ParseFile(pfPath)
if err != nil {
panic(err)
}
msg = append(msg, pb.Messages...)
}
}
writeTypes(tsPath, msg)
}
func writeSrvs(fileName string) {
}
func writeTypes(tsPath string, msg []pbparser.MessageElement) {
tpl := `
{{Commit}}
export interface {{Name}} {
{{Data}}
}`
keyMap := make(map[string]int)
var bodys []string
for _, msgItem := range msg {
var data []string
var msgBody = tpl
for _, fd := range msgItem.Fields {
data = append(data, dispFields(fd))
}
keyFname := strings.ToLower(msgItem.Name)
if _, ok := keyMap[keyFname]; !ok {
msgBody = strings.ReplaceAll(msgBody, "{{Commit}}", msgItem.Documentation)
msgBody = strings.ReplaceAll(msgBody, "{{Name}}", msgItem.Name)
msgBody = strings.ReplaceAll(msgBody, "{{Data}}", strings.Join(data, "\r\n"))
bodys = append(bodys, msgBody)
keyMap[keyFname] = 1
}
}
typesPath := filepath.Join(tsPath, "types.ts")
utils.StringToFile(typesPath, strings.Join(bodys, "\r\n"))
}
func dispFields(fd pbparser.FieldElement) string {
tpl := " {{Name}}?: {{Type}}; {{Commit}}"
name := strings.Trim(fd.Name, "")
name = strings.ReplaceAll(name, "\t", "")
tpl = strings.ReplaceAll(tpl, "{{Name}}", name)
jsonByte, _ := json.Marshal(fd)
fmt.Println(string(jsonByte))
if fd.Documentation == "" {
tpl = strings.ReplaceAll(tpl, "{{Commit}}", "")
} else {
tpl = strings.ReplaceAll(tpl, "{{Commit}}", " // "+fd.Documentation)
}
var fdType string = ""
switch fd.Type.Name() {
case "string":
fdType = "string"
case "int32", "int64", "float":
fdType = "number"
case "bool":
fdType = "bool"
default:
if fd.Label == "repeated" {
fdType = fd.Type.Name() + "[]"
} else {
fdType = fd.Type.Name()
}
}
tpl = strings.ReplaceAll(tpl, "{{Type}}", fdType)
return tpl
}

13
go.mod Normal file
View File

@ -0,0 +1,13 @@
module proto-tools
go 1.22.0
require (
git.apinb.com/bsm-sdk/engine v1.0.5
github.com/tallstoat/pbparser v0.2.0
)
require (
github.com/google/uuid v1.6.0 // indirect
github.com/jaevor/go-nanoid v1.3.0 // indirect
)

8
go.sum Normal file
View File

@ -0,0 +1,8 @@
git.apinb.com/bsm-sdk/engine v1.0.5 h1:nsU+5suyRFaVqHhnrU9ontpgW+54cuY/+7OnI7VEa60=
git.apinb.com/bsm-sdk/engine v1.0.5/go.mod h1:tNYgBhykUTBtpH+4EdAamcKqMwpJ3XUJ0roKqxGM3lM=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/tallstoat/pbparser v0.2.0 h1:lsFH4mdiOv1MIQVmge/idThSTd2uByNodWVfbtysjzg=
github.com/tallstoat/pbparser v0.2.0/go.mod h1:aUC6W9uQLeAXZkknve8ZDO6InhRYpYHlJ9kvsQh1i2k=

109
types.ts Normal file
View File

@ -0,0 +1,109 @@
export interface Crc {
code?: string;
}
export interface StatusReply {
status?: number;
identity?: string;
message?: string;
timeseq?: number;
}
export interface CheckForUpdatesRequest {
app?: string;
os?: string;
arch?: string;
version?: string;
}
export interface CheckForUpdatesReply {
identity?: string;
version?: string;
summary?: string;
files?: string;
pubdate?: string;
}
export interface Empty {
}
export interface ConfigureRequest {
app?: string;
os?: string;
}
export interface ConfigureReply {
data?: ConfigureItem[];
}
export interface ConfigureItem {
Identity?: string;
key?: string;
value?: string;
version?: number;
}
export interface AreasReply {
areas?: AreasItem[];
}
export interface AreasItem {
id?: string;
pid?: string;
deep?: string;
name?: string;
pinyin_prefix?: string;
pinyin?: string;
ext_id?: string;
ext_name?: string;
}
export interface TagsReply {
tags?: TagsItem[];
}
export interface TagsItem {
id?: number;
type?: number;
key?: string;
title?: string;
remark?: string;
icon?: string;
}