Skip to content

Commit 0f4d7b4

Browse files
committed
docs: add JSDoc to withTokenRefresh, all StackClient method implementations, and estimateSourceSize
1 parent 8c0dba6 commit 0f4d7b4

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/stack-client.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export function createStackClient(
5050
const settingsCollection = cozy.collection('io.cozy.settings')
5151
const fileCollection = cozy.collection('io.cozy.files')
5252

53+
/**
54+
* Wraps a Stack operation with 401 token refresh. On 401, fetches a new
55+
* token from the Cloudery, updates the CozyStackClient, and retries once.
56+
* @param operation - Async function to execute and potentially retry
57+
* @returns The result of the operation
58+
* @throws The original error if status is not 401, or the retry error
59+
*/
5360
async function withTokenRefresh<T>(operation: () => Promise<T>): Promise<T> {
5461
try {
5562
return await operation()
@@ -66,6 +73,11 @@ export function createStackClient(
6673
}
6774

6875
return {
76+
/**
77+
* @param accountId - Nextcloud account ID (io.cozy.accounts)
78+
* @param path - Nextcloud directory path to list
79+
* @returns Array of file and directory entries
80+
*/
6981
async listNextcloudDir(accountId: string, path: string): Promise<NextcloudEntry[]> {
7082
const { data } = await withTokenRefresh(() =>
7183
ncCollection.find({
@@ -83,6 +95,14 @@ export function createStackClient(
8395
}))
8496
},
8597

98+
/**
99+
* Copies a file from Nextcloud into a Cozy directory via the Stack's
100+
* downstream route. Fails with 409 if the file already exists.
101+
* @param accountId - Nextcloud account ID (io.cozy.accounts)
102+
* @param ncPath - Source file path on Nextcloud
103+
* @param cozyDirId - Target Cozy directory ID
104+
* @returns The created file's metadata
105+
*/
86106
async transferFile(accountId: string, ncPath: string, cozyDirId: string): Promise<CozyFile> {
87107
const resp = await withTokenRefresh(() =>
88108
ncCollection.moveToCozy(
@@ -102,6 +122,13 @@ export function createStackClient(
102122
}
103123
},
104124

125+
/**
126+
* Creates a directory in Cozy VFS. If the directory already exists (409),
127+
* extracts and returns the existing directory's ID.
128+
* @param parentDirId - Parent directory ID in Cozy VFS
129+
* @param name - Name of the directory to create
130+
* @returns The created or existing directory's ID
131+
*/
105132
async createDir(parentDirId: string, name: string): Promise<string> {
106133
try {
107134
const { data } = await withTokenRefresh(() =>
@@ -111,7 +138,6 @@ export function createStackClient(
111138
} catch (error: unknown) {
112139
const status = (error as { status?: number }).status
113140
if (status === 409) {
114-
// Directory already exists — extract ID from the conflict response
115141
const response = (error as { response?: Response }).response
116142
if (response) {
117143
const body = await response.json() as { errors?: Array<{ source?: { id?: string } }> }
@@ -124,6 +150,9 @@ export function createStackClient(
124150
}
125151
},
126152

153+
/**
154+
* @returns Disk usage (used bytes) and quota for the instance. Quota 0 means unlimited.
155+
*/
127156
async getDiskUsage(): Promise<DiskUsage> {
128157
const { data } = await withTokenRefresh(() =>
129158
settingsCollection.get('io.cozy.settings.disk-usage')
@@ -135,13 +164,21 @@ export function createStackClient(
135164
}
136165
},
137166

167+
/**
168+
* @param id - Tracking document ID (io.cozy.nextcloud.migrations)
169+
* @returns The full tracking document from CouchDB
170+
*/
138171
async getTrackingDoc(id: string): Promise<TrackingDoc> {
139172
const { data } = await withTokenRefresh(() =>
140173
docCollection.get(id)
141174
)
142175
return data as unknown as TrackingDoc
143176
},
144177

178+
/**
179+
* @param doc - Tracking document with _id and _rev
180+
* @returns The document with updated _rev from CouchDB
181+
*/
145182
async updateTrackingDoc(doc: TrackingDoc): Promise<TrackingDoc> {
146183
const { data } = await withTokenRefresh(() =>
147184
docCollection.update(doc as unknown as Record<string, unknown>)

0 commit comments

Comments
 (0)