adding custom date support and fixing api

This commit is contained in:
Fabian Stamm 2019-01-19 13:15:48 +01:00
parent 2c4a0203d5
commit 8dbc2bcb7f
4 changed files with 98 additions and 29 deletions

18
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "secure-file-wrapper", "name": "@hibas123/secure-file-wrapper",
"version": "1.1.0", "version": "2.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -37,6 +37,15 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/uuid": {
"version": "3.4.4",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-3.4.4.tgz",
"integrity": "sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"assertion-error": { "assertion-error": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
@ -316,6 +325,11 @@
"integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==", "integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==",
"dev": true "dev": true
}, },
"uuid": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
},
"whatwg-fetch": { "whatwg-fetch": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz", "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz",

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/secure-file-wrapper", "name": "@hibas123/secure-file-wrapper",
"version": "2.1.0", "version": "2.2.0",
"main": "lib/index.js", "main": "lib/index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",
@ -12,7 +12,8 @@
"test": "mocha lib/test.js" "test": "mocha lib/test.js"
}, },
"dependencies": { "dependencies": {
"isomorphic-fetch": "^2.2.1" "isomorphic-fetch": "^2.2.1",
"uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/chai": "^4.1.4", "@types/chai": "^4.1.4",
@ -20,6 +21,7 @@
"@types/mocha": "^5.2.2", "@types/mocha": "^5.2.2",
"@types/node": "^10.12.18", "@types/node": "^10.12.18",
"@types/node-fetch": "^2.1.4", "@types/node-fetch": "^2.1.4",
"@types/uuid": "^3.4.4",
"chai": "^4.1.2", "chai": "^4.1.2",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"typescript": "^3.2.2" "typescript": "^3.2.2"

View File

@ -152,6 +152,19 @@ export default class SecureFileWrapper {
} }
} }
private fixIFileVersion(version: IFileVersion): IFileVersion {
version.time = new Date(version.time)
return version;
}
private fixIFile(file: IFile): IFile {
file.active.time = new Date(file.active.time)
if (file.versions) {
file.versions = file.versions.map(e => this.fixIFileVersion(e))
}
return file;
}
// async test(jwt): Promise<{ user: string, test: true }> { // async test(jwt): Promise<{ user: string, test: true }> {
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled); // let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
// statusParser(res); // statusParser(res);
@ -162,24 +175,27 @@ export default class SecureFileWrapper {
let query: any = {} let query: any = {}
if (folder) query.folder = folder; if (folder) query.folder = folder;
let res = await this.makeRequest("/files", "GET", query); let res = await this.makeRequest("/files", "GET", query);
let d = await res.json(); let d: { files: IFile[] } = await res.json();
return d.files; return d.files.map(e => this.fixIFile(e));
} }
async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string): Promise<IFile> { async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string, date?: Date): Promise<IFile> {
let params: any = { type: type, name: name }; let params: any = { type: type, name: name };
if (preview) { if (preview)
params.preview = preview; params.preview = preview;
}
if (folder) { if (folder)
params.folder = folder; params.folder = folder;
}
if (id) { if (id)
params.id = id params.id = id
}
if (date)
params.date = date.toJSON()
let res = await this.makeRequest("/files", "POST", params, data); let res = await this.makeRequest("/files", "POST", params, data);
return (await res.json()).file; return this.fixIFile((await res.json()).file);
} }
async get(id: string, version?: string): Promise<ArrayBuffer> { async get(id: string, version?: string): Promise<ArrayBuffer> {
@ -199,25 +215,27 @@ export default class SecureFileWrapper {
} }
} }
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string): Promise<IFile> { async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date): Promise<IFile> {
let put: any = {}; let params: any = {};
if (preview) put.preview = preview; if (preview) params.preview = preview;
let res = await this.makeRequest("/files/" + id, "PUT", put, data); if (date)
params.date = date.toJSON()
let res = await this.makeRequest("/files/" + id, "PUT", params, data);
let json = await res.json() let json = await res.json()
return json.file; return this.fixIFile(json.file);
} }
async delete(id: string): Promise<boolean> { async delete(id: string): Promise<void> {
let res = await this.makeRequest("/files/" + id, "DELETE", {}); let res = await this.makeRequest("/files/" + id, "DELETE", {});
return res.json();
} }
async history(id: string): Promise<IHistory> { async history(id: string): Promise<IHistory> {
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}); let res = await this.makeRequest(`/files/${id}/history`, "GET", {});
statusParser(res); let data: IHistory = await res.json();
return res.json(); data.file = this.fixIFile(data.file)
data.history = data.history.map(v => this.fixIFileVersion(v));
return data;
} }
async restore(id: string, version: string) { async restore(id: string, version: string) {

View File

@ -1,5 +1,5 @@
import SecureFile, { NotFound } from "./index"; import SecureFile, { NotFound } from "./index";
import * as v4 from "uuid/v4"
import { TextEncoder, TextDecoder } from "util"; import { TextEncoder, TextDecoder } from "util";
const testname = "ouiavgbsop687463743" const testname = "ouiavgbsop687463743"
@ -33,6 +33,8 @@ function test(sf: SecureFile) {
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata)); expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
}) })
it("get - fail", async () => { it("get - fail", async () => {
const inverr = new Error("Should have failed!"); const inverr = new Error("Should have failed!");
try { try {
@ -88,8 +90,42 @@ function test(sf: SecureFile) {
}) })
it("delete", async () => { it("delete", async () => {
let res = await sf.delete(testid); await sf.delete(testid);
expect(res, "Res not set").to.exist; })
describe("fixed id", () => {
let id = v4();
it("create", async () => {
let res = await sf.create(testname, testdata, "text", undefined, testprev, id)
expect(res, "Res isnnot set").to.exist;
expect(res._id, "Res has no _id").to.exist;
expect(res._id, "Res has invalid _id").to.be.equal(id)
})
it("get", async () => {
let res = await sf.get(id);
expect(res, "No data returned").to.exist;
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
})
})
describe("predefined date", () => {
let id = v4();
it("create", async () => {
const date = new Date("2017-01-01T00:00:00.000Z")
let res = await sf.create(testname, testdata, "text", undefined, testprev, id, date)
expect(res, "Res isnnot set").to.exist;
expect(res._id, "Res has no _id").to.exist;
expect(res._id, "Res has invalid _id").to.be.equal(id)
expect(res.active.time.toJSON()).to.be.equal(date.toJSON())
})
it("list", async () => {
let res = await sf.get(id);
expect(res, "No data returned").to.exist;
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
})
}) })
describe("folder", () => { describe("folder", () => {
@ -115,8 +151,7 @@ function test(sf: SecureFile) {
}) })
it("delete", async () => { it("delete", async () => {
let res = await sf.delete(ftestid); await sf.delete(ftestid);
expect(res, "Res not set").to.exist;
}); });
}) })
} }