apiclient
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import api from './api';
|
||||
import type {
|
||||
ApiTokenRecord,
|
||||
AssetResponse,
|
||||
CapabilityResponse,
|
||||
CapabilitySetResponse,
|
||||
DownloadLink,
|
||||
DocumentResponse,
|
||||
FolderTreeNode,
|
||||
Identifier,
|
||||
PasskeySummary,
|
||||
} from './apiTypes';
|
||||
|
||||
const normalizeNumber = (value: unknown): number | undefined => {
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : undefined;
|
||||
};
|
||||
|
||||
const normalizeDownload = (input?: DownloadLink | null): DownloadLink | null => {
|
||||
if (!input?.url) {
|
||||
return null;
|
||||
}
|
||||
const expires_at = normalizeNumber(input.expires_at);
|
||||
if (!expires_at) {
|
||||
return null;
|
||||
}
|
||||
return { url: input.url, expires_at };
|
||||
};
|
||||
|
||||
export const fetchDocument = async (id: Identifier): Promise<DocumentResponse> => {
|
||||
const { data } = await api.get<{ document?: DocumentResponse }>(`/documents/${id}`);
|
||||
const doc = data?.document || (data as unknown as DocumentResponse);
|
||||
if (doc?.current_version?.download) {
|
||||
doc.current_version.download = normalizeDownload(doc.current_version.download);
|
||||
}
|
||||
return doc;
|
||||
};
|
||||
|
||||
export const fetchAsset = async (id: Identifier): Promise<AssetResponse> => {
|
||||
const { data } = await api.get<AssetResponse>(`/assets/${id}`);
|
||||
const download = normalizeDownload(data.download);
|
||||
return {
|
||||
...data,
|
||||
download,
|
||||
};
|
||||
};
|
||||
|
||||
export const listDocuments = async (params: Record<string, unknown> = {}): Promise<DocumentResponse[]> => {
|
||||
const { data } = await api.get<DocumentResponse[]>('/documents', { params });
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export const getFolderTree = async (): Promise<FolderTreeNode[]> => {
|
||||
const { data } = await api.get<FolderTreeNode[]>('/folders/tree');
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export const listCapabilitySets = async (): Promise<CapabilitySetResponse[]> => {
|
||||
const { data } = await api.get<CapabilitySetResponse[]>('/capability-sets');
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export const listCapabilities = async (): Promise<CapabilityResponse[]> => {
|
||||
const { data } = await api.get<CapabilityResponse[]>('/capabilities');
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export const listApiTokens = async (): Promise<ApiTokenRecord[]> => {
|
||||
const { data } = await api.get<ApiTokenRecord[]>('/profile/api-tokens');
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export const listPasskeys = async (): Promise<PasskeySummary[]> => {
|
||||
const { data } = await api.get<PasskeySummary[]>('/profile/passkeys');
|
||||
return Array.isArray(data) ? data : [];
|
||||
};
|
||||
|
||||
export type {
|
||||
DownloadLink,
|
||||
DocumentResponse,
|
||||
AssetResponse,
|
||||
FolderTreeNode,
|
||||
CapabilitySetResponse,
|
||||
CapabilityResponse,
|
||||
ApiTokenRecord,
|
||||
PasskeySummary,
|
||||
Identifier,
|
||||
} from './apiTypes';
|
||||
Reference in New Issue
Block a user