Skip to content

Commit

Permalink
feat: axios 1
Browse files Browse the repository at this point in the history
  • Loading branch information
panuhorsmalahti committed Feb 9, 2024
1 parent 12ed3d1 commit 805b6ea
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import { Component } from "@k8slens/extensions";
import axios from "axios";

// Use NodeJS HTTP adapter to get around CORS issues
axios.defaults.adapter = require("axios/lib/adapters/http");
// This is not neded if httpAdapter: true is used.
axios.defaults.adapter = axios.getAdapter("http");

const lensPlatformClient = new LensPlatformClient({
accessToken: "", // the access token for apis
getAccessToken: () => Promise.resolve("<token>"), // the callback to be called before every request, useful if the access token needs to be renew often.
keyCloakAddress: "", // keycloak address, e.g. "https://keycloak.k8slens.dev"
keycloakRealm: "", // the realm name, e.g. "lensCloud"
keycloakRealm: "", // the realm name, e.g. "lensCloud"
apiEndpointAddress: "", // api endpoint address, e.g. "https://api.k8slens.dev"
httpAdapter: false // Optional, defaults to false. If true, the axios HTTP adapter is used instead of xhr
logLevel: "debug" // Optional, defaults to 'silent'. Options are 'silent' | 'debug' | 'error'
Expand Down
34 changes: 23 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"node": ">=12 <=18"
},
"files": [
"dist/"
"dist/**/*"
],
"exports": {
".": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
}
},
"main": "./dist/cjs/index.js",
"main": "dist/cjs/index.js",
"scripts": {
"test": "npm run check:type && npm run test:unit && npm run test:e2e",
"test:unit": "jest",
Expand Down Expand Up @@ -75,7 +75,7 @@
"typescript": "5.0.4"
},
"dependencies": {
"axios": "^0.27.2",
"axios": "^1.6.7",
"jwt-decode": "^3.1.2",
"pino": "^7.5.0",
"zod": "^3.21.4"
Expand Down
36 changes: 34 additions & 2 deletions src/LensPlatformClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,38 @@ import { SSOService } from "./SSOService";
// LensPlatformClient supports using the http adapter if httpAdapter
// option is set to true
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const axiosHttpAdapter = require("axios/lib/adapters/http");
let axiosHttpAdapter: Function | undefined;

// lens-platform-sdk works in multiple environments, namely
// * Node: regular Node.js process and electron's Node.js
// * Browser: Regular browsers and Electron's Chromium environment
// For this reason we need to support different kind of requires
// In particular we might want to load the http adapter to execute
// requests in Node.
const multiEnvironmentRequire = (moduleName: string) => {
try {
return window.require(moduleName);
} catch (error) {
return require(moduleName);
}
};

/**
* Returns axios http adapter (used for Node.js requests).
* It requires the adapter if not loaded before.
* @returns
*/
const getAxiosHttpAdapter = () => {
try {
if (!axiosHttpAdapter) {
axiosHttpAdapter = multiEnvironmentRequire("axios").getAdapter("http");
}

return axiosHttpAdapter;
} catch (error) {
return undefined;
}
};

export interface LensPlatformClientOptions {
accessToken?: string;
Expand Down Expand Up @@ -274,7 +305,7 @@ class LensPlatformClient {

const requestOptions: RequestOptions = {
headers: requestHeaders,
...(httpAdapter ? { adapter: axiosHttpAdapter } : {}),
...(httpAdapter ? { adapter: getAxiosHttpAdapter() } : {}),
...(proxyConfigs ? { proxy: proxyConfigs } : {}),
...(httpAgent ? { httpAgent } : {}),
...(httpsAgent ? { httpsAgent } : {}),
Expand All @@ -283,6 +314,7 @@ class LensPlatformClient {
};

logger.debug(`request arguments ${JSON.stringify(requestOptions)}`);

const response = await (hasBody
? prop(url, requestBody, requestOptions)
: prop(url, requestOptions));
Expand Down

0 comments on commit 805b6ea

Please sign in to comment.