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';
|
||||
@@ -0,0 +1,105 @@
|
||||
// Types aligned with OpenAPI schemas for common endpoints.
|
||||
|
||||
export type Identifier = string | number;
|
||||
|
||||
export interface DownloadLink {
|
||||
url: string;
|
||||
expires_at: number;
|
||||
}
|
||||
|
||||
export interface TagResponse {
|
||||
id: string;
|
||||
label: string;
|
||||
color?: string | null;
|
||||
}
|
||||
|
||||
export interface CorrespondentResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface AssetResponse {
|
||||
id: string;
|
||||
asset_type: string;
|
||||
mime_type: string;
|
||||
metadata: Record<string, unknown>;
|
||||
download?: DownloadLink | null;
|
||||
}
|
||||
|
||||
export interface DocumentVersionResponse {
|
||||
id: string;
|
||||
version_number: number;
|
||||
size_bytes: number;
|
||||
checksum: string;
|
||||
created_at: string;
|
||||
content_type: string | null;
|
||||
metadata: Record<string, unknown>;
|
||||
download: DownloadLink;
|
||||
assets?: AssetResponse[] | null;
|
||||
}
|
||||
|
||||
export interface DocumentResponse {
|
||||
id: string;
|
||||
filename: string;
|
||||
title: string;
|
||||
original_name: string;
|
||||
content_type?: string | null;
|
||||
folder_id?: string | null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
issued_at?: string | null;
|
||||
metadata: Record<string, unknown>;
|
||||
tags: TagResponse[];
|
||||
correspondents?: CorrespondentResponse[];
|
||||
current_version?: DocumentVersionResponse | null;
|
||||
}
|
||||
|
||||
export interface FolderInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
parent_id?: string | null;
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
export interface FolderTreeNode extends FolderInfo {
|
||||
children?: FolderTreeNode[];
|
||||
}
|
||||
|
||||
export interface CapabilitySetResponse {
|
||||
id: string;
|
||||
slug: string;
|
||||
is_system: boolean;
|
||||
cap_version: number;
|
||||
capabilities: string[];
|
||||
}
|
||||
|
||||
export interface CapabilityResponse {
|
||||
id?: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface TenantSnippet {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ApiTokenRecord {
|
||||
id: string;
|
||||
label?: string | null;
|
||||
capability_set_id: string;
|
||||
created_at: string;
|
||||
last_used_at?: string | null;
|
||||
expires_at?: string | null;
|
||||
}
|
||||
|
||||
export interface PasskeySummary {
|
||||
id: string;
|
||||
nickname?: string | null;
|
||||
createdAt: string;
|
||||
lastUsedAt?: string | null;
|
||||
transports?: string[];
|
||||
revokedAt?: string | null;
|
||||
revokedReason?: string | null;
|
||||
}
|
||||
Reference in New Issue
Block a user