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",
"version": "1.1.0",
"name": "@hibas123/secure-file-wrapper",
"version": "2.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -37,6 +37,15 @@
"@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": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
@ -316,6 +325,11 @@
"integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==",
"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": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/secure-file-wrapper",
"version": "2.1.0",
"version": "2.2.0",
"main": "lib/index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT",
@ -12,7 +12,8 @@
"test": "mocha lib/test.js"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
"isomorphic-fetch": "^2.2.1",
"uuid": "^3.3.2"
},
"devDependencies": {
"@types/chai": "^4.1.4",
@ -20,6 +21,7 @@
"@types/mocha": "^5.2.2",
"@types/node": "^10.12.18",
"@types/node-fetch": "^2.1.4",
"@types/uuid": "^3.4.4",
"chai": "^4.1.2",
"mocha": "^5.2.0",
"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 }> {
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
// statusParser(res);
@ -162,24 +175,27 @@ export default class SecureFileWrapper {
let query: any = {}
if (folder) query.folder = folder;
let res = await this.makeRequest("/files", "GET", query);
let d = await res.json();
return d.files;
let d: { files: IFile[] } = await res.json();
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 };
if (preview) {
if (preview)
params.preview = preview;
}
if (folder) {
if (folder)
params.folder = folder;
}
if (id) {
if (id)
params.id = id
}
if (date)
params.date = date.toJSON()
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> {
@ -199,25 +215,27 @@ export default class SecureFileWrapper {
}
}
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string): Promise<IFile> {
let put: any = {};
if (preview) put.preview = preview;
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date): Promise<IFile> {
let params: any = {};
if (preview) params.preview = preview;
if (date)
params.date = date.toJSON()
let res = await this.makeRequest("/files/" + id, "PUT", params, data);
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", {});
return res.json();
}
async history(id: string): Promise<IHistory> {
let res = await this.makeRequest(`/files/${id}/history`, "GET", {});
statusParser(res);
return res.json();
let data: IHistory = await 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) {

View File

@ -1,5 +1,5 @@
import SecureFile, { NotFound } from "./index";
import * as v4 from "uuid/v4"
import { TextEncoder, TextDecoder } from "util";
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));
})
it("get - fail", async () => {
const inverr = new Error("Should have failed!");
try {
@ -88,8 +90,42 @@ function test(sf: SecureFile) {
})
it("delete", async () => {
let res = await sf.delete(testid);
expect(res, "Res not set").to.exist;
await sf.delete(testid);
})
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", () => {
@ -115,8 +151,7 @@ function test(sf: SecureFile) {
})
it("delete", async () => {
let res = await sf.delete(ftestid);
expect(res, "Res not set").to.exist;
await sf.delete(ftestid);
});
})
}