@healthcloudai/hc-sdk 3.24.0 → 3.24.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/dist/index.cjs CHANGED
@@ -68,6 +68,9 @@ __export(index_exports, {
68
68
  assertTimeout: () => assertTimeout,
69
69
  isJsonRpcError: () => isJsonRpcError,
70
70
  isServiceName: () => isServiceName,
71
+ joinServiceUrl: () => joinServiceUrl,
72
+ normalizeBaseUrl: () => normalizeBaseUrl,
73
+ normalizeOverrides: () => normalizeOverrides,
71
74
  resolveBaseUrl: () => resolveBaseUrl,
72
75
  withTenantId: () => withTenantId
73
76
  });
@@ -96,12 +99,67 @@ function isServiceName(value) {
96
99
  return SERVICES.includes(value);
97
100
  }
98
101
  function resolveBaseUrl(environment, service) {
102
+ assertEnvironment(environment);
99
103
  if (service === "ams") {
104
+ if (environment === "dev") return "https://dev-ams-services.health.cloud/api/ams";
100
105
  const prefix = environment === "prod" ? "" : `${environment}-`;
101
106
  return `https://${prefix}amsservices.health.cloud/api/ams`;
102
107
  }
103
108
  return `https://${environment}-api-${service}.health.cloud`;
104
109
  }
110
+ function normalizeBaseUrl(value) {
111
+ const message = "Invalid service base URL: expected HTTPS (or literal loopback HTTP); no credentials, query or fragment";
112
+ try {
113
+ if (typeof value !== "string" || /[^\x21-\x7e]|[\\?#]/.test(value) || !/^https?:\/\//i.test(value)) {
114
+ throw new Error();
115
+ }
116
+ const authority = value.slice(value.indexOf("://") + 3).split("/")[0];
117
+ if (!authority || /[@%]/.test(authority) || authority.endsWith(":")) throw new Error();
118
+ const url = new URL(value);
119
+ const host = url.hostname;
120
+ const allowedScheme = url.protocol === "https:" || url.protocol === "http:" && (host === "127.0.0.1" || host === "[::1]");
121
+ if (!allowedScheme || !host || url.username || url.password || url.port && Number(url.port) < 1) {
122
+ throw new Error();
123
+ }
124
+ if (!host.startsWith("[") && (!/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(host) || host.split(".").some(
125
+ (label) => !label || label.length > 63 || label.startsWith("-") || label.endsWith("-")
126
+ ))) throw new Error();
127
+ const rawHost = authority.startsWith("[") ? authority.slice(0, authority.indexOf("]") + 1) : authority.split(":")[0];
128
+ if (!host.startsWith("[") && host !== rawHost.toLowerCase()) throw new Error();
129
+ const rawPath = value.slice(value.indexOf("://") + 3 + authority.length);
130
+ if (rawPath.includes("//") || /%(?![0-9a-f]{2})|%2f/i.test(rawPath)) throw new Error();
131
+ const decoded = decodePathBytes(rawPath);
132
+ if (hasControls(decoded) || /[\\?#%]/.test(decoded) || decoded.split("/").some((s) => s === "." || s === "..")) {
133
+ throw new Error();
134
+ }
135
+ return `${url.origin}${rawPath.replace(/\/+$/, "")}`;
136
+ } catch {
137
+ throw new Error(message);
138
+ }
139
+ }
140
+ function normalizeOverrides(values) {
141
+ if (values === void 0) return {};
142
+ if (values === null || typeof values !== "object" || Array.isArray(values)) throw new Error("Invalid service URL overrides");
143
+ const result = {};
144
+ for (const [service, value] of Object.entries(values)) {
145
+ if (!isServiceName(service)) throw new Error("Invalid service URL overrides: expected known service names");
146
+ result[service] = normalizeBaseUrl(value);
147
+ }
148
+ return result;
149
+ }
150
+ function joinServiceUrl(base, path) {
151
+ if (hasControls(path) || /[\\?#]/.test(path) || path.includes("://") || path.startsWith("//") || path.split("/").some((segment) => {
152
+ const decoded = decodePathBytes(segment);
153
+ return decoded === "." || decoded === "..";
154
+ })) throw new Error("Invalid service request path");
155
+ return `${base.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
156
+ }
157
+ function hasControls(value) {
158
+ return Array.from(value).some((character) => character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127);
159
+ }
160
+ function decodePathBytes(value) {
161
+ return value.replace(/%([0-9a-f]{2})/gi, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
162
+ }
105
163
  function assertTimeout(timeout, label = "timeout") {
106
164
  if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) {
107
165
  throw new Error(`Invalid HealthCloud SDK ${label}: expected a positive number of milliseconds, got ${String(timeout)}`);
@@ -137,12 +195,13 @@ var SDKError = class extends Error {
137
195
  var HttpClient = class {
138
196
  constructor(state) {
139
197
  this.state = state;
198
+ this.baseUrlOverrides = normalizeOverrides(state.baseUrlOverrides);
140
199
  }
141
200
  async request(service, method, path, options = {}) {
142
201
  var _a, _b;
143
202
  const url = this.buildUrl(service, path, options);
144
203
  const headers = this.buildHeaders(options);
145
- const init = { method, headers };
204
+ const init = { method, headers, redirect: "error" };
146
205
  if (method !== "GET" && method !== "DELETE" && options.body !== void 0) {
147
206
  init.body = this.serializeBody(options.body, headers);
148
207
  }
@@ -159,6 +218,9 @@ var HttpClient = class {
159
218
  let payload;
160
219
  try {
161
220
  response = await fetch(url, init);
221
+ if (response.redirected || response.status >= 300 && response.status < 400 || response.type === "opaqueredirect") {
222
+ throw new SDKError({ message: "Service redirects are not allowed", statusCode: response.status });
223
+ }
162
224
  payload = await this.parseResponse(response);
163
225
  } catch (error) {
164
226
  if (didTimeout) {
@@ -172,7 +234,7 @@ var HttpClient = class {
172
234
  throw error;
173
235
  }
174
236
  throw new SDKError({
175
- message: error instanceof Error ? error.message : "Network request failed.",
237
+ message: "Network request failed.",
176
238
  statusCode: 0,
177
239
  cause: error
178
240
  });
@@ -208,8 +270,8 @@ var HttpClient = class {
208
270
  buildUrl(service, path, options) {
209
271
  var _a, _b, _c;
210
272
  const normalizedPath = this.resolvePath(path, (_a = options.pathParams) != null ? _a : {});
211
- const base = (_c = (_b = this.state.baseUrlOverrides) == null ? void 0 : _b[service]) != null ? _c : resolveBaseUrl(this.state.environment, service);
212
- const url = new URL(`${base}${normalizedPath}`);
273
+ const base = (_c = (_b = this.baseUrlOverrides) == null ? void 0 : _b[service]) != null ? _c : resolveBaseUrl(this.state.environment, service);
274
+ const url = new URL(joinServiceUrl(base, normalizedPath));
213
275
  if (options.query) {
214
276
  for (const [key, value] of Object.entries(options.query)) {
215
277
  this.appendQuery(url, key, value);
@@ -7116,7 +7178,7 @@ var HCSDK = class {
7116
7178
  environment: config.environment,
7117
7179
  tenantId: config.tenantId,
7118
7180
  accessToken: config.accessToken,
7119
- baseUrlOverrides: config.baseUrlOverrides,
7181
+ baseUrlOverrides: normalizeOverrides(config.baseUrlOverrides),
7120
7182
  timeout: (_b = config.timeout) != null ? _b : DEFAULT_TIMEOUT_MS
7121
7183
  };
7122
7184
  this.http = new HttpClient(this.state);
@@ -7290,6 +7352,9 @@ var HCSDK_SWAGGER_OPERATION_COUNTS = {
7290
7352
  assertTimeout,
7291
7353
  isJsonRpcError,
7292
7354
  isServiceName,
7355
+ joinServiceUrl,
7356
+ normalizeBaseUrl,
7357
+ normalizeOverrides,
7293
7358
  resolveBaseUrl,
7294
7359
  withTenantId
7295
7360
  });