(url: string, config?: RequestInit, nothen?: boolean)
| 126 | } |
| 127 | |
| 128 | request(url: string, config?: RequestInit, nothen?: boolean): Promise<Response | any> { |
| 129 | config = config || {}; |
| 130 | const headers = <Headers>config.headers || new Headers(); |
| 131 | if (!url.includes("uploadSession")) { |
| 132 | headers.set(`Authorization`, `Bearer ${this.accessToken}`); |
| 133 | } |
| 134 | config.headers = headers; |
| 135 | const doFetch = () => fetch(url, config); |
| 136 | const retryWithFreshToken = async () => { |
| 137 | const token = await AuthVerify("onedrive", true); |
| 138 | this.accessToken = token; |
| 139 | if (!url.includes("uploadSession")) { |
| 140 | headers.set(`Authorization`, `Bearer ${this.accessToken}`); |
| 141 | } |
| 142 | return doFetch(); |
| 143 | }; |
| 144 | if (nothen) { |
| 145 | return doFetch().then(async (resp) => { |
| 146 | if (resp.status === 401 && !url.includes("uploadSession")) { |
| 147 | return retryWithFreshToken(); |
| 148 | } |
| 149 | return resp; |
| 150 | }); |
| 151 | } |
| 152 | return doFetch() |
| 153 | .then(async (resp) => { |
| 154 | if (resp.status === 401 && !url.includes("uploadSession")) { |
| 155 | resp = await retryWithFreshToken(); |
| 156 | } |
| 157 | if (!resp.ok) { |
| 158 | throw await this.createResponseError(resp); |
| 159 | } |
| 160 | return resp.json(); |
| 161 | }) |
| 162 | .then(async (data) => { |
| 163 | if (data.error) { |
| 164 | if (data.error.code === "InvalidAuthenticationToken") { |
| 165 | return retryWithFreshToken() |
| 166 | .then(async (retryResp) => { |
| 167 | if (!retryResp.ok) { |
| 168 | throw await this.createResponseError(retryResp); |
| 169 | } |
| 170 | return retryResp.json(); |
| 171 | }) |
| 172 | .then((retryData) => { |
| 173 | if (retryData.error) { |
| 174 | throw this.createRequestError(retryData); |
| 175 | } |
| 176 | return retryData; |
| 177 | }); |
| 178 | } |
| 179 | throw this.createRequestError(data); |
| 180 | } |
| 181 | return data; |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | async delete(path: string): Promise<void> { |
no test coverage detected