@ibertran/fortytwo-intra-client 3.0.0 → 3.1.1

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
@@ -107,9 +107,9 @@ await client.delete('teams/123');
107
107
  The `getAll` method automatically handles pagination and returns all results:
108
108
 
109
109
  ```typescript
110
- // Get all users from 42 Lyon (handles pagination automatically)
110
+ // Get all users from 42Lyon (handles pagination automatically)
111
111
  const lyonUsers = await client.getAll('campus/9/users');
112
- console.log(`Paris campus has ${lyonUsers.length} users`);
112
+ console.log(`42Lyon campus has ${lyonUsers.length} users`);
113
113
 
114
114
  // Get all projects with custom page size
115
115
  const allProjects = await client.getAll('projects', {
package/dist/index.d.ts CHANGED
@@ -44,7 +44,13 @@ export declare class FortytwoIntraClient {
44
44
  delete(endpoint: URL | string, options?: Omit<inputOptions, "perPage" | "maxPage">): Promise<any>;
45
45
  getAll(endpoint: URL | string, options?: Omit<inputOptions, "body">): Promise<any>;
46
46
  URL(endpoint: string): URL;
47
- getOAuthUrl(redirect_uri?: string | null): string;
47
+ getOAuthUrl(options?: {
48
+ redirect_uri?: string;
49
+ state?: string;
50
+ }): {
51
+ url: string;
52
+ state: string;
53
+ };
48
54
  exchangeOAuthCode(code: string, redirect_uri?: string): Promise<any>;
49
- tokenInfos(): Promise<any>;
55
+ tokenInfos(options: Omit<inputOptions, "body" | "perPage" | "maxPage">): Promise<any>;
50
56
  }
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ const axios_1 = __importDefault(require("axios"));
8
8
  const axios_rate_limit_1 = __importDefault(require("axios-rate-limit"));
9
9
  const pagination_1 = require("./lib/pagination");
10
10
  const logs_1 = require("./lib/logs");
11
+ const crypto_1 = __importDefault(require("crypto"));
11
12
  const defaultConf = {
12
13
  redirect_uri: null,
13
14
  base_url: "https://api.intra.42.fr/v2/",
@@ -276,17 +277,18 @@ class FortytwoIntraClient {
276
277
  URL(endpoint) {
277
278
  return new URL(endpoint, this.base_url);
278
279
  }
279
- getOAuthUrl(redirect_uri = null) {
280
- redirect_uri ??= this.redirect_uri;
281
- if (!redirect_uri) {
282
- throw new Error(`undefined redirect_uri`);
283
- }
280
+ getOAuthUrl(options = {}) {
281
+ const redirectUri = options.redirect_uri || this.redirect_uri;
282
+ if (!redirectUri)
283
+ throw new Error(`Missing redirect_uri parameter`);
284
+ const state = options.state || crypto_1.default.randomBytes(32).toString('base64url');
284
285
  const url = new URL(this.oauth_url);
285
286
  url.searchParams.set("client_id", this.client_id);
286
- url.searchParams.set("redirect_uri", redirect_uri);
287
+ url.searchParams.set("redirect_uri", redirectUri);
287
288
  url.searchParams.set("response_type", "code");
288
289
  url.searchParams.set("scope", this.scopes.join(" "));
289
- return url.toString();
290
+ url.searchParams.set("state", state);
291
+ return { url: url.toString(), state };
290
292
  }
291
293
  async exchangeOAuthCode(code, redirect_uri) {
292
294
  const res = await this.axiosInstance.post(this.token_url, {
@@ -298,8 +300,8 @@ class FortytwoIntraClient {
298
300
  });
299
301
  return res?.data;
300
302
  }
301
- async tokenInfos() {
302
- return this.get(this.token_info_url);
303
+ async tokenInfos(options) {
304
+ return this.get(this.token_info_url, options);
303
305
  }
304
306
  }
305
307
  exports.FortytwoIntraClient = FortytwoIntraClient;
package/dist/lib/logs.js CHANGED
@@ -9,12 +9,7 @@ function getLogLine(res, options) {
9
9
  const tokens = [];
10
10
  // Get query parameters as object
11
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
- }
12
+ tokens.push(`${green}${res.status}${reset}`);
18
13
  tokens.push(options.method.padEnd(6, " "));
19
14
  // Add the base URL path
20
15
  if (res.config?.url) {
@@ -37,12 +32,7 @@ function getErrorLogLine(err, options) {
37
32
  const tokens = [];
38
33
  // Get query parameters as object
39
34
  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
- }
35
+ tokens.push(`${red}${err.status}${reset}`);
46
36
  tokens.push(options.method.padEnd(6, " "));
47
37
  // Add the base URL path
48
38
  if (err.config?.url) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ibertran/fortytwo-intra-client",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -13,7 +13,8 @@
13
13
  "devDependencies": {
14
14
  "typescript": "^5.9.3",
15
15
  "dotenv": "^16.6.1",
16
- "ts-node": "^10.9.2"
16
+ "ts-node": "^10.9.2",
17
+ "express": "^4.18.2"
17
18
  },
18
19
  "files": [
19
20
  "dist",