1.1.11 • Published 3 years ago
yas-http v1.1.11
yas-http
Support to send http(s) requests, API document and Http(s) mock server
It's an extension for
yaml-scene
Features:
- Create testcases to test APIs
 - Send http(s) requests
 - Create a mock API server
- Server static file
 - Support upload file then save to server
 - Server RESTFul API data
 - Create APIs which auto handle CRUD data
 - Build router for yourself by code
 
 - Generate API document
 
Sharing
Details document
Prerequisite
- Platform 
yaml-scene 
Installation
  yas add yas-http        # npm install -g yas-http OR yard global add yas-httpExample
HTTP Client Request
Send a http request to a server
Send GET request
- yas-http/Get:
    url: http://localhost:3000/postsSend POST request
- yas-http/Post:
    url: http://localhost:3000/posts
    body:
      id: 2
      title: title 2
      author: typicode 2Send PUT request
- yas-http/Put:
    url: http://localhost:3000/posts/:id
    params:
      id: 2
    body:
      id: 2
      title: title 2 updated
      author: typicode 2 updatedSend PATCH request
- yas-http/Patch:
    url: http://localhost:3000/posts/:id
    params:
      id: 2
    body:
      id: 2
      title: title 2 updated
      author: typicode 2 updatedSend DELETE request
- yas-http/Delete:
    url: http://localhost:3000/posts/:id
    params:
      id: 2Send HEAD request
- yas-http/Head:
    url: http://localhost:3000/posts/:id
    params:
      id: 2Send CUSTOM request
- yas-http/Api:
    method: CONNECT
    url: http://localhost:3000/postsMock API Server
Create mock API Server without code
Server static file
- yas-http/Server:
    host: 0.0.0.0                               # Server host
    port: 8000                                  # Server port
    routers:                                    # Defined routes
      - serveIn: 
          - ./assets                            # All of files in list will be served after request toSupport upload file then save to server
- yas-http/Server:
    host: 0.0.0.0                               # Server host
    port: 8000                                  # Server port
    routers:                                    # Defined routes
      - path: /upload                           # Upload path. Default method is POST
        method: POST                            # Request method (POST, PUT, PATCH, DELETE, HEAD)
                                                # - Default method is POST
        uploadTo: ./uploadDir                   # Directory includes uploading filesServer RESTFul API data
- yas-http/Server:
    host: 0.0.0.0                               # Server host
    port: 8000                                  # Server port
    routers:                                    # Defined routes
      - method: GET                             # Request method (GET, POST, PUT, PATCH, DELETE, HEAD) (Default: GET)
        path: /posts/:id                        # Request path
        response:                               # Response data
          status: 200                           # - Response status
          statusMessage: OK                     # - Response status message
          headers:                              # - Response headers
            server: nginx
          data: [                               # - Response data. 
            {                                   #   - Use some variables to replace value to response
              "id": ${+params.id},              # params:  Request params (/:id)
              "title": "title 1",               # headers: Request headers
              "author": "thanh"                 # query:   Request querystring (?name=thanh)
              "des": "des 1",                   # body:    Request body
            }                                   # request: Request
          ]                                     # ctx:     ContextCreate APIs which auto handle CRUD data
- yas-http/Server:
    host: 0.0.0.0                               # Server host
    port: 8000                                  # Server port
    routers:                                    # Defined routes
      - path: '/:model'                         # Use this pattern to use with dynamic model name
        CRUD: true                              # Auto create full RESTful API
        dbFile: ./db.json                       # Store data to file. This make the next time, when server up will load data from the file.
                                                # - Empty then it's stateless
        clean: true                             # Clean db before server up
                                                # - GET    /model            : Return list models
                                                # - GET    /model/:id        : Return model details by id
                                                # - POST   /model            : Create a new model
                                                # - PUT    /model/:id        : Replace entity of post to new model
                                                # - PATCH  /model/:id        : Only update some properties of model
                                                # - DELETE /model/:id        : Delete a model by id
        initData: {                             # Init data for dynamic model name (/:model). 
                                                # - Only init data when 
                                                #   + Db file not existed
                                                #   + OR set "cleaned"
                                                #   + OR not set dbFile
          posts: [{                             # When you request /posts, it returns the value
            "id": 1,
            "label": "label 01"
          }],
          users: [{                             # When you request /users, it returns the value
            "id": 1,
            "label": "user 01"
          }]  
        }Build router for yourself by code
- yas-http/Server:
    host: 0.0.0.0                               # Server host
    port: 8000                                  # Server port
    routers:                                    # Defined routes
      - method: GET                             # Request method (GET, POST, PUT, PATCH, DELETE, HEAD) (Default: GET)
        path: /posts/:id                        # Request path
        handler: !function |                    # Handle code which handle request and response data
          (globalVars) {                                      
            // this.params: Request params
            // this.headers: Request headers
            // this.query: Request query string
            // this.body: Request body
            // this.request: Request
            // this.ctx: Context (koajs)
            const merge = require('lodash.merge')   
            return merge({                          
              params: this.params.id,                    
              name: this.query.name                      
            }, {                                    
              id: 1
            })
          }                Document to markdown
- yas-http/Doc/MD:
    title: Post service
    description: Demo CRUD API to generate to markdown document
    signature: "[Doan Thuan Thanh](mailto:doanthuanthanh88@gmail.com)"
    outFile: ./ApiMD.mdAdd doc in the yas-http/Get... to export to document
- yas-http/Get:
    doc: true
    ...- yas-http/Delete:
    doc: 
      tags: [POST]
    ...API request summary
Collect information of http(s) calls
yas-http/Summary:
  title: Testing result1.1.11
3 years ago
1.1.10
4 years ago
1.1.9
4 years ago
1.1.8
4 years ago
1.1.7
4 years ago
1.1.6
4 years ago
1.1.6-alpha.1
4 years ago
1.1.5
4 years ago
1.1.4
4 years ago
1.1.3
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago