@@ -9,7 +9,6 @@ import type { Node, NodeData } from '../node/index.ts'
99import { getCurrentUser , getRequestToken , onRequestTokenUpdate } from '@nextcloud/auth'
1010import { getSharingToken , isPublicShare } from '@nextcloud/sharing/public'
1111import { generateRemoteUrl } from '@nextcloud/router'
12- import { CancelablePromise } from 'cancelable-promise'
1312import { createClient , getPatcher } from 'webdav'
1413import { parsePermissions } from './davPermissions.ts'
1514import { getFavoritesReport } from './davProperties.ts'
@@ -112,44 +111,52 @@ export const getClient = function(remoteURL = defaultRemoteURL, headers: Record<
112111/**
113112 * Use WebDAV to query for favorite Nodes
114113 *
115- * @param davClient The WebDAV client to use for performing the request
116- * @param path Base path for the favorites, if unset all favorites are queried
117- * @param davRoot The root path for the DAV user (defaults to `defaultRootPath`)
114+ * @param options - Options for the favorite query
115+ * @param options.client - The WebDAV client to use for performing the request
116+ * @param options.path - Base path for the favorites, if unset all favorites are queried
117+ * @param options.davRoot - The root path for the DAV user (defaults to `defaultRootPath`)
118+ * @param options.signal - Optional abort signal to cancel the request
119+ *
120+ * @example
121+ * ```js
122+ * import { getFavoriteNodes } from '@nextcloud/files'
123+ *
124+ * // query favorites for the root
125+ * const favorites = await getFavoriteNodes()
126+ * ```
118127 * @example
119128 * ```js
129+ * // Advanced usage with custom client and path
120130 * import { getClient, defaultRootPath, getFavoriteNodes } from '@nextcloud/files'
121131 *
122132 * const client = getClient()
123- * // query favorites for the root
124- * const favorites = await getFavoriteNodes(client)
125- * // which is the same as writing:
126- * const favorites = await getFavoriteNodes(client, '/', defaultRootPath)
133+ * const controller = new AbortController()
134+ * const favoritesPromise = getFavoriteNodes({ client, path: '/some/folder', signal: controller.signal })
135+ * // you can abort the request if needed
136+ * controller.abort()
137+ * // or await the result
138+ * const favorites = await favoritesPromise
127139 * ```
128140 */
129- export const getFavoriteNodes = ( davClient : WebDAVClient , path = '/' , davRoot = defaultRootPath ) : CancelablePromise < Node [ ] > => {
130- const controller = new AbortController ( )
131- return new CancelablePromise ( async ( resolve , reject , onCancel ) => {
132- onCancel ( ( ) => controller . abort ( ) )
133- try {
134- const contentsResponse = await davClient . getDirectoryContents ( `${ davRoot } ${ path } ` , {
135- signal : controller . signal ,
136- details : true ,
137- data : getFavoritesReport ( ) ,
138- headers : {
139- // see getClient for patched webdav client
140- method : 'REPORT' ,
141- } ,
142- includeSelf : true ,
143- } ) as ResponseDataDetailed < FileStat [ ] >
144-
145- const nodes = contentsResponse . data
146- . filter ( node => node . filename !== path ) // exclude current dir
147- . map ( ( result ) => resultToNode ( result , davRoot ) )
148- resolve ( nodes )
149- } catch ( error ) {
150- reject ( error )
151- }
152- } )
141+ export async function getFavoriteNodes ( options : { client ?: WebDAVClient , path ?: string , davRoot ?: string , signal ?: AbortSignal } = { } ) : Promise < Node [ ] > {
142+ const client = options . client ?? getClient ( )
143+ const path = options . path ?? '/'
144+ const davRoot = options . davRoot ?? defaultRootPath
145+
146+ const contentsResponse = await client . getDirectoryContents ( `${ davRoot } ${ path } ` , {
147+ signal : options . signal ,
148+ details : true ,
149+ data : getFavoritesReport ( ) ,
150+ headers : {
151+ // see getClient for patched webdav client
152+ method : 'REPORT' ,
153+ } ,
154+ includeSelf : true ,
155+ } ) as ResponseDataDetailed < FileStat [ ] >
156+
157+ return contentsResponse . data
158+ . filter ( node => node . filename !== path ) // exclude current dir
159+ . map ( ( result ) => resultToNode ( result , davRoot ) )
153160}
154161
155162/**
0 commit comments