@metarisc/metarisc-js 0.0.1-alpha.3 → 0.0.1-alpha.5

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/lib/core.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { AxiosResponse } from "axios";
2
+ interface RequestConfig {
3
+ body?: any;
4
+ headers?: {
5
+ [name: string]: string | string[];
6
+ };
7
+ params?: {
8
+ [param: string]: string | string[];
9
+ };
10
+ endpoint?: string;
11
+ method?: string;
12
+ }
13
+ export interface MetariscConfig {
14
+ metarisc_url?: string;
15
+ }
16
+ export declare class Core {
17
+ private axios;
18
+ constructor(config: MetariscConfig);
19
+ request<T>(config: RequestConfig): Promise<AxiosResponse<T>>;
20
+ autoPagingIterator<T>(config: RequestConfig): AsyncGenerator<T, void, unknown>;
21
+ }
22
+ export {};
package/lib/core.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Core = void 0;
4
+ const axios_1 = require("axios");
5
+ const axios_retry_1 = require("axios-retry");
6
+ class Core {
7
+ constructor(config) {
8
+ this.axios = axios_1.default.create({
9
+ baseURL: config.metarisc_url ?? 'https://api.metarisc.fr/'
10
+ });
11
+ // Axios interceptor : Retry strategy
12
+ (0, axios_retry_1.default)(this.axios, {
13
+ retries: 3,
14
+ retryDelay: axios_retry_1.default.exponentialDelay
15
+ });
16
+ }
17
+ async request(config) {
18
+ return this.axios.request({
19
+ method: config.method || 'GET',
20
+ url: config.endpoint || '/',
21
+ params: config.params,
22
+ data: config.body,
23
+ headers: config.headers
24
+ });
25
+ }
26
+ async *autoPagingIterator(config) {
27
+ let current_page = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
28
+ const per_page = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
29
+ while (true) {
30
+ const response = await this.request({ ...config, ...{ params: { page: current_page.toString(), per_page: per_page.toString() } } });
31
+ for (const element of response.data.data) {
32
+ yield element;
33
+ }
34
+ if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
35
+ break;
36
+ }
37
+ current_page++;
38
+ }
39
+ }
40
+ }
41
+ exports.Core = Core;
package/lib/index.d.ts CHANGED
@@ -1 +1,3 @@
1
1
  export { Metarisc } from './metarisc';
2
+ export { OAuth2 } from './oauth2';
3
+ export { Core } from './core';
package/lib/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Metarisc = void 0;
3
+ exports.Core = exports.OAuth2 = exports.Metarisc = void 0;
4
4
  var metarisc_1 = require("./metarisc");
5
5
  Object.defineProperty(exports, "Metarisc", { enumerable: true, get: function () { return metarisc_1.Metarisc; } });
6
+ var oauth2_1 = require("./oauth2");
7
+ Object.defineProperty(exports, "OAuth2", { enumerable: true, get: function () { return oauth2_1.OAuth2; } });
8
+ var core_1 = require("./core");
9
+ Object.defineProperty(exports, "Core", { enumerable: true, get: function () { return core_1.Core; } });
package/lib/metarisc.d.ts CHANGED
@@ -1,19 +1,4 @@
1
- import { AxiosResponse } from "axios";
2
- interface RequestConfig {
3
- body?: any;
4
- headers?: {
5
- [name: string]: string | string[];
6
- };
7
- params?: {
8
- [param: string]: string | string[];
9
- };
10
- endpoint?: string;
11
- method?: string;
1
+ import { Core, MetariscConfig } from "./core";
2
+ export declare class Metarisc extends Core {
3
+ constructor(config: MetariscConfig);
12
4
  }
13
- export declare class Metarisc {
14
- private axios;
15
- constructor();
16
- request<T>(config: RequestConfig): Promise<AxiosResponse<T>>;
17
- autoPagingIterator<T>(config: RequestConfig): AsyncGenerator<T, void, unknown>;
18
- }
19
- export {};
package/lib/metarisc.js CHANGED
@@ -1,35 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Metarisc = void 0;
4
- const axios_1 = require("axios");
5
- class Metarisc {
6
- constructor() {
7
- this.axios = axios_1.default.create({
8
- baseURL: 'https://api.metarisc.fr/'
9
- });
10
- }
11
- async request(config) {
12
- return this.axios.request({
13
- method: config.method || 'GET',
14
- url: config.endpoint || '/',
15
- params: config.params,
16
- data: config.body,
17
- headers: config.headers
18
- });
19
- }
20
- async *autoPagingIterator(config) {
21
- let current_page = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
22
- const per_page = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
23
- while (true) {
24
- const response = await this.request({ ...config, ...{ params: { page: current_page.toString(), per_page: per_page.toString() } } });
25
- for (const element of response.data.data) {
26
- yield element;
27
- }
28
- if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
29
- break;
4
+ const core_1 = require("./core");
5
+ class Metarisc extends core_1.Core {
6
+ constructor(config) {
7
+ super(config);
8
+ return new Proxy(this, {
9
+ get: function (metarisc, name) {
10
+ switch (name) {
11
+ default:
12
+ return;
13
+ }
30
14
  }
31
- current_page++;
32
- }
15
+ });
33
16
  }
34
17
  }
35
18
  exports.Metarisc = Metarisc;
@@ -0,0 +1,13 @@
1
+ export declare class OAuth2 {
2
+ /**
3
+ * OAuth2 Authorize URL Generator helper.
4
+ */
5
+ authorizeUrl(options: {
6
+ response_type: string;
7
+ client_id: string;
8
+ redirect_uri?: string;
9
+ state?: string;
10
+ scope?: string;
11
+ [name: string]: string;
12
+ }): string;
13
+ }
package/lib/oauth2.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OAuth2 = void 0;
4
+ class OAuth2 {
5
+ /**
6
+ * OAuth2 Authorize URL Generator helper.
7
+ */
8
+ authorizeUrl(options) {
9
+ const authorize_url = 'https://lemur-17.cloud-iam.com/auth/realms/metariscoidc/protocol/openid-connect/auth';
10
+ const params = new URLSearchParams(options);
11
+ return authorize_url + '?' + params.toString();
12
+ }
13
+ }
14
+ exports.OAuth2 = OAuth2;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@metarisc/metarisc-js",
3
3
  "main": "lib/index.js",
4
4
  "types": "lib/index.d.ts",
5
- "version": "0.0.1-alpha.3",
5
+ "version": "0.0.1-alpha.5",
6
6
  "scripts": {
7
7
  "lint": "eslint . --ext .ts",
8
8
  "compile": "tsc"
@@ -15,6 +15,7 @@
15
15
  "typescript": "^4.9.5"
16
16
  },
17
17
  "dependencies": {
18
- "axios": "^1.4.0"
18
+ "axios": "^1.4.0",
19
+ "axios-retry": "^3.5.0"
19
20
  }
20
21
  }
package/src/core.ts ADDED
@@ -0,0 +1,74 @@
1
+ import axios, { AxiosInstance, AxiosResponse } from "axios";
2
+ import axiosRetry from 'axios-retry';
3
+
4
+ interface RequestConfig {
5
+ body ?: any,
6
+ headers ?: {[name: string]: string | string[]},
7
+ params ?: {[param: string]: string | string[]},
8
+ endpoint ?: string,
9
+ method ?: string
10
+ }
11
+
12
+ export interface MetariscConfig {
13
+ metarisc_url ?: string,
14
+ }
15
+
16
+ export class Core
17
+ {
18
+ private axios : AxiosInstance;
19
+
20
+ constructor(config : MetariscConfig)
21
+ {
22
+ this.axios = axios.create({
23
+ baseURL: config.metarisc_url ?? 'https://api.metarisc.fr/'
24
+ });
25
+
26
+ // Axios interceptor : Retry strategy
27
+ axiosRetry(this.axios, {
28
+ retries: 3,
29
+ retryDelay: axiosRetry.exponentialDelay
30
+ });
31
+ }
32
+
33
+ async request<T>(config : RequestConfig) : Promise<AxiosResponse<T>>
34
+ {
35
+ return this.axios.request<T>({
36
+ method: config.method || 'GET',
37
+ url: config.endpoint || '/',
38
+ params: config.params,
39
+ data: config.body,
40
+ headers: config.headers
41
+ });
42
+ }
43
+
44
+ async * autoPagingIterator<T>(config : RequestConfig) : AsyncGenerator<T, void, unknown>
45
+ {
46
+ let current_page : number = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
47
+ const per_page : number = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
48
+
49
+ while (true) {
50
+ const response = <AxiosResponse<{
51
+ data?: Array<T>;
52
+ meta?: {
53
+ pagination?: {
54
+ total?: number;
55
+ count?: number;
56
+ per_page?: number;
57
+ current_page?: number;
58
+ total_pages?: number;
59
+ };
60
+ };
61
+ }>> await this.request({...config, ...{params: {page: current_page.toString(), per_page: per_page.toString()}}});
62
+
63
+ for (const element of response.data.data) {
64
+ yield element;
65
+ }
66
+
67
+ if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
68
+ break;
69
+ }
70
+
71
+ current_page++;
72
+ }
73
+ }
74
+ }
package/src/index.ts CHANGED
@@ -1 +1,3 @@
1
- export { Metarisc } from './metarisc';
1
+ export { Metarisc } from './metarisc';
2
+ export { OAuth2 } from './oauth2';
3
+ export { Core } from './core';
package/src/metarisc.ts CHANGED
@@ -1,63 +1,18 @@
1
- import axios, { AxiosInstance, AxiosResponse } from "axios";
1
+ import { Core, MetariscConfig } from "./core";
2
2
 
3
- interface RequestConfig {
4
- body ?: any,
5
- headers ?: {[name: string]: string | string[]},
6
- params ?: {[param: string]: string | string[]},
7
- endpoint ?: string,
8
- method ?: string
9
- }
10
-
11
- export class Metarisc
3
+ export class Metarisc extends Core
12
4
  {
13
- private axios : AxiosInstance;
14
-
15
- constructor()
16
- {
17
- this.axios = axios.create({
18
- baseURL: 'https://api.metarisc.fr/'
19
- });
20
- }
21
-
22
- async request<T>(config : RequestConfig) : Promise<AxiosResponse<T>>
23
- {
24
- return this.axios.request<T>({
25
- method: config.method || 'GET',
26
- url: config.endpoint || '/',
27
- params: config.params,
28
- data: config.body,
29
- headers: config.headers
30
- });
31
- }
32
-
33
- async * autoPagingIterator<T>(config : RequestConfig) : AsyncGenerator<T, void, unknown>
5
+ constructor(config : MetariscConfig)
34
6
  {
35
- let current_page : number = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
36
- const per_page : number = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
37
-
38
- while (true) {
39
- const response = <AxiosResponse<{
40
- data?: Array<T>;
41
- meta?: {
42
- pagination?: {
43
- total?: number;
44
- count?: number;
45
- per_page?: number;
46
- current_page?: number;
47
- total_pages?: number;
48
- };
49
- };
50
- }>> await this.request({...config, ...{params: {page: current_page.toString(), per_page: per_page.toString()}}});
51
-
52
- for (const element of response.data.data) {
53
- yield element;
7
+ super(config);
8
+
9
+ return new Proxy(this, {
10
+ get: function(metarisc, name) {
11
+ switch(name) {
12
+ default:
13
+ return;
14
+ }
54
15
  }
55
-
56
- if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
57
- break;
58
- }
59
-
60
- current_page++;
61
- }
16
+ });
62
17
  }
63
18
  }
package/src/oauth2.ts ADDED
@@ -0,0 +1,20 @@
1
+ export class OAuth2
2
+ {
3
+ /**
4
+ * OAuth2 Authorize URL Generator helper.
5
+ */
6
+ authorizeUrl(options : {
7
+ response_type : string,
8
+ client_id : string,
9
+ redirect_uri ?: string,
10
+ state ?: string,
11
+ scope ?: string,
12
+ [name: string]: string
13
+ }) : string
14
+ {
15
+ const authorize_url = 'https://lemur-17.cloud-iam.com/auth/realms/metariscoidc/protocol/openid-connect/auth';
16
+ const params = new URLSearchParams(options);
17
+
18
+ return authorize_url + '?' + params.toString();
19
+ }
20
+ }