Giter Club home page Giter Club logo

raml-request-parameters-editor's Introduction

Build Status

raml-request-parameters-editor

<raml-request-parameters-editor>

An element that renders query/uri parameters forms based on RAML data.

The element is to be used with combination with raml-request-parameteres-model element that produces data model used internally by this view.

The same data model is used by the raml-request-url-editor so it can be shared to reduce number of comutations.

The element is a form element and it validates user input. If the model items are marked as required then the fields are automatically validated.

The element fires uri-parameter-changed and query-parameter-changed custom event to notify about change in the parameters list. The events are also fired when model change to notify about default values loaded into the model. Because of that event notifying about a change to a parameter with the same value can be fired more than once.

Unlike previous version of the element, it doesn't produce the final URL.

Example for Polymer powered application

<raml-request-parameters-editor
  query-model="[[qm]]"
  uri-model="[[um]]"
  has-query-parameters="[[hqp]]"
  has-uri-parameters="[[hup]]"
  has-parameters="[[hp]]"></raml-request-parameters-editor>

<raml-request-parameteres-model
  query-parameters="[[raml.queryParameters]]"
  uri-parameters="[[raml.allUriParameters]]"
  query-model="{{qm}}"
  uri-model="{{um}}"
  has-query-parameters="{{hqp}}"
  has-uri-parameters="{{hup}}"
  has-parameters="{{hp}}"></raml-request-parameteres-model>

Example for vanilla JavaScript

<raml-request-parameters-editor id="editor"></raml-request-parameters-editor>
<raml-request-parameteres-model id="model"></raml-request-parameteres-model>

<script>
var model = document.getElementById('model');
var view = document.getElementById('editor');

function passDataToView(e) {
  var type = e.type;
  type = type.replace('-changed', '');
  var property = type.replace(/-[a-z]/g, function(m) {
    return m[1].toUpperCase();
  });
  view[property] = e.detail.value;
}

model.addEventListener('query-model-changed', passDataToView);
model.addEventListener('uri-model-changed', passDataToView);
model.addEventListener('has-query-parameters-changed', passDataToView);
model.addEventListener('has-uri-parameters-changed', passDataToView);
model.addEventListener('has-parameters-changed', passDataToView);

var raml = await getRamlSomehow();
model.queryParameters = raml.queryParameters;
model.uriParameters = raml.allUriParameters;
</script>

Note: the allUriParameters property used in the example is not a standard RAML JS parser property. It should be computed value of all URI parameters from traits and security schemes.

Validation

This element implements Polymer.IronValidatableBehavior. You can call validate() function to check if the form is valid. An attribute invalid will be set if the form is invalid. It can be used for styling.

URI parameters are always required since they are part of the main URL. Query parameters validation criteria are set according to the RAML spec.

Styling

<raml-request-parameters-editor> provides the following custom properties and mixins for styling:

Custom property Description Default
--raml-request-parameters-editor Mixin applied to the element {}
--raml-request-parameters-editor-subheader Mixin applied to section's subheader --paper-font-subhead
--raml-request-parameters-editor-input-label-color Color of the paper input's labels rgba(0, 0, 0, 0.48)
--inline-documentation-color Color of the documentation string below the input field. Note that it will appy also marked-element styles to this element rgba(0, 0, 0, 0.87)
--raml-request-parameters-editor-row Mixin applied to each row of the form {}
--raml-request-parameters-editor-no-params Mixin applied to the empty info section {}
--raml-request-parameters-editor-no-params-message Mixin applied to the empty info paragraph (the message) {}

Also, use variables and misins defined for paper-input to style inputs, and paper-dropdown-menu, paper-listbox, paper-item to style dropdown menus.

Events

Name Description Params
query-parameter-changed Fired when a query parameter value change in this editor. name String - The name of the parameter
value String - The value of the parameter
uri-parameter-changed Fired when an URI parameter value change in this editor. name String - The name of the parameter
value String - The value of the parameter

raml-request-parameters-form

The raml-request-parameters-form element is responsible for displaying the form od URI / query parameters. It is meant to work with the raml-request-parameters-editor. See its docs to learn how to use this element.

Styling

<raml-request-parameters-form> provides the following custom properties and mixins for styling:

Custom property Description Default
--raml-request-parameters-form Mixin applied to the element {}
--raml-request-parameters-editor-input-label-color Color of the paper input's labels rgba(0, 0, 0, 0.48)
--inline-documentation-color Color of the documentation string below the input field. Note that it will appy also marked-element styles to this element rgba(0, 0, 0, 0.87)
--raml-request-parameters-editor-row Mixin applied to each row of the form {}
--raml-request-parameters-form-optional-toggle-button Mixin applied to a "toggle optional parameters" button {}

raml-request-parameters-model

An element that produces model from RAML Uri/Query properties. The model is used by raml-request-parameters-editor to generate the form view for the user.

Also, model should be propagated to elements responsible for supporting URL computation (like an url-editor).

The model generator dispatches uri-parameter-changed and uri-parameter-changed custom events when a model changes. It is to force inform the application about the change. When RAML values change but result with the same model value (when switching between methods of the same endpoint for example) editor dosn't Notifies about the change because there's no change in the view.

The element caches comnputed model properties in the memory. This way when the user enters a value for a property it will be restored when the user load an endpoint with the same property name.

The same model is used by the raml-request-url-editor so this element can be reused to produce the same model. Because of that this element is not included into the editor element.

Example of use (Polymer app)

<raml-request-url-editor auto-validate required value="{{url}}"
  base-uri="[[raml.baseUri]]"
  endpoint-uri="[[raml.selectedMethod.relativeUri]]"
  query-model="[[qm]]"
  uri-model="[[um]]"></raml-request-url-editor>

<raml-request-parameters-editor
  query-model="[[qm]]"
  uri-model="[[um]]"
  has-query-parameters="[[hqp]]"
  has-uri-parameters="[[hup]]"
  has-parameters="[[hp]]"></raml-request-parameters-editor>

<raml-request-parameteres-model
  query-parameters="[[raml.queryParameters]]"
  uri-parameters="[[raml.allUriParameters]]"
  query-model="{{qm}}"
  uri-model="{{um}}"
  has-query-parameters="{{hqp}}"
  has-uri-parameters="{{hup}}"
  has-parameters="{{hp}}"></raml-request-parameteres-model>

Example for vanilla JavaScript

<raml-request-url-editor auto-validate required id="url"></raml-request-url-editor>
<raml-request-parameters-editor id="editor"></raml-request-parameters-editor>
<raml-request-parameteres-model id="model"></raml-request-parameteres-model>

<script>
var model = document.getElementById('model');
var paramsEditor = document.getElementById('editor');
var urlEditor = document.getElementById('url');

function passDataToView(e) {
  var type = e.type;
  type = type.replace('-changed', '');
  var property = type.replace(/-[a-z]/g, function(m) {
    return m[1].toUpperCase();
  });
  paramsEditor[property] = e.detail.value;
  urlEditor[property] = e.detail.value;
}

model.addEventListener('query-model-changed', passDataToView);
model.addEventListener('uri-model-changed', passDataToView);
model.addEventListener('has-query-parameters-changed', passDataToView);
model.addEventListener('has-uri-parameters-changed', passDataToView);
model.addEventListener('has-parameters-changed', passDataToView);

urlEditor.addEventListener('value-changed', function(e) {
  console.log(e.detail.value);
});

var raml = await getRamlSomehow();
var selected = raml.resources[0].methods[0];
model.queryParameters = selected.queryParameters;
model.uriParameters = selected.allUriParameters;
urlEditor.baseUri = raml.baseUri;
urlEditor.endpointUri = selected.relativeUri;
</script>

Events

Name Description Params
query-parameter-changed Fired when a query parameter value change in this editor. name String - The name of the parameter
value String - The value of the parameter
uri-parameter-changed Fired when an URI parameter value change in this editor. name String - The name of the parameter
value String - The value of the parameter

raml-request-parameters-editor's People

Contributors

arcauto avatar jarrodek avatar

Watchers

 avatar

raml-request-parameters-editor's Issues

Firefox/Chrome RAML-Request-Panel issue

Asset:

https://qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/console/resources/weather/get/

The problem seems to happen only at web startup and only in Chrome.

Basically what we are doing is the following:

In Exchange UI we give the user the chance to fire requests based on the RAML method the users chooses, and to the API instance the user chooses.

So, basically, we want to use the request-panel to build requests, either changing the method or the baseUri.

https://github.com/mulesoft/exchange-ui/blob/bugs/EXC-2189/src/components/APIConsole/RamlRequestPanel/RamlRequestPanel.js
  1. When the page loads (and the React component is rendered), we create a raml-request-panel component and we store the reference to the DOM of that component (line #221)

  2. Once the component is mounted, we assign the method and baseUri to the reference of the request panel (lines #48 and #49)

  3. Whenever the user clicks on another RAML method, we reassign the method object to the request panel (line #60)

  4. Whenever the user selects a different API instance, we reassign the baseUri of the request panel (line #127)

What I see that happens in Firefox when page is loaded (which shows the expected behavior):

  • When the requestPanel.method is assigned in line #48, a url-changed event is fired:

    // The event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // At this moment the queryModel of the requestPanel has these values:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • When the requestPanel.baseUri is assigned in line #49, another url-changed event is fired:

    // The event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?units=metric&APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // At this moment, the queryModel of the requestPanel has these values:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • Then a succession of these events occur:

    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value","value":""}}}
    // queryModel:
    [{"displayName":"q","value":""},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#1.value","value":"metric"}}}
    // queryModel:
    [{"displayName":"q","value":""},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#1.value"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • And when I set a value for query parameter q I get these events:

    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?q=2&units=metric&APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // queryModel:
    [{"displayName":"q","value":"2"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value","value":"2"}}}
    // queryModel:
    [{"displayName":"q","value":"2"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]

What I see that happens in Chrome when page is loaded:

  • When the requestPanel.method is assigned in line #48, a url-changed event is fired:

    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • When the requestPanel.baseUri is assigned in line #49, another url-changed event is fired:

    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?units=metric&APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • Then a succession of these events occur:

    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value","value":""}}}
    // queryModel:
    [{"displayName":"q","value":""},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#1.value","value":"metric"}}}
    // queryModel:
    [{"displayName":"q","value":""},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units","value":"metric"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?APPID=938ffc1ccc85558966b89eaa29a2ddc0"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#1.value"}}}
    // queryModel:
    [{"displayName":"q"},{"displayName":"units"},{"displayName":"APPID","value":"938ffc1ccc85558966b89eaa29a2ddc0"}]
  • And when I set a value for query parameter q I get these events:

    // Event:
    {"type":"url-changed","event":{"isTrusted":false,"detail":{"value":"https://mocksvc-proxy.qax.anypoint.mulesoft.com/exchange/c44be829-3d89-4b70-80be-277b7633bc5a/pep/1.0.2/data/2.5/weather?q=2"}}}
    // queryModel:
    [{"displayName":"q","value":"2"},{"displayName":"units"},{"displayName":"APPID"}]
    // Event:
    {"type":"query-model-changed","event":{"isTrusted":false,"detail":{"path":"queryModel.#0.value","value":"2"}}}
    // queryModel:
    [{"displayName":"q","value":"2"},{"displayName":"units"},{"displayName":"APPID"}]

Optional URI parameters

URI parameters that are not marked as required are missing from the view. URI parameters panel doesn't support "optional" parameters and therefore they always must be visible.

Decode example from RAML

It is possible that the example form the RAML file is already decoded. It is passed as a value to the parameters editor it is encoded again which makes an error while performing a request.

The value should be first url decoded before inserting it to the input field.

RAML request panel shows incorrect `uri-parameter` field

(From JIRA report)

  • There is a RAML with two endpoints
  • Both endpoints have a uri-parameter, with the same name but with different type
  • The request panel-input field is loaded correctly for one endpoint, but incorrectly for the other one.

Example RAML:

/path-param-string/{pathparam}:
  uriParameters:
    pathparam:
      type: string
      example: "aParam"
  head:
    responses:
      201:
        body:
          application/json:
            example: { "path-param-string": 20 }


/path-param-enum/{pathparam}:
  uriParameters:
    pathparam:
      enum: ["option-a", "option-b"]
  patch:
    responses:
      201:
        body:
          application/json:
            example: { "path-param-enum": 40 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.