Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/node bundler v4 #892

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,437 changes: 1,820 additions & 1,617 deletions clientlibs/js/package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions clientlibs/js/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "upgrade_client_lib",
"version": "4.1.0",
"version": "4.1.6",
"description": "Client library to communicate with the Upgrade server",
"main": "dist/bundle.js",
"types": "dist/clientlibs/js/src/index.d.ts",
"files": [
"dist/*"
],
"scripts": {
"build": "npm run clean && webpack",
"build:bundler": "webpack",
"build:types": "./node_modules/.bin/dts-bundle-generator -o dist/browser/index.d.ts src/index.ts && ./node_modules/.bin/dts-bundle-generator -o dist/node/index.d.ts src/index.ts",
"build": "npm run clean && npm run build:bundler && npm run build:types",
"build:watch": "tsc -w",
"clean": "rm -rf dist",
"lint": "eslint -c ../../.eslintrc.js --ext .ts './{src, test}/**/*.ts' && npm run prettier:check",
Expand All @@ -25,6 +25,7 @@
"@types/jest": "^29.2.1",
"@types/node": "^18.11.9",
"@types/uuid": "^9.0.1",
"dts-bundle-generator": "^8.0.1",
"jest": "^29.2.2",
"ts-jest": "^29.0.3",
"ts-loader": "^9.4.1",
Expand All @@ -35,8 +36,8 @@
"webpack-cli": "^4.10.0"
},
"dependencies": {
"axios": "^1.4.0",
"es6-promise": "^4.2.8",
"isomorphic-fetch": "^3.0.0",
"uuid": "^8.3.2"
}
}
48 changes: 35 additions & 13 deletions clientlibs/js/src/common/fetchDataService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Interfaces, Types } from '../identifiers';
import * as fetch from 'isomorphic-fetch';
import axios, { AxiosRequestConfig } from 'axios';
import * as uuid from 'uuid';

// Call this function with url and data which is used in body of request
Expand Down Expand Up @@ -44,28 +44,45 @@ async function fetchData(
? (headers = { ...headers, 'Client-source': 'Browser' })
: (headers = { ...headers, 'Client-source': 'Node' });

let options: Interfaces.IRequestOptions = {
let options: AxiosRequestConfig = {
headers,
method: requestType,
keepalive: sendAsAnalytics,
};

if (
typeof window === 'undefined' &&
typeof process !== 'undefined' &&
process.release &&
process.release.name === 'node'
) {
if (sendAsAnalytics) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const http = require('http');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const https = require('https');
options.httpAgent = new http.Agent({ keepAlive: true });
options.httpsAgent = new https.Agent({ keepAlive: true });
}
}

if (requestType === Types.REQUEST_TYPES.POST || requestType === Types.REQUEST_TYPES.PATCH) {
options = {
...options,
body: JSON.stringify(data),
data,
};
}

const response = await fetch(url, options as RequestInit);
const responseData = await response.json();
// If value of ok is false then it's error
if (response.ok) {
return {
status: true,
data: responseData,
};
} else {
const response = await axios({
url,
...options,
}).then((res) => {
return res;
});

const responseData = response.data;
const statusCode = response.status;

if (statusCode > 400 && statusCode < 500) {
// If response status code is in the skipRetryOnStatusCodes, don't attempt retry
if (skipRetryOnStatusCodes.includes(response.status)) {
return {
Expand Down Expand Up @@ -94,6 +111,11 @@ async function fetchData(
message: responseData,
};
}
} else {
return {
status: true,
data: responseData,
};
}
} catch (error) {
return {
Expand Down
2 changes: 1 addition & 1 deletion clientlibs/js/src/functions/setAltUserIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function setAltUserIds(
token,
clientSessionId,
data,
Types.REQUEST_TYPES.POST,
Types.REQUEST_TYPES.PATCH,
false,
skipRetryOnStatusCodes
);
Expand Down
2 changes: 1 addition & 1 deletion clientlibs/js/src/functions/setGroupMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function setGroupMembership(
token,
clientSessionId,
{ id: userId, group: group },
Types.REQUEST_TYPES.POST
Types.REQUEST_TYPES.PATCH
);
if (response.status) {
return response.data;
Expand Down
2 changes: 1 addition & 1 deletion clientlibs/js/src/functions/setWorkingGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function setWorkingGroup(
token,
clientSessionId,
{ id: userId, workingGroup: workingGroup },
Types.REQUEST_TYPES.POST
Types.REQUEST_TYPES.PATCH
);
if (response.status) {
return response.data;
Expand Down
3 changes: 2 additions & 1 deletion clientlibs/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as UpgradeClient } from './UpgradeClient';
import UpgradeClient from './UpgradeClient';
export default UpgradeClient;
43 changes: 29 additions & 14 deletions clientlibs/js/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
const path = require("path");

module.exports = {
mode: "production",
entry: "./src/index.ts",
const generalConfiguration = {
mode: 'production',
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: [
/node_modules/,
/\.spec.ts$/
],
use: 'ts-loader',
exclude: [/node_modules/, /\.spec.ts$/],
},
],
},
resolve: {
alias: {
upgrade_types: path.resolve(__dirname, "../../types/src"),
upgrade_types: path.resolve(__dirname, '../../types/src'),
},
extensions: [".tsx", ".ts", ".js"],
extensions: ['.tsx', '.ts', '.js'],
},
};

const browser = {
...generalConfiguration,
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
libraryTarget: "umd",
library: "upgrade-client-lib",
filename: 'index.js',
path: path.resolve(__dirname, 'dist/browser'),
libraryTarget: 'umd',
library: 'upgrade-client-lib',
libraryExport: 'default',
},
};

const node = {
...generalConfiguration,
target: 'node',
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist/node'),
libraryTarget: 'umd',
library: 'upgrade-client-lib',
},
};

module.exports = [browser, node];