Files
coconut-leaf/frontend/src/api/collection.ts
T
2026-06-08 10:45:35 +08:00

120 lines
4.2 KiB
TypeScript

import { apiWrapper, boolApiWrapper } from './index';
/** A raw collection row as returned by the API (positional from SQL columns) */
export type CollectionRow = [
uuid: string,
name: string,
lastChange: string,
];
/** A raw shared collection row as returned by the API */
export type SharedCollectionRow = [
uuid: string,
name: string,
user: string,
];
/**
* Fetch all collections owned by the current user.
*
* @param token - The auth token
* @returns An array of {@link CollectionRow} on success, or `undefined` on failure
*/
export async function getFullOwn(token: string): Promise<CollectionRow[] | undefined> {
return apiWrapper<CollectionRow[]>('/api/collection/getFullOwn', { token });
}
/**
* Fetch the detail of a single owned collection by UUID.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @returns A single {@link CollectionRow} on success, or `undefined` on failure
*/
export async function getDetailOwn(token: string, uuid: string): Promise<CollectionRow | undefined> {
return apiWrapper<CollectionRow>('/api/collection/getDetailOwn', { token, uuid });
}
/**
* Create a new collection.
*
* @param token - The auth token
* @param name - The collection name
* @returns The new collection UUID on success, or `undefined` on failure
*/
export async function addOwn(token: string, name: string): Promise<string | undefined> {
return apiWrapper<string>('/api/collection/addOwn', { token, name });
}
/**
* Update an existing collection's name.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @param name - The new collection name
* @param lastChange - The last known `lastChange` value (optimistic concurrency)
* @returns The new `lastChange` value on success, or `undefined` on failure
*/
export async function updateOwn(token: string, uuid: string, name: string, lastChange: string): Promise<string | undefined> {
return apiWrapper<string>('/api/collection/updateOwn', { token, uuid, name, lastChange });
}
/**
* Delete an owned collection.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @param lastChange - The last known `lastChange` value (optimistic concurrency)
* @returns `true` on success, `false` on failure
*/
export async function deleteOwn(token: string, uuid: string, lastChange: string): Promise<boolean> {
return boolApiWrapper('/api/collection/deleteOwn', { token, uuid, lastChange });
}
/**
* Fetch all users this collection is shared with.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @returns An array of target usernames on success, or `undefined` on failure
*/
export async function getSharing(token: string, uuid: string): Promise<string[] | undefined> {
return apiWrapper<string[]>('/api/collection/getSharing', { token, uuid });
}
/**
* Remove a sharing target from a collection.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @param target - The username to unshare with
* @param lastChange - The last known `lastChange` value (optimistic concurrency)
* @returns The new `lastChange` value on success, or `undefined` on failure
*/
export async function deleteSharing(token: string, uuid: string, target: string, lastChange: string): Promise<string | undefined> {
return apiWrapper<string>('/api/collection/deleteSharing', { token, uuid, target, lastChange });
}
/**
* Add a sharing target to a collection.
*
* @param token - The auth token
* @param uuid - The collection UUID
* @param target - The username to share with
* @param lastChange - The last known `lastChange` value (optimistic concurrency)
* @returns The new `lastChange` value on success, or `undefined` on failure
*/
export async function addSharing(token: string, uuid: string, target: string, lastChange: string): Promise<string | undefined> {
return apiWrapper<string>('/api/collection/addSharing', { token, uuid, target, lastChange });
}
/**
* Fetch all collections shared with the current user.
*
* @param token - The auth token
* @returns An array of {@link SharedCollectionRow} on success, or `undefined` on failure
*/
export async function getShared(token: string): Promise<SharedCollectionRow[] | undefined> {
return apiWrapper<SharedCollectionRow[]>('/api/collection/getShared', { token });
}