我可以在RAML文档中包含代码示例吗?

时间:2019-09-09 19:45:16

标签: raml

是否可以在RAML文件中包含代码段,例如有关如何使用不同语言使用每个端点的示例?

理想情况是这样的:

/account:
  post:
    description: Create an account
    (snippets):
      javascript: |
        fetch('http://my-api/account', {method: 'post', body: ...})
          .then(() => console.log('Success!'));
      php: |
        // whatever the php version of the above is
      golang: |
        // you know what I mean. Also, it'd be nice to get color coding for each language
    body:
      ...

1 个答案:

答案 0 :(得分:1)

您可能需要在“文档”下添加文本。在那里,您也可以简单地添加代码段:

#%RAML 1.0
baseUri:
title: My API
version: v1.0
mediaType: [ application/json ]
protocols: [ HTTP, HTTPS ]
documentation:
  - title: Introduction
    content: |
      This is a sample documentation. This API works like this:

      Please see [Official documentation](https://My-URL) for more information
      about the API.

      ```javascript
      var raml2html = require('raml2html');
      Some examples about how to query the REST API
      // Comments if needed
      ```
  - title: Chapter two
    content: |
      More content here. Including **bold** text!
      Small table:

      | A | B | C |
      |---|---|---|
      | 1 | 2 | 3 |
      Done

types:
  Error:
    type: object
    example: |
      {
        "code": 400,
        "message": "Error occured while parsing Json"
      }
    properties:
      code:
        type: integer
        description: The Error code
        required: true
      message:
        type: string
        description: The Error message
        required: true