Skip to content

Commit

Permalink
added typescript client sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwfx committed May 14, 2020
1 parent d266d70 commit 2d04503
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 28 deletions.
13 changes: 4 additions & 9 deletions metactl/cmd/metactl/v0/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ var genCmd = &cobra.Command{
return
}

var sdkNames []string
for _, sdk0 := range c.V0.Gen.Sdks {
sdkNames = append(sdkNames, sdk0.Names...)
}

errs = sdk.Reset(sdkNames)
errs = sdk.Reset(c.V0.Gen.Sdks)
if len(errs) != 0 {
return
}

for _, sdk0 := range c.V0.Gen.Sdks {
for _, name := range sdk0.Names {
errs = sdk.Gen(d.MessageReport, d.FileSystem, d.Version, d.RootNode, name, sdk0.Data, sdk0.Endpoints, nil)
for _, c := range c.V0.Gen.Sdks {
for _, name := range c.Names {
errs = sdk.Gen(d.MessageReport, d.FileSystem, d.Version, d.RootNode, name, c, nil)
if len(errs) != 0 {
return
}
Expand Down
42 changes: 23 additions & 19 deletions metactl/pkg/v0/business/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"github.com/metamatex/metamate/asg/pkg/v0/asg/graph"
"github.com/metamatex/metamate/metactl/pkg/v0/business/gen"
_go "github.com/metamatex/metamate/metactl/pkg/v0/business/sdk/go"
"github.com/metamatex/metamate/metactl/pkg/v0/business/sdk/typescript"
"github.com/metamatex/metamate/metactl/pkg/v0/types"
"github.com/metamatex/metamate/metactl/pkg/v0/utils/ptr"
"github.com/pkg/errors"
"github.com/spf13/afero"
"sort"
)

func GetSdks() (sdks []types.Sdk) {
func GetSdks() (sdks []types.SdkGenerator) {
sdks = append(sdks, _go.GetSdks()...)
sdks = append(sdks, typescript.GetSdks()...)

sort.Slice(sdks, func(i, j int) bool {
return sdks[i].Name < sdks[j].Name
Expand All @@ -22,56 +24,58 @@ func GetSdks() (sdks []types.Sdk) {
return sdks
}

func Format(sdks []types.Sdk) (s string) {
func Format(sdks []types.SdkGenerator) (s string) {
for _, sdk := range sdks {
s += sdk.Name + " : " + sdk.Description + "\n"
}

return
}

func Reset(names []string) (errs []error) {
for _, name := range names {
sdk, err := getSdk(name)
if err != nil {
errs = append(errs, err)
func Reset(cs []types.SdkConfig) (errs []error) {
for _, c := range cs {
for _, n := range c.Names {
g, err := getSdkGenerator(n)
if err != nil {
errs = append(errs, err)

return
}
return
}

err = sdk.Reset()
if err != nil {
errs = append(errs, err)
err = g.Reset(c)
if err != nil {
errs = append(errs, err)

return
return
}
}
}

return
}

func Gen(report *types.MessageReport, fs afero.Fs, version types.Version, rn *graph.RootNode, name string, data map[string]interface{}, endpointFilter *graph.Filter, typeFilter *graph.Filter) (errs []error) {
sdk, err := getSdk(name)
func Gen(report *types.MessageReport, fs afero.Fs, version types.Version, rn *graph.RootNode, name string, c types.SdkConfig, typeFilter *graph.Filter) (errs []error) {
sdk, err := getSdkGenerator(name)
if err != nil {
errs = append(errs, err)

return
}

err = sdk.Init(&sdk, data)
err = sdk.Init(&sdk, c)
if err != nil {
errs = append(errs, err)

return
}

if endpointFilter != nil {
if c.Endpoints != nil {
for i, _ := range sdk.Tasks {
if sdk.Tasks[i].Dependencies == nil {
sdk.Tasks[i].Dependencies = &types.RenderTaskDependencies{}
}

sdk.Tasks[i].Dependencies.Endpoints = endpointFilter
sdk.Tasks[i].Dependencies.Endpoints = c.Endpoints
}
}

Expand Down Expand Up @@ -106,7 +110,7 @@ func Gen(report *types.MessageReport, fs afero.Fs, version types.Version, rn *gr
return
}

func getSdk(name string) (sdk types.Sdk, err error) {
func getSdkGenerator(name string) (sdk types.SdkGenerator, err error) {
for _, sdk = range GetSdks() {
if sdk.Name == name {
return
Expand Down
97 changes: 97 additions & 0 deletions metactl/pkg/v0/business/sdk/typescript/httpjson_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package typescript

import (
"github.com/metamatex/metamate/metactl/pkg/v0/types"
"github.com/metamatex/metamate/metactl/pkg/v0/utils/ptr"
)

const (
TaskHttpJsonClient = "TaskHttpJsonClient"
)

func init() {
tasks[TaskHttpJsonClient] = types.RenderTask{
TemplateData: &typescriptHttpJsonClientTpl,
Out: ptr.String("mql_.ts"),
}
}

var typescriptHttpJsonClientTpl = `import * as axios from 'axios';
export interface ClientOpts {
client: axios.AxiosInstance;
addr: string;
}
export class Client {
opts: ClientOpts;
constructor(opts: ClientOpts) {
this.opts = opts;
}
{{ range $i, $endpoint := .Endpoints }}
async {{ $endpoint.Name }}(req: {{ $endpoint.Edges.Type.Request.Name }}): Promise<{{ $endpoint.Edges.Type.Response.Name }}> {
let rsp = await this.opts.client.request<{{ $endpoint.Edges.Type.Response.Name }}>({
url: this.opts.addr,
method: "post",
data: req,
headers: {
"X-MetaMate-Type": "{{ $endpoint.Edges.Type.Request.Name }}",
"Content-Type": "application/json; charset=utf-8",
},
});
return rsp.data
}
{{ end }}
}
{{ range $i, $enum := .Enums }}
export const {{ $enum.Name }} = Object.freeze({
{{- range $vi, $value := sortAlpha $enum.Data.Values }}
{{ camel $value }}: "{{ $value }}",
{{- end }}
});
{{ end }}
{{- range $i, $type := .Types }}
export interface {{ $type.Name }} {
{{- range $i, $fn := $type.Edges.Fields.Holds.Slice.Sort }}
{{- if $fn.IsBool }}
{{ $fn.Name }}?: boolean;
{{- end }}
{{- if $fn.IsFloat64 }}
{{ $fn.Name }}?: number;
{{- end }}
{{- if $fn.IsString }}
{{ $fn.Name }}?: string;
{{- end }}
{{- if $fn.IsInt32 }}
{{ $fn.Name }}?: number;
{{- end }}
{{- if $fn.IsType }}
{{ $fn.Name }}?: {{ camel $fn.Edges.Type.Holds.Name }};
{{- end }}
{{- if $fn.IsEnum }}
{{ $fn.Name }}?: string;
{{- end }}
{{- if $fn.IsStringList }}
{{ $fn.Name }}?: string[];
{{- end }}
{{- if $fn.IsInt32List }}
{{ $fn.Name }}?: number[];
{{- end }}
{{- if $fn.IsFloat64List }}
{{ $fn.Name }}?: number[];
{{- end }}
{{- if $fn.IsBoolList }}
{{ $fn.Name }}?: boolean[];
{{- end }}
{{- if $fn.IsTypeList }}
{{ $fn.Name }}?: {{ camel $fn.Edges.Type.Holds.Name }}[];
{{- end }}
{{- if $fn.IsEnumList }}
{{ $fn.Name }}?: string[];
{{- end }}
{{- end }}
}
{{ end }}`

0 comments on commit 2d04503

Please sign in to comment.