import type { PaperAuthMeResponse } from './types.js';
const API_BASE_URL = 'https://api.paper.design';
export class PaperAPI {
private cookies: string;
constructor(cookies: string) {
this.cookies = cookies;
}
async getUserInfo(): Promise<PaperAuthMeResponse> {
try {
const response = await fetch(`${API_BASE_URL}/auth/me`, {
method: 'GET',
headers: {
'Cookie': this.cookies,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
if (response.status === 401) {
throw new Error('Authentication failed: Invalid or expired cookies');
}
throw new Error(`Failed to fetch user info: ${response.status} ${response.statusText}`);
}
const data = await response.json() as PaperAuthMeResponse;
if (!data.user) {
throw new Error('Invalid response: missing user data');
}
return data;
} catch (error) {
if (error instanceof Error) {
throw error;
}
throw new Error(`Unexpected error fetching user info: ${String(error)}`);
}
}
}