feat: replace browser login with imported Reddit sessions
This commit is contained in:
1 parent
2b0545afcf
commit
c483c7b8be
76 files changed
+2048
-2828
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
type ControlResponse,
|
||||
type Timers,
|
||||
} from './contracts.js';
|
||||
import { createDashboardRequest } from './request.js';
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
@@ -39,9 +40,7 @@ export interface DashboardApi {
|
||||
}
|
||||
|
||||
export function createDashboardApi(fetchImplementation?: typeof fetch): DashboardApi {
|
||||
function request(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
||||
return (fetchImplementation ?? globalThis.fetch)(input, init);
|
||||
}
|
||||
const request = createDashboardRequest(fetchImplementation);
|
||||
|
||||
return {
|
||||
async getStats() {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
export type DashboardRequest = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
||||
|
||||
function redirectToLogin(): void {
|
||||
globalThis.location.assign('/auth/login');
|
||||
}
|
||||
|
||||
export function createDashboardRequest(
|
||||
fetchImplementation: typeof fetch | undefined = undefined,
|
||||
onUnauthorized: () => void = redirectToLogin,
|
||||
): DashboardRequest {
|
||||
return async (input, init) => {
|
||||
const fetchRequest = fetchImplementation ?? globalThis.fetch;
|
||||
const response = await fetchRequest(input, { credentials: 'same-origin', ...init });
|
||||
if (response.status === 401) onUnauthorized();
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
export const dashboardRequest = createDashboardRequest();
|
||||
Reference in new issue
Block a user