-
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.
Factor out some common stuff from calculator.ts
I want to copy this file for the RI calculator. To avoid too much duplication, this factors out some common logic: the "fetch from API and interpret errors" code, and the static footer. It also changes the `api-path` attribute of the calculator element to `api-host`, which should contain only the protocol and hostname, not the path. Since the RI calculator will be fetching multiple paths, I want to separate path from host. `api-path` is not publicly documented, so I think it's fine to remove it.
- Loading branch information
Showing
3 changed files
with
74 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Fetches a response from the Incentives API. Handles turning an error response | ||
* into an exception with a useful message. | ||
*/ | ||
export async function fetchApi( | ||
apiKey: string, | ||
apiHost: string, | ||
path: string, | ||
query: URLSearchParams, | ||
) { | ||
const url = new URL(apiHost); | ||
url.pathname = path; | ||
url.search = query.toString(); | ||
const response: Response = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
if (response.status >= 400) { | ||
console.error(response); | ||
// statusText isn't always set, but it's a reasonable proxy for a human readable error if it is: | ||
let message = response.statusText; | ||
try { | ||
const error = await response.json(); | ||
console.error(error); | ||
if (error.title && error.detail) { | ||
// Zuplo's API key errors have this form: | ||
message = `${error.title}: ${error.detail}`; | ||
} else if (error.message && error.error) { | ||
// Rewiring America's API errors have this form: | ||
message = `${error.error}: ${error.message}`; | ||
} | ||
} catch (e) { | ||
// if we couldn't get anything off the response, just go with something generic: | ||
message = 'Error loading incentives.'; | ||
} | ||
throw new Error(message); | ||
} | ||
return response.json(); | ||
} |
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,21 @@ | ||
import { html } from 'lit'; | ||
|
||
export const CALCULATOR_FOOTER = html`<div class="calculator__footer"> | ||
<p> | ||
Calculator by | ||
<a target="_blank" href="https://www.rewiringamerica.org"> | ||
Rewiring America | ||
</a> | ||
• | ||
<a | ||
target="_blank" | ||
href="https://content.rewiringamerica.org/view/privacy-policy.pdf" | ||
> | ||
Privacy Policy | ||
</a> | ||
• | ||
<a target="_blank" href="https://content.rewiringamerica.org/api/terms.pdf"> | ||
Terms | ||
</a> | ||
</p> | ||
</div>`; |
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