From 5804ca084d7fb51d5c7dfd10348cd844929e2eab Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 18 Mar 2023 00:41:42 +0200 Subject: [PATCH] return validUntil from login method --- README.md | 4 ++-- src/__tests__/TuitioClient.test.ts | 4 ++-- src/__tests__/TuitioLogin.test.ts | 1 + src/client.ts | 11 ++++++----- src/index.ts | 4 ++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1d37622..ec4eed0 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ The package installation can be done in two ways: ```javascript! const { TuitioClient, fetch } = require("@flare/tuitio-client"); const TuitioClient = require("@flare/tuitio-client"); -const type { TuitioLoginResult, TuitioLogoutResult, TuitioState } = require("@flare/tuitio-client"); +const type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState } = require("@flare/tuitio-client"); ``` ```javascript! import { TuitioClient, fetch } from "@flare/tuitio-client"; import TuitioClient from "@flare/tuitio-client"; -import type { TuitioLoginResult, TuitioLogoutResult, TuitioState } from "@flare/tuitio-client"; +import type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState } from "@flare/tuitio-client"; ``` ## Unit testing diff --git a/src/__tests__/TuitioClient.test.ts b/src/__tests__/TuitioClient.test.ts index c1acfd0..72e4f67 100644 --- a/src/__tests__/TuitioClient.test.ts +++ b/src/__tests__/TuitioClient.test.ts @@ -2,7 +2,7 @@ import { getUrlTemplates } from "../config"; import { TuitioClient } from "../client"; -import type { TuitioLoginResult } from "../client"; +import type { TuitioLoginResponse } from "../client"; test("Get url templates", () => { const result = getUrlTemplates("https://test.com/api"); @@ -42,7 +42,7 @@ test("Tuitio client fake authentication", async () => { const client = new TuitioClient("https://test.com/api"); const spy = jest.spyOn(client, "login").mockImplementation(async (userName, password) => { const token = `mock-${userName}-${password}`; - const authResult = { result: { token, expiresIn: 600000 }, error: null }; + const authResult = { result: { token, expiresIn: 600000 }, error: null }; return authResult; }); diff --git a/src/__tests__/TuitioLogin.test.ts b/src/__tests__/TuitioLogin.test.ts index f6b3456..64602b6 100644 --- a/src/__tests__/TuitioLogin.test.ts +++ b/src/__tests__/TuitioLogin.test.ts @@ -25,6 +25,7 @@ test("Tuitio client: successfully account login", async () => { expect(loginResult.result?.token).toBe("token-mock"); expect(loginResult.result?.expiresIn).toBe(600000); + expect(loginResult.result?.validUntil).toBeDefined(); expect(loginResult.error).toBeNull(); const storage = fetch(); diff --git a/src/client.ts b/src/client.ts index 7865e9d..ba07bbf 100644 --- a/src/client.ts +++ b/src/client.ts @@ -25,8 +25,8 @@ async function request(url: string, method: string) { } } -export type TuitioLoginResult = { result: { token: string; expiresIn: number } | null; error: string | null }; -export type TuitioLogoutResult = { result: { userId: number; userName: string; logoutDate: Date } | null; error: string }; +export type TuitioLoginResponse = { result: { token: string; expiresIn: number; validUntil: Date } | null; error: string | null }; +export type TuitioLogoutResponse = { result: { userId: number; userName: string; logoutDate: Date } | null; error: string }; class TuitioClient { tuitioUrl: string; @@ -41,7 +41,7 @@ class TuitioClient { this.urlTemplates = getUrlTemplates(this.tuitioUrl); } - async login(userName: string, password: string): Promise { + async login(userName: string, password: string): Promise { const url = this.urlTemplates.login.replace("{username}", userName).replace("{password}", password); const response = await request(url, "post"); @@ -51,14 +51,15 @@ class TuitioClient { const validUntil = new Date(currentDate.getTime() + expiresIn); const authData = { token: response.result.token, validUntil, userName }; setItem(storageKeys.AUTH_DATA, authData); + response.result.validUntil = validUntil; } return response; } - async logout(): Promise { + async logout(): Promise { const authData = getItem(storageKeys.AUTH_DATA); - if (!authData) return { result: null, error: "UNAUTHENTICATED" } as TuitioLogoutResult; + if (!authData) return { result: null, error: "UNAUTHENTICATED" } as TuitioLogoutResponse; const url = this.urlTemplates.logout.replace("{token}", authData.token); const response = await request(url, "post"); if (!response.error) { diff --git a/src/index.ts b/src/index.ts index 98fc5a5..164987f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ // Copyright (c) 2023 Tudor Stanciu import { TuitioClient, fetch } from "./client"; -import type { TuitioLoginResult, TuitioLogoutResult, TuitioState } from "./client"; +import type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState } from "./client"; export { TuitioClient, fetch }; -export type { TuitioLoginResult, TuitioLogoutResult, TuitioState }; +export type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState }; export default TuitioClient;