return validUntil from login method

master
Tudor Stanciu 2023-03-18 00:41:42 +02:00
parent 3767a8eeb4
commit 5804ca084d
5 changed files with 13 additions and 11 deletions

View File

@ -16,13 +16,13 @@ The package installation can be done in two ways:
```javascript! ```javascript!
const { TuitioClient, fetch } = require("@flare/tuitio-client"); const { TuitioClient, fetch } = require("@flare/tuitio-client");
const TuitioClient = 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! ```javascript!
import { TuitioClient, fetch } from "@flare/tuitio-client"; import { TuitioClient, fetch } from "@flare/tuitio-client";
import TuitioClient 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 ## Unit testing

View File

@ -2,7 +2,7 @@
import { getUrlTemplates } from "../config"; import { getUrlTemplates } from "../config";
import { TuitioClient } from "../client"; import { TuitioClient } from "../client";
import type { TuitioLoginResult } from "../client"; import type { TuitioLoginResponse } from "../client";
test("Get url templates", () => { test("Get url templates", () => {
const result = getUrlTemplates("https://test.com/api"); 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 client = new TuitioClient("https://test.com/api");
const spy = jest.spyOn(client, "login").mockImplementation(async (userName, password) => { const spy = jest.spyOn(client, "login").mockImplementation(async (userName, password) => {
const token = `mock-${userName}-${password}`; const token = `mock-${userName}-${password}`;
const authResult = <TuitioLoginResult>{ result: { token, expiresIn: 600000 }, error: null }; const authResult = <TuitioLoginResponse>{ result: { token, expiresIn: 600000 }, error: null };
return authResult; return authResult;
}); });

View File

@ -25,6 +25,7 @@ test("Tuitio client: successfully account login", async () => {
expect(loginResult.result?.token).toBe("token-mock"); expect(loginResult.result?.token).toBe("token-mock");
expect(loginResult.result?.expiresIn).toBe(600000); expect(loginResult.result?.expiresIn).toBe(600000);
expect(loginResult.result?.validUntil).toBeDefined();
expect(loginResult.error).toBeNull(); expect(loginResult.error).toBeNull();
const storage = fetch(); const storage = fetch();

View File

@ -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 TuitioLoginResponse = { result: { token: string; expiresIn: number; validUntil: Date } | null; error: string | null };
export type TuitioLogoutResult = { result: { userId: number; userName: string; logoutDate: Date } | null; error: string }; export type TuitioLogoutResponse = { result: { userId: number; userName: string; logoutDate: Date } | null; error: string };
class TuitioClient { class TuitioClient {
tuitioUrl: string; tuitioUrl: string;
@ -41,7 +41,7 @@ class TuitioClient {
this.urlTemplates = getUrlTemplates(this.tuitioUrl); this.urlTemplates = getUrlTemplates(this.tuitioUrl);
} }
async login(userName: string, password: string): Promise<TuitioLoginResult> { async login(userName: string, password: string): Promise<TuitioLoginResponse> {
const url = this.urlTemplates.login.replace("{username}", userName).replace("{password}", password); const url = this.urlTemplates.login.replace("{username}", userName).replace("{password}", password);
const response = await request(url, "post"); const response = await request(url, "post");
@ -51,14 +51,15 @@ class TuitioClient {
const validUntil = new Date(currentDate.getTime() + expiresIn); const validUntil = new Date(currentDate.getTime() + expiresIn);
const authData = { token: response.result.token, validUntil, userName }; const authData = { token: response.result.token, validUntil, userName };
setItem(storageKeys.AUTH_DATA, authData); setItem(storageKeys.AUTH_DATA, authData);
response.result.validUntil = validUntil;
} }
return response; return response;
} }
async logout(): Promise<TuitioLogoutResult> { async logout(): Promise<TuitioLogoutResponse> {
const authData = getItem(storageKeys.AUTH_DATA); 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 url = this.urlTemplates.logout.replace("{token}", authData.token);
const response = await request(url, "post"); const response = await request(url, "post");
if (!response.error) { if (!response.error) {

View File

@ -1,8 +1,8 @@
// Copyright (c) 2023 Tudor Stanciu // Copyright (c) 2023 Tudor Stanciu
import { TuitioClient, fetch } from "./client"; import { TuitioClient, fetch } from "./client";
import type { TuitioLoginResult, TuitioLogoutResult, TuitioState } from "./client"; import type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState } from "./client";
export { TuitioClient, fetch }; export { TuitioClient, fetch };
export type { TuitioLoginResult, TuitioLogoutResult, TuitioState }; export type { TuitioLoginResponse, TuitioLogoutResponse, TuitioState };
export default TuitioClient; export default TuitioClient;