@ibertran/fortytwo-intra-client 3.1.1 → 3.1.4

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/README.md CHANGED
@@ -17,7 +17,7 @@ const client = new FortytwoIntraClient('your_client_id', 'your_client_secret');
17
17
 
18
18
 
19
19
  // Get a specific user by login
20
- const user = await client.get('users/jdoe');
20
+ const user = await client.get('users/ibertran');
21
21
  console.log(user);
22
22
  ```
23
23
 
@@ -32,16 +32,19 @@ The `FortytwoIntraClient` class accepts three parameters:
32
32
  ### Configuration Options
33
33
 
34
34
  ```typescript
35
- interface Conf {
36
- redirect_uri: string | null; // OAuth redirect URI (default: null)
37
- base_url: string; // API base URL (default: "https://api.intra.42.fr/v2/")
38
- token_url: string; // Token endpoint (default: "https://api.intra.42.fr/oauth/token")
39
- oauth_url: string; // OAuth endpoint (default: "https://api.intra.42.fr/oauth/authorize")
40
- scopes: string[]; // OAuth scopes (default: ["public"])
41
- rate: number; // Requests per second (default: 2)
42
- maxRetry: number; // Max retry attempts (default: 5)
43
- logs: boolean; // Enable logging (default: true)
44
- errors: boolean; // Enable error logging (default: true)
35
+ interface FortytwoIntraClientConf {
36
+ redirect_uri: string | null; // OAuth redirect URI (default: null)
37
+ base_url: string; // API base URL (default: "https://api.intra.42.fr/v2/")
38
+ token_url: string; // Token endpoint (default: "https://api.intra.42.fr/oauth/token")
39
+ oauth_url: string; // OAuth endpoint (default: "https://api.intra.42.fr/oauth/authorize")
40
+ token_info_url: string; // Token info endpoint (default: "https://api.intra.42.fr/oauth/token/info")
41
+ scopes: string[]; // OAuth scopes (default: ["public"])
42
+ rateLimitMaxRequests: number; // Max requests per time window (default: 2)
43
+ rateLimitPerMilliseconds: number; // Time window in milliseconds (default: 1200)
44
+ maxRetry: number; // Max retry attempts (default: 5)
45
+ logLine: boolean; // Enable request logging (default: true)
46
+ errLogBody: boolean; // Log error response bodies (default: true)
47
+ throwOnError: boolean; // Throw errors instead of returning them (default: true)
45
48
  }
46
49
  ```
47
50
 
@@ -49,10 +52,13 @@ interface Conf {
49
52
 
50
53
  ```typescript
51
54
  const client = new FortytwoIntraClient('your_client_id', 'your_client_secret', {
52
- rate: 1, // 1 request per second
53
- maxRetry: 3, // Retry up to 3 times
54
- logs: false, // Disable logging
55
- scopes: ['public', 'projects'] // Extended scopes
55
+ rateLimitMaxRequests: 1, // 1 request per 1200ms
56
+ rateLimitPerMilliseconds: 1200, // Time window
57
+ maxRetry: 3, // Retry up to 3 times
58
+ logLine: false, // Disable logging
59
+ errLogBody: false, // Disable error body logging
60
+ throwOnError: false, // Return errors instead of throwing
61
+ scopes: ['public', 'projects'] // Extended scopes
56
62
  });
57
63
  ```
58
64
 
@@ -189,16 +195,16 @@ Requests are automatically throttled based on the configured rate limit. The def
189
195
 
190
196
  ## Logging
191
197
 
192
- By default, the library logs all requests with status codes:
198
+ By default, the library logs all requests with colored status codes and formatted parameters:
193
199
 
194
200
  ```
195
- ✅ 200 GET https://api.intra.42.fr/v2/users/jdoe
196
- ✅ 200 GET https://api.intra.42.fr/v2/campus/1
197
- 🔄 429 GET https://api.intra.42.fr/v2/projects retry 1/5
198
- ✅ 200 GET https://api.intra.42.fr/v2/projects retry 1/5
201
+ 200 GET /v2/users/jdoe
202
+ 200 GET /v2/campus { filter: { city: 'lyon' } }
203
+ 429 GET /v2/projects
204
+ 200 GET /v2/projects retry 1/5
199
205
  ```
200
206
 
201
- Disable logging by setting `logs: false` in the configuration.
207
+ Disable logging by setting `logLine: false` in the configuration. Control error body logging with `errLogBody: false`.
202
208
 
203
209
  ## TypeScript Support
204
210
 
@@ -1 +1,9 @@
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;
1
9
  export {};
package/dist/HttpError.js CHANGED
@@ -1,23 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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
- // }
3
+ exports.simplifySuperagentError = simplifySuperagentError;
4
+ exports.isSuperAgentError = isSuperAgentError;
5
+ class HttpError extends Error {
6
+ status;
7
+ url;
8
+ details;
9
+ constructor(err) {
10
+ super(err.message || "Unknown API error");
11
+ this.name = "HttpError";
12
+ this.status = err.status || null;
13
+ this.url = err.response?.request?.url || null;
14
+ this.details = err.response?.body || null;
15
+ }
16
+ }
17
+ function simplifySuperagentError(err) {
18
+ if (!err) {
19
+ return new HttpError({ message: "Unknown error" });
20
+ }
21
+ return new HttpError(err);
22
+ }
23
+ function isSuperAgentError(err) {
24
+ return (err && typeof err === "object" && ("status" in err || "response" in err));
25
+ }
package/dist/index.d.ts CHANGED
@@ -52,5 +52,5 @@ export declare class FortytwoIntraClient {
52
52
  state: string;
53
53
  };
54
54
  exchangeOAuthCode(code: string, redirect_uri?: string): Promise<any>;
55
- tokenInfos(options: Omit<inputOptions, "body" | "perPage" | "maxPage">): Promise<any>;
55
+ tokenInfos(options?: Omit<inputOptions, "body" | "perPage" | "maxPage">): Promise<any>;
56
56
  }
package/dist/index.js CHANGED
@@ -300,7 +300,7 @@ class FortytwoIntraClient {
300
300
  });
301
301
  return res?.data;
302
302
  }
303
- async tokenInfos(options) {
303
+ async tokenInfos(options = {}) {
304
304
  return this.get(this.token_info_url, options);
305
305
  }
306
306
  }
package/dist/lib/logs.js CHANGED
@@ -2,12 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLogLine = getLogLine;
4
4
  exports.getErrorLogLine = getErrorLogLine;
5
+ const util_1 = require("util");
5
6
  const green = "\x1b[42m";
6
7
  const red = "\x1b[41m";
8
+ const yellow = "\x1b[43m";
7
9
  const reset = "\x1b[0m";
10
+ function formatQueryParams(params) {
11
+ const { per_page, page, ...filteredParams } = params;
12
+ const keys = Object.keys(filteredParams);
13
+ if (keys.length === 0)
14
+ return "";
15
+ return (0, util_1.inspect)(filteredParams, {
16
+ depth: Infinity,
17
+ compact: true,
18
+ breakLength: Infinity,
19
+ colors: true
20
+ });
21
+ }
8
22
  function getLogLine(res, options) {
9
23
  const tokens = [];
10
- // Get query parameters as object
11
24
  const queryParams = res.config?.params || {};
12
25
  tokens.push(`${green}${res.status}${reset}`);
13
26
  tokens.push(options.method.padEnd(6, " "));
@@ -16,21 +29,19 @@ function getLogLine(res, options) {
16
29
  const url = new URL(res.config.url);
17
30
  tokens.push(url.pathname);
18
31
  }
19
- // Add query parameters as object if they exist
20
- console.log(queryParams);
21
- if (Object.keys(queryParams).length > 0) {
22
- delete queryParams.per_page;
23
- delete queryParams.page;
24
- tokens.push(JSON.stringify(queryParams));
32
+ // Add query parameters
33
+ const formattedParams = formatQueryParams(queryParams);
34
+ if (formattedParams) {
35
+ tokens.push(formattedParams);
25
36
  }
37
+ // Add pagination info
26
38
  if (options.currpage) {
27
- tokens.push(`| page: ${options.currpage}/${options.lastPage === Infinity ? "unknown" : options.lastPage}`);
39
+ tokens.push(`| ${options.currpage}/${options.lastPage === Infinity ? "..." : options.lastPage}`);
28
40
  }
29
41
  return tokens.join(" ");
30
42
  }
31
43
  function getErrorLogLine(err, options) {
32
44
  const tokens = [];
33
- // Get query parameters as object
34
45
  const queryParams = err.config?.params || {};
35
46
  tokens.push(`${red}${err.status}${reset}`);
36
47
  tokens.push(options.method.padEnd(6, " "));
@@ -39,9 +50,10 @@ function getErrorLogLine(err, options) {
39
50
  const url = new URL(err.config.url);
40
51
  tokens.push(url.pathname);
41
52
  }
42
- // Add query parameters as object if they exist
43
- if (Object.keys(queryParams).length > 0) {
44
- tokens.push(JSON.stringify(queryParams));
53
+ // Add query parameters
54
+ const formattedParams = formatQueryParams(queryParams);
55
+ if (formattedParams) {
56
+ tokens.push(formattedParams);
45
57
  }
46
58
  return tokens.join(" ");
47
59
  }
package/dist/utils.d.ts CHANGED
@@ -1 +1,4 @@
1
+ import * as superagent from "superagent";
2
+ import { Method } from "./types";
3
+ export declare function initRequest(method: Method, url: URL): superagent.Request;
1
4
  export declare function getLastPage(header: string | undefined): number;
package/dist/utils.js CHANGED
@@ -1,6 +1,56 @@
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
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.initRequest = initRequest;
3
37
  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
+ }
4
54
  function getLastPage(header) {
5
55
  if (!header) {
6
56
  throw Error("No pagination link header");
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@ibertran/fortytwo-intra-client",
3
- "version": "3.1.1",
3
+ "version": "3.1.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
+ "author": {
7
+ "name": "Ian BERTRAND"
8
+ },
6
9
  "scripts": {
7
10
  "build": "tsc"
8
11
  },
@@ -12,8 +15,8 @@
12
15
  },
13
16
  "devDependencies": {
14
17
  "typescript": "^5.9.3",
15
- "dotenv": "^16.6.1",
16
18
  "ts-node": "^10.9.2",
19
+ "dotenv": "^16.6.1",
17
20
  "express": "^4.18.2"
18
21
  },
19
22
  "files": [
package/dist/log.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { reqOptions } from "./types";
2
- import { AxiosResponse, AxiosError } from "axios";
3
- export declare function getLogLine(res: AxiosResponse, options: reqOptions): string;
4
- export declare function getErrorLogLine(err: AxiosError, options: reqOptions): string;
package/dist/log.js DELETED
@@ -1,51 +0,0 @@
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
- }