@c-rex/core 0.1.12 → 0.1.14
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/{api/token.d.mts → OIDC.d.mts} +7 -4
- package/dist/{api/token.d.ts → OIDC.d.ts} +7 -4
- package/dist/OIDC.js +578 -0
- package/dist/OIDC.js.map +1 -0
- package/dist/OIDC.mjs +542 -0
- package/dist/OIDC.mjs.map +1 -0
- package/dist/logger.js +356 -9
- package/dist/logger.js.map +1 -1
- package/dist/logger.mjs +356 -9
- package/dist/logger.mjs.map +1 -1
- package/dist/requests.d.mts +44 -0
- package/dist/requests.d.ts +44 -0
- package/dist/requests.js +670 -0
- package/dist/requests.js.map +1 -0
- package/dist/requests.mjs +635 -0
- package/dist/requests.mjs.map +1 -0
- package/dist/sdk.d.mts +25 -0
- package/dist/sdk.d.ts +25 -0
- package/dist/sdk.js +389 -0
- package/dist/sdk.js.map +1 -0
- package/dist/sdk.mjs +362 -0
- package/dist/sdk.mjs.map +1 -0
- package/package.json +16 -22
- package/dist/api/cookies.d.mts +0 -17
- package/dist/api/cookies.d.ts +0 -17
- package/dist/api/cookies.js +0 -66
- package/dist/api/cookies.js.map +0 -1
- package/dist/api/cookies.mjs +0 -40
- package/dist/api/cookies.mjs.map +0 -1
- package/dist/api/rpc.d.mts +0 -16
- package/dist/api/rpc.d.ts +0 -16
- package/dist/api/rpc.js +0 -231
- package/dist/api/rpc.js.map +0 -1
- package/dist/api/rpc.mjs +0 -194
- package/dist/api/rpc.mjs.map +0 -1
- package/dist/api/token.js +0 -88
- package/dist/api/token.js.map +0 -1
- package/dist/api/token.mjs +0 -63
- package/dist/api/token.mjs.map +0 -1
- package/dist/index.d.mts +0 -81
- package/dist/index.d.ts +0 -81
- package/dist/index.js +0 -418
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -377
- package/dist/index.mjs.map +0 -1
package/dist/api/token.mjs
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// src/api/token.ts
|
|
2
|
-
import { NextResponse } from "next/server";
|
|
3
|
-
|
|
4
|
-
// ../constants/src/index.ts
|
|
5
|
-
var DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1e3;
|
|
6
|
-
var CREX_TOKEN_HEADER_KEY = "crex-token";
|
|
7
|
-
|
|
8
|
-
// src/token.ts
|
|
9
|
-
import { Issuer } from "openid-client";
|
|
10
|
-
|
|
11
|
-
// src/config.ts
|
|
12
|
-
import { cookies } from "next/headers";
|
|
13
|
-
function getServerConfig() {
|
|
14
|
-
if (!global.__GLOBAL_CONFIG__) {
|
|
15
|
-
throw new Error("Server config not initialized");
|
|
16
|
-
}
|
|
17
|
-
return global.__GLOBAL_CONFIG__;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// src/token.ts
|
|
21
|
-
var getToken = async () => {
|
|
22
|
-
console.log("veio buscar");
|
|
23
|
-
const config = getServerConfig();
|
|
24
|
-
const issuer = await Issuer.discover(config.OIDC.client.issuer);
|
|
25
|
-
const client = new issuer.Client({
|
|
26
|
-
client_id: config.OIDC.client.id,
|
|
27
|
-
client_secret: config.OIDC.client.secret
|
|
28
|
-
});
|
|
29
|
-
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
30
|
-
const token = tokenSet.access_token;
|
|
31
|
-
const expiresAt = tokenSet.expires_at;
|
|
32
|
-
if (!token) {
|
|
33
|
-
throw new Error("Failed to get token");
|
|
34
|
-
}
|
|
35
|
-
return { token, expiresAt };
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// src/api/token.ts
|
|
39
|
-
var postMethod = async () => {
|
|
40
|
-
try {
|
|
41
|
-
const { token, expiresAt } = await getToken();
|
|
42
|
-
if (!token) {
|
|
43
|
-
return NextResponse.json({ error: "Failed to get token" }, { status: 500 });
|
|
44
|
-
}
|
|
45
|
-
const response = NextResponse.json({ token });
|
|
46
|
-
response.cookies.set({
|
|
47
|
-
name: CREX_TOKEN_HEADER_KEY,
|
|
48
|
-
value: token,
|
|
49
|
-
httpOnly: true,
|
|
50
|
-
secure: process.env.NODE_ENV === "production",
|
|
51
|
-
sameSite: "lax",
|
|
52
|
-
path: "/",
|
|
53
|
-
expires: expiresAt ? new Date(expiresAt * 1e3) : void 0
|
|
54
|
-
});
|
|
55
|
-
return response;
|
|
56
|
-
} catch (error) {
|
|
57
|
-
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
export {
|
|
61
|
-
postMethod
|
|
62
|
-
};
|
|
63
|
-
//# sourceMappingURL=token.mjs.map
|
package/dist/api/token.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/api/token.ts","../../../constants/src/index.ts","../../src/token.ts","../../src/config.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { CREX_TOKEN_HEADER_KEY } from '@c-rex/constants';\nimport { getToken } from '../token';\n\n/**\n * Retrieves an access token using client credentials flow from the configured OIDC provider\n * \n * @returns NextResponse with success status or error message\n * @throws Error if token retrieval fails\n */\nexport const postMethod = async (): Promise<NextResponse> => {\n try {\n const { token, expiresAt } = await getToken();\n\n if (!token) {\n return NextResponse.json({ error: 'Failed to get token' }, { status: 500 });\n }\n\n const response = NextResponse.json({ token: token });\n\n response.cookies.set({\n name: CREX_TOKEN_HEADER_KEY,\n value: token,\n httpOnly: true,\n secure: process.env.NODE_ENV === 'production',\n sameSite: 'lax',\n path: '/',\n expires: expiresAt ? new Date(expiresAt * 1000) : undefined\n });\n\n return response;\n\n } catch (error) {\n return NextResponse.json({ error: String(error) }, { status: 500 });\n }\n}\n","export const ALL = \"*\"\n\nexport const LOG_CATEGORIES = [\n \"NoLicense\",\n \"Scenario\",\n \"Favorites\",\n \"Subscription\",\n \"Share\",\n \"Document\",\n \"Search\",\n \"History\",\n \"Notification\",\n \"UserProfile\",\n] as const;\n\nexport const LOG_LEVELS = {\n critical: 2,\n error: 3,\n warning: 4,\n info: 6,\n debug: 7,\n} as const;\n\nexport const RESULT_VIEW_STYLES = [\n \"cards\",\n \"table\",\n \"table-with-images\",\n] as const;\n\nexport const API = {\n MAX_RETRY: 3,\n API_TIMEOUT: 10000,\n RETRY_DELAY: 500,\n API_HEADERS: {\n \"content-Type\": \"application/json\",\n },\n};\n\nexport const SDK_CONFIG_KEY = \"crex-sdk-config\";\n\nexport const CONTENT_LANG_KEY = \"CONTENT_LANG_KEY\";\n\nexport const AVAILABLE_CONTENT_LANG_KEY = \"AVAILABLE_CONTENT_LANG_KEY\";\n\nexport const UI_LANG_KEY = \"UI_LANG_KEY\";\n\nexport const FLAGS_BY_LANG = {\n \"en\": \"US\",\n \"de\": \"DE\",\n};\n\nexport const DEFAULT_LANG = \"en-US\";\n\nexport const EN_LANG = \"en\";\n\nexport const UI_LANG_OPTIONS = [\"en-us\", \"de-de\"];\n\nexport const TOPICS_TYPE_AND_LINK = \"topics\";\nexport const BLOG_TYPE_AND_LINK = \"blog\";\nexport const DOCUMENTS_TYPE_AND_LINK = \"documents\";\n\nexport const TOPIC = \"TOPIC\";\nexport const DOCUMENT = \"DOCUMENT\";\nexport const PACKAGE = \"PACKAGE\";\nexport const FRAGMENT = \"FRAGMENT\";\n\nexport const RESULT_TYPES = {\n TOPIC: TOPIC,\n DOCUMENT: DOCUMENT,\n PACKAGE: PACKAGE,\n FRAGMENT: FRAGMENT\n} as const;\n\nexport const FILES_EXTENSIONS = {\n PDF: \"application/pdf\",\n HTML: \"text/html\",\n} as const;\n\nexport const DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds\n\nexport const ICONS_BY_FILE_EXTENSION = {\n \"application/pdf\": \"FaFilePdf\",\n} as const;\n\nexport const DEFAULT_ICON = \"file\";\n\nexport const CREX_TOKEN_HEADER_KEY = \"crex-token\";\n\nexport const WILD_CARD_OPTIONS = {\n BOTH: \"BOTH\",\n END: \"END\",\n START: \"START\",\n NONE: \"NONE\",\n} as const;\n\nexport const OPERATOR_OPTIONS = {\n AND: \"AND\",\n OR: \"OR\",\n} as const;\n\nexport const ARTICLE_PAGE_LAYOUT = {\n BLOG: \"BLOG\",\n DOCUMENT: \"DOCUMENT\",\n} as const;\n\nexport const DEVICE_OPTIONS = {\n MOBILE: \"mobile\",\n TABLET: \"tablet\",\n DESKTOP: \"desktop\",\n}\nexport const BREAKPOINTS = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n \"2xl\": 1536,\n};","import { Issuer } from 'openid-client';\nimport { getServerConfig } from './config';\n\n/**\n * Retrieves an access token using client credentials flow from the configured OIDC provider\n * \n * @returns NextResponse with success status or error message\n * @throws Error if token retrieval fails\n */\nexport const getToken = async (): Promise<{ token: string; expiresAt: number }> => {\n\n console.log(\"veio buscar\")\n\n const config = getServerConfig();\n\n const issuer = await Issuer.discover(config.OIDC.client.issuer);\n const client = new issuer.Client({\n client_id: config.OIDC.client.id,\n client_secret: config.OIDC.client.secret,\n });\n const tokenSet = await client.grant({ grant_type: 'client_credentials' });\n\n const token = tokenSet.access_token!;\n const expiresAt = tokenSet.expires_at!;\n\n if (!token) {\n throw new Error('Failed to get token');\n }\n\n return { token, expiresAt };\n}\n","import { ConfigInterface } from \"@c-rex/interfaces\";\nimport { SDK_CONFIG_KEY } from '@c-rex/constants';\nimport { CookiesConfigs } from '@c-rex/interfaces';\nimport { cookies } from 'next/headers';\n\n/**\n * Retrieves and parses configuration data from a cookie.\n * @returns The parsed configuration object\n * @throws Error if the configuration cookie is not found or cannot be parsed\n */\nexport const getClientConfig = (): CookiesConfigs => {\n const jsonConfigs = cookies().get(SDK_CONFIG_KEY)?.value;\n if (!jsonConfigs) {\n throw new Error('Configs not found');\n }\n\n const configs: CookiesConfigs = JSON.parse(jsonConfigs);\n\n return configs;\n}\n\ndeclare global {\n // eslint-disable-next-line no-var\n var __GLOBAL_CONFIG__: ConfigInterface | null;\n}\n\nexport function initializeConfig(config: ConfigInterface) {\n if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;\n global.__GLOBAL_CONFIG__ = config;\n return global.__GLOBAL_CONFIG__;\n}\n\nexport function getServerConfig() {\n if (!global.__GLOBAL_CONFIG__) {\n throw new Error('Server config not initialized');\n }\n return global.__GLOBAL_CONFIG__;\n}"],"mappings":";AAAA,SAAS,oBAAoB;;;AC8EtB,IAAM,uBAAuB,KAAK,KAAK,KAAK,KAAK;AAQjD,IAAM,wBAAwB;;;ACtFrC,SAAS,cAAc;;;ACGvB,SAAS,eAAe;AA6BjB,SAAS,kBAAkB;AAC9B,MAAI,CAAC,OAAO,mBAAmB;AAC3B,UAAM,IAAI,MAAM,+BAA+B;AAAA,EACnD;AACA,SAAO,OAAO;AAClB;;;AD5BO,IAAM,WAAW,YAA2D;AAE/E,UAAQ,IAAI,aAAa;AAEzB,QAAM,SAAS,gBAAgB;AAE/B,QAAM,SAAS,MAAM,OAAO,SAAS,OAAO,KAAK,OAAO,MAAM;AAC9D,QAAM,SAAS,IAAI,OAAO,OAAO;AAAA,IAC7B,WAAW,OAAO,KAAK,OAAO;AAAA,IAC9B,eAAe,OAAO,KAAK,OAAO;AAAA,EACtC,CAAC;AACD,QAAM,WAAW,MAAM,OAAO,MAAM,EAAE,YAAY,qBAAqB,CAAC;AAExE,QAAM,QAAQ,SAAS;AACvB,QAAM,YAAY,SAAS;AAE3B,MAAI,CAAC,OAAO;AACR,UAAM,IAAI,MAAM,qBAAqB;AAAA,EACzC;AAEA,SAAO,EAAE,OAAO,UAAU;AAC9B;;;AFpBO,IAAM,aAAa,YAAmC;AACzD,MAAI;AACA,UAAM,EAAE,OAAO,UAAU,IAAI,MAAM,SAAS;AAE5C,QAAI,CAAC,OAAO;AACR,aAAO,aAAa,KAAK,EAAE,OAAO,sBAAsB,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC9E;AAEA,UAAM,WAAW,aAAa,KAAK,EAAE,MAAa,CAAC;AAEnD,aAAS,QAAQ,IAAI;AAAA,MACjB,MAAM;AAAA,MACN,OAAO;AAAA,MACP,UAAU;AAAA,MACV,QAAQ,QAAQ,IAAI,aAAa;AAAA,MACjC,UAAU;AAAA,MACV,MAAM;AAAA,MACN,SAAS,YAAY,IAAI,KAAK,YAAY,GAAI,IAAI;AAAA,IACtD,CAAC;AAED,WAAO;AAAA,EAEX,SAAS,OAAO;AACZ,WAAO,aAAa,KAAK,EAAE,OAAO,OAAO,KAAK,EAAE,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtE;AACJ;","names":[]}
|
package/dist/index.d.mts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Method } from 'axios';
|
|
2
|
-
import { ConfigInterface, CookiesConfigs } from '@c-rex/interfaces';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Interface for API call parameters.
|
|
6
|
-
*/
|
|
7
|
-
interface CallParams {
|
|
8
|
-
url: string;
|
|
9
|
-
method: Method;
|
|
10
|
-
body?: any;
|
|
11
|
-
headers?: any;
|
|
12
|
-
params?: any;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* API client class for the CREX application.
|
|
16
|
-
* Handles API requests with caching, authentication, and retry logic.
|
|
17
|
-
*/
|
|
18
|
-
declare class CrexApi {
|
|
19
|
-
private customerConfig;
|
|
20
|
-
private apiClient;
|
|
21
|
-
private logger;
|
|
22
|
-
private publicNextApiUrl;
|
|
23
|
-
/**
|
|
24
|
-
* Initializes the API client if it hasn't been initialized yet.
|
|
25
|
-
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
26
|
-
*
|
|
27
|
-
* @private
|
|
28
|
-
*/
|
|
29
|
-
private initAPI;
|
|
30
|
-
private manageToken;
|
|
31
|
-
/**
|
|
32
|
-
* Executes an API request with caching, authentication, and retry logic.
|
|
33
|
-
*
|
|
34
|
-
* @param options - Request options
|
|
35
|
-
* @param options.url - The URL to request
|
|
36
|
-
* @param options.method - The HTTP method to use
|
|
37
|
-
* @param options.params - Optional query parameters
|
|
38
|
-
* @param options.body - Optional request body
|
|
39
|
-
* @param options.headers - Optional request headers
|
|
40
|
-
* @returns The response data
|
|
41
|
-
* @throws Error if the request fails after maximum retries
|
|
42
|
-
*/
|
|
43
|
-
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<T>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* SDK class for the CREX application.
|
|
48
|
-
* Provides configuration and authentication functionality.
|
|
49
|
-
*/
|
|
50
|
-
declare class CrexSDK {
|
|
51
|
-
userAuthConfig: any;
|
|
52
|
-
customerConfig: ConfigInterface;
|
|
53
|
-
/**
|
|
54
|
-
* Retrieves the customer configuration if it hasn't been loaded yet.
|
|
55
|
-
*
|
|
56
|
-
* @private
|
|
57
|
-
*/
|
|
58
|
-
private getServerConfig;
|
|
59
|
-
/**
|
|
60
|
-
* Retrieves the user authentication configuration.
|
|
61
|
-
* If not already loaded, it will load the customer configuration and
|
|
62
|
-
* create the auth config based on OIDC settings.
|
|
63
|
-
*
|
|
64
|
-
* @returns The user authentication configuration object
|
|
65
|
-
*/
|
|
66
|
-
getUserAuthConfig(): Promise<any>;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Retrieves and parses configuration data from a cookie.
|
|
71
|
-
* @returns The parsed configuration object
|
|
72
|
-
* @throws Error if the configuration cookie is not found or cannot be parsed
|
|
73
|
-
*/
|
|
74
|
-
declare const getClientConfig: () => CookiesConfigs;
|
|
75
|
-
declare global {
|
|
76
|
-
var __GLOBAL_CONFIG__: ConfigInterface | null;
|
|
77
|
-
}
|
|
78
|
-
declare function initializeConfig(config: ConfigInterface): ConfigInterface;
|
|
79
|
-
declare function getServerConfig(): ConfigInterface;
|
|
80
|
-
|
|
81
|
-
export { CrexApi, CrexSDK, getClientConfig, getServerConfig, initializeConfig };
|
package/dist/index.d.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Method } from 'axios';
|
|
2
|
-
import { ConfigInterface, CookiesConfigs } from '@c-rex/interfaces';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Interface for API call parameters.
|
|
6
|
-
*/
|
|
7
|
-
interface CallParams {
|
|
8
|
-
url: string;
|
|
9
|
-
method: Method;
|
|
10
|
-
body?: any;
|
|
11
|
-
headers?: any;
|
|
12
|
-
params?: any;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* API client class for the CREX application.
|
|
16
|
-
* Handles API requests with caching, authentication, and retry logic.
|
|
17
|
-
*/
|
|
18
|
-
declare class CrexApi {
|
|
19
|
-
private customerConfig;
|
|
20
|
-
private apiClient;
|
|
21
|
-
private logger;
|
|
22
|
-
private publicNextApiUrl;
|
|
23
|
-
/**
|
|
24
|
-
* Initializes the API client if it hasn't been initialized yet.
|
|
25
|
-
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
26
|
-
*
|
|
27
|
-
* @private
|
|
28
|
-
*/
|
|
29
|
-
private initAPI;
|
|
30
|
-
private manageToken;
|
|
31
|
-
/**
|
|
32
|
-
* Executes an API request with caching, authentication, and retry logic.
|
|
33
|
-
*
|
|
34
|
-
* @param options - Request options
|
|
35
|
-
* @param options.url - The URL to request
|
|
36
|
-
* @param options.method - The HTTP method to use
|
|
37
|
-
* @param options.params - Optional query parameters
|
|
38
|
-
* @param options.body - Optional request body
|
|
39
|
-
* @param options.headers - Optional request headers
|
|
40
|
-
* @returns The response data
|
|
41
|
-
* @throws Error if the request fails after maximum retries
|
|
42
|
-
*/
|
|
43
|
-
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<T>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* SDK class for the CREX application.
|
|
48
|
-
* Provides configuration and authentication functionality.
|
|
49
|
-
*/
|
|
50
|
-
declare class CrexSDK {
|
|
51
|
-
userAuthConfig: any;
|
|
52
|
-
customerConfig: ConfigInterface;
|
|
53
|
-
/**
|
|
54
|
-
* Retrieves the customer configuration if it hasn't been loaded yet.
|
|
55
|
-
*
|
|
56
|
-
* @private
|
|
57
|
-
*/
|
|
58
|
-
private getServerConfig;
|
|
59
|
-
/**
|
|
60
|
-
* Retrieves the user authentication configuration.
|
|
61
|
-
* If not already loaded, it will load the customer configuration and
|
|
62
|
-
* create the auth config based on OIDC settings.
|
|
63
|
-
*
|
|
64
|
-
* @returns The user authentication configuration object
|
|
65
|
-
*/
|
|
66
|
-
getUserAuthConfig(): Promise<any>;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Retrieves and parses configuration data from a cookie.
|
|
71
|
-
* @returns The parsed configuration object
|
|
72
|
-
* @throws Error if the configuration cookie is not found or cannot be parsed
|
|
73
|
-
*/
|
|
74
|
-
declare const getClientConfig: () => CookiesConfigs;
|
|
75
|
-
declare global {
|
|
76
|
-
var __GLOBAL_CONFIG__: ConfigInterface | null;
|
|
77
|
-
}
|
|
78
|
-
declare function initializeConfig(config: ConfigInterface): ConfigInterface;
|
|
79
|
-
declare function getServerConfig(): ConfigInterface;
|
|
80
|
-
|
|
81
|
-
export { CrexApi, CrexSDK, getClientConfig, getServerConfig, initializeConfig };
|
package/dist/index.js
DELETED
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
CrexApi: () => CrexApi,
|
|
34
|
-
CrexSDK: () => CrexSDK,
|
|
35
|
-
getClientConfig: () => getClientConfig,
|
|
36
|
-
getServerConfig: () => getServerConfig,
|
|
37
|
-
initializeConfig: () => initializeConfig
|
|
38
|
-
});
|
|
39
|
-
module.exports = __toCommonJS(index_exports);
|
|
40
|
-
|
|
41
|
-
// src/requests.ts
|
|
42
|
-
var import_axios = __toESM(require("axios"));
|
|
43
|
-
|
|
44
|
-
// ../constants/src/index.ts
|
|
45
|
-
var ALL = "*";
|
|
46
|
-
var LOG_LEVELS = {
|
|
47
|
-
critical: 2,
|
|
48
|
-
error: 3,
|
|
49
|
-
warning: 4,
|
|
50
|
-
info: 6,
|
|
51
|
-
debug: 7
|
|
52
|
-
};
|
|
53
|
-
var SDK_CONFIG_KEY = "crex-sdk-config";
|
|
54
|
-
var DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1e3;
|
|
55
|
-
var CREX_TOKEN_HEADER_KEY = "crex-token";
|
|
56
|
-
|
|
57
|
-
// src/requests.ts
|
|
58
|
-
var import_headers2 = require("next/headers");
|
|
59
|
-
|
|
60
|
-
// src/logger.ts
|
|
61
|
-
var import_winston = __toESM(require("winston"));
|
|
62
|
-
|
|
63
|
-
// src/transports/matomo.ts
|
|
64
|
-
var import_winston_transport = __toESM(require("winston-transport"));
|
|
65
|
-
var MatomoTransport = class extends import_winston_transport.default {
|
|
66
|
-
matomoTransport;
|
|
67
|
-
configs;
|
|
68
|
-
/**
|
|
69
|
-
* Creates a new instance of MatomoTransport.
|
|
70
|
-
*
|
|
71
|
-
* @param configs - The application configuration containing logging settings
|
|
72
|
-
*/
|
|
73
|
-
constructor(configs) {
|
|
74
|
-
super({
|
|
75
|
-
level: configs.logs.matomo.minimumLevel,
|
|
76
|
-
silent: configs.logs.matomo.silent
|
|
77
|
-
});
|
|
78
|
-
this.matomoTransport = new import_winston_transport.default();
|
|
79
|
-
this.configs = configs;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Logs a message to Matomo if the message category is included in the configured categories.
|
|
83
|
-
*
|
|
84
|
-
* @param info - The log information including level, message, and category
|
|
85
|
-
* @param callback - Callback function to execute after logging
|
|
86
|
-
*/
|
|
87
|
-
log(info, callback) {
|
|
88
|
-
const matomoCategory = this.configs.logs.matomo.categoriesLevel;
|
|
89
|
-
if (matomoCategory.includes(info.category) || matomoCategory.includes(ALL)) {
|
|
90
|
-
this.matomoTransport.log(info, callback);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// src/transports/graylog.ts
|
|
96
|
-
var import_winston_transport2 = __toESM(require("winston-transport"));
|
|
97
|
-
var import_winston_graylog2 = __toESM(require("winston-graylog2"));
|
|
98
|
-
var GraylogTransport = class extends import_winston_transport2.default {
|
|
99
|
-
graylogTransport;
|
|
100
|
-
configs;
|
|
101
|
-
/**
|
|
102
|
-
* Creates a new instance of GraylogTransport.
|
|
103
|
-
*
|
|
104
|
-
* @param configs - The application configuration containing logging settings
|
|
105
|
-
*/
|
|
106
|
-
constructor(configs) {
|
|
107
|
-
if (!configs.logs.graylog.hostname || configs.logs.graylog.port === void 0) {
|
|
108
|
-
throw new Error("Graylog hostname and port must be defined");
|
|
109
|
-
}
|
|
110
|
-
super({
|
|
111
|
-
level: configs.logs.graylog.minimumLevel,
|
|
112
|
-
silent: configs.logs.graylog.silent
|
|
113
|
-
});
|
|
114
|
-
this.configs = configs;
|
|
115
|
-
this.graylogTransport = new import_winston_graylog2.default({
|
|
116
|
-
name: configs.logs.graylog.app,
|
|
117
|
-
silent: configs.logs.graylog.silent,
|
|
118
|
-
handleExceptions: false,
|
|
119
|
-
graylog: {
|
|
120
|
-
servers: [
|
|
121
|
-
{ host: "localhost", port: 12201 },
|
|
122
|
-
{ host: configs.logs.graylog.hostname, port: configs.logs.graylog.port }
|
|
123
|
-
]
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Logs a message to Graylog if the message category is included in the configured categories.
|
|
129
|
-
*
|
|
130
|
-
* @param info - The log information including level, message, and category
|
|
131
|
-
* @param callback - Callback function to execute after logging
|
|
132
|
-
*/
|
|
133
|
-
log(info, callback) {
|
|
134
|
-
const graylogCategory = this.configs.logs.graylog.categoriesLevel;
|
|
135
|
-
if (graylogCategory.includes(info.category) || graylogCategory.includes(ALL)) {
|
|
136
|
-
this.graylogTransport.log(info, callback);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// src/config.ts
|
|
142
|
-
var import_headers = require("next/headers");
|
|
143
|
-
var getClientConfig = () => {
|
|
144
|
-
const jsonConfigs = (0, import_headers.cookies)().get(SDK_CONFIG_KEY)?.value;
|
|
145
|
-
if (!jsonConfigs) {
|
|
146
|
-
throw new Error("Configs not found");
|
|
147
|
-
}
|
|
148
|
-
const configs = JSON.parse(jsonConfigs);
|
|
149
|
-
return configs;
|
|
150
|
-
};
|
|
151
|
-
function initializeConfig(config) {
|
|
152
|
-
if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;
|
|
153
|
-
global.__GLOBAL_CONFIG__ = config;
|
|
154
|
-
return global.__GLOBAL_CONFIG__;
|
|
155
|
-
}
|
|
156
|
-
function getServerConfig() {
|
|
157
|
-
if (!global.__GLOBAL_CONFIG__) {
|
|
158
|
-
throw new Error("Server config not initialized");
|
|
159
|
-
}
|
|
160
|
-
return global.__GLOBAL_CONFIG__;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// src/logger.ts
|
|
164
|
-
var CrexLogger = class {
|
|
165
|
-
customerConfig;
|
|
166
|
-
logger;
|
|
167
|
-
/**
|
|
168
|
-
* Initializes the logger instance if it hasn't been initialized yet.
|
|
169
|
-
* Loads customer configuration and creates the logger with appropriate transports.
|
|
170
|
-
*
|
|
171
|
-
* @private
|
|
172
|
-
*/
|
|
173
|
-
async initLogger() {
|
|
174
|
-
try {
|
|
175
|
-
if (!this.customerConfig) {
|
|
176
|
-
this.customerConfig = getServerConfig();
|
|
177
|
-
}
|
|
178
|
-
if (!this.logger) {
|
|
179
|
-
this.logger = this.createLogger();
|
|
180
|
-
}
|
|
181
|
-
} catch (error) {
|
|
182
|
-
console.log("Error initializing logger:", error);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Logs a message with the specified level and optional category.
|
|
187
|
-
*
|
|
188
|
-
* @param options - Logging options
|
|
189
|
-
* @param options.level - The log level (error, warn, info, etc.)
|
|
190
|
-
* @param options.message - The message to log
|
|
191
|
-
* @param options.category - Optional category for the log message
|
|
192
|
-
*/
|
|
193
|
-
async log({ level, message, category }) {
|
|
194
|
-
await this.initLogger();
|
|
195
|
-
this.logger.log(level, message, category);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Creates a new Winston logger instance with configured transports.
|
|
199
|
-
*
|
|
200
|
-
* @private
|
|
201
|
-
* @returns A configured Winston logger instance
|
|
202
|
-
*/
|
|
203
|
-
createLogger() {
|
|
204
|
-
return import_winston.default.createLogger({
|
|
205
|
-
levels: LOG_LEVELS,
|
|
206
|
-
transports: [
|
|
207
|
-
new import_winston.default.transports.Console({
|
|
208
|
-
level: this.customerConfig.logs.console.minimumLevel,
|
|
209
|
-
silent: this.customerConfig.logs.console.silent
|
|
210
|
-
}),
|
|
211
|
-
new MatomoTransport(this.customerConfig),
|
|
212
|
-
new GraylogTransport(this.customerConfig)
|
|
213
|
-
]
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
// src/token.ts
|
|
219
|
-
var import_openid_client = require("openid-client");
|
|
220
|
-
var getToken = async () => {
|
|
221
|
-
console.log("veio buscar");
|
|
222
|
-
const config = getServerConfig();
|
|
223
|
-
const issuer = await import_openid_client.Issuer.discover(config.OIDC.client.issuer);
|
|
224
|
-
const client = new issuer.Client({
|
|
225
|
-
client_id: config.OIDC.client.id,
|
|
226
|
-
client_secret: config.OIDC.client.secret
|
|
227
|
-
});
|
|
228
|
-
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
229
|
-
const token = tokenSet.access_token;
|
|
230
|
-
const expiresAt = tokenSet.expires_at;
|
|
231
|
-
if (!token) {
|
|
232
|
-
throw new Error("Failed to get token");
|
|
233
|
-
}
|
|
234
|
-
return { token, expiresAt };
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
// src/requests.ts
|
|
238
|
-
var CrexApi = class {
|
|
239
|
-
customerConfig;
|
|
240
|
-
apiClient;
|
|
241
|
-
logger;
|
|
242
|
-
publicNextApiUrl;
|
|
243
|
-
/**
|
|
244
|
-
* Initializes the API client if it hasn't been initialized yet.
|
|
245
|
-
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
246
|
-
*
|
|
247
|
-
* @private
|
|
248
|
-
*/
|
|
249
|
-
async initAPI() {
|
|
250
|
-
this.logger = new CrexLogger();
|
|
251
|
-
if (!this.customerConfig) {
|
|
252
|
-
this.customerConfig = getServerConfig();
|
|
253
|
-
}
|
|
254
|
-
if (!this.publicNextApiUrl) {
|
|
255
|
-
this.publicNextApiUrl = "http://localhost:3002";
|
|
256
|
-
}
|
|
257
|
-
if (!this.apiClient) {
|
|
258
|
-
this.apiClient = import_axios.default.create({
|
|
259
|
-
baseURL: this.customerConfig.baseUrl
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
async manageToken() {
|
|
264
|
-
try {
|
|
265
|
-
let token = "";
|
|
266
|
-
const hasToken = (0, import_headers2.cookies)().get(CREX_TOKEN_HEADER_KEY);
|
|
267
|
-
if (hasToken == void 0 || hasToken.value === null) {
|
|
268
|
-
const { token: tokenResult } = await getToken();
|
|
269
|
-
if (tokenResult === null) throw new Error("Token is undefined");
|
|
270
|
-
token = tokenResult;
|
|
271
|
-
} else {
|
|
272
|
-
console.log("j\xE1 tinha o token");
|
|
273
|
-
token = hasToken.value;
|
|
274
|
-
}
|
|
275
|
-
return token;
|
|
276
|
-
} catch (error) {
|
|
277
|
-
this.logger.log({
|
|
278
|
-
level: "error",
|
|
279
|
-
message: `CrexAPI.manageToken error: ${error}`
|
|
280
|
-
});
|
|
281
|
-
throw error;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Executes an API request with caching, authentication, and retry logic.
|
|
286
|
-
*
|
|
287
|
-
* @param options - Request options
|
|
288
|
-
* @param options.url - The URL to request
|
|
289
|
-
* @param options.method - The HTTP method to use
|
|
290
|
-
* @param options.params - Optional query parameters
|
|
291
|
-
* @param options.body - Optional request body
|
|
292
|
-
* @param options.headers - Optional request headers
|
|
293
|
-
* @returns The response data
|
|
294
|
-
* @throws Error if the request fails after maximum retries
|
|
295
|
-
*/
|
|
296
|
-
async execute({
|
|
297
|
-
url,
|
|
298
|
-
method,
|
|
299
|
-
params,
|
|
300
|
-
body,
|
|
301
|
-
headers = {}
|
|
302
|
-
}) {
|
|
303
|
-
try {
|
|
304
|
-
await this.initAPI();
|
|
305
|
-
let response = void 0;
|
|
306
|
-
if (this.customerConfig.OIDC.client.enabled) {
|
|
307
|
-
const token = await this.manageToken();
|
|
308
|
-
headers = {
|
|
309
|
-
...headers,
|
|
310
|
-
Authorization: `Bearer ${token}`
|
|
311
|
-
};
|
|
312
|
-
this.apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
313
|
-
}
|
|
314
|
-
response = await this.apiClient.request({
|
|
315
|
-
url,
|
|
316
|
-
method,
|
|
317
|
-
data: body,
|
|
318
|
-
params,
|
|
319
|
-
headers
|
|
320
|
-
});
|
|
321
|
-
if (response) {
|
|
322
|
-
return response.data;
|
|
323
|
-
}
|
|
324
|
-
} catch (error) {
|
|
325
|
-
console.log("erro brabo", error);
|
|
326
|
-
}
|
|
327
|
-
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
// src/sdk.ts
|
|
332
|
-
var CrexSDK = class {
|
|
333
|
-
userAuthConfig;
|
|
334
|
-
customerConfig;
|
|
335
|
-
/**
|
|
336
|
-
* Retrieves the customer configuration if it hasn't been loaded yet.
|
|
337
|
-
*
|
|
338
|
-
* @private
|
|
339
|
-
*/
|
|
340
|
-
async getServerConfig() {
|
|
341
|
-
if (!this.customerConfig) {
|
|
342
|
-
this.customerConfig = getServerConfig();
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Retrieves the user authentication configuration.
|
|
347
|
-
* If not already loaded, it will load the customer configuration and
|
|
348
|
-
* create the auth config based on OIDC settings.
|
|
349
|
-
*
|
|
350
|
-
* @returns The user authentication configuration object
|
|
351
|
-
*/
|
|
352
|
-
async getUserAuthConfig() {
|
|
353
|
-
if (this.userAuthConfig) {
|
|
354
|
-
return this.userAuthConfig;
|
|
355
|
-
}
|
|
356
|
-
await this.getServerConfig();
|
|
357
|
-
const user = this.customerConfig.OIDC.user;
|
|
358
|
-
if (user.enabled) {
|
|
359
|
-
this.userAuthConfig = {
|
|
360
|
-
providers: [
|
|
361
|
-
{
|
|
362
|
-
id: "crex",
|
|
363
|
-
name: "CREX",
|
|
364
|
-
type: "oauth",
|
|
365
|
-
version: "2.0",
|
|
366
|
-
clientId: user.id,
|
|
367
|
-
wellKnown: user.issuer,
|
|
368
|
-
clientSecret: user.secret,
|
|
369
|
-
authorization: {
|
|
370
|
-
params: {
|
|
371
|
-
scope: user.scope,
|
|
372
|
-
prompt: "login"
|
|
373
|
-
}
|
|
374
|
-
},
|
|
375
|
-
idToken: true,
|
|
376
|
-
checks: ["pkce", "state"],
|
|
377
|
-
async profile(_, tokens) {
|
|
378
|
-
const res = await fetch(user.userInfoEndPoint, {
|
|
379
|
-
headers: {
|
|
380
|
-
Authorization: `Bearer ${tokens.access_token}`
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
const userinfo = await res.json();
|
|
384
|
-
return {
|
|
385
|
-
id: userinfo.sub,
|
|
386
|
-
name: userinfo.name,
|
|
387
|
-
email: userinfo.email
|
|
388
|
-
};
|
|
389
|
-
},
|
|
390
|
-
callbacks: {
|
|
391
|
-
async jwt({ token, account }) {
|
|
392
|
-
if (account) {
|
|
393
|
-
token.id_token = account.id_token;
|
|
394
|
-
}
|
|
395
|
-
return token;
|
|
396
|
-
},
|
|
397
|
-
async session({ session, token }) {
|
|
398
|
-
session.id_token = token.id_token;
|
|
399
|
-
return session;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
]
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
;
|
|
407
|
-
return this.userAuthConfig;
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
411
|
-
0 && (module.exports = {
|
|
412
|
-
CrexApi,
|
|
413
|
-
CrexSDK,
|
|
414
|
-
getClientConfig,
|
|
415
|
-
getServerConfig,
|
|
416
|
-
initializeConfig
|
|
417
|
-
});
|
|
418
|
-
//# sourceMappingURL=index.js.map
|