From 8dbc2bcb7fd66c8afde2a325f7a5310aa7b9a3e1 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sat, 19 Jan 2019 13:15:48 +0100 Subject: [PATCH] adding custom date support and fixing api --- package-lock.json | 18 +++++++++++++-- package.json | 6 +++-- src/index.ts | 58 +++++++++++++++++++++++++++++++---------------- src/test.ts | 45 ++++++++++++++++++++++++++++++++---- 4 files changed, 98 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 10f459a..ea2f2be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 7e03803..d3beea0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/secure-file-wrapper", - "version": "2.1.0", + "version": "2.2.0", "main": "lib/index.js", "author": "Fabian Stamm ", "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" diff --git a/src/index.ts b/src/index.ts index 57c8b4f..838796f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { + async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string, date?: Date): Promise { 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 { @@ -199,25 +215,27 @@ export default class SecureFileWrapper { } } - async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string): Promise { - 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 { + 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 { + async delete(id: string): Promise { let res = await this.makeRequest("/files/" + id, "DELETE", {}); - - return res.json(); } async history(id: string): Promise { 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) { diff --git a/src/test.ts b/src/test.ts index f9a47a5..aabb949 100644 --- a/src/test.ts +++ b/src/test.ts @@ -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); }); }) }