(dataAPI: any, method = 'GET', data: any, headers = {}, otherProps: any = {})
| 78 | * @returns |
| 79 | */ |
| 80 | export function request(dataAPI: any, method = 'GET', data: any, headers = {}, otherProps: any = {}) { |
| 81 | let processedHeaders = headers || {}; |
| 82 | let payload = data; |
| 83 | if (method === 'PUT' || method === 'DELETE') { |
| 84 | processedHeaders = { |
| 85 | Accept: 'application/json', |
| 86 | 'Content-Type': 'application/json', |
| 87 | ...processedHeaders, |
| 88 | }; |
| 89 | payload = JSON.stringify(payload || {}); |
| 90 | } |
| 91 | return new Promise((resolve, reject) => { |
| 92 | if (otherProps.timeout) { |
| 93 | setTimeout(() => { |
| 94 | reject(new Error('timeout')); |
| 95 | }, otherProps.timeout); |
| 96 | } |
| 97 | fetch(dataAPI, { |
| 98 | method, |
| 99 | credentials: 'include', |
| 100 | headers: processedHeaders, |
| 101 | body: payload, |
| 102 | ...otherProps, |
| 103 | }) |
| 104 | .then((response) => { |
| 105 | switch (response.status) { |
| 106 | case 200: |
| 107 | case 201: |
| 108 | case 202: |
| 109 | return response.json(); |
| 110 | case 204: |
| 111 | if (method === 'DELETE') { |
| 112 | return { |
| 113 | success: true, |
| 114 | }; |
| 115 | } else { |
| 116 | return { |
| 117 | __success: false, |
| 118 | code: response.status, |
| 119 | }; |
| 120 | } |
| 121 | case 400: |
| 122 | case 401: |
| 123 | case 403: |
| 124 | case 404: |
| 125 | case 406: |
| 126 | case 410: |
| 127 | case 422: |
| 128 | case 500: |
| 129 | return response |
| 130 | .json() |
| 131 | .then((res) => { |
| 132 | return { |
| 133 | __success: false, |
| 134 | code: response.status, |
| 135 | data: res, |
| 136 | }; |
| 137 | }) |
no outgoing calls
no test coverage detected
searching dependent graphs…