@centrali-io/centrali-sdk 2.0.1 → 2.0.3

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.
Files changed (3) hide show
  1. package/dist/index.js +28 -9
  2. package/index.ts +29 -10
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.CentraliSDK = void 0;
22
+ exports.getApiUrl = getApiUrl;
22
23
  exports.getAuthUrl = getAuthUrl;
23
24
  exports.fetchClientToken = fetchClientToken;
24
25
  exports.getRecordApiPath = getRecordApiPath;
@@ -31,25 +32,42 @@ function encodeFormData(data) {
31
32
  return new URLSearchParams(data).toString();
32
33
  }
33
34
  /**
34
- * Generate the auth server URL from the API base URL.
35
+ * Generate the API URL from the base URL by adding the 'api.' subdomain.
36
+ * E.g., https://centrali.io -> https://api.centrali.io
35
37
  */
36
- function getAuthUrl(apiBaseUrl) {
37
- const url = new URL(apiBaseUrl);
38
- // assume auth is hosted at auth.<api-domain>
39
- return `${url.protocol}//auth.${url.hostname}`;
38
+ function getApiUrl(baseUrl) {
39
+ const url = new URL(baseUrl);
40
+ // If already has 'api.' subdomain, return as-is
41
+ if (url.hostname.startsWith('api.')) {
42
+ return `${url.protocol}//${url.hostname}`;
43
+ }
44
+ return `${url.protocol}//api.${url.hostname}`;
45
+ }
46
+ /**
47
+ * Generate the auth server URL from the base URL.
48
+ * E.g., https://centrali.io -> https://auth.centrali.io
49
+ */
50
+ function getAuthUrl(baseUrl) {
51
+ const url = new URL(baseUrl);
52
+ // Strip 'api.' prefix if present to get root domain
53
+ const hostname = url.hostname.startsWith('api.')
54
+ ? url.hostname.slice(4)
55
+ : url.hostname;
56
+ return `${url.protocol}//auth.${hostname}`;
40
57
  }
41
58
  /**
42
59
  * Retrieve an access token using the Client Credentials flow.
43
60
  */
44
- function fetchClientToken(clientId, clientSecret, apiBaseUrl) {
61
+ function fetchClientToken(clientId, clientSecret, baseUrl) {
45
62
  return __awaiter(this, void 0, void 0, function* () {
46
- const authUrl = getAuthUrl(apiBaseUrl);
63
+ const authUrl = getAuthUrl(baseUrl);
64
+ const apiUrl = getApiUrl(baseUrl);
47
65
  const tokenEndpoint = `${authUrl}/token`;
48
66
  const form = encodeFormData({
49
67
  grant_type: 'client_credentials',
50
68
  client_id: clientId,
51
69
  client_secret: clientSecret,
52
- resource: `${apiBaseUrl}/data`
70
+ resource: `${apiUrl}/data`
53
71
  });
54
72
  const resp = yield axios_1.default.post(tokenEndpoint, form, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
55
73
  proxy: false
@@ -84,7 +102,8 @@ class CentraliSDK {
84
102
  this.token = null;
85
103
  this.options = options;
86
104
  this.token = options.token || null;
87
- this.axios = axios_1.default.create(Object.assign({ baseURL: options.baseUrl, paramsSerializer: (params) => qs_1.default.stringify(params, { arrayFormat: "repeat" }), proxy: false }, options.axiosConfig));
105
+ const apiUrl = getApiUrl(options.baseUrl);
106
+ this.axios = axios_1.default.create(Object.assign({ baseURL: apiUrl, paramsSerializer: (params) => qs_1.default.stringify(params, { arrayFormat: "repeat" }), proxy: false }, options.axiosConfig));
88
107
  // Attach async interceptor to fetch token on first request if needed
89
108
  this.axios.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
90
109
  if (!this.token && this.options.clientId && this.options.clientSecret) {
package/index.ts CHANGED
@@ -16,7 +16,7 @@ function encodeFormData(data: Record<string, string>): string {
16
16
  * Options for initializing the Centrali SDK client.
17
17
  */
18
18
  export interface CentraliSDKOptions {
19
- /** Base URL of the Centrali API (e.g. https://centrali.io) */
19
+ /** Base URL of Centrali (e.g. https://centrali.io). The SDK automatically uses api.centrali.io for API calls. */
20
20
  baseUrl: string;
21
21
  workspaceId: string;
22
22
  /** Optional initial bearer token for authentication */
@@ -43,12 +43,29 @@ export interface ApiResponse<T> {
43
43
 
44
44
 
45
45
  /**
46
- * Generate the auth server URL from the API base URL.
46
+ * Generate the API URL from the base URL by adding the 'api.' subdomain.
47
+ * E.g., https://centrali.io -> https://api.centrali.io
47
48
  */
48
- export function getAuthUrl(apiBaseUrl: string): string {
49
- const url = new URL(apiBaseUrl);
50
- // assume auth is hosted at auth.<api-domain>
51
- return `${url.protocol}//auth.${url.hostname}`;
49
+ export function getApiUrl(baseUrl: string): string {
50
+ const url = new URL(baseUrl);
51
+ // If already has 'api.' subdomain, return as-is
52
+ if (url.hostname.startsWith('api.')) {
53
+ return `${url.protocol}//${url.hostname}`;
54
+ }
55
+ return `${url.protocol}//api.${url.hostname}`;
56
+ }
57
+
58
+ /**
59
+ * Generate the auth server URL from the base URL.
60
+ * E.g., https://centrali.io -> https://auth.centrali.io
61
+ */
62
+ export function getAuthUrl(baseUrl: string): string {
63
+ const url = new URL(baseUrl);
64
+ // Strip 'api.' prefix if present to get root domain
65
+ const hostname = url.hostname.startsWith('api.')
66
+ ? url.hostname.slice(4)
67
+ : url.hostname;
68
+ return `${url.protocol}//auth.${hostname}`;
52
69
  }
53
70
 
54
71
  /**
@@ -57,15 +74,16 @@ export function getAuthUrl(apiBaseUrl: string): string {
57
74
  export async function fetchClientToken(
58
75
  clientId: string,
59
76
  clientSecret: string,
60
- apiBaseUrl: string
77
+ baseUrl: string
61
78
  ): Promise<string> {
62
- const authUrl = getAuthUrl(apiBaseUrl);
79
+ const authUrl = getAuthUrl(baseUrl);
80
+ const apiUrl = getApiUrl(baseUrl);
63
81
  const tokenEndpoint = `${authUrl}/token`;
64
82
  const form = encodeFormData({
65
83
  grant_type: 'client_credentials',
66
84
  client_id: clientId,
67
85
  client_secret: clientSecret,
68
- resource: `${apiBaseUrl}/data`
86
+ resource: `${apiUrl}/data`
69
87
  });
70
88
  const resp = await axios.post(
71
89
  tokenEndpoint,
@@ -113,8 +131,9 @@ export class CentraliSDK {
113
131
  constructor(options: CentraliSDKOptions) {
114
132
  this.options = options;
115
133
  this.token = options.token || null;
134
+ const apiUrl = getApiUrl(options.baseUrl);
116
135
  this.axios = axios.create({
117
- baseURL: options.baseUrl,
136
+ baseURL: apiUrl,
118
137
  paramsSerializer: (params: Record<string, any>): string =>
119
138
  qs.stringify(params, { arrayFormat: "repeat" }),
120
139
  proxy: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centrali-io/centrali-sdk",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Centrali Node SDK",
5
5
  "main": "dist/index.js",
6
6
  "type": "commonjs",