keepkey-vault-sdk 1.2.4 → 1.2.6

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.
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Universal Fetch Transport Layer
3
+ *
4
+ * Runtime-agnostic fetch implementation that works in:
5
+ * - Browser/WebView (Tauri)
6
+ * - Node 18+ (native fetch)
7
+ * - Node <18 (with injected fetch)
8
+ * - React Native (with injected fetch)
9
+ */
10
+ export type FetchLike = typeof fetch;
11
+ /**
12
+ * HTTP Error with status code and response body
13
+ */
14
+ export declare class HttpError extends Error {
15
+ status: number;
16
+ bodyText?: string | undefined;
17
+ response?: Response | undefined;
18
+ constructor(message: string, status: number, bodyText?: string | undefined, response?: Response | undefined);
19
+ }
20
+ /**
21
+ * Resolve fetch implementation from environment or injection
22
+ */
23
+ export declare function resolveFetch(fetchImpl?: FetchLike): FetchLike;
24
+ /**
25
+ * Create a fetch wrapper with error handling
26
+ */
27
+ export declare function createFetchWrapper(fetchImpl?: FetchLike): (url: URL | RequestInfo, init?: RequestInit) => Promise<Response>;
28
+ /**
29
+ * Environment detection utilities
30
+ */
31
+ export declare const environment: {
32
+ /**
33
+ * Check if running in browser environment
34
+ */
35
+ isBrowser: boolean;
36
+ /**
37
+ * Check if running in Node.js environment
38
+ */
39
+ isNode: boolean;
40
+ /**
41
+ * Check if native fetch is available
42
+ */
43
+ hasNativeFetch: boolean;
44
+ /**
45
+ * Get environment name for debugging
46
+ */
47
+ readonly name: string;
48
+ };
49
+ /**
50
+ * Get appropriate fetch for current environment
51
+ */
52
+ export declare function getEnvironmentFetch(): FetchLike;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ /**
3
+ * Universal Fetch Transport Layer
4
+ *
5
+ * Runtime-agnostic fetch implementation that works in:
6
+ * - Browser/WebView (Tauri)
7
+ * - Node 18+ (native fetch)
8
+ * - Node <18 (with injected fetch)
9
+ * - React Native (with injected fetch)
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getEnvironmentFetch = exports.environment = exports.createFetchWrapper = exports.resolveFetch = exports.HttpError = void 0;
13
+ /**
14
+ * HTTP Error with status code and response body
15
+ */
16
+ class HttpError extends Error {
17
+ constructor(message, status, bodyText, response) {
18
+ super(message);
19
+ this.status = status;
20
+ this.bodyText = bodyText;
21
+ this.response = response;
22
+ this.name = 'HttpError';
23
+ }
24
+ }
25
+ exports.HttpError = HttpError;
26
+ /**
27
+ * Resolve fetch implementation from environment or injection
28
+ */
29
+ function resolveFetch(fetchImpl) {
30
+ const resolved = fetchImpl ?? globalThis.fetch;
31
+ if (!resolved || typeof resolved !== 'function') {
32
+ throw new Error('No fetch implementation found. ' +
33
+ 'Provide `fetch` in SDK options (e.g. undici for Node<18, polyfill for older browsers).');
34
+ }
35
+ return resolved.bind(globalThis);
36
+ }
37
+ exports.resolveFetch = resolveFetch;
38
+ /**
39
+ * Create a fetch wrapper with error handling
40
+ */
41
+ function createFetchWrapper(fetchImpl) {
42
+ const fetchFn = resolveFetch(fetchImpl);
43
+ return async (url, init) => {
44
+ try {
45
+ const response = await fetchFn(url, init);
46
+ // Don't throw on HTTP errors here - let the caller decide
47
+ // This matches the native fetch behavior
48
+ return response;
49
+ }
50
+ catch (error) {
51
+ // Network/connection errors
52
+ if (error instanceof Error) {
53
+ throw new HttpError(`Network error: ${error.message}`, 0, undefined);
54
+ }
55
+ throw error;
56
+ }
57
+ };
58
+ }
59
+ exports.createFetchWrapper = createFetchWrapper;
60
+ /**
61
+ * Environment detection utilities
62
+ */
63
+ exports.environment = {
64
+ /**
65
+ * Check if running in browser environment
66
+ */
67
+ isBrowser: typeof window !== 'undefined' && typeof window.fetch === 'function',
68
+ /**
69
+ * Check if running in Node.js environment
70
+ */
71
+ isNode: typeof process !== 'undefined' && process.versions?.node !== undefined,
72
+ /**
73
+ * Check if native fetch is available
74
+ */
75
+ hasNativeFetch: typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'function',
76
+ /**
77
+ * Get environment name for debugging
78
+ */
79
+ get name() {
80
+ if (this.isBrowser)
81
+ return 'browser';
82
+ if (this.isNode)
83
+ return 'node';
84
+ return 'unknown';
85
+ },
86
+ };
87
+ /**
88
+ * Get appropriate fetch for current environment
89
+ */
90
+ function getEnvironmentFetch() {
91
+ if (exports.environment.hasNativeFetch) {
92
+ return globalThis.fetch.bind(globalThis);
93
+ }
94
+ // This should rarely happen in modern environments
95
+ throw new Error(`No native fetch found in ${exports.environment.name} environment. ` +
96
+ 'Please provide a fetch implementation in SDK options.');
97
+ }
98
+ exports.getEnvironmentFetch = getEnvironmentFetch;
@@ -37,4 +37,6 @@ export type { DeviceStatus } from './device-status';
37
37
  export { DeviceStatusChecker } from './device-status';
38
38
  export type { SDKConfigurationParameters } from './sdk-config';
39
39
  export { createSDKConfig, createSDKConfigWithVersion, SDKConfiguration } from './sdk-config';
40
+ export type { FetchLike, HttpError } from './fetch';
41
+ export { createFetchWrapper, environment, getEnvironmentFetch, resolveFetch } from './fetch';
40
42
  export type { Configuration, ConfigurationParameters } from './generated/v2/runtime';
package/dist/src/index.js CHANGED
@@ -41,7 +41,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
41
41
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
42
42
  };
43
43
  Object.defineProperty(exports, "__esModule", { value: true });
44
- exports.SDKConfiguration = exports.createSDKConfigWithVersion = exports.createSDKConfig = exports.DeviceStatusChecker = exports.VersionDetector = exports.SpecLoader = exports.VersionRouter = exports.KeepKeySdk = void 0;
44
+ exports.resolveFetch = exports.getEnvironmentFetch = exports.environment = exports.createFetchWrapper = exports.SDKConfiguration = exports.createSDKConfigWithVersion = exports.createSDKConfig = exports.DeviceStatusChecker = exports.VersionDetector = exports.SpecLoader = exports.VersionRouter = exports.KeepKeySdk = void 0;
45
45
  var KeepKeySdk_1 = require("./KeepKeySdk");
46
46
  Object.defineProperty(exports, "KeepKeySdk", { enumerable: true, get: function () { return KeepKeySdk_1.KeepKeySdk; } });
47
47
  // Export generated API (v2 by default for backward compatibility)
@@ -59,3 +59,8 @@ var sdk_config_1 = require("./sdk-config");
59
59
  Object.defineProperty(exports, "createSDKConfig", { enumerable: true, get: function () { return sdk_config_1.createSDKConfig; } });
60
60
  Object.defineProperty(exports, "createSDKConfigWithVersion", { enumerable: true, get: function () { return sdk_config_1.createSDKConfigWithVersion; } });
61
61
  Object.defineProperty(exports, "SDKConfiguration", { enumerable: true, get: function () { return sdk_config_1.SDKConfiguration; } });
62
+ var fetch_1 = require("./fetch");
63
+ Object.defineProperty(exports, "createFetchWrapper", { enumerable: true, get: function () { return fetch_1.createFetchWrapper; } });
64
+ Object.defineProperty(exports, "environment", { enumerable: true, get: function () { return fetch_1.environment; } });
65
+ Object.defineProperty(exports, "getEnvironmentFetch", { enumerable: true, get: function () { return fetch_1.getEnvironmentFetch; } });
66
+ Object.defineProperty(exports, "resolveFetch", { enumerable: true, get: function () { return fetch_1.resolveFetch; } });
@@ -4,12 +4,18 @@
4
4
  * Extends the generated Configuration with automatic API version detection
5
5
  * and intelligent routing between v1 and v2 APIs.
6
6
  */
7
+ import type { FetchLike } from './fetch';
7
8
  import type { ConfigurationParameters } from './generated/v2/runtime';
8
9
  import { Configuration } from './generated/v2/runtime';
9
10
  import { VersionRouter } from './middleware/version-router';
10
11
  import type { ApiVersionInfo } from './version-detector';
11
12
  import { VersionDetector } from './version-detector';
12
13
  export interface SDKConfigurationParameters extends ConfigurationParameters {
14
+ /**
15
+ * Optional fetch implementation for environments without native fetch
16
+ * (e.g. Node.js <18, React Native)
17
+ */
18
+ fetch?: FetchLike;
13
19
  /**
14
20
  * Enable automatic version detection via /api/health endpoint
15
21
  * @default true
@@ -7,15 +7,24 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.createSDKConfigWithVersion = exports.createSDKConfig = exports.SDKConfiguration = void 0;
10
+ const fetch_1 = require("./fetch");
10
11
  const runtime_1 = require("./generated/v2/runtime");
11
12
  const version_router_1 = require("./middleware/version-router");
12
13
  const version_detector_1 = require("./version-detector");
13
14
  class SDKConfiguration extends runtime_1.Configuration {
14
15
  constructor(params = {}) {
15
- super(params);
16
+ // Create universal fetch wrapper
17
+ const fetchImpl = params.fetch || (0, fetch_1.getEnvironmentFetch)();
18
+ const fetchWrapper = (0, fetch_1.createFetchWrapper)(fetchImpl);
19
+ // Configure with wrapped fetch
20
+ super({
21
+ ...params,
22
+ fetchApi: fetchWrapper,
23
+ });
16
24
  this.autoDetectVersion = params.autoDetectVersion !== false; // Default to true
17
25
  this.manualApiVersion = params.apiVersion;
18
- this.versionDetector = params.versionDetector || new version_detector_1.VersionDetector(this.basePath);
26
+ this.versionDetector =
27
+ params.versionDetector || new version_detector_1.VersionDetector(this.basePath, fetchWrapper);
19
28
  }
20
29
  /**
21
30
  * Initialize version detection.
@@ -12,7 +12,8 @@ export interface ApiVersionInfo {
12
12
  export declare class VersionDetector {
13
13
  private basePath;
14
14
  private cachedVersion;
15
- constructor(basePath: string);
15
+ private fetchFn;
16
+ constructor(basePath: string, fetchFn?: typeof fetch);
16
17
  /**
17
18
  * Detects API version by fetching /spec/swagger.json and reading info.version.
18
19
  * Returns version info or defaults to v1 if detection fails.
@@ -8,9 +8,10 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.VersionDetector = void 0;
10
10
  class VersionDetector {
11
- constructor(basePath) {
11
+ constructor(basePath, fetchFn) {
12
12
  this.cachedVersion = null;
13
13
  this.basePath = basePath.replace(/\/+$/, ''); // Remove trailing slashes
14
+ this.fetchFn = fetchFn || globalThis.fetch;
14
15
  }
15
16
  /**
16
17
  * Detects API version by fetching /spec/swagger.json and reading info.version.
@@ -23,7 +24,7 @@ class VersionDetector {
23
24
  return this.cachedVersion;
24
25
  }
25
26
  try {
26
- const response = await fetch(`${this.basePath}/spec/swagger.json`, {
27
+ const response = await this.fetchFn(`${this.basePath}/spec/swagger.json`, {
27
28
  method: 'GET',
28
29
  headers: {
29
30
  Accept: 'application/json',