forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1230825
commit 8d9cfe2
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
apps/skolplattformen-sthlm/__mocks__/@react-native-cookies/cookies.ts
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,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<boolean> | ||
setFromResponse(url: string, cookie: string): Promise<boolean> | ||
|
||
get(url: string, useWebKit?: boolean): Promise<Cookies> | ||
|
||
clearAll(useWebKit?: boolean): Promise<boolean> | ||
} | ||
|
||
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 |
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
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