From 63837955a33e3e913b6c88a72ae3c37d95d6b321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sarstr=C3=B6m?= Date: Fri, 12 Nov 2021 08:11:23 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fixes=20tests,=20added=20?= =?UTF-8?q?esModuleInterop=20true=20to=20tsconfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../@react-native-cookies/cookies.ts | 81 +++++++++++++++++++ libs/api/tsconfig.json | 3 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 libs/api/__mocks__/@react-native-cookies/cookies.ts diff --git a/libs/api/__mocks__/@react-native-cookies/cookies.ts b/libs/api/__mocks__/@react-native-cookies/cookies.ts new file mode 100644 index 000000000..7ce777bc7 --- /dev/null +++ b/libs/api/__mocks__/@react-native-cookies/cookies.ts @@ -0,0 +1,81 @@ +import { CookieJar, Cookie as TCookie } from 'tough-cookie' + +export interface Cookie { + name: string + value: string + path?: string + domain?: string + version?: string + expires?: string + secure?: boolean + httpOnly?: boolean +} + +export interface Cookies { + [key: string]: Cookie +} + +export interface CookieManagerStatic { + set(url: string, cookie: Cookie, useWebKit?: boolean): Promise + setFromResponse(url: string, cookie: string): Promise + + get(url: string, useWebKit?: boolean): Promise + + clearAll(useWebKit?: boolean): Promise +} + +const convertTtoC = (cookie: string | TCookie): Cookie => { + if (typeof cookie === 'string') { + return convertTtoC(TCookie.parse(cookie) as TCookie) + } + return { + name: cookie.key, + value: cookie.value, + domain: cookie.domain || undefined, + expires: + cookie.expires === 'Infinity' ? undefined : cookie.expires.toUTCString(), + httpOnly: cookie.httpOnly || undefined, + path: cookie.path || undefined, + secure: cookie.secure, + } +} +const convertCtoT = (cookie: Cookie): TCookie => + new TCookie({ + key: cookie.name, + value: cookie.value, + domain: cookie.domain, + expires: cookie.expires ? new Date(cookie.expires) : undefined, + httpOnly: cookie.httpOnly || false, + path: cookie.path, + secure: cookie.secure || false, + }) +const convertCookies = (cookies: TCookie[]): Cookies => + cookies.reduce( + (map, cookie) => ({ + ...map, + [cookie.key]: convertTtoC(cookie), + }), + {} as Cookies + ) + +const jar = new CookieJar() +const CookieManager: CookieManagerStatic = { + clearAll: async () => { + await jar.removeAllCookies() + return true + }, + get: async (url) => { + const cookies = await jar.getCookies(url) + return convertCookies(cookies) + }, + set: async (url, cookie) => { + await jar.setCookie(convertCtoT(cookie), url) + return true + }, + setFromResponse: async (url, cookie) => { + await jar.setCookie(cookie, url) + return true + }, +} + +export default CookieManager diff --git a/libs/api/tsconfig.json b/libs/api/tsconfig.json index 243885b7c..c85f92e71 100644 --- a/libs/api/tsconfig.json +++ b/libs/api/tsconfig.json @@ -7,7 +7,8 @@ "outDir": "./dist", "strict": true, "allowSyntheticDefaultImports": true, - "sourceMap": true + "sourceMap": true, + "esModuleInterop": true }, "include": ["lib", "../../__mocks__" ], "exclude": [