Adding folder support

This commit is contained in:
hibas123 2018-02-05 10:32:48 +01:00
parent ed0a4d7aa2
commit dd23d930dd
6 changed files with 84 additions and 16 deletions

4
index.d.ts vendored
View File

@ -18,8 +18,8 @@ export default class SecureFile {
constructor(server: string, username: string, private_key: string); constructor(server: string, username: string, private_key: string);
private getCode(); private getCode();
private makeRequest(endpoint, method, query, body); private makeRequest(endpoint, method, query, body);
list(): Promise<File[]>; list(folder?: string): Promise<File[]>;
create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File>; create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File>;
get(id: string, version?: string): Promise<Buffer>; get(id: string, version?: string): Promise<Buffer>;
update(id: string, data: Buffer, preview?: Buffer): Promise<File>; update(id: string, data: Buffer, preview?: Buffer): Promise<File>;
delete(id: string): Promise<boolean>; delete(id: string): Promise<boolean>;

View File

@ -29,13 +29,15 @@ class SecureFile {
} }
return await node_fetch_1.default(this.Server + endpoint + query_str, { method: method, body: body }); return await node_fetch_1.default(this.Server + endpoint + query_str, { method: method, body: body });
} }
async list() { async list(folder) {
let res = await this.makeRequest("/files", "GET", {}, undefined); if (!folder)
folder = "root";
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined);
statusParser(res); statusParser(res);
return await res.json(); return await res.json();
} }
async create(name, data, type, preview) { async create(name, data, type, folder, preview) {
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data); let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data);
statusParser(res); statusParser(res);
return res.json(); return res.json();
} }

View File

@ -47,14 +47,15 @@ export default class SecureFile {
return await fetch(this.Server + endpoint + query_str, { method: method, body: body }); return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
} }
async list(): Promise<File[]> { async list(folder?: string): Promise<File[]> {
let res = await this.makeRequest("/files", "GET", {}, undefined) if (!folder) folder = "root";
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined)
statusParser(res); statusParser(res);
return await res.json(); return await res.json();
} }
async create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File> { async create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File> {
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data) let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data)
statusParser(res); statusParser(res);
return res.json(); return res.json();
} }

1
test.d.ts vendored
View File

@ -0,0 +1 @@
export {};

38
test.js
View File

@ -11,15 +11,17 @@ const testprev = new buffer_1.Buffer("Ich bin...");
let testid; let testid;
let testver; let testver;
let testver2; let testver2;
const testfolder = "iabos";
let ftestid;
module.exports = { module.exports = {
setUp: function (finished) { setUp: function (finished) {
let pk = fs_1.readFileSync("./private.pem"); let pk = fs_1.readFileSync("./private.pem");
sf = new index_1.default("http://localhost:3005", "test", pk.toString("utf8")); sf = new index_1.default("http://localhost:3005/api", "test", pk.toString("utf8"));
finished(); finished();
}, },
create: async function (test) { create: async function (test) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", testprev); let res = await sf.create(testname, testdata, "text", undefined, testprev);
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
testid = res._id; testid = res._id;
@ -36,8 +38,8 @@ module.exports = {
list: async function (test) { list: async function (test) {
test.expect(4); test.expect(4);
let res = await sf.list(); let res = await sf.list();
console.log(res);
test.ok(res); test.ok(res);
console.log(res);
test.ok(Array.isArray(res), "Is from type Array"); test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?"); test.ok(res.length > 0, "Do elements exist?");
res.forEach(e => { res.forEach(e => {
@ -77,5 +79,35 @@ module.exports = {
let res = await sf.delete(testid); let res = await sf.delete(testid);
test.ok(res); test.ok(res);
test.done(); test.done();
},
folder: {
create: async function (test) {
test.expect(2);
let res = await sf.create(testname, testdata, "text", testfolder, testprev);
test.ok(res);
test.ok(res._id);
ftestid = res._id;
testver = res.version;
test.done();
},
list: async function (test) {
test.expect(4);
let res = await sf.list(testfolder);
test.ok(res);
test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?");
res.forEach(e => {
if (e._id === ftestid) {
test.ok(true, "Element is in List");
}
});
test.done();
},
delete: async function (test) {
test.expect(1);
let res = await sf.delete(ftestid);
test.ok(res);
test.done();
},
} }
}; };

38
test.ts
View File

@ -13,6 +13,8 @@ let testid: string;
let testver; let testver;
let testver2; let testver2;
const testfolder = "iabos";
let ftestid;
interface TestType { interface TestType {
expect(cnt: number): void; expect(cnt: number): void;
ok(value, message?: string): void; ok(value, message?: string): void;
@ -31,12 +33,12 @@ interface TestType {
module.exports = { module.exports = {
setUp: function (finished) { setUp: function (finished) {
let pk = readFileSync("./private.pem"); let pk = readFileSync("./private.pem");
sf = new SecureFile("http://localhost:3005", "test", pk.toString("utf8")); sf = new SecureFile("http://localhost:3005/api", "test", pk.toString("utf8"));
finished(); finished();
}, },
create: async function (test: TestType) { create: async function (test: TestType) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", testprev) let res = await sf.create(testname, testdata, "text", undefined, testprev)
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
testid = res._id; testid = res._id;
@ -53,8 +55,8 @@ module.exports = {
list: async function (test: TestType) { list: async function (test: TestType) {
test.expect(4); test.expect(4);
let res = await sf.list(); let res = await sf.list();
console.log(res);
test.ok(res); test.ok(res);
console.log(res);
test.ok(Array.isArray(res), "Is from type Array"); test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?") test.ok(res.length > 0, "Do elements exist?")
res.forEach(e => { res.forEach(e => {
@ -96,5 +98,35 @@ module.exports = {
let res = await sf.delete(testid); let res = await sf.delete(testid);
test.ok(res); test.ok(res);
test.done(); test.done();
},
folder: {
create: async function (test: TestType) {
test.expect(2);
let res = await sf.create(testname, testdata, "text", testfolder, testprev)
test.ok(res);
test.ok(res._id);
ftestid = res._id;
testver = res.version;
test.done();
},
list: async function (test: TestType) {
test.expect(4);
let res = await sf.list(testfolder);
test.ok(res);
test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?")
res.forEach(e => {
if (e._id === ftestid) {
test.ok(true, "Element is in List")
}
})
test.done()
},
delete: async function (test: TestType) {
test.expect(1);
let res = await sf.delete(ftestid);
test.ok(res);
test.done();
},
} }
} }