|
| 1 | +openapi: 3.1.0 |
| 2 | +info: |
| 3 | + title: Event Handling |
| 4 | + version: "1.0" |
| 5 | + description: | |
| 6 | + |
| 7 | + ### The code demonstrate how some events can be used. Listed below are some common use cases |
| 8 | +
|
| 9 | + - `before-render` - Good place to twaek the OpenAPI spec if needed. In this example the Title above is dynamically created |
| 10 | + - `before-try` - Good place to twaek the AJAX request sucha as adding a request header or abort the request |
| 11 | + - `after-try` - Good place to inspect the response received |
| 12 | +
|
| 13 | + #### To test it out go ahead and click TRY below, you will notice all `POST` requests are aborted and `GET` are success <br/><br/> |
| 14 | +
|
| 15 | +
|
| 16 | + ```markup |
| 17 | + <!doctype html> |
| 18 | +
|
| 19 | + <html> |
| 20 | + <body> |
| 21 | + <rapi-doc id = "thedoc" spec-url = "..."> </rapi-doc> |
| 22 | + |
| 23 | + <script> |
| 24 | + document.addEventListener('DOMContentLoaded', (event) => { |
| 25 | + let docEl = document.getElementById("thedoc"); |
| 26 | +
|
| 27 | + // Add various event listeners |
| 28 | +
|
| 29 | + // 1. before-render (Dynamically changes the Title of this Spec) |
| 30 | + docEl.addEventListener('before-render', (e) => { |
| 31 | + e.detail.spec.info.title = "EVENTS - This text is updated during `before-render` event"; |
| 32 | + }); |
| 33 | +
|
| 34 | + // 2. before-try (Aborts all post calls) |
| 35 | + docEl.addEventListener('before-try', (e) => { |
| 36 | + if (e.detail.request.method === 'POST') { |
| 37 | + e.detail.controller.abort(); |
| 38 | + } |
| 39 | + }); |
| 40 | +
|
| 41 | + // 3. after-try |
| 42 | + docEl.addEventListener('after-try', (e) => { |
| 43 | + alert("Hello from 'after-try' event "); |
| 44 | + }); |
| 45 | +
|
| 46 | + // 4. request-aborted |
| 47 | + docEl.addEventListener('request-aborted', (e) => { |
| 48 | + calert("POST Requests are aborted in 'before-try' event "); |
| 49 | + }); |
| 50 | +
|
| 51 | + }) |
| 52 | + </script> |
| 53 | + </body> |
| 54 | +
|
| 55 | + </html> |
| 56 | + ``` |
| 57 | +servers: |
| 58 | + - url: https://reqres.in/api/ |
| 59 | +paths: |
| 60 | + /users: |
| 61 | + get: |
| 62 | + description: List of users (paginated) |
| 63 | + parameters: |
| 64 | + - name: page |
| 65 | + in: query |
| 66 | + schema: |
| 67 | + type: integer |
| 68 | + examples: |
| 69 | + - 1 |
| 70 | + - 2 |
| 71 | + responses: |
| 72 | + '200': |
| 73 | + description: Successful operation |
| 74 | + content: |
| 75 | + application/json: |
| 76 | + schema: |
| 77 | + type: object |
| 78 | + description: Description of **User** object |
| 79 | + properties: |
| 80 | + page: |
| 81 | + description: Current Page number |
| 82 | + type: integer |
| 83 | + per_page: |
| 84 | + description: Number of records per page |
| 85 | + type: integer |
| 86 | + total: |
| 87 | + description: Total number of records |
| 88 | + type: integer |
| 89 | + total_pages: |
| 90 | + type: integer |
| 91 | + data: |
| 92 | + type: array |
| 93 | + description: List of users |
| 94 | + items: |
| 95 | + $ref: '#/components/schemas/user' |
| 96 | + support: |
| 97 | + $ref: '#/components/schemas/support' |
| 98 | + post: |
| 99 | + description: Create a user |
| 100 | + requestBody: |
| 101 | + content: |
| 102 | + application/json: |
| 103 | + schema: |
| 104 | + $ref: "#/components/schemas/userInput" |
| 105 | + responses: |
| 106 | + 201: |
| 107 | + description: User creation response |
| 108 | + content: |
| 109 | + application/json: |
| 110 | + schema: |
| 111 | + allOf: |
| 112 | + - $ref: '#/components/schemas/userInput' |
| 113 | + - $ref: '#/components/schemas/createUserResponse' |
| 114 | + |
| 115 | +components: |
| 116 | + schemas: |
| 117 | + user: |
| 118 | + type: object |
| 119 | + properties: |
| 120 | + id: |
| 121 | + description: User ID |
| 122 | + type: integer |
| 123 | + email: |
| 124 | + description: User Email |
| 125 | + type: string |
| 126 | + first_name: |
| 127 | + description: First Name |
| 128 | + type: string |
| 129 | + last_name: |
| 130 | + description: Last Name |
| 131 | + type: string |
| 132 | + avatar: |
| 133 | + description: Avatar URL |
| 134 | + type: string |
| 135 | + support: |
| 136 | + type: object |
| 137 | + properties: |
| 138 | + url: |
| 139 | + description: Support URL |
| 140 | + type: string |
| 141 | + text: |
| 142 | + description: Support URL - Description |
| 143 | + type: string |
| 144 | + userInput: |
| 145 | + type: object |
| 146 | + description: user object with `name` and `job` properties |
| 147 | + properties: |
| 148 | + name: |
| 149 | + description: User Name |
| 150 | + type: string |
| 151 | + job: |
| 152 | + description: Job |
| 153 | + type: string |
| 154 | + createUserResponse: |
| 155 | + type: object |
| 156 | + properties: |
| 157 | + id: |
| 158 | + type: integer |
| 159 | + createdAt: |
| 160 | + type: string |
0 commit comments