github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/console/api/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
    27  	}
    28  	<%= for (method) in service.Methods { %>
    29  	<%= print_comment(method.Comment) %><%= method.Name %>(<%= camelize_down(method.RequestStructure.Name) %> = null) {
    30  		return this.<%= method.Name %>Multi([<%= camelize_down(method.RequestStructure.Name) %>]).then(function(responses) {
    31  			return responses[0]
    32  		})
    33  	}
    34  
    35  	// <%= method.Name %>Multi is the batch version of <%= method.Name %>.
    36  	<%= method.Name %>Multi(<%= camelize_down(method.RequestStructure.Name) %>s) {
    37  		var data = new FormData()
    38  		<%= camelize_down(method.RequestStructure.Name) %>s.forEach(function(request){
    39  			if (request && !request instanceof <%= method.RequestStructure.Name %>) {
    40  				throw '<%= service.Name %>Client.<%= method.Name %>: requests must be instances of <%= method.RequestStructure.Name %>'
    41  			}
    42  			let allfiles = request.allFiles()
    43  			allfiles.forEach(function(fieldname) {
    44  				data.set(fieldname, allfiles[fieldname])
    45  			})
    46  		})
    47  		data.set('json', JSON.stringify(<%= camelize_down(method.RequestStructure.Name) %>s))
    48  		return fetch(this.options.endpoint() + '/remoto/<%= service.Name %>.<%= method.Name %>', {
    49  			method: 'post', body: data,
    50  			headers: {'Content-Type': 'multipart/form-data', 'Accept':'application/json'}
    51  		}).then(function(responseData){ // success
    52  			var responses = []
    53  			responseData.json().forEach(function(response){
    54  				responses.push(new <%= method.ResponseStructure.Name %>(response))
    55  			})
    56  			return responses
    57  		}, function(error){ // error
    58  			throw '<%= service.Name %>Client.<%= method.Name %>: ' + error.message
    59  		})
    60  	}
    61  	<% } %>
    62  }
    63  <% } %>
    64  <%= for (structure) in unique_structures(def) { %>
    65  <%= print_comment(structure.Comment) %>export class <%= structure.Name %> {
    66  	constructor(data = {}) {
    67  		this._data = data
    68  		this._files = {}
    69  	}
    70  	<%= if (structure.IsRequestObject) { %>
    71  	// addFile adds a file to the request and returns its unique name.
    72  	// This method is not usually called directly, instead callers should use the setters
    73  	// on the objects.
    74  	addFile(filename, file) {
    75  		let fieldname = 'files['+(_filesCount++)+']'
    76  		this._files[fieldname] = file
    77  		return fieldname
    78  	}
    79  
    80  	// allFiles gets an object of files in this request, keyed with
    81  	// the fieldname.
    82  	get allFiles() { return this._files }
    83  	// filesCount gets the number of files in this request.
    84  	get filesCount() { return _filesCount }
    85  	<% } %>
    86  	// toJSON gets a JSON string describing this object.
    87  	toJSON() { return JSON.stringify(this._data) }
    88  <%= for (field) in structure.Fields { %>
    89  	get <%= camelize_down(field.Name) %>() { return this._data.<%= underscore(field.Name) %> }
    90  	<%= 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) %>) }<% } %>
    91  	<%= if (!structure.IsResponseObject && field.Type.Name != "remototypes.File") { %>set <%= camelize_down(field.Name) %>(<%= underscore(field.Name) %>) { this._data.<%= underscore(field.Name) %> = <%= underscore(field.Name) %> }<% } %><% } %>
    92  }
    93  <% } %>