forked from hibas123/SecureFileWrapper
Adding folder support
This commit is contained in:
parent
ed0a4d7aa2
commit
dd23d930dd
4
index.d.ts
vendored
4
index.d.ts
vendored
@ -18,8 +18,8 @@ export default class SecureFile {
|
||||
constructor(server: string, username: string, private_key: string);
|
||||
private getCode();
|
||||
private makeRequest(endpoint, method, query, body);
|
||||
list(): Promise<File[]>;
|
||||
create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File>;
|
||||
list(folder?: string): Promise<File[]>;
|
||||
create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File>;
|
||||
get(id: string, version?: string): Promise<Buffer>;
|
||||
update(id: string, data: Buffer, preview?: Buffer): Promise<File>;
|
||||
delete(id: string): Promise<boolean>;
|
||||
|
10
index.js
10
index.js
@ -29,13 +29,15 @@ class SecureFile {
|
||||
}
|
||||
return await node_fetch_1.default(this.Server + endpoint + query_str, { method: method, body: body });
|
||||
}
|
||||
async list() {
|
||||
let res = await this.makeRequest("/files", "GET", {}, undefined);
|
||||
async list(folder) {
|
||||
if (!folder)
|
||||
folder = "root";
|
||||
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined);
|
||||
statusParser(res);
|
||||
return await res.json();
|
||||
}
|
||||
async create(name, data, type, preview) {
|
||||
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data);
|
||||
async create(name, data, type, folder, preview) {
|
||||
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data);
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
|
9
index.ts
9
index.ts
@ -47,14 +47,15 @@ export default class SecureFile {
|
||||
return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
|
||||
}
|
||||
|
||||
async list(): Promise<File[]> {
|
||||
let res = await this.makeRequest("/files", "GET", {}, undefined)
|
||||
async list(folder?: string): Promise<File[]> {
|
||||
if (!folder) folder = "root";
|
||||
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined)
|
||||
statusParser(res);
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
async create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File> {
|
||||
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data)
|
||||
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, folder: folder }, data)
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
|
38
test.js
38
test.js
@ -11,15 +11,17 @@ const testprev = new buffer_1.Buffer("Ich bin...");
|
||||
let testid;
|
||||
let testver;
|
||||
let testver2;
|
||||
const testfolder = "iabos";
|
||||
let ftestid;
|
||||
module.exports = {
|
||||
setUp: function (finished) {
|
||||
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();
|
||||
},
|
||||
create: async function (test) {
|
||||
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._id);
|
||||
testid = res._id;
|
||||
@ -36,8 +38,8 @@ module.exports = {
|
||||
list: async function (test) {
|
||||
test.expect(4);
|
||||
let res = await sf.list();
|
||||
console.log(res);
|
||||
test.ok(res);
|
||||
console.log(res);
|
||||
test.ok(Array.isArray(res), "Is from type Array");
|
||||
test.ok(res.length > 0, "Do elements exist?");
|
||||
res.forEach(e => {
|
||||
@ -77,5 +79,35 @@ module.exports = {
|
||||
let res = await sf.delete(testid);
|
||||
test.ok(res);
|
||||
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
38
test.ts
@ -13,6 +13,8 @@ let testid: string;
|
||||
let testver;
|
||||
let testver2;
|
||||
|
||||
const testfolder = "iabos";
|
||||
let ftestid;
|
||||
interface TestType {
|
||||
expect(cnt: number): void;
|
||||
ok(value, message?: string): void;
|
||||
@ -31,12 +33,12 @@ interface TestType {
|
||||
module.exports = {
|
||||
setUp: function (finished) {
|
||||
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();
|
||||
},
|
||||
create: async function (test: TestType) {
|
||||
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._id);
|
||||
testid = res._id;
|
||||
@ -53,8 +55,8 @@ module.exports = {
|
||||
list: async function (test: TestType) {
|
||||
test.expect(4);
|
||||
let res = await sf.list();
|
||||
console.log(res);
|
||||
test.ok(res);
|
||||
console.log(res);
|
||||
test.ok(Array.isArray(res), "Is from type Array");
|
||||
test.ok(res.length > 0, "Do elements exist?")
|
||||
res.forEach(e => {
|
||||
@ -96,5 +98,35 @@ module.exports = {
|
||||
let res = await sf.delete(testid);
|
||||
test.ok(res);
|
||||
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();
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user