From 395e68502806de12a7931a7118739b25c4731dd7 Mon Sep 17 00:00:00 2001 From: uimaxbai <61615730+uimaxbai@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:00:22 +0100 Subject: [PATCH] wrapping up --- src/lib/fetchIt.js | 102 --------------------------- src/lib/fetchIt.ts | 2 +- src/routes/authenticate/+page.svelte | 18 ++++- 3 files changed, 16 insertions(+), 106 deletions(-) delete mode 100644 src/lib/fetchIt.js diff --git a/src/lib/fetchIt.js b/src/lib/fetchIt.js deleted file mode 100644 index 08b0a9f..0000000 --- a/src/lib/fetchIt.js +++ /dev/null @@ -1,102 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); -function fetchIt(type, url, header, requestBody) { - if (header === void 0) { header = { 'Content-Type': 'application/json' }; } - if (requestBody === void 0) { requestBody = {}; } - return __awaiter(this, void 0, void 0, function () { - var settings, response, url1_1, response, _a, e_1; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - settings = header; - if (!(Object.keys(requestBody).length === 0)) return [3 /*break*/, 2]; - return [4 /*yield*/, window.fetch(url, { - method: type, - headers: settings - })]; - case 1: - response = _b.sent(); - return [3 /*break*/, 8]; - case 2: - if (!(type === "GET")) return [3 /*break*/, 4]; - url1_1 = new URL(url); - requestBody = { - hry: 'a', - }; - Object.keys(requestBody).forEach(function (key) { return url1_1.searchParams.append(key, requestBody.key); }); - return [4 /*yield*/, window.fetch(url1_1, { - method: type, - headers: settings, - })]; - case 3: - _b.sent(); - return [3 /*break*/, 8]; - case 4: - if (!(type === "POST")) return [3 /*break*/, 6]; - return [4 /*yield*/, window.fetch(url, { - method: type, - headers: settings, - body: JSON.stringify(requestBody), - })]; - case 5: - _b.sent(); - return [3 /*break*/, 8]; - case 6: return [4 /*yield*/, window.fetch(url, { - method: type, - headers: settings - })]; - case 7: - response = _b.sent(); - _b.label = 8; - case 8: - _b.trys.push([8, 11, , 12]); - return [4 /*yield*/, response.json()]; - case 9: - _a = [_b.sent()]; - return [4 /*yield*/, response.status]; - case 10: return [2 /*return*/, _a.concat([_b.sent()])]; - case 11: - e_1 = _b.sent(); - return [2 /*return*/, 1]; - case 12: return [2 /*return*/]; - } - }); - }); -} -exports.default = fetchIt; diff --git a/src/lib/fetchIt.ts b/src/lib/fetchIt.ts index 93f8d1a..be07b11 100644 --- a/src/lib/fetchIt.ts +++ b/src/lib/fetchIt.ts @@ -42,7 +42,7 @@ async function fetchIt(type: string, url: string, requestBody: object = {}, head } try { - return [await response.json(), await response.status]; + return await response.json(); } catch (e) { return 1; diff --git a/src/routes/authenticate/+page.svelte b/src/routes/authenticate/+page.svelte index bdcb56f..9106776 100644 --- a/src/routes/authenticate/+page.svelte +++ b/src/routes/authenticate/+page.svelte @@ -53,12 +53,24 @@ e.preventDefault(); var username = jQuery('#logInput').text(); var password = jQuery('#passwInput').text(); - fetchIt('GET', 'https://api.dispatch.eu.org/api/v2/user/${username}/auth', { + let response: object = fetchIt( + 'GET', + 'https://api.dispatch.eu.org/api/v2/user/${username}/auth', + { + password: password + } + ); + localStorage.setItem('session', response.toString()); + }); + + jQuery('#regForm').submit((e) => { + var username = jQuery('#nameInput').text(); + var password = jQuery('#passInput').text(); + fetchIt('GET', 'https://api.dispatch.eu.org/api/v2/users/new', { + user: username, password: password }); }); - - jQuery('#regForm').submit((e) => {}); }); // TODO: make one for register button