Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug report: 'file list' command throws an exception when 'folderUrl' ends with a '/' #4658

Closed
martinlingstuyl opened this issue Mar 16, 2023 · 3 comments
Assignees
Labels
Milestone

Comments

@martinlingstuyl
Copy link
Contributor

martinlingstuyl commented Mar 16, 2023

Description

The following script throws an error:

m365 file list --folderUrl "/sites/somesite/Shared Documents/" --webUrl "https://contoso.sharepoint.com/sites/somesite" --verbose

While excluding the trailing slash does work:

m365 file list --folderUrl "/sites/somesite/Shared Documents" --webUrl "https://contoso.sharepoint.com/sites/somesite" --verbose

CLI for Microsoft 365 version

6.4 (next)

nodejs version

16.15.0

Operating system (environment)

Windows

Shell

PowerShell

Additional Info

The error is caused by the url manipulation here. documentLibraryRelativeFolderUrl probably contains /. Which causes the URL to include root: instead of the expected root.

const documentLibraryRelativeFolderUrl: string = folderUrl.href.replace(new RegExp(documentLibrary.webUrl as string, 'i'), '');
const requestOptions: CliRequestOptions = {
  url: `${this.resource}/v1.0/drives/${documentLibrary.id}/root${documentLibraryRelativeFolderUrl.length > 0 ? `:${documentLibraryRelativeFolderUrl}` : ''}?$select=id`,
  headers: {
    accept: 'application/json;odata.metadata=none'
  },
  responseType: 'json'
};
@nicodecleyre
Copy link
Contributor

nicodecleyre commented Mar 16, 2023

Great catch! If we update this line of code the last '/' will be removed if it exist and the bug is gone

const documentLibraryRelativeFolderUrl: string = folderUrl.href.replace(new RegExp(documentLibrary.webUrl as string, 'i'), '').replace(new RegExp(/\/$/),'');

@martinlingstuyl martinlingstuyl self-assigned this Mar 16, 2023
@martinlingstuyl
Copy link
Contributor Author

We should have some kind of URL normalize Utility function for this...

@MathijsVerbeeck
Copy link
Contributor

Doesn't regex seems a bit like an overkill (mostly for readability)? Couldn't you add an if-statement using endsWith('/') and if true then do string.slice(0, -1)? Seems more readable for newer developers and performance-wise will execute faster than a regex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants