@ibertran/fortytwo-intra-client 2.0.0 → 3.0.0
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.
- package/dist/HttpError.d.ts +0 -8
- package/dist/HttpError.js +21 -23
- package/dist/index.d.ts +18 -11
- package/dist/index.js +120 -95
- package/dist/lib/logs.d.ts +4 -0
- package/dist/lib/logs.js +57 -0
- package/dist/lib/pagination.d.ts +1 -0
- package/dist/lib/pagination.js +29 -0
- package/dist/log.d.ts +4 -0
- package/dist/log.js +51 -0
- package/dist/types.d.ts +9 -3
- package/dist/utils.d.ts +0 -3
- package/dist/utils.js +0 -50
- package/package.json +7 -8
package/dist/HttpError.d.ts
CHANGED
|
@@ -1,9 +1 @@
|
|
|
1
|
-
declare class HttpError extends Error {
|
|
2
|
-
status: number | null;
|
|
3
|
-
url: string | null;
|
|
4
|
-
details: any;
|
|
5
|
-
constructor(err: any);
|
|
6
|
-
}
|
|
7
|
-
export declare function simplifySuperagentError(err: any): HttpError;
|
|
8
|
-
export declare function isSuperAgentError(err: any): boolean;
|
|
9
1
|
export {};
|
package/dist/HttpError.js
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return (err && typeof err === "object" && ("status" in err || "response" in err));
|
|
25
|
-
}
|
|
3
|
+
// class HttpError extends Error {
|
|
4
|
+
// status: number | null;
|
|
5
|
+
// url: string | null;
|
|
6
|
+
// details: any;
|
|
7
|
+
// constructor(err: any) {
|
|
8
|
+
// super(err.message || "Unknown API error");
|
|
9
|
+
// this.name = "HttpError";
|
|
10
|
+
// this.status = err.status || err.response?.status || null;
|
|
11
|
+
// this.url = err.config?.url || err.request?.responseURL || null;
|
|
12
|
+
// this.details = err.response?.data || null;
|
|
13
|
+
// }
|
|
14
|
+
// }
|
|
15
|
+
// export function simplifyAxiosError(err: any): HttpError {
|
|
16
|
+
// if (!err) {
|
|
17
|
+
// return new HttpError({ message: "Unknown error" });
|
|
18
|
+
// }
|
|
19
|
+
// return new HttpError(err);
|
|
20
|
+
// }
|
|
21
|
+
// export function isAxiosError(err: any): err is AxiosError {
|
|
22
|
+
// return err && err.isAxiosError === true;
|
|
23
|
+
// }
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inputOptions
|
|
1
|
+
import { inputOptions } from "./types";
|
|
2
2
|
export interface FortytwoIntraClientConf {
|
|
3
3
|
redirect_uri: string | null;
|
|
4
4
|
base_url: string;
|
|
@@ -6,10 +6,12 @@ export interface FortytwoIntraClientConf {
|
|
|
6
6
|
oauth_url: string;
|
|
7
7
|
token_info_url: string;
|
|
8
8
|
scopes: string[];
|
|
9
|
-
|
|
9
|
+
rateLimitMaxRequests: number;
|
|
10
|
+
rateLimitPerMilliseconds: number;
|
|
10
11
|
maxRetry: number;
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
logLine: boolean;
|
|
13
|
+
errLogBody: boolean;
|
|
14
|
+
throwOnError: boolean;
|
|
13
15
|
}
|
|
14
16
|
export declare class FortytwoIntraClient {
|
|
15
17
|
private client_id;
|
|
@@ -20,10 +22,14 @@ export declare class FortytwoIntraClient {
|
|
|
20
22
|
private oauth_url;
|
|
21
23
|
private token_info_url;
|
|
22
24
|
private scopes;
|
|
23
|
-
private
|
|
25
|
+
private rateLimitMaxRequests;
|
|
26
|
+
private rateLimitPerMilliseconds;
|
|
27
|
+
private axiosInstance;
|
|
24
28
|
private retryOn;
|
|
25
29
|
private maxRetry;
|
|
26
|
-
private
|
|
30
|
+
private logLine;
|
|
31
|
+
private errLogBody;
|
|
32
|
+
private throwOnError;
|
|
27
33
|
private access_token;
|
|
28
34
|
constructor(client_id: string, client_secret: string, conf: Partial<FortytwoIntraClientConf>);
|
|
29
35
|
private generateToken;
|
|
@@ -31,11 +37,12 @@ export declare class FortytwoIntraClient {
|
|
|
31
37
|
private reqHandler;
|
|
32
38
|
private logSuccess;
|
|
33
39
|
private logError;
|
|
34
|
-
get(endpoint: URL | string, options?: Omit<inputOptions, "body">): Promise<any>;
|
|
35
|
-
post(endpoint: URL | string, options?: inputOptions): Promise<any>;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
get(endpoint: URL | string, options?: Omit<inputOptions, "body" | "perPage" | "maxPage">): Promise<any>;
|
|
41
|
+
post(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPage">): Promise<any>;
|
|
42
|
+
put(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPage">): Promise<any>;
|
|
43
|
+
patch(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPage">): Promise<any>;
|
|
44
|
+
delete(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPage">): Promise<any>;
|
|
45
|
+
getAll(endpoint: URL | string, options?: Omit<inputOptions, "body">): Promise<any>;
|
|
39
46
|
URL(endpoint: string): URL;
|
|
40
47
|
getOAuthUrl(redirect_uri?: string | null): string;
|
|
41
48
|
exchangeOAuthCode(code: string, redirect_uri?: string): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
4
|
};
|
|
38
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
6
|
exports.FortytwoIntraClient = void 0;
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
const HttpError_1 = require("./HttpError");
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const axios_rate_limit_1 = __importDefault(require("axios-rate-limit"));
|
|
9
|
+
const pagination_1 = require("./lib/pagination");
|
|
10
|
+
const logs_1 = require("./lib/logs");
|
|
45
11
|
const defaultConf = {
|
|
46
12
|
redirect_uri: null,
|
|
47
13
|
base_url: "https://api.intra.42.fr/v2/",
|
|
@@ -49,10 +15,12 @@ const defaultConf = {
|
|
|
49
15
|
oauth_url: "https://api.intra.42.fr/oauth/authorize",
|
|
50
16
|
token_info_url: "https://api.intra.42.fr/oauth/token/info",
|
|
51
17
|
scopes: ["public"],
|
|
52
|
-
|
|
18
|
+
rateLimitMaxRequests: 2,
|
|
19
|
+
rateLimitPerMilliseconds: 1200,
|
|
53
20
|
maxRetry: 5,
|
|
54
|
-
|
|
55
|
-
|
|
21
|
+
logLine: true,
|
|
22
|
+
errLogBody: true,
|
|
23
|
+
throwOnError: true,
|
|
56
24
|
};
|
|
57
25
|
class FortytwoIntraClient {
|
|
58
26
|
client_id;
|
|
@@ -63,10 +31,14 @@ class FortytwoIntraClient {
|
|
|
63
31
|
oauth_url;
|
|
64
32
|
token_info_url;
|
|
65
33
|
scopes;
|
|
66
|
-
|
|
34
|
+
rateLimitMaxRequests;
|
|
35
|
+
rateLimitPerMilliseconds;
|
|
36
|
+
axiosInstance;
|
|
67
37
|
retryOn;
|
|
68
38
|
maxRetry;
|
|
69
|
-
|
|
39
|
+
logLine;
|
|
40
|
+
errLogBody;
|
|
41
|
+
throwOnError;
|
|
70
42
|
access_token;
|
|
71
43
|
constructor(client_id, client_secret, conf) {
|
|
72
44
|
this.client_id = client_id;
|
|
@@ -78,28 +50,32 @@ class FortytwoIntraClient {
|
|
|
78
50
|
this.oauth_url = config.oauth_url;
|
|
79
51
|
this.token_info_url = config.token_info_url;
|
|
80
52
|
this.scopes = config.scopes;
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
53
|
+
this.rateLimitMaxRequests = config.rateLimitMaxRequests;
|
|
54
|
+
this.rateLimitPerMilliseconds = config.rateLimitPerMilliseconds;
|
|
55
|
+
// Create axios instance with rate limiting
|
|
56
|
+
const axiosInstance = axios_1.default.create();
|
|
57
|
+
this.axiosInstance = (0, axios_rate_limit_1.default)(axiosInstance, {
|
|
58
|
+
maxRequests: this.rateLimitMaxRequests,
|
|
59
|
+
perMilliseconds: this.rateLimitPerMilliseconds,
|
|
85
60
|
});
|
|
86
61
|
this.retryOn = [401, 429, 500];
|
|
87
62
|
this.maxRetry = config.maxRetry;
|
|
88
|
-
this.
|
|
63
|
+
this.logLine = config.logLine;
|
|
64
|
+
this.errLogBody = config.errLogBody;
|
|
65
|
+
this.throwOnError = config.throwOnError;
|
|
89
66
|
this.access_token = null;
|
|
90
67
|
}
|
|
91
68
|
async generateToken() {
|
|
92
|
-
const res = await
|
|
69
|
+
const res = await this.axiosInstance.post(this.token_url, {
|
|
93
70
|
grant_type: "client_credentials",
|
|
94
71
|
client_id: this.client_id,
|
|
95
72
|
client_secret: this.client_secret,
|
|
96
73
|
scope: this.scopes.join(" "),
|
|
97
74
|
});
|
|
98
|
-
return res.
|
|
75
|
+
return res.data.access_token;
|
|
99
76
|
}
|
|
100
77
|
async fetch(url, options) {
|
|
101
|
-
const { method, body } = options;
|
|
102
|
-
const req = (0, utils_1.initRequest)(method, url);
|
|
78
|
+
const { method, body, query } = options;
|
|
103
79
|
if (!options.token && !this.access_token) {
|
|
104
80
|
this.access_token = await this.generateToken();
|
|
105
81
|
}
|
|
@@ -107,16 +83,26 @@ class FortytwoIntraClient {
|
|
|
107
83
|
const accessToken = options.token
|
|
108
84
|
? options.token.access_token
|
|
109
85
|
: this.access_token;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
86
|
+
// Extract query parameters from URL and combine with options.query
|
|
87
|
+
const urlParams = {};
|
|
88
|
+
url.searchParams.forEach((value, key) => {
|
|
89
|
+
urlParams[key] = value;
|
|
90
|
+
});
|
|
91
|
+
// Combine URL params with query params (query params take precedence)
|
|
92
|
+
const combinedParams = { ...urlParams, ...query };
|
|
93
|
+
// Create clean URL without query parameters
|
|
94
|
+
const cleanUrl = new URL(url);
|
|
95
|
+
cleanUrl.search = '';
|
|
96
|
+
// Use the rate-limited axios instance
|
|
97
|
+
return this.axiosInstance.request({
|
|
98
|
+
method: method.toLowerCase(),
|
|
99
|
+
url: cleanUrl.toString(),
|
|
100
|
+
headers: {
|
|
101
|
+
Authorization: `Bearer ${accessToken}`,
|
|
102
|
+
},
|
|
103
|
+
params: combinedParams,
|
|
104
|
+
data: body ? body : undefined,
|
|
105
|
+
});
|
|
120
106
|
}
|
|
121
107
|
async reqHandler(url, options) {
|
|
122
108
|
try {
|
|
@@ -125,20 +111,20 @@ class FortytwoIntraClient {
|
|
|
125
111
|
return res;
|
|
126
112
|
}
|
|
127
113
|
catch (err) {
|
|
128
|
-
if (
|
|
114
|
+
if (err.isAxiosError) {
|
|
115
|
+
this.logError(err, options);
|
|
129
116
|
const { attempt, maxRetry } = options;
|
|
130
|
-
|
|
131
|
-
if (maxRetry > 0 &&
|
|
132
|
-
|
|
133
|
-
this.retryOn.includes(err.status)) {
|
|
134
|
-
if (err.status === 401) {
|
|
117
|
+
const status = err.response?.status;
|
|
118
|
+
if (maxRetry > 0 && attempt < maxRetry && status && this.retryOn.includes(status)) {
|
|
119
|
+
if (status === 401) {
|
|
135
120
|
this.access_token = null;
|
|
136
121
|
}
|
|
137
122
|
options.attempt++;
|
|
138
123
|
return this.reqHandler(url, options);
|
|
139
124
|
}
|
|
140
125
|
else {
|
|
141
|
-
|
|
126
|
+
if (this.throwOnError)
|
|
127
|
+
throw err;
|
|
142
128
|
}
|
|
143
129
|
}
|
|
144
130
|
else {
|
|
@@ -147,18 +133,23 @@ class FortytwoIntraClient {
|
|
|
147
133
|
}
|
|
148
134
|
}
|
|
149
135
|
logSuccess(res, options) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
console.log(msg);
|
|
136
|
+
if (!options.logLine)
|
|
137
|
+
return;
|
|
138
|
+
const line = (0, logs_1.getLogLine)(res, options);
|
|
139
|
+
console.log(line);
|
|
155
140
|
}
|
|
156
|
-
logError(err,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
|
|
141
|
+
logError(err, options) {
|
|
142
|
+
if (!options.logLine)
|
|
143
|
+
return;
|
|
144
|
+
const line = (0, logs_1.getErrorLogLine)(err, options);
|
|
145
|
+
const body = err.response?.data;
|
|
146
|
+
const logBody = options.errLogBody && body && Object.keys(body).length;
|
|
147
|
+
try {
|
|
148
|
+
console.log(line, logBody ? JSON.stringify(body, null, 2) : "");
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
console.log(line, logBody ? body : "");
|
|
152
|
+
}
|
|
162
153
|
}
|
|
163
154
|
// Public methods
|
|
164
155
|
async get(endpoint, options = {}) {
|
|
@@ -169,9 +160,11 @@ class FortytwoIntraClient {
|
|
|
169
160
|
method: "GET",
|
|
170
161
|
attempt: 0,
|
|
171
162
|
maxRetry: this.maxRetry,
|
|
163
|
+
logLine: this.logLine,
|
|
164
|
+
errLogBody: this.errLogBody,
|
|
172
165
|
...options,
|
|
173
166
|
});
|
|
174
|
-
return res
|
|
167
|
+
return res?.data;
|
|
175
168
|
}
|
|
176
169
|
async post(endpoint, options = {}) {
|
|
177
170
|
if (endpoint instanceof URL === false) {
|
|
@@ -181,11 +174,27 @@ class FortytwoIntraClient {
|
|
|
181
174
|
method: "POST",
|
|
182
175
|
attempt: 0,
|
|
183
176
|
maxRetry: this.maxRetry,
|
|
177
|
+
logLine: this.logLine,
|
|
178
|
+
errLogBody: this.errLogBody,
|
|
184
179
|
...options,
|
|
185
180
|
});
|
|
186
|
-
return res
|
|
181
|
+
return res?.data;
|
|
187
182
|
}
|
|
188
|
-
async
|
|
183
|
+
async put(endpoint, options = {}) {
|
|
184
|
+
if (endpoint instanceof URL === false) {
|
|
185
|
+
endpoint = new URL(endpoint, this.base_url);
|
|
186
|
+
}
|
|
187
|
+
const res = await this.reqHandler(endpoint, {
|
|
188
|
+
method: "PUT",
|
|
189
|
+
attempt: 0,
|
|
190
|
+
maxRetry: this.maxRetry,
|
|
191
|
+
logLine: this.logLine,
|
|
192
|
+
errLogBody: this.errLogBody,
|
|
193
|
+
...options,
|
|
194
|
+
});
|
|
195
|
+
return res?.data;
|
|
196
|
+
}
|
|
197
|
+
async patch(endpoint, options = {}) {
|
|
189
198
|
if (endpoint instanceof URL === false) {
|
|
190
199
|
endpoint = new URL(endpoint, this.base_url);
|
|
191
200
|
}
|
|
@@ -193,11 +202,13 @@ class FortytwoIntraClient {
|
|
|
193
202
|
method: "PATCH",
|
|
194
203
|
attempt: 0,
|
|
195
204
|
maxRetry: this.maxRetry,
|
|
205
|
+
logLine: this.logLine,
|
|
206
|
+
errLogBody: this.errLogBody,
|
|
196
207
|
...options,
|
|
197
208
|
});
|
|
198
|
-
return res
|
|
209
|
+
return res?.data;
|
|
199
210
|
}
|
|
200
|
-
async delete(endpoint, options) {
|
|
211
|
+
async delete(endpoint, options = {}) {
|
|
201
212
|
if (endpoint instanceof URL === false) {
|
|
202
213
|
endpoint = new URL(endpoint, this.base_url);
|
|
203
214
|
}
|
|
@@ -205,9 +216,11 @@ class FortytwoIntraClient {
|
|
|
205
216
|
method: "DELETE",
|
|
206
217
|
attempt: 0,
|
|
207
218
|
maxRetry: this.maxRetry,
|
|
219
|
+
logLine: this.logLine,
|
|
220
|
+
errLogBody: this.errLogBody,
|
|
208
221
|
...options,
|
|
209
222
|
});
|
|
210
|
-
return res
|
|
223
|
+
return res?.data;
|
|
211
224
|
}
|
|
212
225
|
async getAll(endpoint, options = {}) {
|
|
213
226
|
if (endpoint instanceof URL === false) {
|
|
@@ -218,34 +231,46 @@ class FortytwoIntraClient {
|
|
|
218
231
|
const initialRes = await this.reqHandler(url, {
|
|
219
232
|
method: "GET",
|
|
220
233
|
attempt: 0,
|
|
234
|
+
currpage: 1,
|
|
235
|
+
lastPage: Infinity,
|
|
221
236
|
maxRetry: this.maxRetry,
|
|
237
|
+
logLine: this.logLine,
|
|
238
|
+
errLogBody: this.errLogBody,
|
|
222
239
|
...options,
|
|
223
240
|
query: {
|
|
241
|
+
...options.query,
|
|
224
242
|
page: 1,
|
|
225
|
-
per_page:
|
|
226
|
-
}
|
|
243
|
+
per_page: perPage,
|
|
244
|
+
}
|
|
227
245
|
});
|
|
228
246
|
let lastPage;
|
|
229
247
|
try {
|
|
230
|
-
lastPage = (0,
|
|
248
|
+
lastPage = Math.min((0, pagination_1.getLastPage)(initialRes.headers["link"]), options.maxPages || Infinity);
|
|
231
249
|
}
|
|
232
250
|
catch (err) {
|
|
233
|
-
return initialRes
|
|
251
|
+
return initialRes?.data;
|
|
234
252
|
}
|
|
235
253
|
const promises = [];
|
|
236
254
|
for (let i = 2; i <= lastPage; i++) {
|
|
237
255
|
url = new URL(endpoint);
|
|
238
|
-
url.searchParams.append("per_page", perPage.toString());
|
|
239
|
-
url.searchParams.append("page", i.toString());
|
|
240
256
|
promises.push(this.reqHandler(url, {
|
|
241
257
|
method: "GET",
|
|
242
258
|
attempt: 0,
|
|
259
|
+
currpage: i,
|
|
260
|
+
lastPage: lastPage,
|
|
243
261
|
maxRetry: this.maxRetry,
|
|
262
|
+
logLine: this.logLine,
|
|
263
|
+
errLogBody: this.errLogBody,
|
|
244
264
|
...options,
|
|
265
|
+
query: {
|
|
266
|
+
...options.query,
|
|
267
|
+
page: i,
|
|
268
|
+
per_page: perPage,
|
|
269
|
+
}
|
|
245
270
|
}));
|
|
246
271
|
}
|
|
247
272
|
return Promise.all(promises).then((values) => {
|
|
248
|
-
return initialRes.
|
|
273
|
+
return initialRes?.data.concat(...values.map((value) => value.data));
|
|
249
274
|
});
|
|
250
275
|
}
|
|
251
276
|
URL(endpoint) {
|
|
@@ -264,14 +289,14 @@ class FortytwoIntraClient {
|
|
|
264
289
|
return url.toString();
|
|
265
290
|
}
|
|
266
291
|
async exchangeOAuthCode(code, redirect_uri) {
|
|
267
|
-
const res = await
|
|
292
|
+
const res = await this.axiosInstance.post(this.token_url, {
|
|
268
293
|
grant_type: "authorization_code",
|
|
269
294
|
client_id: this.client_id,
|
|
270
295
|
client_secret: this.client_secret,
|
|
271
296
|
redirect_uri: redirect_uri ? redirect_uri : this.redirect_uri,
|
|
272
297
|
code: code,
|
|
273
298
|
});
|
|
274
|
-
return res
|
|
299
|
+
return res?.data;
|
|
275
300
|
}
|
|
276
301
|
async tokenInfos() {
|
|
277
302
|
return this.get(this.token_info_url);
|
package/dist/lib/logs.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogLine = getLogLine;
|
|
4
|
+
exports.getErrorLogLine = getErrorLogLine;
|
|
5
|
+
const green = "\x1b[42m";
|
|
6
|
+
const red = "\x1b[41m";
|
|
7
|
+
const reset = "\x1b[0m";
|
|
8
|
+
function getLogLine(res, options) {
|
|
9
|
+
const tokens = [];
|
|
10
|
+
// Get query parameters as object
|
|
11
|
+
const queryParams = res.config?.params || {};
|
|
12
|
+
if (process.stdout.isTTY) {
|
|
13
|
+
tokens.push(`${green}${res.status}${reset}`);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
tokens.push(`${res.status}`);
|
|
17
|
+
}
|
|
18
|
+
tokens.push(options.method.padEnd(6, " "));
|
|
19
|
+
// Add the base URL path
|
|
20
|
+
if (res.config?.url) {
|
|
21
|
+
const url = new URL(res.config.url);
|
|
22
|
+
tokens.push(url.pathname);
|
|
23
|
+
}
|
|
24
|
+
// Add query parameters as object if they exist
|
|
25
|
+
console.log(queryParams);
|
|
26
|
+
if (Object.keys(queryParams).length > 0) {
|
|
27
|
+
delete queryParams.per_page;
|
|
28
|
+
delete queryParams.page;
|
|
29
|
+
tokens.push(JSON.stringify(queryParams));
|
|
30
|
+
}
|
|
31
|
+
if (options.currpage) {
|
|
32
|
+
tokens.push(`| page: ${options.currpage}/${options.lastPage === Infinity ? "unknown" : options.lastPage}`);
|
|
33
|
+
}
|
|
34
|
+
return tokens.join(" ");
|
|
35
|
+
}
|
|
36
|
+
function getErrorLogLine(err, options) {
|
|
37
|
+
const tokens = [];
|
|
38
|
+
// Get query parameters as object
|
|
39
|
+
const queryParams = err.config?.params || {};
|
|
40
|
+
if (process.stdout.isTTY) {
|
|
41
|
+
tokens.push(`${red}${err.status}${reset}`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
tokens.push(`${err.status}`);
|
|
45
|
+
}
|
|
46
|
+
tokens.push(options.method.padEnd(6, " "));
|
|
47
|
+
// Add the base URL path
|
|
48
|
+
if (err.config?.url) {
|
|
49
|
+
const url = new URL(err.config.url);
|
|
50
|
+
tokens.push(url.pathname);
|
|
51
|
+
}
|
|
52
|
+
// Add query parameters as object if they exist
|
|
53
|
+
if (Object.keys(queryParams).length > 0) {
|
|
54
|
+
tokens.push(JSON.stringify(queryParams));
|
|
55
|
+
}
|
|
56
|
+
return tokens.join(" ");
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getLastPage(header: string | undefined): number;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLastPage = getLastPage;
|
|
4
|
+
function getLastPage(header) {
|
|
5
|
+
if (!header) {
|
|
6
|
+
throw Error("No pagination link header");
|
|
7
|
+
}
|
|
8
|
+
const paginationlinks = header
|
|
9
|
+
.split(",")
|
|
10
|
+
.reduce((links, part) => {
|
|
11
|
+
const match = part.match(/<([^>]+)>\s*;\s*rel="([^"]+)"/);
|
|
12
|
+
if (match) {
|
|
13
|
+
const url = match[1];
|
|
14
|
+
const rel = match[2];
|
|
15
|
+
links[rel] = url;
|
|
16
|
+
}
|
|
17
|
+
return links;
|
|
18
|
+
}, {});
|
|
19
|
+
if (!paginationlinks.last) {
|
|
20
|
+
throw Error("Missing last page link");
|
|
21
|
+
}
|
|
22
|
+
const lastPageUrl = new URL(paginationlinks.last);
|
|
23
|
+
const rawLastPage = lastPageUrl.searchParams.get("page");
|
|
24
|
+
const lastPage = rawLastPage ? parseInt(rawLastPage, 10) : undefined;
|
|
25
|
+
if (!lastPage || isNaN(lastPage)) {
|
|
26
|
+
throw Error("Invalid last page link");
|
|
27
|
+
}
|
|
28
|
+
return lastPage;
|
|
29
|
+
}
|
package/dist/log.d.ts
ADDED
package/dist/log.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogLine = getLogLine;
|
|
4
|
+
exports.getErrorLogLine = getErrorLogLine;
|
|
5
|
+
const green = "\x1b[42m";
|
|
6
|
+
const red = "\x1b[41m";
|
|
7
|
+
const reset = "\x1b[0m";
|
|
8
|
+
function getLogLine(res, options) {
|
|
9
|
+
const tokens = [];
|
|
10
|
+
// Get query parameters as object
|
|
11
|
+
const queryParams = res.config?.params || {};
|
|
12
|
+
if (process.stdout.isTTY) {
|
|
13
|
+
tokens.push(`${green}${res.status}${reset}`);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
tokens.push(`${res.status}`);
|
|
17
|
+
}
|
|
18
|
+
tokens.push(options.method.padEnd(6, " "));
|
|
19
|
+
// Add the base URL path
|
|
20
|
+
if (res.config?.url) {
|
|
21
|
+
const url = new URL(res.config.url);
|
|
22
|
+
tokens.push(url.pathname);
|
|
23
|
+
}
|
|
24
|
+
// Add query parameters as object if they exist
|
|
25
|
+
if (Object.keys(queryParams).length > 0) {
|
|
26
|
+
tokens.push(JSON.stringify(queryParams));
|
|
27
|
+
}
|
|
28
|
+
return tokens.join(" ");
|
|
29
|
+
}
|
|
30
|
+
function getErrorLogLine(err, options) {
|
|
31
|
+
const tokens = [];
|
|
32
|
+
// Get query parameters as object
|
|
33
|
+
const queryParams = err.config?.params || {};
|
|
34
|
+
if (process.stdout.isTTY) {
|
|
35
|
+
tokens.push(`${red}${err.status}${reset}`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
tokens.push(`${err.status}`);
|
|
39
|
+
}
|
|
40
|
+
tokens.push(options.method.padEnd(6, " "));
|
|
41
|
+
// Add the base URL path
|
|
42
|
+
if (err.config?.url) {
|
|
43
|
+
const url = new URL(err.config.url);
|
|
44
|
+
tokens.push(url.pathname);
|
|
45
|
+
}
|
|
46
|
+
// Add query parameters as object if they exist
|
|
47
|
+
if (Object.keys(queryParams).length > 0) {
|
|
48
|
+
tokens.push(JSON.stringify(queryParams));
|
|
49
|
+
}
|
|
50
|
+
return tokens.join(" ");
|
|
51
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
export type Method = "GET" | "POST" | "PATCH" | "DELETE";
|
|
1
|
+
export type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
2
2
|
export type userToken = {
|
|
3
3
|
access_token: string;
|
|
4
4
|
};
|
|
5
5
|
export type reqOptions = {
|
|
6
6
|
method: Method;
|
|
7
|
-
maxRetry: number;
|
|
8
7
|
attempt: number;
|
|
8
|
+
currpage?: number;
|
|
9
|
+
lastPage?: number;
|
|
10
|
+
maxRetry: number;
|
|
11
|
+
perPage?: number;
|
|
12
|
+
maxPages?: number;
|
|
9
13
|
body?: any;
|
|
10
14
|
token?: userToken;
|
|
11
15
|
query?: querystring;
|
|
16
|
+
logLine?: boolean;
|
|
17
|
+
errLogBody?: boolean;
|
|
12
18
|
};
|
|
13
19
|
export type querystring = Record<string, string | number | boolean | Array<string | number | boolean>>;
|
|
14
20
|
export type perPage = {
|
|
15
21
|
perPage?: number;
|
|
16
22
|
};
|
|
17
|
-
type internal = "method" | "attempt";
|
|
23
|
+
type internal = "method" | "attempt" | "page";
|
|
18
24
|
type optional = "maxRetry";
|
|
19
25
|
export type inputOptions = Omit<reqOptions, internal | optional> & Partial<Pick<reqOptions, optional>>;
|
|
20
26
|
export {};
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,56 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.initRequest = initRequest;
|
|
37
3
|
exports.getLastPage = getLastPage;
|
|
38
|
-
const superagent = __importStar(require("superagent"));
|
|
39
|
-
function initRequest(method, url) {
|
|
40
|
-
const endpoint = url.toString();
|
|
41
|
-
switch (method) {
|
|
42
|
-
case "GET":
|
|
43
|
-
return superagent.get(endpoint);
|
|
44
|
-
case "POST":
|
|
45
|
-
return superagent.post(endpoint);
|
|
46
|
-
case "PATCH":
|
|
47
|
-
return superagent.patch(endpoint);
|
|
48
|
-
case "DELETE":
|
|
49
|
-
return superagent.delete(endpoint);
|
|
50
|
-
default:
|
|
51
|
-
throw Error("Invalid HTTP method");
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
4
|
function getLastPage(header) {
|
|
55
5
|
if (!header) {
|
|
56
6
|
throw Error("No pagination link header");
|
package/package.json
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ibertran/fortytwo-intra-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "tsc"
|
|
8
|
-
"prepare": "npm run build"
|
|
7
|
+
"build": "tsc"
|
|
9
8
|
},
|
|
10
9
|
"dependencies": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"axios": "^1.6.2",
|
|
11
|
+
"axios-rate-limit": "^1.3.0"
|
|
13
12
|
},
|
|
14
13
|
"devDependencies": {
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
14
|
+
"typescript": "^5.9.3",
|
|
15
|
+
"dotenv": "^16.6.1",
|
|
16
|
+
"ts-node": "^10.9.2"
|
|
18
17
|
},
|
|
19
18
|
"files": [
|
|
20
19
|
"dist",
|