github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/templates/remotohttp/client.es6.js.plush (about) 1 // Code generated by Remoto; DO NOT EDIT. 2 3 // Remoto JavaScript Client 4 // 5 // uses the Fetch API: to support older browsers, use the polyfil https://github.github.io/fetch/ 6 7 "use strict"; 8 9 // _filesCount keeps track of the number of files being added, and is used 10 // to generate unique field names. 11 var _filesCount = 0 12 13 <%= for (service) in def.Services { %> 14 // <%= service.Name %>ClientOptions are the options for the <%= service.Name %>Client. 15 export class <%= service.Name %>ClientOptions { 16 constructor(data = {}) { 17 this._data = data 18 this._data.endpoint = this._data.endpoint || "http://localhost:8080" 19 } 20 get endpoint() { return this._data.endpoint } 21 set endpoint(endpoint) { this._data.endpoint = endpoint } 22 } 23 24 <%= print_comment(service.Comment) %>export class <%= service.Name %>Client { 25 constructor(options) { 26 this.options = options || new <%= service.Name %>ClientOptions() 27 } 28 <%= for (method) in service.Methods { %> 29 <%= print_comment(method.Comment) %><%= camelize_down(method.Name) %>(<%= camelize_down(method.RequestStructure.Name) %> = null) { 30 if (<%= camelize_down(method.RequestStructure.Name) %> == null) { 31 <%= camelize_down(method.RequestStructure.Name) %> = new <%= method.RequestStructure.Name %>() // empty request by default is ok 32 } 33 return this.<%= camelize_down(method.Name) %>Multi([<%= camelize_down(method.RequestStructure.Name) %>]).then(function(responses) { 34 return responses[0] 35 }) 36 } 37 38 // <%= method.Name %>Multi is the batch version of <%= method.Name %>. 39 <%= camelize_down(method.Name) %>Multi(<%= camelize_down(method.RequestStructure.Name) %>s) { 40 if (<%= camelize_down(method.RequestStructure.Name) %>s == null) { 41 throw new Error('<%= service.Name %>Client.<%= camelize_down(method.Name) %>Multi: <%= camelize_down(method.RequestStructure.Name) %>s cannot be empty') 42 } 43 var data = new FormData() 44 <%= camelize_down(method.RequestStructure.Name) %>s.forEach(function(request){ 45 if (request && !request instanceof <%= method.RequestStructure.Name %>) { 46 throw '<%= service.Name %>Client.<%= camelize_down(method.Name) %>Multi: requests must be instances of <%= method.RequestStructure.Name %>' 47 } 48 Object.keys(request.allFiles).forEach(function(fieldname) { 49 data.set(fieldname, request.allFiles[fieldname]) 50 }) 51 }) 52 data.set('json', JSON.stringify(<%= camelize_down(method.RequestStructure.Name) %>s)) 53 return fetch(this.options.endpoint + '/remoto/<%= service.Name %>.<%= method.Name %>', { 54 method: 'post', data: data, 55 headers: { 56 'Content-Type': 'multipart/form-data', 57 'Accept':'application/json', 58 } 59 }).then(function(responseData){ // success 60 return responseData.json().then( 61 json => { 62 var responses = [] 63 json.forEach(response => { 64 responses.push(new GetRoutesResponse(response)) 65 }) 66 return responses 67 }, 68 error => { 69 throw error 70 } 71 ) 72 }, function(error){ // error 73 throw new Error('<%= service.Name %>Client.<%= camelize_down(method.Name) %>Multi: ' + error.message) 74 }) 75 } 76 <% } %> 77 } 78 <% } %> 79 <%= for (structure) in unique_structures(def) { %> 80 <%= print_comment(structure.Comment) %>export class <%= structure.Name %> { 81 constructor(data = {}) { 82 this._data = data 83 this._files = {} 84 } 85 <%= if (structure.IsRequestObject) { %> 86 // addFile adds a file to the request and returns its unique name. 87 // This method is not usually called directly, instead callers should use the setters 88 // on the objects. 89 addFile(filename, file) { 90 let fieldname = 'files['+(_filesCount++)+']' 91 this._files[fieldname] = file 92 return fieldname 93 } 94 95 // allFiles gets an object of files in this request, keyed with 96 // the fieldname. 97 get allFiles() { return this._files } 98 // filesCount gets the number of files in this request. 99 get filesCount() { return _filesCount } 100 <% } %> 101 // toJSON gets a JSON string describing this object. 102 toJSON() { return this._data } 103 <%= for (field) in structure.Fields { %> 104 get <%= camelize_down(field.Name) %>() { return this._data.<%= underscore(field.Name) %> } 105 <%= if (field.Type.Name == "remototypes.File") { %>set<%= field.Name %>(request, filename, <%= underscore(field.Name) %>) { this._data.<%= underscore(field.Name) %> = request.addFile(filename, <%= underscore(field.Name) %>) }<% } %> 106 <%= if (!structure.IsResponseObject && field.Type.Name != "remototypes.File") { %>set <%= camelize_down(field.Name) %>(<%= underscore(field.Name) %>) { 107 <%= if (field.Type.IsStruct) { %> 108 if (!<%= camelize_down(field.Name) %> instanceof <%= field.Type.Name %>) { 109 throw '<%= structure.Name %>: <%= camelize_down(field.Name) %> should be of type <%= field.Type.Name %>' 110 } 111 <% } %> 112 this._data.<%= underscore(field.Name) %> = <%= underscore(field.Name) %> 113 }<% } %><% } %> 114 } 115 <% } %>