@sassoftware/af-axios
Advanced tools
Comparing version 0.6.1 to 0.7.0
{ | ||
"name": "@sassoftware/af-axios", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "Minimal Axios HTTP client wrapper with SAS authentication support", | ||
@@ -21,2 +21,4 @@ "author": "SAS", | ||
"scripts": { | ||
"prepack": "git clean -d -f; npm ci; npm run build:release", | ||
"postpack": "git clean -d -f", | ||
"lint": "npx eslint src/*", | ||
@@ -30,5 +32,5 @@ "build:release": "tsc -p tsconfig.release.json && npx rollup -c", | ||
"peerDependencies": { | ||
"@azure/msal-browser": "^2.36.0", | ||
"@azure/msal-react": "^1.5.6", | ||
"axios": "0.26.x - 0.27.x" | ||
"@azure/msal-browser": "^3.3.0", | ||
"@azure/msal-react": "^2.0.5", | ||
"axios": "^1.5.1" | ||
}, | ||
@@ -38,4 +40,4 @@ "devDependencies": { | ||
"@types/react-dom": "^18.0.11", | ||
"@typescript-eslint/eslint-plugin": "^5.33.1", | ||
"@typescript-eslint/parser": "^5.33.1", | ||
"@typescript-eslint/eslint-plugin": "^6.9.0", | ||
"@typescript-eslint/parser": "^6.9.0", | ||
"eslint": "^8.22.0", | ||
@@ -45,10 +47,10 @@ "eslint-plugin-import": "^2.26.0", | ||
"madge": "^6.0.0", | ||
"prettier": "^2.7.1", | ||
"prettier": "^3.0.3", | ||
"react": "^18.0.2", | ||
"react-dom": "^18.0.2", | ||
"rollup": "^3.20.6", | ||
"rollup-plugin-dts": "^5.3.0", | ||
"rollup-plugin-esbuild": "^5.0.0", | ||
"rollup": "^4.1.4", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-esbuild": "^6.1.0", | ||
"typescript": "^5.0.4" | ||
} | ||
} |
import { AuthError, PublicClientApplication, RedirectRequest, SilentRequest } from '@azure/msal-browser'; | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
import { ReactNode } from 'react'; | ||
interface AfAuthProviderBaseProps { | ||
children: ReactNode; | ||
url: string; | ||
clientId: string; | ||
axiosConfig?: Omit<AxiosRequestConfig, 'baseURL'>; | ||
authenticationRequest?: RedirectRequest | SilentRequest; | ||
@@ -13,2 +11,7 @@ loadingState?: ReactNode; | ||
onAuthError?: (error: AuthError) => void; | ||
/** @deprecated provide a full axiosInstance instead of a config */ | ||
axiosConfig?: Omit<AxiosRequestConfig, 'baseURL'>; | ||
/** @deprecated provide a full axiosInstance with a configured url */ | ||
url: string; | ||
axiosInstance: AxiosInstance; | ||
} | ||
@@ -26,4 +29,4 @@ interface AfAuthProviderWithVariables extends AfAuthProviderBaseProps { | ||
type AfAuthProviderProps = AfAuthProviderWithVariables | AfAuthProviderWithMsal; | ||
declare const AfAuthProvider: ({ children, url, clientId, environment, tenantId, authenticationRequest, axiosConfig, loadingState, errorState, msalInstance, onAuthError, }: AfAuthProviderProps) => JSX.Element; | ||
declare const AfAuthProvider: ({ children, url, clientId, environment, tenantId, authenticationRequest, axiosConfig, loadingState, errorState, msalInstance, onAuthError, axiosInstance: customAxiosInstance, }: AfAuthProviderProps) => JSX.Element; | ||
export default AfAuthProvider; | ||
//# sourceMappingURL=AfAuthProvider.d.ts.map |
@@ -7,2 +7,4 @@ import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { MsalProvider } from '@azure/msal-react'; | ||
import axios from 'axios'; | ||
import { useMemo } from 'react'; | ||
import useMsalInstance from './msalInstance'; | ||
@@ -20,3 +22,6 @@ import MsalAuth from './Auth'; | ||
scopes: [`${clientId}/.default`], | ||
}, axiosConfig, loadingState, errorState, msalInstance, onAuthError, }) => { | ||
}, axiosConfig, loadingState, errorState, msalInstance, onAuthError, axiosInstance: customAxiosInstance, }) => { | ||
// this useMemo is here until we completely remove support for axiosConfig | ||
// when we will be able to just pass the given instance from props | ||
const axiosInstance = useMemo(() => customAxiosInstance || axios.create(Object.assign({ baseURL: url }, axiosConfig)), [customAxiosInstance, axiosConfig]); | ||
// Because of our props we can be sure that either a valid msalInstance | ||
@@ -26,3 +31,3 @@ // OR BOTH the environment and tenantId are provided so we can be sure | ||
const instance = useMsalInstance(clientId, environment, tenantId); | ||
return (_jsx(MsalProvider, Object.assign({ instance: msalInstance || instance }, { children: _jsx(MsalAuth, Object.assign({ axiosConfig: Object.assign({ baseURL: url }, axiosConfig), onAuthError: onAuthError || | ||
return (_jsx(MsalProvider, Object.assign({ instance: msalInstance || instance }, { children: _jsx(MsalAuth, Object.assign({ axiosInstance: axiosInstance, onAuthError: onAuthError || | ||
((err) => { | ||
@@ -29,0 +34,0 @@ console.error(err); |
@@ -1,2 +0,2 @@ | ||
import { type AxiosRequestConfig } from 'axios'; | ||
import { type AxiosInstance, type AxiosRequestConfig } from 'axios'; | ||
import { type ReactNode } from 'react'; | ||
@@ -12,6 +12,8 @@ import { useMsalAuthentication } from '@azure/msal-react'; | ||
errorElement?: ReactNode; | ||
/** @deprecated provide a custom axiosInstance instead */ | ||
axiosConfig?: AxiosRequestConfig; | ||
axiosInstance: AxiosInstance; | ||
} | ||
declare const Auth: ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, }: AuthContextProviderProps) => JSX.Element; | ||
declare const Auth: ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, axiosInstance: customAxiosInstance, }: AuthContextProviderProps) => JSX.Element; | ||
export default Auth; | ||
//# sourceMappingURL=Auth.d.ts.map |
@@ -20,6 +20,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import AxiosInstanceContext from './AxiosInstanceContext'; | ||
const Auth = ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, }) => { | ||
const Auth = ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, axiosInstance: customAxiosInstance, }) => { | ||
const { instance: msalInstance } = useMsal(); | ||
const { error } = useMsalAuthentication(InteractionType.Redirect, authenticationRequest, accountIdentifiers); | ||
const axiosInstance = useMemo(() => axios.create(axiosConfig), []); | ||
// useMemo should stay until we completely remove support for axiosConfig | ||
// when we will be able to just pass the given instance from props | ||
const axiosInstance = useMemo(() => customAxiosInstance || axios.create(axiosConfig), [customAxiosInstance, axiosConfig]); | ||
const isAuthenticated = useIsAuthenticated(); | ||
@@ -34,4 +36,2 @@ const context = useMsal(); | ||
const interceptor = axiosInstance.interceptors.request.use((req) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
(_a = req.headers) !== null && _a !== void 0 ? _a : (req.headers = {}); | ||
const account = msalInstance.getActiveAccount(); | ||
@@ -43,3 +43,3 @@ if (!account) { | ||
const response = yield msalInstance.acquireTokenSilent(Object.assign(Object.assign({}, authenticationRequest), { account })); | ||
req.headers['Authorization'] = `Bearer ${response.accessToken}`; | ||
req.headers.set('Authorization', `Bearer ${response.accessToken}`); | ||
} | ||
@@ -46,0 +46,0 @@ catch (err) { |
@@ -19,5 +19,7 @@ /* | ||
errorElement?: ReactNode; | ||
/** @deprecated provide a custom axiosInstance instead */ | ||
axiosConfig?: AxiosRequestConfig; | ||
axiosInstance: AxiosInstance; | ||
} | ||
declare const Auth: ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, }: AuthContextProviderProps) => JSX.Element; | ||
declare const Auth: ({ children, onAuthError, authenticationRequest, accountIdentifiers, loadingElement, errorElement, axiosConfig, axiosInstance: customAxiosInstance, }: AuthContextProviderProps) => JSX.Element; | ||
@@ -30,5 +32,3 @@ declare const useAxios: () => axios.AxiosInstance; | ||
children: ReactNode; | ||
url: string; | ||
clientId: string; | ||
axiosConfig?: Omit<AxiosRequestConfig, 'baseURL'>; | ||
authenticationRequest?: RedirectRequest | SilentRequest; | ||
@@ -38,2 +38,7 @@ loadingState?: ReactNode; | ||
onAuthError?: (error: AuthError) => void; | ||
/** @deprecated provide a full axiosInstance instead of a config */ | ||
axiosConfig?: Omit<AxiosRequestConfig, 'baseURL'>; | ||
/** @deprecated provide a full axiosInstance with a configured url */ | ||
url: string; | ||
axiosInstance: AxiosInstance; | ||
} | ||
@@ -51,4 +56,4 @@ interface AfAuthProviderWithVariables extends AfAuthProviderBaseProps { | ||
type AfAuthProviderProps = AfAuthProviderWithVariables | AfAuthProviderWithMsal; | ||
declare const AfAuthProvider: ({ children, url, clientId, environment, tenantId, authenticationRequest, axiosConfig, loadingState, errorState, msalInstance, onAuthError, }: AfAuthProviderProps) => JSX.Element; | ||
declare const AfAuthProvider: ({ children, url, clientId, environment, tenantId, authenticationRequest, axiosConfig, loadingState, errorState, msalInstance, onAuthError, axiosInstance: customAxiosInstance, }: AfAuthProviderProps) => JSX.Element; | ||
export { AfAuthProvider, AxiosInstanceContext, Auth as MsalAuth, useAxios }; |
@@ -15,41 +15,2 @@ /* | ||
var __defProp$1 = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols; | ||
var __hasOwnProp$1 = Object.prototype.hasOwnProperty; | ||
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues$1 = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp$1.call(b, prop)) | ||
__defNormalProp$1(a, prop, b[prop]); | ||
if (__getOwnPropSymbols$1) | ||
for (var prop of __getOwnPropSymbols$1(b)) { | ||
if (__propIsEnum$1.call(b, prop)) | ||
__defNormalProp$1(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
const Auth = ({ | ||
@@ -62,3 +23,4 @@ children, | ||
errorElement, | ||
axiosConfig | ||
axiosConfig, | ||
axiosInstance: customAxiosInstance | ||
}) => { | ||
@@ -71,3 +33,6 @@ const { instance: msalInstance } = msalReact.useMsal(); | ||
); | ||
const axiosInstance = react.useMemo(() => axios.create(axiosConfig), []); | ||
const axiosInstance = react.useMemo( | ||
() => customAxiosInstance || axios.create(axiosConfig), | ||
[customAxiosInstance, axiosConfig] | ||
); | ||
const isAuthenticated = msalReact.useIsAuthenticated(); | ||
@@ -77,9 +42,7 @@ const context = msalReact.useMsal(); | ||
if (error) { | ||
onAuthError == null ? void 0 : onAuthError(error); | ||
onAuthError?.(error); | ||
} | ||
}, [error, onAuthError]); | ||
react.useEffect(() => { | ||
const interceptor = axiosInstance.interceptors.request.use((req) => __async(void 0, null, function* () { | ||
var _a; | ||
(_a = req.headers) != null ? _a : req.headers = {}; | ||
const interceptor = axiosInstance.interceptors.request.use(async (req) => { | ||
const account = msalInstance.getActiveAccount(); | ||
@@ -92,11 +55,12 @@ if (!account) { | ||
try { | ||
const response = yield msalInstance.acquireTokenSilent(__spreadProps(__spreadValues$1({}, authenticationRequest), { | ||
const response = await msalInstance.acquireTokenSilent({ | ||
...authenticationRequest, | ||
account | ||
})); | ||
req.headers["Authorization"] = `Bearer ${response.accessToken}`; | ||
}); | ||
req.headers.set("Authorization", `Bearer ${response.accessToken}`); | ||
} catch (err) { | ||
yield msalInstance.acquireTokenRedirect(authenticationRequest); | ||
await msalInstance.acquireTokenRedirect(authenticationRequest); | ||
} | ||
return req; | ||
})); | ||
}); | ||
return () => { | ||
@@ -119,3 +83,2 @@ axiosInstance.interceptors.request.eject(interceptor); | ||
const msalInstance = react.useMemo(() => { | ||
var _a; | ||
const instance = new msalBrowser.PublicClientApplication({ | ||
@@ -129,5 +92,5 @@ auth: { | ||
instance.setActiveAccount( | ||
(_a = instance.getAllAccounts().find( | ||
instance.getAllAccounts().find( | ||
(account) => account.environment === environment && account.tenantId === tenantId | ||
)) != null ? _a : null | ||
) ?? null | ||
); | ||
@@ -147,18 +110,2 @@ } | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
const loadingWrapperStyle = { | ||
@@ -183,4 +130,9 @@ height: "100vh", | ||
msalInstance, | ||
onAuthError | ||
onAuthError, | ||
axiosInstance: customAxiosInstance | ||
}) => { | ||
const axiosInstance = react.useMemo( | ||
() => customAxiosInstance || axios.create({ baseURL: url, ...axiosConfig }), | ||
[customAxiosInstance, axiosConfig] | ||
); | ||
const instance = useMsalIntance(clientId, environment, tenantId); | ||
@@ -190,3 +142,3 @@ return /* @__PURE__ */ jsxRuntime.jsx(msalReact.MsalProvider, { instance: msalInstance || instance, children: /* @__PURE__ */ jsxRuntime.jsx( | ||
{ | ||
axiosConfig: __spreadValues({ baseURL: url }, axiosConfig), | ||
axiosInstance, | ||
onAuthError: onAuthError || ((err) => { | ||
@@ -193,0 +145,0 @@ console.error(err); |
@@ -12,2 +12,3 @@ # SAS® af-axios | ||
// Auth.js | ||
import axios from 'axios'; | ||
import { useNavigate } from 'react-router'; | ||
@@ -17,2 +18,5 @@ import { MsalAuth } from '@sassoftware/af-axios/react'; | ||
const clientId = 'myClientId' | ||
const axiosInstance = axios.create({ baseUrl: 'https://myServer.com' }) | ||
const Auth = ({ children }) => { | ||
@@ -25,3 +29,10 @@ const navigate = useNavigate(); | ||
<MsalProvider instance={msalInstance}> | ||
<MsalAuth>{children}</MsalAuth> | ||
<MsalAuth | ||
axiosInstance={axiosInstance} | ||
authenticationRequest={{ | ||
scopes: [`${clientId}/.default`], | ||
}} | ||
> | ||
{children} | ||
</MsalAuth> | ||
</MsalProvider> | ||
@@ -28,0 +39,0 @@ ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67272
125
720