import { AuthClientBase } from "./AuthClientBase" ; import { File } from "./File" ; import { FIELDS , GOOGLE_DRIVE_API } from "./TsGoogleDrive" ; import { ITsGoogleDriveOptions } from "./types" ; const oAuth2ClientSymbol = Symbol ( "oAuth2Client" ) ; type IOperator = "=" | ">" | ">=" | "<" | "<=" ; type orderByKey = "createdTime" | "folder" | "modifiedByMeTime" | "modifiedTime" | "name" | "name_natural" | "quotaBytesUsed" | "recency" | "sharedWithMeTime" | "starred" | "viewedByMeTime" ; export class Query extends AuthClientBase { public queries : string [ ] = [ ] ; public pageSize : number = 100 ; public orderBy : string [ ] = [ ] ; private nextPageToken ? : string ; constructor ( options ) { super ( options ) ; } public hasNextPage ( ) { return this . nextPageToken === undefined || ! ! this . nextPageToken ; } public setPageSize ( value ) { this . pageSize = value ; return this ; } public setOrderBy ( value ) { if ( Array . isArray ( value ) ) { this . orderBy = this . orderBy . concat ( value ) ; } else { this . orderBy . push ( value ) ; } return this ; } public setFolderOnly ( ) { this . queries . push ( "mimeType='application/vnd.google-apps.folder'" ) ; return this ; } public setFileOnly ( ) { this . queries . push ( "mimeType!='application/vnd.google-apps.folder'" ) ; return this ; } public setFullTextContains ( name ) { this . queries . push ( `fullText contains '${ name }'` ) ; return this ; } public setNameContains ( name ) { this . queries . push ( `name contains '${ name }'` ) ; return this ; } public setNameEqual ( name ) { this . queries . push ( `name = '${ name }'` ) ; return this ; } public setModifiedTime ( operator , date ) { this . queries . push ( `modifiedTime ${ operator } '${ date . toISOString ( ) }'` ) ; return this ; } public setCreatedTime ( operator , date ) { this . queries . push ( `createdTime ${ operator } '${ date . toISOString ( ) }'` ) ; return this ; } public setQuery ( query ) { this . queries . push ( query ) ; return this ; } public inFolder ( folderId ) { this . queries . push ( `'${ folderId }' in parents` ) ; return this ; } public inTrash ( ) { this . queries . push ( `trashed = true` ) ; return this ; } public async runOnce ( ) { this . setPageSize ( 1 ) ; const list = await this . run ( ) ; return list . length ? list [ 0 ] : undefined ; } public async run ( ) { if ( this . nextPageToken === "" ) { throw new Error ( "The query has no more next page." ) ; } const client = await this . _getClient ( ) ; const url = `/files` ; const params = { q : this . queries . join ( " and " ) , spaces : "drive" , pageSize : this . pageSize , pageToken : this . nextPageToken , fields : `kind,nextPageToken,incompleteSearch,files(${ FIELDS })` , orderBy : this . orderBy . join ( "," ) , } ; const res = await client . request ( { baseURL : GOOGLE_DRIVE_API , url , params } ) ; const result = res . data as any ; this . nextPageToken = result . nextPageToken || "" ; const list = [ ] ; if ( result . files && Array . isArray ( result . files ) ) { for ( const item of result . files ) { const file = new File ( client ) ; Object . assign ( file , item ) ; list . push ( file ) ; } } return list ; } }