diff --git a/package.json b/package.json index e0cce7b2..233f6518 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.21.0", + "version": "1.21.1", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", diff --git a/src/drive/storage/index.ts b/src/drive/storage/index.ts index 3205bd85..46c50f5f 100644 --- a/src/drive/storage/index.ts +++ b/src/drive/storage/index.ts @@ -864,7 +864,8 @@ export class Storage { * @returns {Promise} A promise that resolves the folder on that path. */ public getFolderByPath(folderPath: string): Promise { - return this.client.get(`folders/meta?path=${folderPath}`, this.headers()); + const query = new URLSearchParams({ path: folderPath }); + return this.client.get(`folders/meta?${query}`, this.headers()); } /** @@ -874,7 +875,8 @@ export class Storage { * @returns {Promise} A promise that resolves the file on that path. */ public getFileByPath(filePath: string): Promise { - return this.client.get(`files/meta?path=${filePath}`, this.headers()); + const query = new URLSearchParams({ path: filePath }); + return this.client.get(`files/meta?${query}`, this.headers()); } /** diff --git a/test/drive/storage/index.test.ts b/test/drive/storage/index.test.ts index ff331e38..da1096d7 100644 --- a/test/drive/storage/index.test.ts +++ b/test/drive/storage/index.test.ts @@ -27,6 +27,15 @@ import { randomMoveFolderPayload } from './mothers/moveFolderPayload.mother'; import { randomUpdateFolderMetadataPayload } from './mothers/updateFolderMetadataPayload.mother'; describe('# storage service tests', () => { + const pathEncodingCases = [ + ['/report 50%.txt', '%2Freport+50%25.txt'], + ['/a&b/c#d.txt', '%2Fa%26b%2Fc%23d.txt'], + ['/tom+jerry/note.txt', '%2Ftom%2Bjerry%2Fnote.txt'], + ['/literal%2Fescape.txt', '%2Fliteral%252Fescape.txt'], + ['/ñ émoji 🎉.txt', '%2F%C3%B1+%C3%A9moji+%F0%9F%8E%89.txt'], + ['/plain/path.txt', '%2Fplain%2Fpath.txt'], + ]; + beforeEach(() => { vi.restoreAllMocks(); }); @@ -481,6 +490,37 @@ describe('# storage service tests', () => { expect(body).toEqual(mockResponse); }); }); + + describe('get folder by path', () => { + it('When a folder path is provided then it should be sent as an encoded query param', async () => { + // Arrange + const response = { uuid: 'folder-uuid' }; + const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue(response); + const { client, headers } = clientAndHeaders({}); + + // Act + const body = await client.getFolderByPath('/folder1/folder2'); + + // Assert + expect(callStub).toHaveBeenCalledWith('folders/meta?path=%2Ffolder1%2Ffolder2', headers); + expect(body).toEqual(response); + }); + + it.each(pathEncodingCases)( + 'When the folder path is %j then it should encode it as %s', + async (folderPath, encodedPath) => { + // Arrange + const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue({}); + const { client, headers } = clientAndHeaders({}); + + // Act + await client.getFolderByPath(folderPath); + + // Assert + expect(callStub).toHaveBeenCalledWith(`folders/meta?path=${encodedPath}`, headers); + }, + ); + }); }); describe('-> files', () => { @@ -724,6 +764,37 @@ describe('# storage service tests', () => { expect(body).toEqual(response); }); }); + + describe('get file by path', () => { + it('When a file path is provided then it should be sent as an encoded query param', async () => { + // Arrange + const response = randomFileMetaData(); + const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue(response); + const { client, headers } = clientAndHeaders({}); + + // Act + const body = await client.getFileByPath('/folder1/file.png'); + + // Assert + expect(callStub).toHaveBeenCalledWith('files/meta?path=%2Ffolder1%2Ffile.png', headers); + expect(body).toEqual(response); + }); + + it.each(pathEncodingCases)( + 'When the file path is %j then it should encode it as %s', + async (filePath, encodedPath) => { + // Arrange + const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue({}); + const { client, headers } = clientAndHeaders({}); + + // Act + await client.getFileByPath(filePath); + + // Assert + expect(callStub).toHaveBeenCalledWith(`files/meta?path=${encodedPath}`, headers); + }, + ); + }); }); describe('-> favorites', () => {