lhisp-oauth-client 1.0.29 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,10 +17,10 @@ const lhisp_oauth_client_1 = require("../src/lhisp-oauth-client");
|
|
|
17
17
|
const lhisp_oauth_client_t_1 = require("../src/lhisp-oauth-client.t");
|
|
18
18
|
// Mock jest and set the type
|
|
19
19
|
jest.mock("axios");
|
|
20
|
-
const mockedAxios = axios_1.default;
|
|
20
|
+
const mockedAxios = jest.mocked(axios_1.default);
|
|
21
21
|
const apiUrl = "https://myapi.com";
|
|
22
22
|
const authUrl = "https://auth.myapi.com/oauth/token";
|
|
23
|
-
const clientId = "
|
|
23
|
+
const clientId = "testClientId";
|
|
24
24
|
const clientSecret = "testClientSecret";
|
|
25
25
|
const baseClientParams = { apiUrl, authUrl, clientId, clientSecret };
|
|
26
26
|
const basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`;
|
|
@@ -33,15 +33,15 @@ describe("Get Access Token", () => {
|
|
|
33
33
|
mockedAxios.request.mockReset();
|
|
34
34
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
35
35
|
});
|
|
36
|
-
it("
|
|
36
|
+
it("Should Get with Standard Config", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
37
|
const cli = getOauthClient();
|
|
38
38
|
yield accessTokenValidator(cli);
|
|
39
39
|
validateDefaultGetAccessToken();
|
|
40
40
|
}));
|
|
41
|
-
it("
|
|
41
|
+
it("Should Get with Custom Auth Header", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
42
|
const cli = getOauthClient(Object.assign(Object.assign({}, baseClientParams), { authHeaderName: "CustomAuthorizationHeader" }));
|
|
43
43
|
yield accessTokenValidator(cli);
|
|
44
|
-
expect(mockedAxios.request).
|
|
44
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
45
45
|
url: authUrl,
|
|
46
46
|
method: "POST",
|
|
47
47
|
headers: {
|
|
@@ -51,10 +51,10 @@ describe("Get Access Token", () => {
|
|
|
51
51
|
data: defaultGrantType,
|
|
52
52
|
}));
|
|
53
53
|
}));
|
|
54
|
-
it("
|
|
54
|
+
it("Should Get with Custom Grant Type", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
55
|
const cli = getOauthClient(Object.assign(Object.assign({}, baseClientParams), { grantType: "PermissaoCustom" }));
|
|
56
56
|
yield accessTokenValidator(cli);
|
|
57
|
-
expect(mockedAxios.request).
|
|
57
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
58
58
|
url: authUrl,
|
|
59
59
|
method: "POST",
|
|
60
60
|
headers: {
|
|
@@ -64,10 +64,10 @@ describe("Get Access Token", () => {
|
|
|
64
64
|
data: "grant_type=PermissaoCustom",
|
|
65
65
|
}));
|
|
66
66
|
}));
|
|
67
|
-
it("
|
|
67
|
+
it("Should Get with Custom Auth Scope", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
68
|
const cli = getOauthClient(Object.assign(Object.assign({}, baseClientParams), { authScope: "EscopoCustom" }));
|
|
69
69
|
yield accessTokenValidator(cli);
|
|
70
|
-
expect(mockedAxios.request).
|
|
70
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
71
71
|
url: authUrl,
|
|
72
72
|
method: "POST",
|
|
73
73
|
headers: {
|
|
@@ -77,10 +77,10 @@ describe("Get Access Token", () => {
|
|
|
77
77
|
data: `grant_type=${defaultGrantValue}&scope=EscopoCustom`,
|
|
78
78
|
}));
|
|
79
79
|
}));
|
|
80
|
-
it("
|
|
80
|
+
it("Should Get with Credentials on Request body", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
81
|
const cli = getOauthClient(Object.assign(Object.assign({}, baseClientParams), { authContentType: contentTypeApplicationJson, sendAuthCredentialsOnRequestBody: true }));
|
|
82
82
|
yield accessTokenValidator(cli);
|
|
83
|
-
expect(mockedAxios.request).
|
|
83
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
84
84
|
url: authUrl,
|
|
85
85
|
method: "POST",
|
|
86
86
|
headers: {
|
|
@@ -101,7 +101,7 @@ describe("Request", () => {
|
|
|
101
101
|
const cli = getOauthClient();
|
|
102
102
|
const resp = yield cli.get({ path: "/my-test-url" });
|
|
103
103
|
validateDefaultGetAccessToken();
|
|
104
|
-
expect(mockedAxios.request).
|
|
104
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
105
105
|
url: `${apiUrl}/my-test-url`,
|
|
106
106
|
method: "GET",
|
|
107
107
|
headers: {
|
|
@@ -116,7 +116,7 @@ describe("Request", () => {
|
|
|
116
116
|
const cli = getOauthClient();
|
|
117
117
|
const resp = yield cli.get({ path: "/my-test-url", params: { id: 1 } });
|
|
118
118
|
validateDefaultGetAccessToken();
|
|
119
|
-
expect(mockedAxios.request).
|
|
119
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
120
120
|
url: `${apiUrl}/my-test-url`,
|
|
121
121
|
method: "GET",
|
|
122
122
|
headers: {
|
|
@@ -132,7 +132,7 @@ describe("Request", () => {
|
|
|
132
132
|
const cli = getOauthClient();
|
|
133
133
|
const resp = yield cli.post({ path: "/my-test-url-post", data: { id: 1, user: "test" } });
|
|
134
134
|
validateDefaultGetAccessToken();
|
|
135
|
-
expect(mockedAxios.request).
|
|
135
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
136
136
|
url: `${apiUrl}/my-test-url-post`,
|
|
137
137
|
method: "POST",
|
|
138
138
|
headers: {
|
|
@@ -151,7 +151,7 @@ describe("Request", () => {
|
|
|
151
151
|
contentType: lhisp_oauth_client_t_1.ContentType.APPLICATION_X_WWW_FORM_URLENCODED,
|
|
152
152
|
});
|
|
153
153
|
validateDefaultGetAccessToken();
|
|
154
|
-
expect(mockedAxios.request).
|
|
154
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
155
155
|
url: `${apiUrl}/my-test-url-post`,
|
|
156
156
|
method: "POST",
|
|
157
157
|
headers: {
|
|
@@ -170,7 +170,7 @@ describe("Request", () => {
|
|
|
170
170
|
contentType: lhisp_oauth_client_t_1.ContentType.APPLICATION_X_WWW_FORM_URLENCODED,
|
|
171
171
|
});
|
|
172
172
|
validateDefaultGetAccessToken();
|
|
173
|
-
expect(mockedAxios.request).
|
|
173
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
174
174
|
url: `${apiUrl}/my-test-url-post`,
|
|
175
175
|
method: "POST",
|
|
176
176
|
headers: {
|
|
@@ -183,7 +183,7 @@ describe("Request", () => {
|
|
|
183
183
|
}));
|
|
184
184
|
});
|
|
185
185
|
function validateDefaultGetAccessToken() {
|
|
186
|
-
expect(mockedAxios.request).
|
|
186
|
+
expect(mockedAxios.request).toHaveBeenCalledWith(expect.objectContaining({
|
|
187
187
|
url: authUrl,
|
|
188
188
|
method: "POST",
|
|
189
189
|
headers: {
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lhisp-oauth-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"main": "src/index",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
|
-
"repository":
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://git.lhprovedor.com.br/leandro/lhisp-oauth-client.git"
|
|
9
|
+
},
|
|
7
10
|
"author": "Leandro Costa <contato@leandrocosta.pro.br>",
|
|
8
11
|
"license": "MIT",
|
|
9
12
|
"scripts": {
|
|
@@ -12,14 +15,14 @@
|
|
|
12
15
|
"test:watch": "jest --watchAll"
|
|
13
16
|
},
|
|
14
17
|
"devDependencies": {
|
|
15
|
-
"@types/jest": "^
|
|
16
|
-
"@types/node": "^
|
|
17
|
-
"jest": "^
|
|
18
|
-
"ts-jest": "^29.
|
|
19
|
-
"typescript": "^5.
|
|
18
|
+
"@types/jest": "^30.0.0",
|
|
19
|
+
"@types/node": "^24.0.4",
|
|
20
|
+
"jest": "^30.0.3",
|
|
21
|
+
"ts-jest": "^29.4.0",
|
|
22
|
+
"typescript": "^5.8.3"
|
|
20
23
|
},
|
|
21
24
|
"dependencies": {
|
|
22
|
-
"axios": "^1.
|
|
23
|
-
"lhisp-logger": "^1.0.
|
|
25
|
+
"axios": "^1.10.0",
|
|
26
|
+
"lhisp-logger": "^1.0.16"
|
|
24
27
|
}
|
|
25
28
|
}
|
|
@@ -87,8 +87,8 @@ class LhispOauthClient {
|
|
|
87
87
|
return timeDiff < this.tokenExpiresIn - 10;
|
|
88
88
|
}
|
|
89
89
|
getAccessToken() {
|
|
90
|
-
var _a;
|
|
91
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
var _a;
|
|
92
92
|
try {
|
|
93
93
|
if (this.accessToken && this.isTokenValid()) {
|
|
94
94
|
return this.accessToken;
|
|
@@ -139,8 +139,8 @@ class LhispOauthClient {
|
|
|
139
139
|
return `${(_a = this.accessToken) === null || _a === void 0 ? void 0 : _a.token_type} ${(_b = this.accessToken) === null || _b === void 0 ? void 0 : _b.access_token}`;
|
|
140
140
|
}
|
|
141
141
|
executarRequest(_a) {
|
|
142
|
-
var { method, path, data, params, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON } = _a, opt = __rest(_a, ["method", "path", "data", "params", "contentType"]);
|
|
143
142
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
var { method, path, data, params, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON } = _a, opt = __rest(_a, ["method", "path", "data", "params", "contentType"]);
|
|
144
144
|
try {
|
|
145
145
|
yield this.getAccessToken();
|
|
146
146
|
const headers = Object.assign({ "Content-Type": contentType, [this.tokenHeaderName]: this.getAuthToken() }, (this.headers || {}));
|