marvin-auth-kit
Advanced tools
Comparing version 5.2.6 to 5.2.7
@@ -1,2 +0,2 @@ | ||
import { AuthAPI } from "./types"; | ||
import type { AuthAPI } from "./types"; | ||
export declare const auth: AuthAPI; |
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { credentialsRequiredError, UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { credentialsRequiredError, UnexpectedAPIError } from "./errors"; | ||
export const auth = (() => { | ||
@@ -41,3 +41,3 @@ // clean axios, no interceptors | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -47,2 +47,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -70,9 +71,7 @@ function logout() { | ||
} | ||
else { | ||
throw response.data; | ||
} | ||
throw response.data; | ||
} | ||
catch (error) { | ||
_retryCounter = 0; | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -82,5 +81,6 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
function applyInterceptors(instance) { | ||
if (!interceptorInstancesMap.get(instance)) { | ||
if (instance && !interceptorInstancesMap.get(instance)) { | ||
instance = instance || axios; //normalize fallback to default instance. | ||
@@ -97,3 +97,5 @@ //@ts-ignore: Fix is for axios? > https://github.com/axios/axios/issues/5573 | ||
function ejectInterceptors(instance) { | ||
instance.interceptors.request.eject(interceptorInstancesMap.get(instance)); | ||
const id = interceptorInstancesMap.get(instance); | ||
if (id !== undefined) | ||
instance.interceptors.request.eject(id); | ||
interceptorInstancesMap.delete(instance); | ||
@@ -111,4 +113,3 @@ } | ||
async function responseErrorHandler(error) { | ||
if (error && | ||
error.response && | ||
if (error?.response && | ||
"status" in error.response && | ||
@@ -124,3 +125,3 @@ error.response.status === 401 && | ||
headers: { | ||
...error.config.headers, | ||
...error.config?.headers, | ||
Authorization: `Bearer ${token}`, | ||
@@ -138,3 +139,3 @@ }, | ||
_retryCounter = 0; | ||
if (error && error.response) | ||
if (axios.isAxiosError(error) && error?.response) | ||
throw error.response; | ||
@@ -141,0 +142,0 @@ throw error; |
@@ -1,2 +0,2 @@ | ||
import { ConfigAPI } from "./types"; | ||
import type { ConfigAPI } from "./types"; | ||
export declare const config: ConfigAPI; |
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { defaultConfig } from "./config/DefaultConfig"; | ||
import { defaultServerConfig } from "./config/DefaultServerConfig"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { validateConfig, fullUrl } from "./utils/config.utils"; | ||
import { fullUrl, validateConfig } from "./utils/config.utils"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
export const config = (() => { | ||
const _config = defaultConfig; | ||
const _config = { ...defaultConfig }; | ||
const _cachedServerconfig = defaultServerConfig; | ||
function set(config) { | ||
const validatedConfig = validateConfig(config); | ||
// persist config in closure variable | ||
Object.keys(validatedConfig).forEach((value, key) => { | ||
_config[value] = validatedConfig[value]; | ||
}); | ||
const validatedConfig = validateConfig({ ..._config, ...config }); | ||
Object.assign(_config, validatedConfig); | ||
} | ||
@@ -33,14 +30,5 @@ function get() { | ||
if (response.status === 200) { | ||
const copyObject = { ...response.data }; | ||
Object.keys(copyObject).forEach((value, key) => { | ||
if (value !== "metadata_configuration") { | ||
_cachedServerconfig[value] = copyObject[value]; | ||
} | ||
else { | ||
_cachedServerconfig[value] = | ||
copyObject[value] && copyObject[value].substring(0, 1) === "[" | ||
? JSON.parse(copyObject[value]) | ||
: []; | ||
} | ||
}); | ||
const { metadata_configuration, ...copyObject } = { ...response.data }; | ||
Object.assign(_cachedServerconfig, copyObject); | ||
_cachedServerconfig["metadata_configuration"] = JSON.parse(metadata_configuration); | ||
return _cachedServerconfig; | ||
@@ -62,3 +50,3 @@ } | ||
} | ||
else if (error.response) | ||
else if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -68,2 +56,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -82,13 +71,14 @@ async function setServerConfig(config) { | ||
if (response.status === 200) { | ||
Object.keys(requestBody).forEach((value) => { | ||
_cachedServerconfig[value] = requestBody[value]; | ||
}); | ||
return { ..._cachedServerconfig, metadata_configuration }; | ||
Object.assign(_cachedServerconfig, requestBody); | ||
return { | ||
..._cachedServerconfig, | ||
metadata_configuration: metadata_configuration | ||
? metadata_configuration | ||
: [], | ||
}; | ||
} | ||
else { | ||
throw response.data; | ||
} | ||
throw response.data; | ||
} | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error?.response) | ||
handleAxiosError(error.response); | ||
@@ -98,2 +88,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -100,0 +91,0 @@ return { |
@@ -0,6 +1,6 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios from "axios"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const email = (() => { | ||
@@ -20,3 +20,3 @@ async function request(email) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -43,3 +43,3 @@ else | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -49,2 +49,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -51,0 +52,0 @@ function baseUrl() { |
@@ -0,8 +1,8 @@ | ||
import MailSlurp from "mailslurp-client"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { email } from "./email"; | ||
import { users } from "./users"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
describe("EMail", () => { | ||
@@ -34,3 +34,3 @@ config.set(TestsServerConfig); | ||
}); | ||
const result = await email.request(inbox.emailAddress); | ||
const result = await email.request(inbox.emailAddress || ""); | ||
const getToken = async (inboxId) => { | ||
@@ -40,11 +40,13 @@ if (inbox) { | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || "-1"); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
return matches.length >= 2 ? matches[1] : ""; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody || ""); | ||
return matches !== null && (matches?.length || 0) >= 2 | ||
? matches[1] | ||
: ""; | ||
} | ||
}; | ||
const confirmToken = await getToken(inbox.id); | ||
const confirmToken = await getToken(inbox.id || ""); | ||
expect(confirmToken).toMatch(/.{50,}/); | ||
const confirmResult = await email.confirm(confirmToken); | ||
const confirmResult = await email.confirm(confirmToken || ""); | ||
if (confirmResult) { | ||
@@ -55,3 +57,3 @@ expect(confirmResult).toMatchObject({ | ||
}); | ||
const result = await auth.login(inbox.emailAddress, "newusers3cr3t!"); | ||
const result = await auth.login(inbox.emailAddress || "", "newusers3cr3t!"); | ||
if (result) { | ||
@@ -58,0 +60,0 @@ expect(result).toMatchObject({ |
export declare const UnexpectedAPIError: import("./types").APIError; | ||
export declare const credentialsRequiredError: (args: any) => import("./types").APIError; | ||
export declare const credentialsRequiredError: (args: string[]) => import("./types").APIError; | ||
export declare const RefreshWithoutRefreshTokenError: import("./types").APIError; | ||
export declare const UnauthorizedError: import("./types").APIError; | ||
export declare const unSupportedAuthAPIVersion: (version: string, range: string) => Error; |
@@ -15,2 +15,3 @@ import { useEffect, useState } from "react"; | ||
setHookState({ loading: false, authenticated: result !== false }); | ||
return tokens.token; | ||
})(); | ||
@@ -17,0 +18,0 @@ }, [tokens.token]); |
@@ -0,5 +1,5 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const metadata = (() => { | ||
@@ -12,3 +12,3 @@ async function get() { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -18,2 +18,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -26,3 +27,3 @@ async function update(metdata) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -32,2 +33,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -42,3 +44,3 @@ async function remove() { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -45,0 +47,0 @@ else |
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const password = (() => { | ||
@@ -18,3 +18,3 @@ async function change(password) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -36,3 +36,3 @@ else | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -58,3 +58,3 @@ else | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -64,2 +64,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -66,0 +67,0 @@ function baseUrl() { |
@@ -0,8 +1,8 @@ | ||
import MailSlurp from "mailslurp-client"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { password } from "./password"; | ||
import { users } from "./users"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
describe("Password", () => { | ||
@@ -34,3 +34,3 @@ config.set(TestsServerConfig); | ||
}); | ||
const result = await password.request(inbox.emailAddress); | ||
await password.request(inbox.emailAddress || ""); | ||
const getToken = async (inboxId) => { | ||
@@ -40,11 +40,11 @@ if (inbox) { | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || ""); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
return matches.length >= 2 ? matches[1] : ""; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody || ""); | ||
return matches !== null && matches.length >= 2 ? matches[1] : ""; | ||
} | ||
}; | ||
const confirmToken = await getToken(inbox.id); | ||
const confirmToken = await getToken(inbox.id || ""); | ||
expect(confirmToken).toMatch(/.{50,}/); | ||
const confirmResult = await password.confirm(confirmToken, "newusers3cr3t!"); | ||
const confirmResult = await password.confirm(confirmToken || "", "newusers3cr3t!"); | ||
if (confirmResult) { | ||
@@ -55,3 +55,3 @@ expect(confirmResult).toMatchObject({ | ||
}); | ||
const result = await auth.login(inbox.emailAddress, "newusers3cr3t!"); | ||
const result = await auth.login(inbox.emailAddress || "", "newusers3cr3t!"); | ||
if (result) { | ||
@@ -58,0 +58,0 @@ expect(result).toMatchObject({ |
@@ -1,2 +0,2 @@ | ||
import { ReactElement } from "react"; | ||
import React, { type ReactElement } from "react"; | ||
interface Props { | ||
@@ -6,3 +6,3 @@ redirectTo: string; | ||
} | ||
export declare const LoginGuard: (props: Props) => ReactElement; | ||
export declare function LoginGuard(props: Props): React.JSX.Element; | ||
export {}; |
import React from "react"; | ||
import { Navigate, Outlet } from "react-router-dom"; | ||
import { Navigate, Outlet, useOutletContext } from "react-router-dom"; | ||
import { useIsAuthenticated } from "../../../hooks/useIsAuthenticated"; | ||
export const LoginGuard = (props) => { | ||
export function LoginGuard(props) { | ||
const { redirectTo, loader } = props; | ||
const context = useOutletContext(); | ||
const [isLoading, isAuthenticated] = useIsAuthenticated(); | ||
console.log("LoginGuard() isLoading:", isLoading, "\n isAuthenticated:", isAuthenticated); | ||
return (React.createElement(React.Fragment, null, | ||
isLoading && loader, | ||
!isLoading && isAuthenticated && (React.createElement(Navigate, { to: { pathname: redirectTo } })), | ||
!isLoading && !isAuthenticated && React.createElement(Outlet, null))); | ||
}; | ||
!isLoading && !isAuthenticated && React.createElement(Outlet, { context: context }))); | ||
} |
@@ -1,2 +0,2 @@ | ||
import { ReactElement } from "react"; | ||
import React, { type ReactElement } from "react"; | ||
interface Props { | ||
@@ -6,8 +6,3 @@ redirectTo: string; | ||
} | ||
export declare const PrivateGuard: { | ||
(props: Props): ReactElement; | ||
defaultProps: { | ||
redirectTo: string; | ||
}; | ||
}; | ||
export declare function PrivateGuard(props: Props): React.JSX.Element; | ||
export {}; |
import React from "react"; | ||
import { Outlet, Navigate } from "react-router-dom"; | ||
import { Navigate, Outlet, useOutletContext } from "react-router-dom"; | ||
import { useIsAuthenticated } from "../../../hooks/useIsAuthenticated"; | ||
export const PrivateGuard = (props) => { | ||
const { redirectTo, loader } = props; | ||
export function PrivateGuard(props) { | ||
const { redirectTo = "/login", loader } = props; | ||
const context = useOutletContext(); | ||
const [isLoading, isAuthenticated] = useIsAuthenticated(); | ||
return (React.createElement(React.Fragment, null, | ||
isLoading && loader, | ||
!isLoading && isAuthenticated && React.createElement(Outlet, null), | ||
!isLoading && isAuthenticated && React.createElement(Outlet, { context: context }), | ||
!isLoading && !isAuthenticated && (React.createElement(Navigate, { to: { pathname: redirectTo } })))); | ||
}; | ||
PrivateGuard.defaultProps = { redirectTo: "/login" }; | ||
} |
@@ -0,6 +1,6 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios from "axios"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const register = (() => { | ||
@@ -20,3 +20,3 @@ async function request(email, password, metadata) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -26,2 +26,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -43,3 +44,3 @@ async function confirm(token) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -49,2 +50,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -51,0 +53,0 @@ function baseUrl() { |
@@ -1,7 +0,7 @@ | ||
import { register } from "./register"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { register } from "./register"; | ||
describe("Register", () => { | ||
@@ -27,3 +27,3 @@ config.set(TestsServerConfig); | ||
inbox = await mailslurp.createInbox(); | ||
const result = await register.request(inbox.emailAddress, "SlurpS3cr3t!"); | ||
const result = await register.request(inbox.emailAddress || "", "SlurpS3cr3t!"); | ||
if (result) { | ||
@@ -38,11 +38,11 @@ expect(result).toMatchObject({ | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || ""); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
return matches[1]; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody || ""); | ||
return (matches !== null && matches.length > 0 && matches[1]) || ""; | ||
} | ||
}; | ||
const registrationToken = await getToken(inbox.id); | ||
const registrationToken = await getToken(inbox.id || ""); | ||
expect(registrationToken).toMatch(/.{50,}/); | ||
const confirmResult = await register.confirm(registrationToken); | ||
const confirmResult = await register.confirm(registrationToken || ""); | ||
if (confirmResult) { | ||
@@ -49,0 +49,0 @@ expect(confirmResult).toMatchObject({ |
@@ -0,6 +1,6 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios from "axios"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const reset = (() => { | ||
@@ -15,3 +15,3 @@ async function request(id) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -21,2 +21,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -34,3 +35,3 @@ async function confirm(token, password) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -40,2 +41,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -42,0 +44,0 @@ function baseUrl() { |
@@ -0,5 +1,5 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { createAPIError, handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
export const roles = (() => { | ||
@@ -16,3 +16,3 @@ async function create(role) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -28,12 +28,18 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
async function queryRoles(query) { | ||
const { role, ...params } = query; | ||
const { role, ...rest } = query || { role: "", offset: 0, limit: 10 }; | ||
const params = new URLSearchParams(); | ||
//normalize role | ||
if (role && typeof role !== "string" && "name" in role) { | ||
params["name"] = role.name; | ||
params.set("name", role.name); | ||
} | ||
else if (role && typeof role === "string") { | ||
params["name"] = role; | ||
params.set("name", role); | ||
} | ||
if (rest.offset) | ||
params.set("offset", rest.offset.toString()); | ||
if (rest.limit) | ||
params.set("limit", rest.limit.toString()); | ||
const requestConfig = { params: params }; | ||
@@ -48,3 +54,3 @@ try { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -54,2 +60,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -66,3 +73,3 @@ async function update(role) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -72,2 +79,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -88,3 +96,3 @@ async function remove(idOrRole) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -91,0 +99,0 @@ else |
@@ -0,3 +1,3 @@ | ||
import Cookies from "js-cookie"; | ||
import { config } from "./config"; | ||
import Cookies from "js-cookie"; | ||
import { defaultServerConfig } from "./config/DefaultServerConfig"; | ||
@@ -34,3 +34,3 @@ const TOKEN_COOKIE = "Token"; | ||
} | ||
let expireMilliseconds = 10, refreshExpireMilliseconds = 10, initialisationExpireMilliseconds = 10, expireDate = new Date(), refreshExpireDate = new Date(), initialisationExpireDate = new Date(); | ||
let expireMilliseconds = 10, refreshExpireMilliseconds = 10, initialisationExpireMilliseconds = 10, refreshExpireDate = new Date(), initialisationExpireDate = new Date(); | ||
if (token && refreshToken) { | ||
@@ -45,5 +45,2 @@ if (serverConfig.version === "===default===") { | ||
} | ||
expireDate = new Date(Date.now() + | ||
(expireMilliseconds = | ||
serverConfig.token_expiration_seconds_in_the_future_access * 1000)); | ||
refreshExpireDate = new Date(Date.now() + | ||
@@ -85,3 +82,3 @@ (refreshExpireMilliseconds = | ||
function booleanToString(bool) { | ||
return bool ? "true" : "false"; | ||
return bool === true ? "true" : "false"; | ||
} | ||
@@ -88,0 +85,0 @@ function getTokens() { |
@@ -1,2 +0,2 @@ | ||
import { AxiosResponseHeaders, AxiosStatic, RawAxiosResponseHeaders } from "axios"; | ||
import type { AxiosResponseHeaders, AxiosStatic, RawAxiosResponseHeaders } from "axios"; | ||
export interface AuthConfig extends Omit<AuthConfigServer, "metadata_configuration"> { | ||
@@ -129,3 +129,3 @@ metadata_configuration: MetadataProperty[]; | ||
} | ||
export declare type SortableFields = "id" | "created" | "email" | "emailValidated" | "enabled"; | ||
export type SortableFields = "id" | "created" | "email" | "emailValidated" | "enabled"; | ||
export interface UserSort { | ||
@@ -132,0 +132,0 @@ field: SortableFields; |
@@ -0,5 +1,5 @@ | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { createAPIError, handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
function isRole(role) { | ||
@@ -18,3 +18,3 @@ return role.hasOwnProperty("id") && role.hasOwnProperty("name"); | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -24,10 +24,8 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
async function retrieve(idOrUser) { | ||
if (idOrUser && typeof idOrUser !== "number") { | ||
idOrUser = idOrUser.id; | ||
} | ||
if (typeof idOrUser === "number") { | ||
async function doRetrieve(id) { | ||
try { | ||
const url = `${baseUrl()}/${idOrUser}`; | ||
const url = `${baseUrl()}/${id}`; | ||
const response = await axios.get(url); | ||
@@ -40,3 +38,3 @@ if (response.status === 200) | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -47,2 +45,8 @@ else | ||
} | ||
if (idOrUser && typeof idOrUser !== "number" && idOrUser.id) { | ||
return doRetrieve(idOrUser.id); | ||
} | ||
if (typeof idOrUser === "number") { | ||
return doRetrieve(idOrUser); | ||
} | ||
throw [ | ||
@@ -55,9 +59,9 @@ createAPIError("incorrect_data", "please provide an id, or a (user) object with id"), | ||
...query, | ||
role: query.role | ||
role: query?.role | ||
? query.role.map((role) => (isRole(role) ? role.name : role)) | ||
: undefined, | ||
sort: query.sort | ||
sort: query?.sort | ||
? query.sort.map((sorting) => `${sorting.field}|${sorting.direction}`) | ||
: undefined, | ||
enabled: query.enabled === "any" ? undefined : query.enabled, | ||
enabled: query?.enabled === "any" ? undefined : query?.enabled, | ||
}; | ||
@@ -86,3 +90,3 @@ const requestConfig = { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -92,2 +96,3 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -111,3 +116,3 @@ async function update(user) { | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -117,10 +122,8 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
async function remove(idOrUser) { | ||
if (typeof idOrUser !== "number") { | ||
idOrUser = idOrUser.id; | ||
} | ||
if (typeof idOrUser === "number") { | ||
async function doRemove(id) { | ||
try { | ||
const url = `${baseUrl()}/${idOrUser}`; | ||
const url = `${baseUrl()}/${id}`; | ||
const response = await axios.delete(url); | ||
@@ -133,3 +136,3 @@ if (response.status === 200) | ||
catch (error) { | ||
if (error.response) | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
@@ -139,3 +142,10 @@ else | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
if (typeof idOrUser !== "number" && idOrUser.id) { | ||
return doRemove(idOrUser.id); | ||
} | ||
if (typeof idOrUser === "number") { | ||
return doRemove(idOrUser); | ||
} | ||
else { | ||
@@ -142,0 +152,0 @@ throw [ |
@@ -1,5 +0,5 @@ | ||
import { users } from "./users"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { users } from "./users"; | ||
describe("Users", () => { | ||
@@ -6,0 +6,0 @@ config.set(TestsServerConfig); |
import { Config } from ".."; | ||
export declare function validateConfig(config: Config): any; | ||
export declare function validateConfig(config: Config): Config; | ||
export declare function validatePropsConfig(customConfig: Config): Config; | ||
export declare function validateCompatibility(config: Config): Promise<Config>; | ||
export declare function fullUrl(config: any): string; | ||
export declare function fullUrl(config: Config): string; |
import axios from "axios"; | ||
import { unSupportedAuthAPIVersion } from "../errors"; | ||
import { satisfies } from "semver"; | ||
import { API_COMPATIBLE_VERSION_RANGE } from ".."; | ||
import { unSupportedAuthAPIVersion } from "../errors"; | ||
import { defaultConfig } from "../config/DefaultConfig"; | ||
import { pipe } from "../utils/fp.utils"; | ||
import { API_COMPATIBLE_VERSION_RANGE } from ".."; | ||
export function validateConfig(config) { | ||
@@ -14,3 +14,3 @@ return pipe(validatePropsConfig, validateCompatibility)(config); | ||
if (config.url && config.url.substring(-1) === "/") { | ||
config.url = config.url.substr(0, -2); | ||
config.url = config.url.substring(0, -2); | ||
} | ||
@@ -17,0 +17,0 @@ config.secure = |
@@ -1,5 +0,5 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { APIError } from '../types/index'; | ||
import type { AxiosResponse } from "axios"; | ||
import type { APIError } from "../types"; | ||
export declare const parseServerError: (error: APIError) => string; | ||
export declare function createAPIError(key: string, message: string, args?: any[]): APIError; | ||
export declare function createAPIError(key: string, message: string, args?: string[]): APIError; | ||
export declare function handleAxiosError(error: AxiosResponse<APIError[]> | Error): void; | ||
@@ -6,0 +6,0 @@ export declare function isAPIErrorList(error: APIError[] | unknown): error is APIError[]; |
export const parseServerError = (error) => { | ||
const replaceRegEx = /\{([0-9]+)\}/gi; | ||
return error.message.replace(replaceRegEx, (matched, digit) => error.arguments[digit]); | ||
return error.message.replace(replaceRegEx, (_matched, digit) => error.arguments[digit]); | ||
}; | ||
@@ -15,7 +15,6 @@ export function createAPIError(key, message, args = []) { | ||
throw error.data; | ||
else | ||
throw error; | ||
throw error; | ||
} | ||
function isAxiosResponse(error) { | ||
return Object.keys(error).includes('status'); | ||
return Object.keys(error).includes("status"); | ||
} | ||
@@ -26,14 +25,13 @@ export function isAPIErrorList(error) { | ||
} | ||
else | ||
return false; | ||
return false; | ||
} | ||
export function isAPIError(error) { | ||
const keys = Object.keys(error); | ||
return (keys.includes('key') && | ||
keys.includes('message') && | ||
keys.includes('arguments')); | ||
return (keys.includes("key") && | ||
keys.includes("message") && | ||
keys.includes("arguments")); | ||
} | ||
export function isError(error) { | ||
return (typeof error === 'object' && | ||
Object.getOwnPropertyNames(error).includes('message')); | ||
return (typeof error === "object" && | ||
Object.getOwnPropertyNames(error).includes("message")); | ||
} |
@@ -1,1 +0,1 @@ | ||
export declare function pipe(...fns: any[]): (arg: any) => any; | ||
export declare function pipe<R = any>(...fns: any[]): R; |
export function pipe(...fns) { | ||
//@ts-ignore | ||
return (arg) => fns.reduce((prev, fn) => fn(prev), arg); | ||
} |
{ | ||
"name": "marvin-auth-kit", | ||
"version": "5.2.6", | ||
"version": "5.2.7", | ||
"description": "Javscript authentication kit", | ||
@@ -27,7 +27,4 @@ "main": "dist/index.js", | ||
"@babel/core": "^7.11.5", | ||
"@types/react": "^18.2.18", | ||
"@types/semver": "^7.3.9", | ||
"axios": "^1.4.0", | ||
"install": "^0.13.0", | ||
"react-router-dom": "^6.2.1", | ||
"axios": "^1.7.7", | ||
"react-router-dom": "^6.28.0", | ||
"semver": "^7.3.5" | ||
@@ -43,13 +40,17 @@ }, | ||
"devDependencies": { | ||
"@ianvs/prettier-plugin-sort-imports": "^4.4.0", | ||
"@types/jest": "^26.0.12", | ||
"@types/js-cookie": "^3.0.3", | ||
"install-peers": "^1.0.3", | ||
"jest": "^26.4.2", | ||
"@types/react": "^18.3.12", | ||
"@types/semver": "^7.5.8", | ||
"jest": "^29.7.0", | ||
"js-cookie": "^3.0.5", | ||
"mailslurp-client": "^8.7.8", | ||
"prettier": "^3.3.3", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"ts-jest": "^26.3.0", | ||
"typescript": "^4.5.4" | ||
} | ||
"ts-jest": "^29.2.5", | ||
"typescript": "^5.6.3" | ||
}, | ||
"packageManager": "yarn@4.5.1" | ||
} |
@@ -1,8 +0,13 @@ | ||
import axios, { AxiosError, AxiosRequestConfig, AxiosStatic } from "axios"; | ||
import { AuthAPI, Config, Tokens } from "./types"; | ||
import axios, { | ||
RawAxiosRequestHeaders, | ||
type AxiosError, | ||
type AxiosRequestConfig, | ||
type AxiosStatic, | ||
} from "axios"; | ||
import { config } from "./config"; | ||
import { credentialsRequiredError, UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import type { AuthAPI, Config, Tokens } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { credentialsRequiredError, UnexpectedAPIError } from "./errors"; | ||
@@ -52,5 +57,7 @@ export const auth: AuthAPI = ((): AuthAPI => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -81,14 +88,15 @@ | ||
return storedTokens; | ||
} else { | ||
throw response.data; | ||
} | ||
throw response.data; | ||
} catch (error) { | ||
_retryCounter = 0; | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
function applyInterceptors(instance?: AxiosStatic): void { | ||
if (!interceptorInstancesMap.get(instance)) { | ||
if (instance && !interceptorInstancesMap.get(instance)) { | ||
instance = instance || axios; //normalize fallback to default instance. | ||
@@ -110,3 +118,4 @@ //@ts-ignore: Fix is for axios? > https://github.com/axios/axios/issues/5573 | ||
function ejectInterceptors(instance: AxiosStatic): void { | ||
instance.interceptors.request.eject(interceptorInstancesMap.get(instance)); | ||
const id = interceptorInstancesMap.get(instance); | ||
if (id !== undefined) instance.interceptors.request.eject(id); | ||
interceptorInstancesMap.delete(instance); | ||
@@ -119,3 +128,3 @@ } | ||
const { token, initialisationToken } = tokens.get(); | ||
const headers = { ...requestConfig.headers }; | ||
const headers: RawAxiosRequestHeaders = { ...requestConfig.headers }; | ||
if (token) headers.Authorization = `Bearer ${token}`; | ||
@@ -130,4 +139,3 @@ if (initialisationToken) | ||
if ( | ||
error && | ||
error.response && | ||
error?.response && | ||
"status" in error.response && | ||
@@ -144,3 +152,3 @@ error.response.status === 401 && | ||
headers: { | ||
...error.config.headers, | ||
...error.config?.headers, | ||
Authorization: `Bearer ${token}`, | ||
@@ -157,3 +165,3 @@ }, | ||
_retryCounter = 0; | ||
if (error && error.response) throw error.response; | ||
if (axios.isAxiosError(error) && error?.response) throw error.response; | ||
throw error; | ||
@@ -160,0 +168,0 @@ } |
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import type { AuthConfig, AuthConfigServer, Config, ConfigAPI } from "./types"; | ||
import { defaultConfig } from "./config/DefaultConfig"; | ||
import { defaultServerConfig } from "./config/DefaultServerConfig"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { AuthConfig, AuthConfigServer, Config, ConfigAPI } from "./types"; | ||
import { validateConfig, fullUrl } from "./utils/config.utils"; | ||
import { fullUrl, validateConfig } from "./utils/config.utils"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
export const config: ConfigAPI = ((): ConfigAPI => { | ||
const _config: Config = defaultConfig; | ||
const _config: Config = { ...defaultConfig }; | ||
const _cachedServerconfig: AuthConfig = defaultServerConfig; | ||
function set(config: Config): void { | ||
const validatedConfig = validateConfig(config); | ||
function set(config: Partial<Config>): void { | ||
const validatedConfig = validateConfig({ ..._config, ...config }); | ||
// persist config in closure variable | ||
Object.keys(validatedConfig).forEach((value, key) => { | ||
_config[value] = validatedConfig[value]; | ||
}); | ||
Object.assign(_config, validatedConfig); | ||
} | ||
@@ -40,14 +38,7 @@ | ||
if (response.status === 200) { | ||
const copyObject = { ...response.data }; | ||
Object.keys(copyObject).forEach((value, key) => { | ||
if (value !== "metadata_configuration") { | ||
_cachedServerconfig[value] = copyObject[value]; | ||
} else { | ||
_cachedServerconfig[value] = | ||
copyObject[value] && copyObject[value].substring(0, 1) === "[" | ||
? JSON.parse(copyObject[value]) | ||
: []; | ||
} | ||
}); | ||
const { metadata_configuration, ...copyObject } = { ...response.data }; | ||
Object.assign(_cachedServerconfig, copyObject); | ||
_cachedServerconfig["metadata_configuration"] = JSON.parse( | ||
metadata_configuration | ||
); | ||
return _cachedServerconfig; | ||
@@ -66,5 +57,8 @@ } else if (response.status === 401) { | ||
return _cachedServerconfig; | ||
} else if (error.response) handleAxiosError(error.response); | ||
} else if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -76,3 +70,8 @@ | ||
// remove ready only && toSerialize props | ||
const { auth_keypair_public, version, metadata_configuration, ...requestBody } = config; | ||
const { | ||
auth_keypair_public, | ||
version, | ||
metadata_configuration, | ||
...requestBody | ||
} = config; | ||
try { | ||
@@ -87,14 +86,19 @@ const url = `${fullUrl(_config)}/config`; | ||
if (response.status === 200) { | ||
Object.keys(requestBody).forEach((value) => { | ||
_cachedServerconfig[value] = requestBody[value]; | ||
}); | ||
Object.assign(_cachedServerconfig, requestBody); | ||
return {..._cachedServerconfig, metadata_configuration}; | ||
} else { | ||
throw response.data; | ||
return { | ||
..._cachedServerconfig, | ||
metadata_configuration: metadata_configuration | ||
? metadata_configuration | ||
: [], | ||
}; | ||
} | ||
throw response.data; | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error?.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -101,0 +105,0 @@ |
@@ -0,9 +1,10 @@ | ||
import MailSlurp from "mailslurp-client"; | ||
import { Inbox } from "mailslurp-client/dist/generated"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { email } from "./email"; | ||
import { users } from "./users"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { Inbox } from "mailslurp-client/dist/generated"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
@@ -38,19 +39,21 @@ describe("EMail", () => { | ||
}); | ||
const result = await email.request(inbox.emailAddress); | ||
const result = await email.request(inbox.emailAddress || ""); | ||
const getToken = async (inboxId) => { | ||
const getToken = async (inboxId: string) => { | ||
if (inbox) { | ||
const emails = await mailslurp.getEmails(inboxId, { minCount: 1 }); | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || "-1"); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody || ""); | ||
return matches.length >= 2 ? matches[1] : ""; | ||
return matches !== null && (matches?.length || 0) >= 2 | ||
? matches[1] | ||
: ""; | ||
} | ||
}; | ||
const confirmToken = await getToken(inbox.id); | ||
const confirmToken = await getToken(inbox.id || ""); | ||
expect(confirmToken).toMatch(/.{50,}/); | ||
const confirmResult = await email.confirm(confirmToken); | ||
const confirmResult = await email.confirm(confirmToken || ""); | ||
if (confirmResult) { | ||
@@ -61,3 +64,6 @@ expect(confirmResult).toMatchObject({ | ||
}); | ||
const result = await auth.login(inbox.emailAddress, "newusers3cr3t!"); | ||
const result = await auth.login( | ||
inbox.emailAddress || "", | ||
"newusers3cr3t!" | ||
); | ||
if (result) { | ||
@@ -64,0 +70,0 @@ expect(result).toMatchObject({ |
@@ -1,7 +0,8 @@ | ||
import { EMailAPI, Tokens } from "./types"; | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios from "axios"; | ||
import { EMailAPI, Tokens } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -20,3 +21,4 @@ export const email: EMailAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -40,5 +42,8 @@ } | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -45,0 +50,0 @@ |
@@ -8,3 +8,3 @@ import { createAPIError } from "./utils/error.utils"; | ||
export const credentialsRequiredError = (args) => | ||
export const credentialsRequiredError = (args: string[]) => | ||
createAPIError( | ||
@@ -11,0 +11,0 @@ "credentials_required", |
@@ -17,2 +17,3 @@ import { useEffect, useState } from "react"; | ||
setHookState({ loading: false, authenticated: result !== false }); | ||
return tokens.token; | ||
})(); | ||
@@ -19,0 +20,0 @@ }, [tokens.token]); |
@@ -1,6 +0,7 @@ | ||
import { APIError, MetadataAPI } from "./types"; | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { MetadataAPI } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -13,5 +14,8 @@ export const metadata: MetadataAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -24,5 +28,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -36,3 +42,4 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -39,0 +46,0 @@ } |
@@ -0,9 +1,10 @@ | ||
import MailSlurp from "mailslurp-client"; | ||
import { Inbox } from "mailslurp-client/dist/generated"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { password } from "./password"; | ||
import { users } from "./users"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { Inbox } from "mailslurp-client/dist/generated"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
@@ -38,19 +39,19 @@ describe("Password", () => { | ||
}); | ||
const result = await password.request(inbox.emailAddress); | ||
const getToken = async (inboxId) => { | ||
await password.request(inbox.emailAddress || ""); | ||
const getToken = async (inboxId: string) => { | ||
if (inbox) { | ||
const emails = await mailslurp.getEmails(inboxId, { minCount: 1 }); | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || ""); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody || ""); | ||
return matches.length >= 2 ? matches[1] : ""; | ||
return matches !== null && matches.length >= 2 ? matches[1] : ""; | ||
} | ||
}; | ||
const confirmToken = await getToken(inbox.id); | ||
const confirmToken = await getToken(inbox.id || ""); | ||
expect(confirmToken).toMatch(/.{50,}/); | ||
const confirmResult = await password.confirm( | ||
confirmToken, | ||
confirmToken || "", | ||
"newusers3cr3t!" | ||
@@ -63,3 +64,6 @@ ); | ||
}); | ||
const result = await auth.login(inbox.emailAddress, "newusers3cr3t!"); | ||
const result = await auth.login( | ||
inbox.emailAddress || "", | ||
"newusers3cr3t!" | ||
); | ||
if (result) { | ||
@@ -66,0 +70,0 @@ expect(result).toMatchObject({ |
import axios from "axios"; | ||
import { PasswordAPI, Tokens } from "./types"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import { PasswordAPI, Tokens } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -17,3 +18,4 @@ export const password: PasswordAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -31,3 +33,4 @@ } | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -50,5 +53,7 @@ } | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -55,0 +60,0 @@ |
@@ -1,9 +0,9 @@ | ||
import { register } from "./register"; | ||
import { users } from "./users"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import MailSlurp from "mailslurp-client"; | ||
import { Inbox } from "mailslurp-client/dist/generated"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { mailSlurpAPIKey } from "./config/DefaultConfig"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { register } from "./register"; | ||
@@ -32,3 +32,6 @@ describe("Register", () => { | ||
inbox = await mailslurp.createInbox(); | ||
const result = await register.request(inbox.emailAddress, "SlurpS3cr3t!"); | ||
const result = await register.request( | ||
inbox.emailAddress || "", | ||
"SlurpS3cr3t!" | ||
); | ||
if (result) { | ||
@@ -39,16 +42,18 @@ expect(result).toMatchObject({ | ||
}); | ||
const getToken = async (inboxId) => { | ||
const getToken = async (inboxId: string) => { | ||
if (inbox) { | ||
const emails = await mailslurp.getEmails(inboxId, { minCount: 1 }); | ||
const firstMail = emails.shift(); | ||
const latestEmail = await mailslurp.getEmail(firstMail.id); | ||
const latestEmail = await mailslurp.getEmail(firstMail?.id || ""); | ||
const latestEmailBody = latestEmail.body; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec(latestEmailBody); | ||
return matches[1]; | ||
const matches = /\&token=(.*)\>here<\/a\>/.exec( | ||
latestEmailBody || "" | ||
); | ||
return (matches !== null && matches.length > 0 && matches[1]) || ""; | ||
} | ||
}; | ||
const registrationToken = await getToken(inbox.id); | ||
const registrationToken = await getToken(inbox.id || ""); | ||
expect(registrationToken).toMatch(/.{50,}/); | ||
const confirmResult = await register.confirm(registrationToken); | ||
const confirmResult = await register.confirm(registrationToken || ""); | ||
if (confirmResult) { | ||
@@ -55,0 +60,0 @@ expect(confirmResult).toMatchObject({ |
@@ -1,7 +0,8 @@ | ||
import { RegisterAPI, Tokens } from "./types"; | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import { RegisterAPI, Tokens } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -25,5 +26,7 @@ export const register: RegisterAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -45,5 +48,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -50,0 +55,0 @@ |
@@ -1,7 +0,8 @@ | ||
import { ResetAPI, Tokens } from "./types"; | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { tokens } from "./tokens"; | ||
import axios from "axios"; | ||
import { ResetAPI, Tokens } from "./types"; | ||
import { handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -16,5 +17,7 @@ export const reset: ResetAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -33,5 +36,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -38,0 +43,0 @@ |
@@ -1,6 +0,7 @@ | ||
import { Role, RoleAPI, RoleQuery, PagedResult } from "./types"; | ||
import axios from "axios"; | ||
import { config } from "./config"; | ||
import axios from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { PagedResult, Role, RoleAPI, RoleQuery } from "./types"; | ||
import { createAPIError, handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -15,3 +16,4 @@ export const roles: RoleAPI = (() => { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -24,12 +26,16 @@ } | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
async function queryRoles(query?: RoleQuery): Promise<PagedResult<Role>> { | ||
const { role, ...params } = query; | ||
const { role, ...rest } = query || { role: "", offset: 0, limit: 10 }; | ||
const params = new URLSearchParams(); | ||
//normalize role | ||
if (role && typeof role !== "string" && "name" in role) { | ||
params["name"] = role.name; | ||
params.set("name", role.name); | ||
} else if (role && typeof role === "string") { | ||
params["name"] = role; | ||
params.set("name", role); | ||
} | ||
if (rest.offset) params.set("offset", rest.offset.toString()); | ||
if (rest.limit) params.set("limit", rest.limit.toString()); | ||
const requestConfig = { params: params }; | ||
@@ -42,5 +48,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -55,5 +63,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -74,3 +84,4 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
@@ -77,0 +88,0 @@ } |
@@ -1,5 +0,6 @@ | ||
import { AuthConfig, TokenAPI, Tokens } from "./types"; | ||
import Cookies from "js-cookie"; | ||
import { config } from "./config"; | ||
import Cookies from "js-cookie"; | ||
import { defaultServerConfig } from "./config/DefaultServerConfig"; | ||
import { AuthConfig, TokenAPI, Tokens } from "./types"; | ||
@@ -21,5 +22,5 @@ const TOKEN_COOKIE: string = "Token"; | ||
let _token: string = ""; | ||
let _refreshTokenTimeout: NodeJS.Timeout; | ||
let _refreshTokenTimeout: NodeJS.Timeout; | ||
let _refreshToken: string = ""; | ||
let _initialisationTokenTimeout: NodeJS.Timeout; | ||
let _initialisationTokenTimeout: NodeJS.Timeout; | ||
let _initialisationToken: string = ""; | ||
@@ -50,3 +51,2 @@ | ||
initialisationExpireMilliseconds: number = 10, | ||
expireDate: Date = new Date(), | ||
refreshExpireDate: Date = new Date(), | ||
@@ -63,7 +63,2 @@ initialisationExpireDate: Date = new Date(); | ||
} | ||
expireDate = new Date( | ||
Date.now() + | ||
(expireMilliseconds = | ||
serverConfig.token_expiration_seconds_in_the_future_access * 1000) | ||
); | ||
refreshExpireDate = new Date( | ||
@@ -111,4 +106,4 @@ Date.now() + | ||
function booleanToString(bool: boolean): string { | ||
return bool ? "true" : "false"; | ||
function booleanToString(bool: boolean | undefined): string { | ||
return bool === true ? "true" : "false"; | ||
} | ||
@@ -115,0 +110,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { | ||
import type { | ||
AxiosResponseHeaders, | ||
@@ -124,3 +124,3 @@ AxiosStatic, | ||
set( | ||
headers?: AxiosResponseHeaders | Partial<RawAxiosResponseHeaders> | ||
headers?: AxiosResponseHeaders | Partial<RawAxiosResponseHeaders>, | ||
): Tokens; | ||
@@ -130,3 +130,3 @@ set( | ||
refreshToken: string, | ||
initialisationToken?: string | ||
initialisationToken?: string, | ||
): Tokens; | ||
@@ -133,0 +133,0 @@ get: () => Tokens; |
@@ -1,6 +0,5 @@ | ||
import { users } from "./users"; | ||
import { roles } from "./roles"; | ||
import { auth } from "./auth"; | ||
import { config } from "./config"; | ||
import { auth } from "./auth"; | ||
import { TestsServerConfig } from "./config/TestsServerConfig"; | ||
import { users } from "./users"; | ||
@@ -7,0 +6,0 @@ describe("Users", () => { |
@@ -1,6 +0,7 @@ | ||
import { User, UserAPI, UserQuery, PagedResult, Role } from "./types"; | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import { config } from "./config"; | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
import { PagedResult, Role, User, UserAPI, UserQuery } from "./types"; | ||
import { createAPIError, handleAxiosError } from "./utils/error.utils"; | ||
import { UnexpectedAPIError } from "./errors"; | ||
@@ -24,5 +25,7 @@ interface UserQueryParams extends Omit<UserQuery, "sort" | "enabled" | "role"> { | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -33,8 +36,5 @@ | ||
async function retrieve(idOrUser: Partial<User> | number): Promise<User> { | ||
if (idOrUser && typeof idOrUser !== "number") { | ||
idOrUser = idOrUser.id; | ||
} | ||
if (typeof idOrUser === "number") { | ||
async function doRetrieve(id: number) { | ||
try { | ||
const url = `${baseUrl()}/${idOrUser}`; | ||
const url = `${baseUrl()}/${id}`; | ||
const response = await axios.get(url); | ||
@@ -44,6 +44,14 @@ if (response.status === 200) return response.data; | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
} | ||
if (idOrUser && typeof idOrUser !== "number" && idOrUser.id) { | ||
return doRetrieve(idOrUser.id); | ||
} | ||
if (typeof idOrUser === "number") { | ||
return doRetrieve(idOrUser); | ||
} | ||
throw [ | ||
@@ -60,9 +68,9 @@ createAPIError( | ||
...query, | ||
role: query.role | ||
role: query?.role | ||
? query.role.map((role) => (isRole(role) ? role.name : role)) | ||
: undefined, | ||
sort: query.sort | ||
sort: query?.sort | ||
? query.sort.map((sorting) => `${sorting.field}|${sorting.direction}`) | ||
: undefined, | ||
enabled: query.enabled === "any" ? undefined : query.enabled, | ||
enabled: query?.enabled === "any" ? undefined : query?.enabled, | ||
}; | ||
@@ -91,5 +99,8 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -114,5 +125,7 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
@@ -123,8 +136,5 @@ | ||
async function remove(idOrUser: Partial<User> | number): Promise<void> { | ||
if (typeof idOrUser !== "number") { | ||
idOrUser = idOrUser.id; | ||
} | ||
if (typeof idOrUser === "number") { | ||
async function doRemove(id: number) { | ||
try { | ||
const url = `${baseUrl()}/${idOrUser}`; | ||
const url = `${baseUrl()}/${id}`; | ||
const response = await axios.delete(url); | ||
@@ -135,5 +145,14 @@ | ||
} catch (error) { | ||
if (error.response) handleAxiosError(error.response); | ||
if (axios.isAxiosError(error) && error.response) | ||
handleAxiosError(error.response); | ||
else throw UnexpectedAPIError; | ||
} | ||
throw UnexpectedAPIError; | ||
} | ||
if (typeof idOrUser !== "number" && idOrUser.id) { | ||
return doRemove(idOrUser.id); | ||
} | ||
if (typeof idOrUser === "number") { | ||
return doRemove(idOrUser); | ||
} else { | ||
@@ -140,0 +159,0 @@ throw [ |
import axios from "axios"; | ||
import { unSupportedAuthAPIVersion } from "../errors"; | ||
import { satisfies } from "semver"; | ||
import { API_COMPATIBLE_VERSION_RANGE, Config } from ".."; | ||
import { unSupportedAuthAPIVersion } from "../errors"; | ||
import { defaultConfig } from "../config/DefaultConfig"; | ||
import { pipe } from "../utils/fp.utils"; | ||
import { API_COMPATIBLE_VERSION_RANGE, Config } from ".."; | ||
export function validateConfig(config: Config) { | ||
export function validateConfig(config: Config): Config { | ||
return pipe(validatePropsConfig, validateCompatibility)(config); | ||
@@ -18,3 +18,3 @@ } | ||
if (config.url && config.url.substring(-1) === "/") { | ||
config.url = config.url.substr(0, -2); | ||
config.url = config.url.substring(0, -2); | ||
} | ||
@@ -56,3 +56,3 @@ config.secure = | ||
} | ||
export function fullUrl(config): string { | ||
export function fullUrl(config: Config): string { | ||
if (config.url !== "") { | ||
@@ -59,0 +59,0 @@ return `${config.secure ? `https://` : `http://`}${config.url}${ |
@@ -1,3 +0,3 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { APIError } from '../types/index'; | ||
import type { AxiosResponse } from "axios"; | ||
import type { APIError } from "../types"; | ||
@@ -8,3 +8,3 @@ export const parseServerError = (error: APIError): string => { | ||
replaceRegEx, | ||
(matched: string, digit: number) => error.arguments[digit] | ||
(_matched: string, digit: number) => error.arguments[digit] | ||
); | ||
@@ -16,3 +16,3 @@ }; | ||
message: string, | ||
args = [] | ||
args: string[] = [] | ||
): APIError { | ||
@@ -28,3 +28,3 @@ return { | ||
if (isAxiosResponse(error)) throw error.data; | ||
else throw error; | ||
throw error; | ||
} | ||
@@ -35,3 +35,3 @@ | ||
): error is AxiosResponse<APIError[]> { | ||
return Object.keys(error).includes('status'); | ||
return Object.keys(error).includes("status"); | ||
} | ||
@@ -44,11 +44,12 @@ | ||
return isAPIError(error[0]); | ||
} else return false; | ||
} | ||
return false; | ||
} | ||
export function isAPIError(error: APIError | unknown): error is APIError { | ||
const keys = Object.keys(error as {}); | ||
const keys = Object.keys(error as APIError); | ||
return ( | ||
keys.includes('key') && | ||
keys.includes('message') && | ||
keys.includes('arguments') | ||
keys.includes("key") && | ||
keys.includes("message") && | ||
keys.includes("arguments") | ||
); | ||
@@ -59,5 +60,5 @@ } | ||
return ( | ||
typeof error === 'object' && | ||
Object.getOwnPropertyNames(error).includes('message') | ||
typeof error === "object" && | ||
Object.getOwnPropertyNames(error).includes("message") | ||
); | ||
} |
@@ -1,3 +0,5 @@ | ||
export function pipe(...fns) { | ||
return (arg) => fns.reduce((prev, fn) => fn(prev), arg); | ||
export function pipe<R = any>(...fns: any[]): R { | ||
//@ts-ignore | ||
return (arg: any[]) => | ||
fns.reduce((prev, fn) => fn(prev), arg) as unknown as R; | ||
} |
@@ -12,2 +12,3 @@ { | ||
"jsx": "react", | ||
"skipLibCheck": true,//FIXME: this is NOT how it should be fixed! @ WOUT!!!!!!! | ||
"lib": ["esnext", "dom"] | ||
@@ -14,0 +15,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
576755
9
98
4249
13
- Removed@types/react@^18.2.18
- Removed@types/semver@^7.3.9
- Removedinstall@^0.13.0
- Removed@types/prop-types@15.7.14(transitive)
- Removed@types/react@18.3.18(transitive)
- Removed@types/semver@7.5.8(transitive)
- Removedcsstype@3.1.3(transitive)
- Removedinstall@0.13.0(transitive)
Updatedaxios@^1.7.7
Updatedreact-router-dom@^6.28.0