Skip to content

Commit

Permalink
better template
Browse files Browse the repository at this point in the history
dahernan authored Jun 16, 2021
1 parent 160711d commit ae436fd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions otohttp/templates/client.ts.plush
Original file line number Diff line number Diff line change
@@ -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.
@@ -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 %>();
}
@@ -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,
@@ -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]));
}
@@ -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 ae436fd

Please sign in to comment.