@ibertran/fortytwo-intra-client 4.0.0 → 4.1.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/errors/validationError.d.ts +7 -0
- package/dist/errors/validationError.js +22 -0
- package/dist/index.d.ts +12 -11
- package/dist/index.js +29 -40
- package/dist/lib/errors.js +1 -3
- package/dist/lib/logs.js +1 -1
- package/dist/lib/pagination.js +1 -3
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ZodError } from "zod";
|
|
2
|
+
export declare class FortytwoIntraClientValidationError extends Error {
|
|
3
|
+
schemaErrors: ZodError;
|
|
4
|
+
data: unknown;
|
|
5
|
+
constructor(schemaErrors: ZodError, data: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare function isFortytwoIntraClientValidationError(error: unknown): error is FortytwoIntraClientValidationError;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FortytwoIntraClientValidationError = void 0;
|
|
4
|
+
exports.isFortytwoIntraClientValidationError = isFortytwoIntraClientValidationError;
|
|
5
|
+
class FortytwoIntraClientValidationError extends Error {
|
|
6
|
+
schemaErrors;
|
|
7
|
+
data;
|
|
8
|
+
constructor(schemaErrors, data) {
|
|
9
|
+
// const errorMessages = schemaErrors.errors
|
|
10
|
+
// .map((error) => `${error.path.join(".")} - ${error.message}`)
|
|
11
|
+
// .join("; ");
|
|
12
|
+
const message = `Schema validation failed: `;
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "FortytwoIntraClientValidationError";
|
|
15
|
+
this.schemaErrors = schemaErrors;
|
|
16
|
+
this.data = data;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.FortytwoIntraClientValidationError = FortytwoIntraClientValidationError;
|
|
20
|
+
function isFortytwoIntraClientValidationError(error) {
|
|
21
|
+
return error instanceof FortytwoIntraClientValidationError;
|
|
22
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -37,24 +37,25 @@ export declare class FortytwoIntraClient {
|
|
|
37
37
|
private reqHandler;
|
|
38
38
|
private logSuccess;
|
|
39
39
|
private logError;
|
|
40
|
-
|
|
41
|
-
get
|
|
40
|
+
private validate;
|
|
41
|
+
get(endpoint: URL | string, options?: Omit<inputOptions, "body" | "perPage" | "maxPages">): Promise<any>;
|
|
42
|
+
get<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "body" | "perPage" | "maxPages"> & {
|
|
42
43
|
schema: S;
|
|
43
44
|
}): Promise<z.infer<S>>;
|
|
44
|
-
post(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "
|
|
45
|
-
post<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "
|
|
45
|
+
post(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPages">): Promise<any>;
|
|
46
|
+
post<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "maxPages"> & {
|
|
46
47
|
schema: S;
|
|
47
48
|
}): Promise<z.infer<S>>;
|
|
48
|
-
put(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "
|
|
49
|
-
put<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "
|
|
49
|
+
put(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPages">): Promise<any>;
|
|
50
|
+
put<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "maxPages"> & {
|
|
50
51
|
schema: S;
|
|
51
52
|
}): Promise<z.infer<S>>;
|
|
52
|
-
patch(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "
|
|
53
|
-
patch<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "
|
|
53
|
+
patch(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPages">): Promise<any>;
|
|
54
|
+
patch<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "maxPages"> & {
|
|
54
55
|
schema: S;
|
|
55
56
|
}): Promise<z.infer<S>>;
|
|
56
|
-
delete(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "
|
|
57
|
-
delete<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "
|
|
57
|
+
delete(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPages">): Promise<any>;
|
|
58
|
+
delete<S extends z.ZodType>(endpoint: URL | string, options: Omit<inputOptions, "perPage" | "maxPages"> & {
|
|
58
59
|
schema: S;
|
|
59
60
|
}): Promise<z.infer<S>>;
|
|
60
61
|
getAll(endpoint: URL | string, options?: Omit<inputOptions, "body">): Promise<any>;
|
|
@@ -70,6 +71,6 @@ export declare class FortytwoIntraClient {
|
|
|
70
71
|
state: string | null;
|
|
71
72
|
};
|
|
72
73
|
exchangeOAuthCode(code: string, redirect_uri?: string): Promise<any>;
|
|
73
|
-
tokenInfos(options?: Omit<inputOptions, "body" | "perPage" | "
|
|
74
|
+
tokenInfos(options?: Omit<inputOptions, "body" | "perPage" | "maxPages">): Promise<any>;
|
|
74
75
|
}
|
|
75
76
|
export { FortytwoIntraClientError, isFortytwoIntraClientError };
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,8 @@ const logs_1 = require("./lib/logs");
|
|
|
44
44
|
const errors_1 = require("./lib/errors");
|
|
45
45
|
Object.defineProperty(exports, "FortytwoIntraClientError", { enumerable: true, get: function () { return errors_1.FortytwoIntraClientError; } });
|
|
46
46
|
Object.defineProperty(exports, "isFortytwoIntraClientError", { enumerable: true, get: function () { return errors_1.isFortytwoIntraClientError; } });
|
|
47
|
+
const zod_1 = require("zod");
|
|
48
|
+
const validationError_1 = require("./errors/validationError");
|
|
47
49
|
const defaultConf = {
|
|
48
50
|
redirect_uri: null,
|
|
49
51
|
base_url: "https://api.intra.42.fr/v2/",
|
|
@@ -113,9 +115,7 @@ class FortytwoIntraClient {
|
|
|
113
115
|
this.access_token = await this.generateToken();
|
|
114
116
|
}
|
|
115
117
|
// Attach access_token
|
|
116
|
-
const accessToken = options.token
|
|
117
|
-
? options.token.access_token
|
|
118
|
-
: this.access_token;
|
|
118
|
+
const accessToken = options.token ? options.token.access_token : this.access_token;
|
|
119
119
|
// Extract query parameters from URL and combine with options.query
|
|
120
120
|
const urlParams = {};
|
|
121
121
|
url.searchParams.forEach((value, key) => {
|
|
@@ -125,7 +125,7 @@ class FortytwoIntraClient {
|
|
|
125
125
|
const combinedParams = { ...urlParams, ...query };
|
|
126
126
|
// Create clean URL without query parameters
|
|
127
127
|
const cleanUrl = new URL(url);
|
|
128
|
-
cleanUrl.search =
|
|
128
|
+
cleanUrl.search = "";
|
|
129
129
|
// Use the rate-limited axios instance
|
|
130
130
|
return this.axiosInstance.request({
|
|
131
131
|
method: method.toLowerCase(),
|
|
@@ -183,6 +183,20 @@ class FortytwoIntraClient {
|
|
|
183
183
|
console.log(line, logBody ? body : "");
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
+
validate(data, schema) {
|
|
187
|
+
if (!schema) {
|
|
188
|
+
return data;
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
return schema.parse(data);
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
if (err instanceof zod_1.z.ZodError) {
|
|
195
|
+
throw new validationError_1.FortytwoIntraClientValidationError(err, data);
|
|
196
|
+
}
|
|
197
|
+
throw err;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
186
200
|
async get(endpoint, options = {}) {
|
|
187
201
|
if (endpoint instanceof URL === false) {
|
|
188
202
|
endpoint = new URL(endpoint, this.base_url);
|
|
@@ -195,10 +209,7 @@ class FortytwoIntraClient {
|
|
|
195
209
|
errLogBody: this.errLogBody,
|
|
196
210
|
...options,
|
|
197
211
|
});
|
|
198
|
-
|
|
199
|
-
return options.schema.parse(res.data);
|
|
200
|
-
}
|
|
201
|
-
return res.data;
|
|
212
|
+
return this.validate(res.data, options.schema);
|
|
202
213
|
}
|
|
203
214
|
async post(endpoint, options = {}) {
|
|
204
215
|
if (endpoint instanceof URL === false) {
|
|
@@ -212,10 +223,7 @@ class FortytwoIntraClient {
|
|
|
212
223
|
errLogBody: this.errLogBody,
|
|
213
224
|
...options,
|
|
214
225
|
});
|
|
215
|
-
|
|
216
|
-
return options.schema.parse(res.data);
|
|
217
|
-
}
|
|
218
|
-
return res.data;
|
|
226
|
+
return this.validate(res.data, options.schema);
|
|
219
227
|
}
|
|
220
228
|
async put(endpoint, options = {}) {
|
|
221
229
|
if (endpoint instanceof URL === false) {
|
|
@@ -229,10 +237,7 @@ class FortytwoIntraClient {
|
|
|
229
237
|
errLogBody: this.errLogBody,
|
|
230
238
|
...options,
|
|
231
239
|
});
|
|
232
|
-
|
|
233
|
-
return options.schema.parse(res.data);
|
|
234
|
-
}
|
|
235
|
-
return res.data;
|
|
240
|
+
return this.validate(res.data, options.schema);
|
|
236
241
|
}
|
|
237
242
|
async patch(endpoint, options = {}) {
|
|
238
243
|
if (endpoint instanceof URL === false) {
|
|
@@ -246,10 +251,7 @@ class FortytwoIntraClient {
|
|
|
246
251
|
errLogBody: this.errLogBody,
|
|
247
252
|
...options,
|
|
248
253
|
});
|
|
249
|
-
|
|
250
|
-
return options.schema.parse(res.data);
|
|
251
|
-
}
|
|
252
|
-
return res.data;
|
|
254
|
+
return this.validate(res.data, options.schema);
|
|
253
255
|
}
|
|
254
256
|
async delete(endpoint, options = {}) {
|
|
255
257
|
if (endpoint instanceof URL === false) {
|
|
@@ -263,10 +265,7 @@ class FortytwoIntraClient {
|
|
|
263
265
|
errLogBody: this.errLogBody,
|
|
264
266
|
...options,
|
|
265
267
|
});
|
|
266
|
-
|
|
267
|
-
return options.schema.parse(res.data);
|
|
268
|
-
}
|
|
269
|
-
return res.data;
|
|
268
|
+
return this.validate(res.data, options.schema);
|
|
270
269
|
}
|
|
271
270
|
async getAll(endpoint, options = {}) {
|
|
272
271
|
if (endpoint instanceof URL === false) {
|
|
@@ -287,26 +286,19 @@ class FortytwoIntraClient {
|
|
|
287
286
|
...options.query,
|
|
288
287
|
page: 1,
|
|
289
288
|
per_page: perPage,
|
|
290
|
-
}
|
|
289
|
+
},
|
|
291
290
|
});
|
|
292
291
|
if (!Array.isArray(firstPage.data)) {
|
|
293
|
-
|
|
294
|
-
return options.schema.parse(firstPage.data);
|
|
295
|
-
}
|
|
296
|
-
return firstPage.data;
|
|
292
|
+
return this.validate(firstPage.data, options.schema);
|
|
297
293
|
}
|
|
298
294
|
let lastPage;
|
|
299
295
|
try {
|
|
300
296
|
lastPage = Math.min((0, pagination_1.getLastPage)(firstPage.headers["link"]), options.maxPages || Infinity);
|
|
301
297
|
}
|
|
302
298
|
catch (err) {
|
|
303
|
-
|
|
304
|
-
return options.schema.parse(firstPage.data);
|
|
305
|
-
}
|
|
306
|
-
return firstPage.data;
|
|
299
|
+
return this.validate(firstPage.data, options.schema);
|
|
307
300
|
}
|
|
308
|
-
const promises = Array.from({ length: lastPage - 1 }, (_, i) => i + 2)
|
|
309
|
-
.map(pageNumber => this.reqHandler(url, {
|
|
301
|
+
const promises = Array.from({ length: lastPage - 1 }, (_, i) => i + 2).map((pageNumber) => this.reqHandler(url, {
|
|
310
302
|
method: "GET",
|
|
311
303
|
attempt: 0,
|
|
312
304
|
currpage: pageNumber,
|
|
@@ -319,14 +311,11 @@ class FortytwoIntraClient {
|
|
|
319
311
|
...options.query,
|
|
320
312
|
page: pageNumber,
|
|
321
313
|
per_page: perPage,
|
|
322
|
-
}
|
|
314
|
+
},
|
|
323
315
|
}));
|
|
324
316
|
const otherPages = await Promise.all(promises);
|
|
325
317
|
const allData = [...firstPage.data, ...otherPages.flatMap((res) => res.data)];
|
|
326
|
-
|
|
327
|
-
return options.schema.parse(allData);
|
|
328
|
-
}
|
|
329
|
-
return allData;
|
|
318
|
+
return this.validate(allData, options.schema);
|
|
330
319
|
}
|
|
331
320
|
URL(endpoint) {
|
|
332
321
|
return new URL(endpoint, this.base_url);
|
package/dist/lib/errors.js
CHANGED
|
@@ -11,9 +11,7 @@ class FortytwoIntraClientError extends Error {
|
|
|
11
11
|
const route = error.config?.url ?? null;
|
|
12
12
|
const statusCode = error.response?.status ?? NaN;
|
|
13
13
|
const statusText = error.response?.statusText ?? "Unknown";
|
|
14
|
-
const message = method && route
|
|
15
|
-
? `${method} ${route} - HTTP ${statusCode} ${statusText}`
|
|
16
|
-
: error.message;
|
|
14
|
+
const message = method && route ? `${method} ${route} - HTTP ${statusCode} ${statusText}` : error.message;
|
|
17
15
|
super(message);
|
|
18
16
|
this.name = "FortytwoIntraClientError";
|
|
19
17
|
this.statusCode = statusCode;
|
package/dist/lib/logs.js
CHANGED
package/dist/lib/pagination.js
CHANGED
|
@@ -5,9 +5,7 @@ function getLastPage(header) {
|
|
|
5
5
|
if (!header) {
|
|
6
6
|
throw Error("No pagination link header");
|
|
7
7
|
}
|
|
8
|
-
const paginationlinks = header
|
|
9
|
-
.split(",")
|
|
10
|
-
.reduce((links, part) => {
|
|
8
|
+
const paginationlinks = header.split(",").reduce((links, part) => {
|
|
11
9
|
const match = part.match(/<([^>]+)>\s*;\s*rel="([^"]+)"/);
|
|
12
10
|
if (match) {
|
|
13
11
|
const url = match[1];
|