-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cookie support via interceptors
Remove axios-cookiejar-support depdenency. Add cookie-support.ts for providing interceptors to interact with tough-cookie.
- Loading branch information
Showing
6 changed files
with
219 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* (C) Copyright IBM Corp. 2022. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import extend from 'extend'; | ||
import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { CookieJar } from 'tough-cookie'; | ||
import logger from './logger'; | ||
|
||
export class CookieInterceptor { | ||
private readonly cookieJar: CookieJar; | ||
|
||
constructor(cookieJar: CookieJar | boolean) { | ||
if (cookieJar) { | ||
if (cookieJar === true) { | ||
logger.debug('CookieInterceptor: creating new CookieJar'); | ||
this.cookieJar = new CookieJar(); | ||
} else { | ||
logger.debug('CookieInterceptor: using supplied CookieJar'); | ||
this.cookieJar = cookieJar; | ||
} | ||
} else { | ||
throw new Error('Must supply a cookie jar or true.'); | ||
} | ||
} | ||
|
||
public async requestInterceptor(config: AxiosRequestConfig) { | ||
logger.debug('CookieInterceptor: intercepting request'); | ||
if (config && config.url) { | ||
logger.debug(`CookieInterceptor: getting cookies for: ${config.url}`); | ||
const cookieHeaderValue = await this.cookieJar.getCookieString(config.url); | ||
if (cookieHeaderValue) { | ||
logger.debug('CookieInterceptor: setting cookie header'); | ||
const cookieHeader = { cookie: cookieHeaderValue }; | ||
config.headers = extend(true, {}, config.headers, cookieHeader); | ||
} else { | ||
logger.debug(`CookieInterceptor: no cookies for: ${config.url}`); | ||
} | ||
} else { | ||
logger.debug('CookieInterceptor: no request URL.'); | ||
} | ||
return config; | ||
} | ||
|
||
public async responseInterceptor(response: AxiosResponse) { | ||
logger.debug('CookieInterceptor: intercepting response.'); | ||
if (response && response.headers) { | ||
logger.debug('CookieInterceptor: checking for set-cookie headers.'); | ||
const cookies: string[] = response.headers['set-cookie']; | ||
if (cookies) { | ||
logger.debug('CookieInterceptor: setting cookies in jar.'); | ||
cookies.forEach(async (cookie: string) => { | ||
await this.cookieJar.setCookie(cookie, response.request.url); | ||
}); | ||
} else { | ||
logger.debug('CookieInterceptor: no set-cookie headers.'); | ||
} | ||
} else { | ||
logger.debug('CookieInterceptor: no response headers.'); | ||
} | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.