-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from TouK/NU-1103/prettier
NU-1103 prettier project
- Loading branch information
Showing
57 changed files
with
2,422 additions
and
2,961 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 |
---|---|---|
@@ -1,68 +1,57 @@ | ||
const ACCESS_TOKEN_NAMESPACE = "accessToken"; | ||
|
||
const addHeaders = | ||
(customHeaders: Record<string, string>) => | ||
({ headers, ...req }: RequestInit) => ({ | ||
...req, | ||
headers: { | ||
...headers, | ||
...customHeaders, | ||
}, | ||
}); | ||
(customHeaders: Record<string, string>) => | ||
({ headers, ...req }: RequestInit) => ({ | ||
...req, | ||
headers: { | ||
...headers, | ||
...customHeaders, | ||
}, | ||
}); | ||
|
||
const addTokenHeader = (token: string) => | ||
addHeaders({ Authorization: `Bearer ${token}` }); | ||
const addTokenHeader = (token: string) => addHeaders({ Authorization: `Bearer ${token}` }); | ||
|
||
const withContentType = addHeaders({ "Content-Type": "application/json" }); | ||
|
||
export const fetchSecured = async <R>( | ||
url: string, | ||
init: RequestInit = {} | ||
): Promise<R> => { | ||
const storageToken = localStorage.getItem(ACCESS_TOKEN_NAMESPACE); | ||
const queryParamToken = getQueryParameters()[ACCESS_TOKEN_NAMESPACE]; | ||
const token = queryParamToken ? queryParamToken : storageToken; | ||
if (queryParamToken) { | ||
localStorage.setItem(ACCESS_TOKEN_NAMESPACE, queryParamToken); | ||
window.history.replaceState(null, null, window.location.pathname); | ||
} | ||
const withAuth = addTokenHeader(token); | ||
return await fetchJson(url, withAuth(init)); | ||
export const fetchSecured = async <R>(url: string, init: RequestInit = {}): Promise<R> => { | ||
const storageToken = localStorage.getItem(ACCESS_TOKEN_NAMESPACE); | ||
const queryParamToken = getQueryParameters()[ACCESS_TOKEN_NAMESPACE]; | ||
const token = queryParamToken ? queryParamToken : storageToken; | ||
if (queryParamToken) { | ||
localStorage.setItem(ACCESS_TOKEN_NAMESPACE, queryParamToken); | ||
window.history.replaceState(null, null, window.location.pathname); | ||
} | ||
const withAuth = addTokenHeader(token); | ||
return await fetchJson(url, withAuth(init)); | ||
}; | ||
|
||
export const fetchJson = async <R>( | ||
url: string, | ||
init: RequestInit = {} | ||
): Promise<R> => { | ||
const response = await fetch(url, withContentType(init)); | ||
export const fetchJson = async <R>(url: string, init: RequestInit = {}): Promise<R> => { | ||
const response = await fetch(url, withContentType(init)); | ||
|
||
let json: R & { error?: unknown; message?: unknown }; | ||
let json: R & { error?: unknown; message?: unknown }; | ||
|
||
try { | ||
json = await response.json(); | ||
} catch { | ||
return; | ||
} | ||
try { | ||
json = await response.json(); | ||
} catch { | ||
return; | ||
} | ||
|
||
if (!response.ok || json.error) { | ||
throw json.message || json.error; | ||
} | ||
return json as R; | ||
if (!response.ok || json.error) { | ||
throw json.message || json.error; | ||
} | ||
return json as R; | ||
}; | ||
|
||
export const getQueryParameters = () => { | ||
const queryStringKeyValue = window.location.search | ||
.replace("?", "") | ||
.split("&"); | ||
return queryStringKeyValue.reduce((acc, curr) => { | ||
const [key, value] = curr.split("="); | ||
return { | ||
...acc, | ||
[key]: value, | ||
}; | ||
}, {}); | ||
const queryStringKeyValue = window.location.search.replace("?", "").split("&"); | ||
return queryStringKeyValue.reduce((acc, curr) => { | ||
const [key, value] = curr.split("="); | ||
return { | ||
...acc, | ||
[key]: value, | ||
}; | ||
}, {}); | ||
}; | ||
|
||
export const fetchFn = Object.keys(getQueryParameters()).length | ||
? fetchSecured | ||
: fetchJson; | ||
export const fetchFn = Object.keys(getQueryParameters()).length ? fetchSecured : fetchJson; |
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
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.