Files
coconut-leaf/frontend/src/api/collection.ts
T

120 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-06-08 10:45:35 +08:00
import { apiWrapper, boolApiWrapper } from './index';
2026-05-14 21:13:13 +08:00
2026-06-08 10:45:35 +08:00
/** A raw collection row as returned by the API (positional from SQL columns) */
export type CollectionRow = [
uuid: string,
2026-05-14 21:13:13 +08:00
name: string,
2026-06-08 10:45:35 +08:00
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 });
2026-05-14 21:13:13 +08:00
}
/**
2026-06-08 10:45:35 +08:00
* 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
2026-05-14 21:13:13 +08:00
*/
2026-06-08 10:45:35 +08:00
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> {
2026-05-14 21:13:13 +08:00
return boolApiWrapper('/api/collection/deleteOwn', { token, uuid, lastChange });
}
/**
2026-06-08 10:45:35 +08:00
* 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
2026-05-14 21:13:13 +08:00
*/
2026-06-08 10:45:35 +08:00
export async function getSharing(token: string, uuid: string): Promise<string[] | undefined> {
return apiWrapper<string[]>('/api/collection/getSharing', { token, uuid });
2026-05-14 21:13:13 +08:00
}
/**
2026-06-08 10:45:35 +08:00
* 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
2026-05-14 21:13:13 +08:00
*/
2026-06-08 10:45:35 +08:00
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 });
2026-05-14 21:13:13 +08:00
}
/**
2026-06-08 10:45:35 +08:00
* 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
2026-05-14 21:13:13 +08:00
*/
2026-06-08 10:45:35 +08:00
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 });
2026-05-14 21:13:13 +08:00
}
/**
2026-06-08 10:45:35 +08:00
* 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
2026-05-14 21:13:13 +08:00
*/
2026-06-08 10:45:35 +08:00
export async function getShared(token: string): Promise<SharedCollectionRow[] | undefined> {
return apiWrapper<SharedCollectionRow[]>('/api/collection/getShared', { token });
2026-05-14 21:13:13 +08:00
}