63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
export const FEEDBIN_DEFAULT_URL = 'https://feedbin.com';
|
|
|
|
// Normalizes the user-supplied instance URL to a bare origin. Feedbin's API
|
|
// lives on the `api.` subdomain of the instance (self-hosted deployments are
|
|
// documented to enable `api.<domain>` as well), so the API base is derived
|
|
// from whatever instance URL is configured.
|
|
export function normalizeFeedbinUrl(input) {
|
|
let value = (input ?? '').trim();
|
|
|
|
// Reject empty input and anything containing whitespace; the WHATWG parser is
|
|
// lenient enough to accept some malformed hosts that this should not.
|
|
if (!value || /\s/.test(value)) {
|
|
return null;
|
|
}
|
|
|
|
if (!/^https?:\/\//i.test(value)) {
|
|
value = `https://${value}`;
|
|
}
|
|
|
|
try {
|
|
return new URL(value).origin;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function apiBaseUrl(instanceUrl = FEEDBIN_DEFAULT_URL) {
|
|
const url = new URL(normalizeFeedbinUrl(instanceUrl) ?? FEEDBIN_DEFAULT_URL);
|
|
const host = url.hostname.startsWith('api.')
|
|
? url.hostname
|
|
: `api.${url.hostname}`;
|
|
|
|
return `${url.protocol}//${host}`;
|
|
}
|
|
|
|
// Credentials are checked against this endpoint when the form is submitted.
|
|
export function authenticationUrl(instanceUrl) {
|
|
return `${apiBaseUrl(instanceUrl)}/v2/authentication.json`;
|
|
}
|
|
|
|
// `read=false` returns unread entries and, via the `X-Feedbin-Record-Count`
|
|
// response header, the total unread count without downloading every entry id.
|
|
export function unreadCountUrl(instanceUrl) {
|
|
return `${apiBaseUrl(instanceUrl)}/v2/entries.json?read=false&per_page=1`;
|
|
}
|
|
|
|
// Basic auth requires the password, so it is stored pre-encoded. Encoding is
|
|
// done byte-wise to keep non-ASCII passwords intact.
|
|
export function encodeBasicToken(email, password) {
|
|
const bytes = new TextEncoder().encode(`${email}:${password}`);
|
|
let binary = '';
|
|
|
|
for (const byte of bytes) {
|
|
binary += String.fromCharCode(byte);
|
|
}
|
|
|
|
return btoa(binary);
|
|
}
|
|
|
|
export function authorizationHeader(token) {
|
|
return `Basic ${token}`;
|
|
}
|