Adding offline exception

This commit is contained in:
Fabian Stamm 2018-03-10 18:33:47 +01:00
parent 95112d3f31
commit bbf9b0c008
4 changed files with 58 additions and 11 deletions

4
index.d.ts vendored
View File

@ -30,6 +30,10 @@ export default class SecureFile {
delete(id: string): Promise<boolean>; delete(id: string): Promise<boolean>;
history(id: string): Promise<History>; history(id: string): Promise<History>;
} }
export declare class NoConnection extends Error {
type: string;
constructor();
}
export declare class Unauthorized extends Error { export declare class Unauthorized extends Error {
type: string; type: string;
constructor(); constructor();

View File

@ -62,7 +62,7 @@ var SecureFile = /** @class */ (function () {
} }
SecureFile.prototype.getCode = function () { SecureFile.prototype.getCode = function () {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
var myHeaders, myInit, code_res, code, r; var myHeaders, myInit, code_res, err_1, code, r;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
@ -73,12 +73,21 @@ var SecureFile = /** @class */ (function () {
method: 'GET', method: 'GET',
headers: myHeaders, headers: myHeaders,
}; };
return [4 /*yield*/, fetch(this.Server + "/code?username=" + this.Username, myInit)]; _a.label = 1;
case 1: case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, fetch(this.Server + "/code?username=" + this.Username, myInit)];
case 2:
code_res = _a.sent(); code_res = _a.sent();
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
//TODO probably better fail check
throw new NoConnection();
case 4:
statusParser(code_res); statusParser(code_res);
return [4 /*yield*/, code_res.json()]; return [4 /*yield*/, code_res.json()];
case 2: case 5:
code = (_a.sent()).code; code = (_a.sent()).code;
r = new rsa(this.PrivateKey, "pkcs1-pem"); r = new rsa(this.PrivateKey, "pkcs1-pem");
return [2 /*return*/, { code: code, signature: r.sign(code).toString("base64") }]; return [2 /*return*/, { code: code, signature: r.sign(code).toString("base64") }];
@ -88,7 +97,7 @@ var SecureFile = /** @class */ (function () {
}; };
SecureFile.prototype.makeRequest = function (endpoint, method, query, body) { SecureFile.prototype.makeRequest = function (endpoint, method, query, body) {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
var code, query_str, first, key, myHeaders; var code, query_str, first, key, myHeaders, res, err_2;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: return [4 /*yield*/, this.getCode()]; case 0: return [4 /*yield*/, this.getCode()];
@ -107,8 +116,17 @@ var SecureFile = /** @class */ (function () {
myHeaders = new Headers(); myHeaders = new Headers();
myHeaders.append('pragma', 'no-cache'); myHeaders.append('pragma', 'no-cache');
myHeaders.append('cache-control', 'no-cache'); myHeaders.append('cache-control', 'no-cache');
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
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 2: return [2 /*return*/, _a.sent()]; case 3:
res = _a.sent();
return [2 /*return*/, res];
case 4:
err_2 = _a.sent();
throw new NoConnection();
case 5: return [2 /*return*/];
} }
}); });
}); });
@ -267,6 +285,16 @@ var SecureFile = /** @class */ (function () {
return SecureFile; return SecureFile;
}()); }());
exports.default = SecureFile; exports.default = SecureFile;
var NoConnection = /** @class */ (function (_super) {
__extends(NoConnection, _super);
function NoConnection() {
var _this = _super.call(this, "No connection") || this;
_this.type = "noconnection";
return _this;
}
return NoConnection;
}(Error));
exports.NoConnection = NoConnection;
var Unauthorized = /** @class */ (function (_super) { var Unauthorized = /** @class */ (function (_super) {
__extends(Unauthorized, _super); __extends(Unauthorized, _super);
function Unauthorized() { function Unauthorized() {

View File

@ -41,10 +41,13 @@ export default class SecureFile {
method: 'GET', method: 'GET',
headers: myHeaders, headers: myHeaders,
}; };
try {
let code_res = await fetch(this.Server + "/code?username=" + this.Username, myInit); var code_res = await fetch(this.Server + "/code?username=" + this.Username, myInit);
} catch (err) {
//TODO probably better fail check
throw new NoConnection();
}
statusParser(code_res); statusParser(code_res);
//ToDo check status Codes
let code = (await code_res.json()).code; let code = (await code_res.json()).code;
let r = new rsa(this.PrivateKey, "pkcs1-pem"); let r = new rsa(this.PrivateKey, "pkcs1-pem");
return { code: code, signature: r.sign(code).toString("base64") }; return { code: code, signature: r.sign(code).toString("base64") };
@ -64,8 +67,12 @@ export default class SecureFile {
var myHeaders = new Headers(); var myHeaders = new Headers();
myHeaders.append('pragma', 'no-cache'); myHeaders.append('pragma', 'no-cache');
myHeaders.append('cache-control', 'no-cache'); myHeaders.append('cache-control', 'no-cache');
try {
return await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders }); let res = await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders });
return res;
} catch (err) {
throw new NoConnection();
}
} }
async test(): Promise<{ user: string, test: true }> { async test(): Promise<{ user: string, test: true }> {
@ -138,6 +145,14 @@ export default class SecureFile {
} }
} }
export class NoConnection extends Error {
type: string;
constructor() {
super("No connection");
this.type = "noconnection"
}
}
export class Unauthorized extends Error { export class Unauthorized extends Error {
type: string; type: string;
constructor() { constructor() {

View File

@ -1,6 +1,6 @@
{ {
"name": "secure-file-wrapper", "name": "secure-file-wrapper",
"version": "1.0.9", "version": "1.0.10",
"main": "index.js", "main": "index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",