@c-rex/core 0.0.7 → 0.0.9
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/index.d.mts +8 -4
- package/dist/index.d.ts +8 -4
- package/dist/index.js +89 -68
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +87 -66
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.d.mts
CHANGED
|
@@ -9,16 +9,20 @@ interface CallParams {
|
|
|
9
9
|
params?: any;
|
|
10
10
|
}
|
|
11
11
|
declare class CrexApi {
|
|
12
|
+
private config;
|
|
12
13
|
private apiClient;
|
|
13
14
|
private logger;
|
|
14
|
-
constructor(
|
|
15
|
-
|
|
15
|
+
constructor(config: ConfigInterface, logger: any);
|
|
16
|
+
private manageToken;
|
|
17
|
+
private getToken;
|
|
18
|
+
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<T>;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
declare class CrexSDK {
|
|
19
|
-
customerConfig: ConfigInterface;
|
|
20
|
-
logger: any;
|
|
21
22
|
api: CrexApi;
|
|
23
|
+
logger: any;
|
|
24
|
+
userAuthConfig: any;
|
|
25
|
+
customerConfig: ConfigInterface;
|
|
22
26
|
constructor();
|
|
23
27
|
static setConfig(config: ConfigInterface): void;
|
|
24
28
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,16 +9,20 @@ interface CallParams {
|
|
|
9
9
|
params?: any;
|
|
10
10
|
}
|
|
11
11
|
declare class CrexApi {
|
|
12
|
+
private config;
|
|
12
13
|
private apiClient;
|
|
13
14
|
private logger;
|
|
14
|
-
constructor(
|
|
15
|
-
|
|
15
|
+
constructor(config: ConfigInterface, logger: any);
|
|
16
|
+
private manageToken;
|
|
17
|
+
private getToken;
|
|
18
|
+
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<T>;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
declare class CrexSDK {
|
|
19
|
-
customerConfig: ConfigInterface;
|
|
20
|
-
logger: any;
|
|
21
22
|
api: CrexApi;
|
|
23
|
+
logger: any;
|
|
24
|
+
userAuthConfig: any;
|
|
25
|
+
customerConfig: ConfigInterface;
|
|
22
26
|
constructor();
|
|
23
27
|
static setConfig(config: ConfigInterface): void;
|
|
24
28
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,72 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _class;// src/requests.ts
|
|
2
2
|
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
3
3
|
var _constants = require('@c-rex/constants');
|
|
4
|
+
var _openidclient = require('openid-client');
|
|
5
|
+
var _utils = require('@c-rex/utils');
|
|
6
|
+
var CREX_TOKEN_HEADER_KEY = "crex-token";
|
|
7
|
+
var CREX_TOKEN_EXPIRY_HEADER_KEY = "crex-token-expiry";
|
|
4
8
|
var CrexApi = class {
|
|
5
9
|
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
|
|
12
|
+
constructor(config, logger) {
|
|
8
13
|
this.apiClient = _axios2.default.create({
|
|
9
|
-
baseURL: baseUrl,
|
|
14
|
+
baseURL: config.baseUrl,
|
|
10
15
|
headers: {
|
|
11
16
|
"content-Type": "application/json"
|
|
12
17
|
}
|
|
13
18
|
});
|
|
19
|
+
this.config = config;
|
|
14
20
|
this.logger = logger;
|
|
15
21
|
}
|
|
22
|
+
async manageToken() {
|
|
23
|
+
const headersAux = {};
|
|
24
|
+
if (this.config.OIDC.client.enabled) {
|
|
25
|
+
let token = _utils.getFromMemory.call(void 0, CREX_TOKEN_HEADER_KEY);
|
|
26
|
+
let tokenExpiry = _utils.getFromMemory.call(void 0, CREX_TOKEN_EXPIRY_HEADER_KEY);
|
|
27
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
28
|
+
if (!(token && tokenExpiry > now + 60)) {
|
|
29
|
+
const { token: tokenAux, tokenExpiry: tokenExpiryAux } = await this.getToken();
|
|
30
|
+
token = tokenAux;
|
|
31
|
+
_utils.saveInMemory.call(void 0, token, CREX_TOKEN_HEADER_KEY);
|
|
32
|
+
_utils.saveInMemory.call(void 0, tokenExpiryAux, CREX_TOKEN_EXPIRY_HEADER_KEY);
|
|
33
|
+
}
|
|
34
|
+
headersAux["Authorization"] = `Bearer ${token}`;
|
|
35
|
+
}
|
|
36
|
+
return headersAux;
|
|
37
|
+
}
|
|
38
|
+
async getToken() {
|
|
39
|
+
let token = "";
|
|
40
|
+
let tokenExpiry = 0;
|
|
41
|
+
try {
|
|
42
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
43
|
+
const issuer = await _openidclient.Issuer.discover(this.config.OIDC.client.issuer);
|
|
44
|
+
const client = new issuer.Client({
|
|
45
|
+
client_id: this.config.OIDC.client.id,
|
|
46
|
+
client_secret: this.config.OIDC.client.secret
|
|
47
|
+
});
|
|
48
|
+
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
49
|
+
token = tokenSet.access_token, tokenExpiry = now + tokenSet.expires_at;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.log("error", `API.getToken error when request ${this.config.OIDC.client.issuer}. Error: ${error}`);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
token,
|
|
55
|
+
tokenExpiry
|
|
56
|
+
};
|
|
57
|
+
}
|
|
16
58
|
async execute({
|
|
17
59
|
url,
|
|
18
60
|
method,
|
|
19
61
|
params,
|
|
20
62
|
body,
|
|
21
|
-
headers
|
|
63
|
+
headers = {}
|
|
22
64
|
}) {
|
|
23
65
|
let response = void 0;
|
|
66
|
+
headers = {
|
|
67
|
+
...headers,
|
|
68
|
+
...await this.manageToken()
|
|
69
|
+
};
|
|
24
70
|
for (let retry = 0; retry < _constants.API.MAX_RETRY; retry++) {
|
|
25
71
|
try {
|
|
26
72
|
response = await this.apiClient.request({
|
|
@@ -30,9 +76,15 @@ var CrexApi = class {
|
|
|
30
76
|
params,
|
|
31
77
|
headers
|
|
32
78
|
});
|
|
79
|
+
break;
|
|
33
80
|
} catch (error) {
|
|
34
|
-
|
|
35
|
-
|
|
81
|
+
console.log(
|
|
82
|
+
"error",
|
|
83
|
+
`API.execute ${retry + 1}\xBA error when request ${url}. Error: ${error}`
|
|
84
|
+
);
|
|
85
|
+
if (retry === _constants.API.MAX_RETRY - 1) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
36
88
|
}
|
|
37
89
|
}
|
|
38
90
|
if (response) {
|
|
@@ -42,76 +94,45 @@ var CrexApi = class {
|
|
|
42
94
|
}
|
|
43
95
|
};
|
|
44
96
|
|
|
45
|
-
// src/cookies.ts
|
|
46
|
-
var GLOBAL_KEY = "__CREX_INITIAL_CONFIG__";
|
|
47
|
-
var CONFIG_COOKIE_KEY = "crex_config";
|
|
48
|
-
function isBrowser() {
|
|
49
|
-
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
50
|
-
}
|
|
51
|
-
function parseCookies() {
|
|
52
|
-
const cookies = {};
|
|
53
|
-
if (typeof document === "undefined") {
|
|
54
|
-
return cookies;
|
|
55
|
-
}
|
|
56
|
-
document.cookie.split(";").forEach((cookie) => {
|
|
57
|
-
const [key, value] = cookie.split("=");
|
|
58
|
-
if (key && value) {
|
|
59
|
-
cookies[key.trim()] = decodeURIComponent(value.trim());
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return cookies;
|
|
63
|
-
}
|
|
64
|
-
function setCookie(name, value, days = 30) {
|
|
65
|
-
if (typeof document === "undefined") return;
|
|
66
|
-
const expires = new Date(Date.now() + days * 86400 * 1e3).toUTCString();
|
|
67
|
-
document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;
|
|
68
|
-
}
|
|
69
|
-
function setConfig(config) {
|
|
70
|
-
if (isBrowser()) {
|
|
71
|
-
setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));
|
|
72
|
-
} else {
|
|
73
|
-
if (typeof global !== "undefined" && !(GLOBAL_KEY in global)) {
|
|
74
|
-
global[GLOBAL_KEY] = null;
|
|
75
|
-
}
|
|
76
|
-
const globalConfig = global[GLOBAL_KEY];
|
|
77
|
-
if (globalConfig === null) {
|
|
78
|
-
global[GLOBAL_KEY] = config;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function getConfig() {
|
|
83
|
-
let returnValue;
|
|
84
|
-
if (isBrowser()) {
|
|
85
|
-
const cookies = parseCookies();
|
|
86
|
-
const configStr = cookies[CONFIG_COOKIE_KEY];
|
|
87
|
-
if (configStr) {
|
|
88
|
-
try {
|
|
89
|
-
return JSON.parse(configStr);
|
|
90
|
-
} catch (e) {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
} else {
|
|
96
|
-
returnValue = global[GLOBAL_KEY];
|
|
97
|
-
}
|
|
98
|
-
return returnValue;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
97
|
// src/sdk.ts
|
|
102
|
-
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
var CrexSDK = (_class = class {
|
|
103
101
|
|
|
104
102
|
|
|
103
|
+
__init() {this.userAuthConfig = {}}
|
|
105
104
|
|
|
106
|
-
constructor() {
|
|
107
|
-
const config =
|
|
105
|
+
constructor() {;_class.prototype.__init.call(this);
|
|
106
|
+
const config = _utils.getFromMemory.call(void 0, _constants.SDK_CONFIG_KEY);
|
|
108
107
|
this.customerConfig = config;
|
|
109
|
-
this.api = new CrexApi(this.customerConfig
|
|
108
|
+
this.api = new CrexApi(this.customerConfig, null);
|
|
109
|
+
const user = this.customerConfig.OIDC.user;
|
|
110
|
+
this.userAuthConfig = {
|
|
111
|
+
providers: [
|
|
112
|
+
{
|
|
113
|
+
id: "crex",
|
|
114
|
+
name: "CREX",
|
|
115
|
+
type: "oauth",
|
|
116
|
+
clientId: user.id,
|
|
117
|
+
wellKnown: user.issuer,
|
|
118
|
+
clientSecret: user.secret,
|
|
119
|
+
authorization: { params: { scope: user.scope } },
|
|
120
|
+
profile(profile) {
|
|
121
|
+
return {
|
|
122
|
+
id: profile.id || "fake Id",
|
|
123
|
+
name: profile.name || "Fake Name",
|
|
124
|
+
email: profile.email || "fake Email",
|
|
125
|
+
image: profile.image || "fake Image"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
};
|
|
110
131
|
}
|
|
111
132
|
static setConfig(config) {
|
|
112
|
-
|
|
133
|
+
_utils.saveInMemory.call(void 0, config, _constants.SDK_CONFIG_KEY);
|
|
113
134
|
}
|
|
114
|
-
};
|
|
135
|
+
}, _class);
|
|
115
136
|
|
|
116
137
|
|
|
117
138
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/periotto/Desktop/workspace/c-rex.net-web-client-foundation/packages/core/dist/index.js","../src/requests.ts","../src/
|
|
1
|
+
{"version":3,"sources":["/Users/periotto/Desktop/workspace/c-rex.net-web-client-foundation/packages/core/dist/index.js","../src/requests.ts","../src/sdk.ts"],"names":[],"mappings":"AAAA;ACAA,4EAA4D;AAC5D,6CAAoB;AACpB,6CAAuB;AAEvB,qCAA4C;AAE5C,IAAM,sBAAA,EAAwB,YAAA;AAC9B,IAAM,6BAAA,EAA+B,mBAAA;AAe9B,IAAM,QAAA,EAAN,MAAc;AAAA,EACT;AAAA,EACA;AAAA,EACA;AAAA,EAED,WAAA,CAAY,MAAA,EAAyB,MAAA,EAAa;AACrD,IAAA,IAAA,CAAK,UAAA,EAAY,eAAA,CAAM,MAAA,CAAO;AAAA,MAC1B,OAAA,EAAS,MAAA,CAAO,OAAA;AAAA,MAChB,OAAA,EAAS;AAAA,QACL,cAAA,EAAgB;AAAA,MACpB;AA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AACW,IAAA;AACY,EAAA;AAC2B,IAAA;AAExC,IAAA;AACH,MAAA;AACuB,MAAA;AAC9B,IAAA;AAE2C,IAAA;AACnC,MAAA;AACwC,QAAA;AACpC,UAAA;AACA,UAAA;AACM,UAAA;AACN,UAAA;AACA,UAAA;AACH,QAAA;AAED,QAAA;AACY,MAAA;AACJ,QAAA;AACJ,UAAA;AACwB,UAAA;AAC5B,QAAA;AAEiC,QAAA;AACvB,UAAA;AACV,QAAA;AACJ,MAAA;AACJ,IAAA;AAEc,IAAA;AACM,MAAA;AACpB,IAAA;AAEgB,IAAA;AACpB,EAAA;AACJ;ADvCoD;AACA;AEhGrB;AAGP;AAGH;AACV,EAAA;AACA,EAAA;AACuB,iBAAA;AACvB,EAAA;AAEc,EAAA;AAC0B,IAAA;AAErB,IAAA;AAC0B,IAAA;AAEV,IAAA;AAChB,IAAA;AACP,MAAA;AACP,QAAA;AACQ,UAAA;AACE,UAAA;AACA,UAAA;AACS,UAAA;AACC,UAAA;AACG,UAAA;AACoB,UAAA;AACjB,UAAA;AACX,YAAA;AACe,cAAA;AACI,cAAA;AACE,cAAA;AACA,cAAA;AAC5B,YAAA;AACJ,UAAA;AACJ,QAAA;AACJ,MAAA;AACJ,IAAA;AACJ,EAAA;AAEiD,EAAA;AACV,IAAA;AACvC,EAAA;AACJ;AF0FoD;AACA;AACA;AACA","file":"/Users/periotto/Desktop/workspace/c-rex.net-web-client-foundation/packages/core/dist/index.js","sourcesContent":[null,"import axios, { AxiosResponse, Method, AxiosInstance } from \"axios\";\nimport { API } from \"@c-rex/constants\";\nimport { Issuer } from \"openid-client\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getFromMemory, saveInMemory } from \"@c-rex/utils\";\n\nconst CREX_TOKEN_HEADER_KEY = \"crex-token\";\nconst CREX_TOKEN_EXPIRY_HEADER_KEY = \"crex-token-expiry\";\n\ninterface APIGenericResponse<T> extends AxiosResponse {\n data: T;\n statusCode: number;\n}\n\ninterface CallParams {\n url: string;\n method: Method;\n body?: any;\n headers?: any;\n params?: any;\n}\n\nexport class CrexApi {\n private config: ConfigInterface;\n private apiClient: AxiosInstance;\n private logger: any;\n\n public constructor(config: ConfigInterface, logger: any) {\n this.apiClient = axios.create({\n baseURL: config.baseUrl,\n headers: {\n \"content-Type\": \"application/json\",\n },\n });\n\n this.config = config;\n this.logger = logger;\n }\n\n private async manageToken() {\n const headersAux: Record<string, string> = {};\n\n if (this.config.OIDC.client.enabled) {\n let token = getFromMemory(CREX_TOKEN_HEADER_KEY);\n let tokenExpiry = getFromMemory(CREX_TOKEN_EXPIRY_HEADER_KEY);\n\n const now = Math.floor(Date.now() / 1000);\n\n if (!(token && tokenExpiry > now + 60)) {\n const { token: tokenAux, tokenExpiry: tokenExpiryAux } = await this.getToken();\n\n token = tokenAux;\n\n saveInMemory(token, CREX_TOKEN_HEADER_KEY);\n saveInMemory(tokenExpiryAux, CREX_TOKEN_EXPIRY_HEADER_KEY);\n }\n\n headersAux['Authorization'] = `Bearer ${token}`;\n }\n\n return headersAux;\n }\n\n private async getToken(): Promise<{\n token: string;\n tokenExpiry: number;\n }> {\n let token = \"\";\n let tokenExpiry = 0;\n\n try {\n const now = Math.floor(Date.now() / 1000);\n const issuer = await Issuer.discover(this.config.OIDC.client.issuer);\n const client = new issuer.Client({\n client_id: this.config.OIDC.client.id,\n client_secret: this.config.OIDC.client.secret,\n });\n const tokenSet = await client.grant({ grant_type: 'client_credentials' });\n\n token = tokenSet.access_token!,\n tokenExpiry = now + tokenSet.expires_at!\n } catch (error) {\n console.log(\"error\", `API.getToken error when request ${this.config.OIDC.client.issuer}. Error: ${error}`);\n }\n\n return {\n token,\n tokenExpiry\n };\n }\n\n async execute<T>({\n url,\n method,\n params,\n body,\n headers = {},\n }: CallParams): Promise<T> {\n let response: APIGenericResponse<T> | undefined = undefined;\n\n headers = {\n ...headers,\n ...await this.manageToken(),\n };\n\n for (let retry = 0; retry < API.MAX_RETRY; retry++) {\n try {\n response = await this.apiClient.request({\n url,\n method,\n data: body,\n params,\n headers,\n });\n\n break;\n } catch (error) {\n console.log(\n \"error\",\n `API.execute ${retry + 1}º error when request ${url}. Error: ${error}`\n );\n\n if (retry === API.MAX_RETRY - 1) {\n throw error;\n }\n }\n }\n\n if (response) {\n return response.data;\n }\n\n throw new Error(\"API.execute error: Failed to retrieve a valid response\");\n }\n}","import { SDK_CONFIG_KEY } from \"@c-rex/constants\";\nimport { CrexApi } from \"./requests\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getFromMemory, saveInMemory } from \"@c-rex/utils\";\n\n\nexport class CrexSDK {\n public api!: CrexApi;\n public logger!: any;\n public userAuthConfig: any = {};\n public customerConfig!: ConfigInterface;\n\n public constructor() {\n const config = getFromMemory(SDK_CONFIG_KEY);\n\n this.customerConfig = config as ConfigInterface;\n this.api = new CrexApi(this.customerConfig, null);\n\n const user = this.customerConfig.OIDC.user;\n this.userAuthConfig = {\n providers: [\n {\n id: \"crex\",\n name: \"CREX\",\n type: \"oauth\",\n clientId: user.id,\n wellKnown: user.issuer,\n clientSecret: user.secret,\n authorization: { params: { scope: user.scope } },\n profile(profile: any) {\n return {\n id: profile.id || \"fake Id\",\n name: profile.name || \"Fake Name\",\n email: profile.email || \"fake Email\",\n image: profile.image || \"fake Image\",\n }\n },\n },\n ]\n }\n }\n\n public static setConfig(config: ConfigInterface) {\n saveInMemory(config, SDK_CONFIG_KEY);\n }\n}"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,72 @@
|
|
|
1
1
|
// src/requests.ts
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
import { API } from "@c-rex/constants";
|
|
4
|
+
import { Issuer } from "openid-client";
|
|
5
|
+
import { getFromMemory, saveInMemory } from "@c-rex/utils";
|
|
6
|
+
var CREX_TOKEN_HEADER_KEY = "crex-token";
|
|
7
|
+
var CREX_TOKEN_EXPIRY_HEADER_KEY = "crex-token-expiry";
|
|
4
8
|
var CrexApi = class {
|
|
9
|
+
config;
|
|
5
10
|
apiClient;
|
|
6
11
|
logger;
|
|
7
|
-
constructor(
|
|
12
|
+
constructor(config, logger) {
|
|
8
13
|
this.apiClient = axios.create({
|
|
9
|
-
baseURL: baseUrl,
|
|
14
|
+
baseURL: config.baseUrl,
|
|
10
15
|
headers: {
|
|
11
16
|
"content-Type": "application/json"
|
|
12
17
|
}
|
|
13
18
|
});
|
|
19
|
+
this.config = config;
|
|
14
20
|
this.logger = logger;
|
|
15
21
|
}
|
|
22
|
+
async manageToken() {
|
|
23
|
+
const headersAux = {};
|
|
24
|
+
if (this.config.OIDC.client.enabled) {
|
|
25
|
+
let token = getFromMemory(CREX_TOKEN_HEADER_KEY);
|
|
26
|
+
let tokenExpiry = getFromMemory(CREX_TOKEN_EXPIRY_HEADER_KEY);
|
|
27
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
28
|
+
if (!(token && tokenExpiry > now + 60)) {
|
|
29
|
+
const { token: tokenAux, tokenExpiry: tokenExpiryAux } = await this.getToken();
|
|
30
|
+
token = tokenAux;
|
|
31
|
+
saveInMemory(token, CREX_TOKEN_HEADER_KEY);
|
|
32
|
+
saveInMemory(tokenExpiryAux, CREX_TOKEN_EXPIRY_HEADER_KEY);
|
|
33
|
+
}
|
|
34
|
+
headersAux["Authorization"] = `Bearer ${token}`;
|
|
35
|
+
}
|
|
36
|
+
return headersAux;
|
|
37
|
+
}
|
|
38
|
+
async getToken() {
|
|
39
|
+
let token = "";
|
|
40
|
+
let tokenExpiry = 0;
|
|
41
|
+
try {
|
|
42
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
43
|
+
const issuer = await Issuer.discover(this.config.OIDC.client.issuer);
|
|
44
|
+
const client = new issuer.Client({
|
|
45
|
+
client_id: this.config.OIDC.client.id,
|
|
46
|
+
client_secret: this.config.OIDC.client.secret
|
|
47
|
+
});
|
|
48
|
+
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
49
|
+
token = tokenSet.access_token, tokenExpiry = now + tokenSet.expires_at;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.log("error", `API.getToken error when request ${this.config.OIDC.client.issuer}. Error: ${error}`);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
token,
|
|
55
|
+
tokenExpiry
|
|
56
|
+
};
|
|
57
|
+
}
|
|
16
58
|
async execute({
|
|
17
59
|
url,
|
|
18
60
|
method,
|
|
19
61
|
params,
|
|
20
62
|
body,
|
|
21
|
-
headers
|
|
63
|
+
headers = {}
|
|
22
64
|
}) {
|
|
23
65
|
let response = void 0;
|
|
66
|
+
headers = {
|
|
67
|
+
...headers,
|
|
68
|
+
...await this.manageToken()
|
|
69
|
+
};
|
|
24
70
|
for (let retry = 0; retry < API.MAX_RETRY; retry++) {
|
|
25
71
|
try {
|
|
26
72
|
response = await this.apiClient.request({
|
|
@@ -30,9 +76,15 @@ var CrexApi = class {
|
|
|
30
76
|
params,
|
|
31
77
|
headers
|
|
32
78
|
});
|
|
79
|
+
break;
|
|
33
80
|
} catch (error) {
|
|
34
|
-
|
|
35
|
-
|
|
81
|
+
console.log(
|
|
82
|
+
"error",
|
|
83
|
+
`API.execute ${retry + 1}\xBA error when request ${url}. Error: ${error}`
|
|
84
|
+
);
|
|
85
|
+
if (retry === API.MAX_RETRY - 1) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
36
88
|
}
|
|
37
89
|
}
|
|
38
90
|
if (response) {
|
|
@@ -42,74 +94,43 @@ var CrexApi = class {
|
|
|
42
94
|
}
|
|
43
95
|
};
|
|
44
96
|
|
|
45
|
-
// src/cookies.ts
|
|
46
|
-
var GLOBAL_KEY = "__CREX_INITIAL_CONFIG__";
|
|
47
|
-
var CONFIG_COOKIE_KEY = "crex_config";
|
|
48
|
-
function isBrowser() {
|
|
49
|
-
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
50
|
-
}
|
|
51
|
-
function parseCookies() {
|
|
52
|
-
const cookies = {};
|
|
53
|
-
if (typeof document === "undefined") {
|
|
54
|
-
return cookies;
|
|
55
|
-
}
|
|
56
|
-
document.cookie.split(";").forEach((cookie) => {
|
|
57
|
-
const [key, value] = cookie.split("=");
|
|
58
|
-
if (key && value) {
|
|
59
|
-
cookies[key.trim()] = decodeURIComponent(value.trim());
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return cookies;
|
|
63
|
-
}
|
|
64
|
-
function setCookie(name, value, days = 30) {
|
|
65
|
-
if (typeof document === "undefined") return;
|
|
66
|
-
const expires = new Date(Date.now() + days * 86400 * 1e3).toUTCString();
|
|
67
|
-
document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;
|
|
68
|
-
}
|
|
69
|
-
function setConfig(config) {
|
|
70
|
-
if (isBrowser()) {
|
|
71
|
-
setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));
|
|
72
|
-
} else {
|
|
73
|
-
if (typeof global !== "undefined" && !(GLOBAL_KEY in global)) {
|
|
74
|
-
global[GLOBAL_KEY] = null;
|
|
75
|
-
}
|
|
76
|
-
const globalConfig = global[GLOBAL_KEY];
|
|
77
|
-
if (globalConfig === null) {
|
|
78
|
-
global[GLOBAL_KEY] = config;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function getConfig() {
|
|
83
|
-
let returnValue;
|
|
84
|
-
if (isBrowser()) {
|
|
85
|
-
const cookies = parseCookies();
|
|
86
|
-
const configStr = cookies[CONFIG_COOKIE_KEY];
|
|
87
|
-
if (configStr) {
|
|
88
|
-
try {
|
|
89
|
-
return JSON.parse(configStr);
|
|
90
|
-
} catch {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
} else {
|
|
96
|
-
returnValue = global[GLOBAL_KEY];
|
|
97
|
-
}
|
|
98
|
-
return returnValue;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
97
|
// src/sdk.ts
|
|
98
|
+
import { SDK_CONFIG_KEY } from "@c-rex/constants";
|
|
99
|
+
import { getFromMemory as getFromMemory2, saveInMemory as saveInMemory2 } from "@c-rex/utils";
|
|
102
100
|
var CrexSDK = class {
|
|
103
|
-
customerConfig;
|
|
104
|
-
logger;
|
|
105
101
|
api;
|
|
102
|
+
logger;
|
|
103
|
+
userAuthConfig = {};
|
|
104
|
+
customerConfig;
|
|
106
105
|
constructor() {
|
|
107
|
-
const config =
|
|
106
|
+
const config = getFromMemory2(SDK_CONFIG_KEY);
|
|
108
107
|
this.customerConfig = config;
|
|
109
|
-
this.api = new CrexApi(this.customerConfig
|
|
108
|
+
this.api = new CrexApi(this.customerConfig, null);
|
|
109
|
+
const user = this.customerConfig.OIDC.user;
|
|
110
|
+
this.userAuthConfig = {
|
|
111
|
+
providers: [
|
|
112
|
+
{
|
|
113
|
+
id: "crex",
|
|
114
|
+
name: "CREX",
|
|
115
|
+
type: "oauth",
|
|
116
|
+
clientId: user.id,
|
|
117
|
+
wellKnown: user.issuer,
|
|
118
|
+
clientSecret: user.secret,
|
|
119
|
+
authorization: { params: { scope: user.scope } },
|
|
120
|
+
profile(profile) {
|
|
121
|
+
return {
|
|
122
|
+
id: profile.id || "fake Id",
|
|
123
|
+
name: profile.name || "Fake Name",
|
|
124
|
+
email: profile.email || "fake Email",
|
|
125
|
+
image: profile.image || "fake Image"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
};
|
|
110
131
|
}
|
|
111
132
|
static setConfig(config) {
|
|
112
|
-
|
|
133
|
+
saveInMemory2(config, SDK_CONFIG_KEY);
|
|
113
134
|
}
|
|
114
135
|
};
|
|
115
136
|
export {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/requests.ts","../src/
|
|
1
|
+
{"version":3,"sources":["../src/requests.ts","../src/sdk.ts"],"sourcesContent":["import axios, { AxiosResponse, Method, AxiosInstance } from \"axios\";\nimport { API } from \"@c-rex/constants\";\nimport { Issuer } from \"openid-client\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getFromMemory, saveInMemory } from \"@c-rex/utils\";\n\nconst CREX_TOKEN_HEADER_KEY = \"crex-token\";\nconst CREX_TOKEN_EXPIRY_HEADER_KEY = \"crex-token-expiry\";\n\ninterface APIGenericResponse<T> extends AxiosResponse {\n data: T;\n statusCode: number;\n}\n\ninterface CallParams {\n url: string;\n method: Method;\n body?: any;\n headers?: any;\n params?: any;\n}\n\nexport class CrexApi {\n private config: ConfigInterface;\n private apiClient: AxiosInstance;\n private logger: any;\n\n public constructor(config: ConfigInterface, logger: any) {\n this.apiClient = axios.create({\n baseURL: config.baseUrl,\n headers: {\n \"content-Type\": \"application/json\",\n },\n });\n\n this.config = config;\n this.logger = logger;\n }\n\n private async manageToken() {\n const headersAux: Record<string, string> = {};\n\n if (this.config.OIDC.client.enabled) {\n let token = getFromMemory(CREX_TOKEN_HEADER_KEY);\n let tokenExpiry = getFromMemory(CREX_TOKEN_EXPIRY_HEADER_KEY);\n\n const now = Math.floor(Date.now() / 1000);\n\n if (!(token && tokenExpiry > now + 60)) {\n const { token: tokenAux, tokenExpiry: tokenExpiryAux } = await this.getToken();\n\n token = tokenAux;\n\n saveInMemory(token, CREX_TOKEN_HEADER_KEY);\n saveInMemory(tokenExpiryAux, CREX_TOKEN_EXPIRY_HEADER_KEY);\n }\n\n headersAux['Authorization'] = `Bearer ${token}`;\n }\n\n return headersAux;\n }\n\n private async getToken(): Promise<{\n token: string;\n tokenExpiry: number;\n }> {\n let token = \"\";\n let tokenExpiry = 0;\n\n try {\n const now = Math.floor(Date.now() / 1000);\n const issuer = await Issuer.discover(this.config.OIDC.client.issuer);\n const client = new issuer.Client({\n client_id: this.config.OIDC.client.id,\n client_secret: this.config.OIDC.client.secret,\n });\n const tokenSet = await client.grant({ grant_type: 'client_credentials' });\n\n token = tokenSet.access_token!,\n tokenExpiry = now + tokenSet.expires_at!\n } catch (error) {\n console.log(\"error\", `API.getToken error when request ${this.config.OIDC.client.issuer}. Error: ${error}`);\n }\n\n return {\n token,\n tokenExpiry\n };\n }\n\n async execute<T>({\n url,\n method,\n params,\n body,\n headers = {},\n }: CallParams): Promise<T> {\n let response: APIGenericResponse<T> | undefined = undefined;\n\n headers = {\n ...headers,\n ...await this.manageToken(),\n };\n\n for (let retry = 0; retry < API.MAX_RETRY; retry++) {\n try {\n response = await this.apiClient.request({\n url,\n method,\n data: body,\n params,\n headers,\n });\n\n break;\n } catch (error) {\n console.log(\n \"error\",\n `API.execute ${retry + 1}º error when request ${url}. Error: ${error}`\n );\n\n if (retry === API.MAX_RETRY - 1) {\n throw error;\n }\n }\n }\n\n if (response) {\n return response.data;\n }\n\n throw new Error(\"API.execute error: Failed to retrieve a valid response\");\n }\n}","import { SDK_CONFIG_KEY } from \"@c-rex/constants\";\nimport { CrexApi } from \"./requests\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getFromMemory, saveInMemory } from \"@c-rex/utils\";\n\n\nexport class CrexSDK {\n public api!: CrexApi;\n public logger!: any;\n public userAuthConfig: any = {};\n public customerConfig!: ConfigInterface;\n\n public constructor() {\n const config = getFromMemory(SDK_CONFIG_KEY);\n\n this.customerConfig = config as ConfigInterface;\n this.api = new CrexApi(this.customerConfig, null);\n\n const user = this.customerConfig.OIDC.user;\n this.userAuthConfig = {\n providers: [\n {\n id: \"crex\",\n name: \"CREX\",\n type: \"oauth\",\n clientId: user.id,\n wellKnown: user.issuer,\n clientSecret: user.secret,\n authorization: { params: { scope: user.scope } },\n profile(profile: any) {\n return {\n id: profile.id || \"fake Id\",\n name: profile.name || \"Fake Name\",\n email: profile.email || \"fake Email\",\n image: profile.image || \"fake Image\",\n }\n },\n },\n ]\n }\n }\n\n public static setConfig(config: ConfigInterface) {\n saveInMemory(config, SDK_CONFIG_KEY);\n }\n}"],"mappings":";AAAA,OAAO,WAAqD;AAC5D,SAAS,WAAW;AACpB,SAAS,cAAc;AAEvB,SAAS,eAAe,oBAAoB;AAE5C,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ACJ,MAAM;AAAA,UACN,MAAM;AAAA,UACN,UAAU,KAAK;AAAA,UACf,WAAW,KAAK;AAAA,UAChB,cAAc,KAAK;AAAA,UACnB,eAAe,EAAE,QAAQ,EAAE,OAAO,KAAK,MAAM,EAAE;AAAA,UAC/C,QAAQ,SAAc;AAClB,mBAAO;AAAA,cACH,IAAI,QAAQ,MAAM;AAAA,cAClB,MAAM,QAAQ,QAAQ;AAAA,cACtB,OAAO,QAAQ,SAAS;AAAA,cACxB,OAAO,QAAQ,SAAS;AAAA,YAC5B;AAAA,UACJ;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,OAAc,UAAU,QAAyB;AAC7C,IAAAC,cAAa,QAAQ,cAAc;AAAA,EACvC;AACJ;","names":["getFromMemory","saveInMemory"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c-rex/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -46,7 +46,10 @@
|
|
|
46
46
|
"@c-rex/constants": "*",
|
|
47
47
|
"@c-rex/interfaces": "*",
|
|
48
48
|
"@c-rex/types": "*",
|
|
49
|
+
"@c-rex/utils": "*",
|
|
49
50
|
"axios": "^1.8.4",
|
|
51
|
+
"next-auth": "^4.24.11",
|
|
52
|
+
"openid-client": "^5.7.1",
|
|
50
53
|
"winston": "^3.17.0",
|
|
51
54
|
"winston-graylog2": "^2.1.2"
|
|
52
55
|
}
|