Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pacedotdev/oto
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Aug 6, 2021
2 parents 373e70a + ae436fd commit 27ec713
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/pacedotdev/oto

go 1.13

replace github.com/pacedotdev/oto/parser => ./parser

require (
github.com/dustin/go-humanize v1.0.0
github.com/fatih/structtag v1.2.0
Expand Down
17 changes: 13 additions & 4 deletions otohttp/templates/client.ts.plush
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// HeadersFunc allows you to mutate headers for each request.
// Useful for adding authorization into the client.
interface HeadersFunc {
(headers: Headers);
(headers: Headers): void;
}

// Client provides access to remote services.
Expand All @@ -20,7 +20,7 @@ export class Client {
<%= format_comment_text(service.Comment) %>export class <%= service.Name %> {
constructor(readonly client: Client) {}
<%= for (method) in service.Methods { %>
<%= format_comment_text(method.Comment) %> async <%= method.NameLowerCamel %>(<%= camelize_down(method.InputObject.TypeName) %>: <%= method.InputObject.TypeName %> = null) {
<%= format_comment_text(method.Comment) %> async <%= method.NameLowerCamel %>(<%= camelize_down(method.InputObject.TypeName) %>?: <%= method.InputObject.TypeName %>, modifyHeaders?: HeadersFunc): Promise<<%= method.OutputObject.TypeName %>> {
if (<%= camelize_down(method.InputObject.TypeName) %> == null) {
<%= camelize_down(method.InputObject.TypeName) %> = new <%= method.InputObject.TypeName %>();
}
Expand All @@ -30,6 +30,9 @@ export class Client {
if (this.client.headers) {
await this.client.headers(headers);
}
if (modifyHeaders) {
await modifyHeaders(headers)
}
const response = await fetch(this.client.basepath + '<%= service.Name %>.<%= method.Name %>', {
method: 'POST',
headers: headers,
Expand Down Expand Up @@ -57,7 +60,7 @@ export class Client {
<%= if (field.Type.IsObject) { %>
<%= if (field.Type.Multiple) { %>
if (data.<%= field.NameLowerCamel %>) {
this.<%= field.NameLowerCamel %> = new Array<<%= field.Type.ObjectName %>>()
this.<%= field.NameLowerCamel %> = []
for (let i = 0; i < data.<%= field.NameLowerCamel %>.length; i++) {
this.<%= field.NameLowerCamel %>.push(new <%= field.Type.ObjectName %>(data.<%= field.NameLowerCamel %>[i]));
}
Expand All @@ -72,7 +75,13 @@ export class Client {
}
}
<%= for (field) in object.Fields { %>
<%= format_comment_text(field.Comment) %> <%= field.NameLowerCamel %>: <%= if (field.Type.IsObject) { %><%= field.Type.TypeName %><% } else { %><%= field.Type.JSType %><% } %><%= if (field.Type.Multiple) { %>[]<% } %>;
<%= format_comment_text(field.Comment) %> <%= field.NameLowerCamel %><%= if (field.Type.IsObject || field.Type.Multiple) { %>?<% } %>: <%= if (field.Type.IsObject) { %><%= field.Type.TypeName %><%= if (field.Type.Multiple) { %>[]<% } %><% } else { %><%= field.Type.JSType %><%= if (field.Type.Multiple) { %>[]<% } %><%= if (!field.Type.Multiple) { %> = <%= field.Type.JSType %>Default<% } %><% } %>;
<% } %>
}
<% } %>

// these defaults make the template easier to write.
const stringDefault = ''
const numberDefault = 0
const booleanDefault = false
const anyDefault = null

0 comments on commit 27ec713

Please sign in to comment.