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!
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

View File

@ -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 = <TuitioLoginResult>{ result: { token, expiresIn: 600000 }, error: null };
const authResult = <TuitioLoginResponse>{ result: { token, expiresIn: 600000 }, error: null };
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?.expiresIn).toBe(600000);
expect(loginResult.result?.validUntil).toBeDefined();
expect(loginResult.error).toBeNull();
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 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<TuitioLoginResult> {
async login(userName: string, password: string): Promise<TuitioLoginResponse> {
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<TuitioLogoutResult> {
async logout(): Promise<TuitioLogoutResponse> {
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) {

View File

@ -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;