( input, options = {} )
| 107 | } |
| 108 | |
| 109 | export async function request ( input, options = {} ) { |
| 110 | const config = toRequestConfig( input, options ) |
| 111 | const { |
| 112 | attempts = 1, |
| 113 | cache, |
| 114 | credentials, |
| 115 | data, |
| 116 | delayMs = 1000, |
| 117 | headers: inputHeaders, |
| 118 | method: inputMethod = 'GET', |
| 119 | mode, |
| 120 | redirect, |
| 121 | responseType = 'json', |
| 122 | signal, |
| 123 | url |
| 124 | } = config |
| 125 | const method = inputMethod.toUpperCase() |
| 126 | const headers = createHeaders( inputHeaders ) |
| 127 | |
| 128 | let body |
| 129 | if ( data !== undefined ) { |
| 130 | body = JSON.stringify( data ) |
| 131 | |
| 132 | if ( !headers.has( 'Accept' ) ) { |
| 133 | headers.set( 'Accept', 'application/json' ) |
| 134 | } |
| 135 | |
| 136 | if ( !headers.has( 'Content-Type' ) ) { |
| 137 | headers.set( 'Content-Type', 'application/json' ) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | let lastError |
| 142 | |
| 143 | for ( let attempt = 1; attempt <= attempts; attempt += 1 ) { |
| 144 | try { |
| 145 | const response = await fetch( url, buildRequestInit({ |
| 146 | body, |
| 147 | cache, |
| 148 | credentials, |
| 149 | headers, |
| 150 | method, |
| 151 | mode, |
| 152 | redirect, |
| 153 | signal |
| 154 | }) ) |
| 155 | const responseData = await parseResponseBody( response, responseType ) |
| 156 | |
| 157 | if ( !response.ok ) { |
| 158 | throw new HttpError( |
| 159 | `${ method } ${ url } failed with status ${ response.status }`, |
| 160 | { |
| 161 | data: responseData, |
| 162 | method, |
| 163 | status: response.status, |
| 164 | statusText: response.statusText, |
| 165 | url |
| 166 | } |
no test coverage detected