All files / lib/accounts types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                                                                                                                                                                                                                                       
import type * as DB from '$lib/database.js';
import type { DatabaseHandle } from '$lib/idb.svelte.js';
import type { SessionRemoteID } from '$lib/schemas/sessions.js';
 
export type AuthenticationMethod = 'oauth' | 'token' | 'password';
 
export type LoginData<S extends string = string> = {
	server: S;
	token?: string;
	password?: { username: string; password: string };
	oauth?: { authorize: URL; identity: URL; token: URL };
};
 
export interface Account {
	username: string;
	displayName: string;
	avatarURL: URL | undefined;
	/** Database ID of the account. */
	id: string | undefined;
 
	logout(): Promise<void>;
 
	/**
	 * Upload a session to the account
	 * @param session session object from the database
	 */
	upload(session: DB.Session): Promise<{
		/** If the session has a remote ID that can be used to import it back later into CIGALE. Useful if the Account is ALSO a {@link AccountRemoteSessions} */
		remoteID?: SessionRemoteID;
		/**
		 * URL to where the session can be visited on the account
		 */
		page?: URL;
	}>;
 
	/**
	 * List available remote sessions on the account
	 * @param protocol chosen protocol
	 * @param options
	 * @param options.cursor see nextCursor in the response
	 * @param options.limit change the number of sessions returned in each call. Default is implementation-specific
	 * @param options.mine only return sessions created by the account's user
	 */
	sessions(options?: {
		cursor?: string | undefined;
		limit?: number;
		mine?: boolean;
	}): AsyncIterable<
		| {
				/** Signals the total number of sessions */
				total: number;
		  }
		| {
				id: SessionRemoteID;
				protocol: string;
				page: URL | undefined;
				name: string;
				submittedAt: Date;
				submittedBy?: string;
				thumbnails: URL[];
				/** Cursor to pass to the method to get the next results. Must be undefined once we have finished listing all sessions. Must be the same on all items */
				nextCursor: string | undefined;
				filesCount: number;
				imagesCount: number;
		  }
	>;
 
	/**
	 * Get the remote session
	 * @param protocol protocol of the session
	 * @param id remote ID of the session
	 */
	session(
		protocol: DB.Protocol,
		id: SessionRemoteID
	): Promise<Omit<(typeof DB.Schemas.Session)['inferIn'], 'id' | 'account'>>;
 
	/**
	 * Fetch the thumbnail for a session,
	 * returning a blob:// URL ready for use
	 */
	thumbnail(url: URL): Promise<URL>;
 
	/**
	 * Get all observations/images/image files of the remote session
	 * @param protocol protocol of the session
	 * @param session remote ID of the session (in Session.remote.id in the database)
	 */
	items(
		protocol: DB.Protocol,
		session: SessionRemoteID
	): Promise<{
		observations: Array<(typeof DB.Schemas.Observation)['inferIn']>;
		images: Array<(typeof DB.Schemas.Image)['inferIn']>;
		files: Array<(typeof DB.Tables.ImageFile)['inferIn']>;
	}>;
 
	/**
	 * Get files from file-type session metadata values
	 * @param protocol protocol of the session
	 * @param session remote ID of the session
	 */
	files(
		protocol: DB.Protocol,
		session: SessionRemoteID
	): AsyncIterable<Omit<(typeof DB.Tables.MetadataValueFile)['inferIn'], 'sessionId'>>;
}
 
export interface AccountConstructor<
	Auth extends AuthenticationMethod = AuthenticationMethod,
	Server extends string = string,
> {
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	new (...args: any[]): Account;
 
	id: string;
	logoURL: URL;
	displayName: string;
	capabilities: readonly ('sessions' | 'images' | 'upload')[];
	auth: Auth;
	servers: readonly { domain: Server; name?: string }[];
 
	/** Returns the error message, or undefined if everything is a-ok */
	checkAuth(data: LoginData<Server>): Promise<undefined | string>;
 
	fromDatabase(db: DatabaseHandle, account: DB.Account): Account;
	login(
		db: DatabaseHandle,
		data: LoginData<Server>
	): Promise<Omit<(typeof DB.Schemas.Account)['inferIn'], 'id'>>;
}