forked from hibas123/SecureFileWrapper
Adding metadata support
This commit is contained in:
parent
0db2a025e0
commit
9f8a60dba5
5
index.d.ts
vendored
5
index.d.ts
vendored
@ -7,6 +7,7 @@ export interface File {
|
|||||||
time: string;
|
time: string;
|
||||||
preview: string;
|
preview: string;
|
||||||
version: string;
|
version: string;
|
||||||
|
meta: string;
|
||||||
}
|
}
|
||||||
export interface History {
|
export interface History {
|
||||||
file: File;
|
file: File;
|
||||||
@ -27,9 +28,9 @@ export default class SecureFile {
|
|||||||
test: true;
|
test: true;
|
||||||
}>;
|
}>;
|
||||||
list(folder?: string): Promise<File[]>;
|
list(folder?: string): Promise<File[]>;
|
||||||
create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File>;
|
create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer, meta?: string): 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, meta?: string): Promise<File>;
|
||||||
delete(id: string): Promise<boolean>;
|
delete(id: string): Promise<boolean>;
|
||||||
history(id: string): Promise<History>;
|
history(id: string): Promise<History>;
|
||||||
}
|
}
|
||||||
|
11
index.js
11
index.js
@ -157,7 +157,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })];
|
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })];
|
||||||
case 7:
|
case 7:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
if (res.status === 418) { // JWT invalid
|
if (res.status === 418) {
|
||||||
this.JWT = undefined;
|
this.JWT = undefined;
|
||||||
return [2 /*return*/, this.makeRequest(endpoint, method, query, body, jwt)];
|
return [2 /*return*/, this.makeRequest(endpoint, method, query, body, jwt)];
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
SecureFile.prototype.create = function (name, data, type, folder, preview) {
|
SecureFile.prototype.create = function (name, data, type, folder, preview, meta) {
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var params, res;
|
var params, res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
@ -219,6 +219,9 @@ var SecureFile = /** @class */ (function () {
|
|||||||
if (folder) {
|
if (folder) {
|
||||||
params.folder = folder;
|
params.folder = folder;
|
||||||
}
|
}
|
||||||
|
if (meta) {
|
||||||
|
params.meta = meta;
|
||||||
|
}
|
||||||
return [4 /*yield*/, this.makeRequest("/files", "POST", params, data, this.jwt_enabled)];
|
return [4 /*yield*/, this.makeRequest("/files", "POST", params, data, this.jwt_enabled)];
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
@ -274,7 +277,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
SecureFile.prototype.update = function (id, data, preview) {
|
SecureFile.prototype.update = function (id, data, preview, meta) {
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var put, res;
|
var put, res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
@ -283,6 +286,8 @@ var SecureFile = /** @class */ (function () {
|
|||||||
put = {};
|
put = {};
|
||||||
if (preview)
|
if (preview)
|
||||||
put.preview = preview;
|
put.preview = preview;
|
||||||
|
if (meta)
|
||||||
|
put.meta = meta;
|
||||||
return [4 /*yield*/, this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled)];
|
return [4 /*yield*/, this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled)];
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
|
10
index.ts
10
index.ts
@ -10,6 +10,7 @@ export interface File {
|
|||||||
time: string;
|
time: string;
|
||||||
preview: string;
|
preview: string;
|
||||||
version: string;
|
version: string;
|
||||||
|
meta: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface History {
|
export interface History {
|
||||||
@ -113,7 +114,7 @@ export default class SecureFile {
|
|||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File> {
|
async create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer, meta?: string): Promise<File> {
|
||||||
let params: any = { type: type, name: name };
|
let params: any = { type: type, name: name };
|
||||||
if (preview) {
|
if (preview) {
|
||||||
params.preview = preview;
|
params.preview = preview;
|
||||||
@ -121,6 +122,10 @@ export default class SecureFile {
|
|||||||
if (folder) {
|
if (folder) {
|
||||||
params.folder = folder;
|
params.folder = folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (meta) {
|
||||||
|
params.meta = meta;
|
||||||
|
}
|
||||||
let res = await this.makeRequest("/files", "POST", params, data, this.jwt_enabled)
|
let res = await this.makeRequest("/files", "POST", params, data, this.jwt_enabled)
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
return res.json();
|
return res.json();
|
||||||
@ -146,9 +151,10 @@ export default class SecureFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {
|
async update(id: string, data: Buffer, preview?: Buffer, meta?: string): Promise<File> {
|
||||||
let put: any = {};
|
let put: any = {};
|
||||||
if (preview) put.preview = preview;
|
if (preview) put.preview = preview;
|
||||||
|
if (meta) put.meta = meta;
|
||||||
let res = await this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled);
|
let res = await this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled);
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
return res.json();
|
return res.json();
|
||||||
|
12
test.js
12
test.js
@ -42,6 +42,7 @@ var testname = "ouiavgbsop687463743";
|
|||||||
var testdata = new buffer_1.Buffer("Ich bin ein Test");
|
var testdata = new buffer_1.Buffer("Ich bin ein Test");
|
||||||
var newTestData = new buffer_1.Buffer("neue test daten");
|
var newTestData = new buffer_1.Buffer("neue test daten");
|
||||||
var testprev = new buffer_1.Buffer("Ich bin...");
|
var testprev = new buffer_1.Buffer("Ich bin...");
|
||||||
|
var testmeta = "testaa";
|
||||||
var testfolder = "iabos";
|
var testfolder = "iabos";
|
||||||
var ftestid;
|
var ftestid;
|
||||||
var e_testid;
|
var e_testid;
|
||||||
@ -56,7 +57,7 @@ function test(sf) {
|
|||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, testprev)];
|
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, testprev, testmeta)];
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
chai_1.assert.ok(res, "Res is not set");
|
chai_1.assert.ok(res, "Res is not set");
|
||||||
@ -94,7 +95,7 @@ function test(sf) {
|
|||||||
});
|
});
|
||||||
}); });
|
}); });
|
||||||
it("list", function () { return __awaiter(_this, void 0, void 0, function () {
|
it("list", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
var res;
|
var res, found;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0: return [4 /*yield*/, sf.list()];
|
case 0: return [4 /*yield*/, sf.list()];
|
||||||
@ -103,11 +104,14 @@ function test(sf) {
|
|||||||
chai_1.assert.ok(res);
|
chai_1.assert.ok(res);
|
||||||
chai_1.assert.ok(Array.isArray(res), "Is not from type Array");
|
chai_1.assert.ok(Array.isArray(res), "Is not from type Array");
|
||||||
chai_1.assert.ok(res.length > 0, "No elements returned");
|
chai_1.assert.ok(res.length > 0, "No elements returned");
|
||||||
|
found = false;
|
||||||
res.forEach(function (e) {
|
res.forEach(function (e) {
|
||||||
if (e._id === testid) {
|
if (e._id === testid) {
|
||||||
chai_1.assert.ok(true, "Element not in List");
|
found = true;
|
||||||
|
chai_1.assert.equal(e.meta, testmeta, "Meta data dows not fit the expected value!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
chai_1.assert.ok(found, "Element not in List");
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -116,7 +120,7 @@ function test(sf) {
|
|||||||
var res, res2;
|
var res, res2;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0: return [4 /*yield*/, sf.update(testid, newTestData)];
|
case 0: return [4 /*yield*/, sf.update(testid, newTestData, undefined, testmeta + "2")];
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
chai_1.assert.ok(res, "No data returned");
|
chai_1.assert.ok(res, "No data returned");
|
||||||
|
10
test.ts
10
test.ts
@ -7,6 +7,7 @@ const testname = "ouiavgbsop687463743"
|
|||||||
const testdata = new Buffer("Ich bin ein Test");
|
const testdata = new Buffer("Ich bin ein Test");
|
||||||
const newTestData = new Buffer("neue test daten");
|
const newTestData = new Buffer("neue test daten");
|
||||||
const testprev = new Buffer("Ich bin...");
|
const testprev = new Buffer("Ich bin...");
|
||||||
|
const testmeta = "testaa";
|
||||||
|
|
||||||
const testfolder = "iabos";
|
const testfolder = "iabos";
|
||||||
let ftestid;
|
let ftestid;
|
||||||
@ -22,7 +23,7 @@ function test(sf: SecureFile) {
|
|||||||
let testver2: string;
|
let testver2: string;
|
||||||
|
|
||||||
it("create", async () => {
|
it("create", async () => {
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, testprev)
|
let res = await sf.create(testname, testdata, "text", undefined, testprev, testmeta)
|
||||||
assert.ok(res, "Res is not set");
|
assert.ok(res, "Res is not set");
|
||||||
assert.ok(res._id, "Res has not _id");
|
assert.ok(res._id, "Res has not _id");
|
||||||
testid = res._id;
|
testid = res._id;
|
||||||
@ -46,15 +47,18 @@ function test(sf: SecureFile) {
|
|||||||
assert.ok(res);
|
assert.ok(res);
|
||||||
assert.ok(Array.isArray(res), "Is not from type Array");
|
assert.ok(Array.isArray(res), "Is not from type Array");
|
||||||
assert.ok(res.length > 0, "No elements returned")
|
assert.ok(res.length > 0, "No elements returned")
|
||||||
|
let found = false;
|
||||||
res.forEach(e => {
|
res.forEach(e => {
|
||||||
if (e._id === testid) {
|
if (e._id === testid) {
|
||||||
assert.ok(true, "Element not in List")
|
found = true;
|
||||||
|
assert.equal(e.meta, testmeta, "Meta data dows not fit the expected value!")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
assert.ok(found, "Element not in List")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("update", async () => {
|
it("update", async () => {
|
||||||
let res = await sf.update(testid, newTestData);
|
let res = await sf.update(testid, newTestData, undefined, testmeta + "2");
|
||||||
assert.ok(res, "No data returned");
|
assert.ok(res, "No data returned");
|
||||||
assert.ok(res._id, "_id missing");
|
assert.ok(res._id, "_id missing");
|
||||||
assert.notEqual(res.version, testver, "No new version was created")
|
assert.notEqual(res.version, testver, "No new version was created")
|
||||||
|
Loading…
Reference in New Issue
Block a user