UNPKG

api-console-assets

Version:

This repo only exists to publish api console components to npm

128 lines (122 loc) 3.78 kB
<!-- @license Copyright 2016 The Advanced REST client authors <arc@mulesoft.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <link rel="import" href="../polymer/polymer.html"> <link rel="import" href="../fetch-polyfill/fetch-polyfill.html"> <link rel="import" href="../arc-polyfills/arc-polyfills.html"> <!-- An element that contains methods to transform FormData object into Multipart message and ArrayBuffer ### Example ``` <multipart-payload-transformer form-data="[[formData]]"></multipart-payload-transformer> ``` @group Logic Elements @element multipart-payload-transformer --> <script> Polymer({ is: 'multipart-payload-transformer', properties: { // A form data object to transform. formData: Object, /** * Latest generated boundary value for the multipart forms. * Each call to `generateMessage()` or `generatePreview()` will * generate new content type and therefore boundary value. */ boundary: { type: String, notify: true }, /** * Latest generated content-type value for the multipart forms. * Each call to `generateMessage()` or `generatePreview()` will * generate new content type value. */ contentType: { type: String, notify: true } }, /** * Generates an ArrayBuffer instance from the FormData object. * * @return {Promise} A resolved promise when produces ArrayBuffer. */ generateMessage: function() { var request = new Request('/', { method: 'POST', body: this.formData }); var ct = request.headers.get('content-type'); this._processContentType(ct); if (!request.arrayBuffer) { return Promise.reject(new Error('Your browser do not support this method.')); } return request.arrayBuffer(); }, _processContentType: function(contentType) { this.set('contentType', contentType); this.fire('content-type-changed', { value: contentType }); var match = contentType.match(/boundary=(.*)/); if (!match) { return; } var boundary = match[1]; this.set('boundary', boundary); this.fire('multipart-boundary-changed', { value: boundary }); }, generatePreview: function() { if (!this.formData) { return Promise.reject(new Error('The FormData property is not set.')); } return this.generateMessage() .then(function(ab) { return this._previewFromBuffer(ab); }.bind(this)); }, _previewFromBuffer: function(buffer) { return this.arrayBufferToString(buffer); }, /** * Convert ArrayBuffer to readable form * @param {ArrayBuffer} buff * @returns {String} Converted string */ arrayBufferToString: function(buffer) { if (!!buffer.buffer) { // Not a ArrayBuffer, need and instance of AB // It can't just get buff.buffer because it will use original buffer if the buff is a slice // of it. var b = buffer.slice(0); buffer = b.buffer; } if ('TextDecoder' in window) { var decoder = new TextDecoder('utf-8'); var view = new DataView(buffer); return decoder.decode(view); } var array = new Uint8Array(buffer); var str = ''; for (var i = 0; i < array.length; ++i) { str += String.fromCharCode(array[i]); } return str; } }); </script>