Skip to content

Commit b754cd4

Browse files
BKNDLSS-29862 JS API to create a directory (#234)
1 parent ee19d51 commit b754cd4

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

backendless.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ declare module Backendless {
809809

810810
function exists(path: string): Promise<Object>;
811811

812+
function createDirectory(path: string): Promise<void>;
813+
812814
function removeDirectory(path: string): Promise<number>;
813815
}
814816

src/files/index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,6 @@ export default class Files {
279279
})
280280
}
281281

282-
async removeDirectory(directoryPath) {
283-
if (!directoryPath || typeof directoryPath !== 'string') {
284-
throw new Error('Directory "path" must be provided and must be a string.')
285-
}
286-
287-
directoryPath = FilesUtils.trimSlashesInPath(directoryPath)
288-
289-
return this.app.request.delete({
290-
url: this.app.urls.filePath(directoryPath),
291-
})
292-
}
293-
294282
async getFileCount(filesPath, pattern, sub, countDirectories) {
295283
if (!filesPath || typeof filesPath !== 'string') {
296284
throw new Error('"filesPath" must be provided and must be a string.')
@@ -313,5 +301,28 @@ export default class Files {
313301
})
314302
}
315303

304+
async createDirectory(directoryPath) {
305+
if (!directoryPath || typeof directoryPath !== 'string') {
306+
throw new Error('Directory "path" must be provided and must be a string.')
307+
}
308+
309+
directoryPath = FilesUtils.trimSlashesInPath(directoryPath)
310+
311+
return this.app.request.post({
312+
url: this.app.urls.directoryPath(directoryPath)
313+
})
314+
}
315+
316+
async removeDirectory(directoryPath) {
317+
if (!directoryPath || typeof directoryPath !== 'string') {
318+
throw new Error('Directory "path" must be provided and must be a string.')
319+
}
320+
321+
directoryPath = FilesUtils.trimSlashesInPath(directoryPath)
322+
323+
return this.app.request.delete({
324+
url: this.app.urls.filePath(directoryPath),
325+
})
326+
}
316327
}
317328

src/urls.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ export default class Urls {
247247
return `${this.files()}/${path}`
248248
}
249249

250+
directoryPath(path) {
251+
return `${this.files()}/${path}/`
252+
}
253+
250254
fileCopy() {
251255
return `${this.files()}/copy`
252256
}

test/tsd.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ async function testFilesService() {
15731573
let promiseObject: Promise<object>;
15741574
let promiseBoolean: Promise<boolean>;
15751575
let promiseNumber: Promise<number>;
1576+
let promiseVoid: Promise<void>;
15761577

15771578
interface IFileUploadResult {
15781579
fileURL: string
@@ -1643,6 +1644,7 @@ async function testFilesService() {
16431644

16441645
promiseObject = Backendless.Files.exists(path);
16451646

1647+
promiseVoid = Backendless.Files.createDirectory(path);
16461648
promiseNumber = Backendless.Files.removeDirectory(path);
16471649

16481650
promiseBoolean = Backendless.Files.Permissions.READ.grantUser(userid, url);

test/unit/specs/files/basic.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,50 @@ describe('<Files> Basic', function() {
679679

680680
})
681681

682+
describe('Create Directory', () => {
683+
it('creates a directory by a relative path', async () => {
684+
const req1 = prepareMockRequest()
685+
const req2 = prepareMockRequest()
686+
687+
const result1 = await Backendless.Files.createDirectory(dirPath)
688+
const result2 = await Backendless.Files.createDirectory(filePathWithSlash)
689+
690+
expect(req1).to.deep.include({
691+
method : 'POST',
692+
path : `${APP_PATH}/files/test/path/`,
693+
headers: {},
694+
body : undefined
695+
})
696+
697+
expect(req2).to.deep.include({
698+
method : 'POST',
699+
path : `${APP_PATH}/files/test/path/`,
700+
headers: {},
701+
body : undefined
702+
})
703+
704+
expect(result1).to.be.eql(undefined)
705+
expect(result2).to.be.eql(undefined)
706+
})
707+
708+
it('fails when directoryPath is invalid', async () => {
709+
const errorMsg = 'Directory "path" must be provided and must be a string.'
710+
711+
await expect(Backendless.Files.createDirectory()).to.eventually.be.rejectedWith(errorMsg)
712+
await expect(Backendless.Files.createDirectory(undefined)).to.eventually.be.rejectedWith(errorMsg)
713+
await expect(Backendless.Files.createDirectory(null)).to.eventually.be.rejectedWith(errorMsg)
714+
await expect(Backendless.Files.createDirectory(true)).to.eventually.be.rejectedWith(errorMsg)
715+
await expect(Backendless.Files.createDirectory(false)).to.eventually.be.rejectedWith(errorMsg)
716+
await expect(Backendless.Files.createDirectory(0)).to.eventually.be.rejectedWith(errorMsg)
717+
await expect(Backendless.Files.createDirectory(123)).to.eventually.be.rejectedWith(errorMsg)
718+
await expect(Backendless.Files.createDirectory('')).to.eventually.be.rejectedWith(errorMsg)
719+
await expect(Backendless.Files.createDirectory({})).to.eventually.be.rejectedWith(errorMsg)
720+
await expect(Backendless.Files.createDirectory([])).to.eventually.be.rejectedWith(errorMsg)
721+
await expect(Backendless.Files.createDirectory(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
722+
})
723+
724+
})
725+
682726
describe('Remove Directory', () => {
683727
it('removes a directory by a relative path', async () => {
684728
const req1 = prepareMockRequest({ resultFileURL })

0 commit comments

Comments
 (0)