diff --git a/DataTypes.php b/DataTypes.php index 7e02741..1df2190 100644 --- a/DataTypes.php +++ b/DataTypes.php @@ -18,27 +18,27 @@ namespace Nelmio\ApiDocBundle; */ class DataTypes { - const INTEGER = 'integer'; + const INTEGER = 'integer'; - const FLOAT = 'float'; + const FLOAT = 'float'; - const STRING = 'string'; + const STRING = 'string'; - const BOOLEAN = 'boolean'; + const BOOLEAN = 'boolean'; - const FILE = 'file'; + const FILE = 'file'; - const ENUM = 'choice'; + const ENUM = 'choice'; const COLLECTION = 'collection'; - const MODEL = 'model'; + const MODEL = 'model'; - const DATE = 'date'; + const DATE = 'date'; - const DATETIME = 'datetime'; + const DATETIME = 'datetime'; - const TIME = 'time'; + const TIME = 'time'; /** * Returns true if the supplied `actualType` value is considered a primitive type. Returns false, otherwise. diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index bddfa05..14a4028 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -46,6 +46,7 @@ class FormTypeParser implements ParserInterface 'textarea' => DataTypes::STRING, 'country' => DataTypes::STRING, 'choice' => DataTypes::ENUM, + 'file' => DataTypes::FILE, ); public function __construct(FormFactoryInterface $formFactory) diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 2c964fe..773d39e 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -101,6 +101,39 @@ }); {% if enableSandbox %} + var setParameterType = function ($context,setType) { + // no 2nd argument, use default from parameters + if (typeof setType == "undefined") { + setType = $context.parent().attr("data-dataType"); + $context.val(setType); + } + + $context.parent().find('.value').remove(); + var placeholder = ""; + if ($context.parent().attr("data-dataType") != "" && typeof $context.parent().attr("data-dataType") != "undefined") { + placeholder += "[" + $context.parent().attr("data-dataType") + "] "; + } + if ($context.parent().attr("data-format") != "" && typeof $context.parent().attr("data-format") != "undefined") { + placeholder += $context.parent().attr("data-dataType"); + } + if ($context.parent().attr("data-description") != "" && typeof $context.parent().attr("data-description") != "undefined") { + placeholder += $context.parent().attr("data-description"); + } else { + placeholder += "Value"; + } + + switch(setType) { + case "boolean": + $('').insertAfter($context); + break; + case "file": + $('').insertAfter($context); + break; + default: + $('').insertAfter($context); + } + }; + var toggleButtonText = function ($btn) { if ($btn.text() === 'Default') { $btn.text('Raw'); @@ -221,6 +254,7 @@ method = $(this).attr('method'), self = this, params = {}, + formData = new FormData(), headers = {}, content = $(this).find('textarea.content').val(), result_container = $('.result', $(this).parent()); @@ -235,6 +269,7 @@ var requestFormatMethod = '{{ requestFormatMethod }}'; if (requestFormatMethod == 'format_param') { params['_format'] = $('#request_format option:selected').text(); + formData.append('_format',$('#request_format option:selected').text()); } else if (requestFormatMethod == 'accept_header') { headers['Accept'] = $('#request_format').val(); } @@ -246,6 +281,37 @@ headers['Content-type'] = 'application/'+bodyFormat; } + var hasFileTypes = false; + $('.parameters .tuple_type', $(this)).each(function() { + if ($(this).val() == 'file') { + hasFileTypes = true; + } + }); + + if (hasFileTypes && method != 'POST') { + alert("Sorry, you can only submit files via POST."); + return false; + } + + if (hasFileTypes) { + // retrieve all the parameters to send for file upload + $('.parameters .tuple', $(this)).each(function() { + var key, value; + + key = $('.key', $(this)).val(); + if ($('.value', $(this)).attr('type') === 'file' ) { + value = $('.value', $(this)).prop('files')[0]; + } else { + value = $('.value', $(this)).val(); + } + + if (value) { + formData.append(key,value); + } + }); + } + + // retrieve all the parameters to send $('.parameters .tuple', $(this)).each(function() { var key, value; @@ -258,6 +324,10 @@ } }); + + + + // retrieve the additional headers to send $('.headers .tuple', $(this)).each(function() { var key, value; @@ -279,7 +349,7 @@ } }; - // disable all the fiels and buttons + // disable all the fields and buttons $('input, button', $(this)).attr('disabled', 'disabled'); // append the query authentication @@ -307,9 +377,7 @@ body = params; } var data = content.length ? content : body; - - // and trigger the API call - $.ajax({ + var ajaxOptions = { url: endpoint + url, type: method, data: data, @@ -328,7 +396,18 @@ // and enable them back $('input:not(.content-type), button', $(self)).removeAttr('disabled'); } - }); + }; + + // overrides body format to send data properly + if (hasFileTypes) { + ajaxOptions.data = formData; + ajaxOptions.processData = false; + ajaxOptions.contentType = false; + delete(ajaxOptions.headers); + } + + // and trigger the API call + $.ajax(ajaxOptions); return false; }); @@ -369,8 +448,30 @@ e.preventDefault(); }); - $('.pane.sandbox').on('click', '.add', function() { - var html = $(this).parents('.pane').find('.tuple_template').html(); + + // sets the correct parameter type on load + $('.pane.sandbox .tuple_type').each(function() { + setParameterType($(this)); + }); + + + // handles parameter type change + $('.pane.sandbox').on('change', '.tuple_type', function() { + setParameterType($(this),$(this).val()); + }); + + + + $('.pane.sandbox').on('click', '.add_parameter', function() { + var html = $(this).parents('.pane').find('.parameters_tuple_template').html(); + + $(this).before(html); + + return false; + }); + + $('.pane.sandbox').on('click', '.add_header', function() { + var html = $(this).parents('.pane').find('.headers_tuple_template').html(); $(this).before(html); diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index b4b4ac2..448fba7 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -228,14 +228,20 @@

Parameters

{% for name, infos in data.parameters %} {% if not infos.readonly %} -

+

= + -

{% endif %} {% endfor %} - + {% endif %} @@ -257,7 +263,7 @@ -

- +
@@ -282,10 +288,25 @@

= + -

+ + +

Request URL