Skip to content

Commit 902129c

Browse files
authored
Merge pull request #79 from AddSearch/sc-11365/convert-library-to-typescript
[sc-11365] add types, setup linting
2 parents 0e2013a + b8ad7c2 commit 902129c

33 files changed

+2839
-1946
lines changed

.eslintrc

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@
44
"commonjs": true,
55
"es2021": true
66
},
7+
"parser": "@typescript-eslint/parser",
78
"parserOptions": {
8-
"ecmaVersion": "latest"
9+
"ecmaVersion": "latest",
10+
"sourceType": "module",
11+
"project": "./tsconfig.json"
912
},
10-
"extends": "eslint:recommended",
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:@typescript-eslint/recommended",
16+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
17+
"plugin:prettier/recommended"
18+
],
19+
"plugins": ["@typescript-eslint"],
1120
"rules": {
1221
"no-eval": ["error"],
1322
"no-trailing-spaces": "error",
14-
"no-irregular-whitespace": "off"
15-
}
23+
"no-irregular-whitespace": "off",
24+
"@typescript-eslint/no-unused-vars": ["warn"],
25+
"@typescript-eslint/explicit-function-return-type": "off"
26+
},
27+
"ignorePatterns": [
28+
"dist/",
29+
"node_modules/",
30+
"webpack.config.js",
31+
"test/"
32+
]
1633
}

index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

package-lock.json

Lines changed: 906 additions & 352 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "addsearch-js-client",
3-
"version": "0.9.0",
3+
"version": "1.0.0",
44
"description": "AddSearch API JavaScript client",
55
"repository": {
66
"type": "git",
@@ -28,41 +28,54 @@
2828
"url": "https://www.addsearch.com"
2929
}
3030
],
31-
"main": "index.js",
31+
"main": "dist/index.js",
3232
"jsdelivr": "./dist/addsearch-js-client.min.js",
33-
"types": "index.d.ts",
33+
"types": "dist/types/index.d.ts",
3434
"files": [
3535
"dist",
36-
"index.d.ts",
37-
"src"
36+
"index.d.ts"
3837
],
3938
"scripts": {
40-
"build": "npm run test && ./node_modules/webpack-cli/bin/cli.js",
39+
"build": "npm run test && webpack && tsc",
40+
"watch": "webpack --watch",
41+
"watch:tsc": "tsc --watch",
4142
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
42-
"test": "./node_modules/mocha/bin/mocha.js --recursive --require @babel/register",
43-
"watch": "./node_modules/webpack-cli/bin/cli.js --watch"
43+
"test": "mocha --recursive --require ts-node/register 'test/**/*.test.ts'",
44+
"lint": "eslint . --ext .ts,.js",
45+
"lint:fix": "eslint . --ext .ts,.js --fix",
46+
"lint:watch": "nodemon --exec 'npm run lint' --watch src --ext ts"
4447
},
4548
"dependencies": {
4649
"axios": "^1.7.2",
4750
"buffer": "^6.0.3",
4851
"es6-promise": "^4.2.8",
4952
"js-base64": "^3.6.0",
50-
"uuid": "^9.0.0"
53+
"uuid": "^11.0.3"
5154
},
5255
"devDependencies": {
5356
"@babel/cli": "^7.23.9",
5457
"@babel/core": "^7.23.9",
5558
"@babel/preset-env": "^7.23.9",
59+
"@babel/preset-typescript": "^7.26.0",
5660
"@babel/register": "^7.12.1",
61+
"@types/mocha": "^10.0.10",
62+
"@typescript-eslint/parser": "^8.18.1",
5763
"axios-mock-adapter": "^1.21.2",
5864
"babel-loader": "^9.1.2",
5965
"eslint": "^8.45.0",
66+
"eslint-config-prettier": "^9.1.0",
67+
"eslint-plugin-prettier": "^5.2.1",
6068
"eslint-webpack-plugin": "^3.1.1",
6169
"esm": "^3.2.25",
6270
"fetch-mock": "^12.1.0",
63-
"mocha": "^10.0.0",
71+
"mocha": "^10.8.2",
72+
"nodemon": "^3.1.9",
6473
"prettier": "^3.3.3",
6574
"terser-webpack-plugin": "^5.3.1",
75+
"ts-loader": "^9.5.1",
76+
"ts-node": "^10.9.2",
77+
"typescript": "^5.7.2",
78+
"typescript-eslint": "^8.18.1",
6679
"uglify-js": "^3.12.0",
6780
"webpack": "^5.76.1",
6881
"webpack-cli": "^4.10.0"

src/api.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/api.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
2+
3+
const apiInstance: AxiosInstance = axios.create();
4+
const statsInstance: AxiosInstance = axios.create();
5+
const conversationalSearchInteractionsInstance: AxiosInstance = axios.create();
6+
7+
const RESPONSE_BAD_REQUEST = 400;
8+
const RESPONSE_SERVER_ERROR = 500;
9+
10+
export type RequestInterceptorCallback = (config: {
11+
url?: string;
12+
headers?: Record<string, string>;
13+
}) => Partial<InternalAxiosRequestConfig>;
14+
15+
type RequestType = 'searchApi' | 'statsApi';
16+
17+
const setRequestInterceptor = (
18+
callback: RequestInterceptorCallback,
19+
requestType: RequestType
20+
): void => {
21+
const axiosInstance = requestType === 'searchApi' ? apiInstance : statsInstance;
22+
23+
axiosInstance.interceptors.request.use(
24+
(config: InternalAxiosRequestConfig) => {
25+
const updatedConfig = callback({
26+
url: config.url,
27+
headers: config.headers as Record<string, string>
28+
});
29+
config = { ...config, ...updatedConfig };
30+
return config;
31+
},
32+
(error) => {
33+
return Promise.reject(new Error(JSON.stringify(error)));
34+
}
35+
);
36+
};
37+
38+
export {
39+
apiInstance,
40+
statsInstance,
41+
conversationalSearchInteractionsInstance,
42+
setRequestInterceptor,
43+
RESPONSE_BAD_REQUEST,
44+
RESPONSE_SERVER_ERROR
45+
};

0 commit comments

Comments
 (0)