@provablehq/sdk 0.11.7 → 0.11.8

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,102 @@
1
+ import { TransportFunction } from "./utils/utils.js";
2
+ /**
3
+ * Interface for the JWT data.
4
+ *
5
+ * @property jwt {string} The JWT token string.
6
+ * @property expiration {number} The expiration time of the JWT token in UNIX timestamp format (milliseconds).
7
+ */
8
+ export interface JWTData {
9
+ jwt: string;
10
+ expiration: number;
11
+ }
12
+ /** Default header carrying a provisioned API key (e.g. edge.provable.com). */
13
+ export declare const DEFAULT_API_KEY_HEADER = "X-API-Key";
14
+ /**
15
+ * Authentication configuration for Provable API services.
16
+ *
17
+ * Two live modes exist, matching the two gateways:
18
+ * - `jwt` (api.provable.com): the apiKey + consumerId pair mints a short-lived JWT at
19
+ * `/jwts/{consumerId}` which is sent as an `Authorization` header and refreshed near expiry.
20
+ * - `api-key` (edge.provable.com): a provisioned key is sent verbatim on every request in a
21
+ * header (default `X-API-Key`). There is no registration, minting, or refresh in this mode;
22
+ * a 401 means the key is invalid or revoked and retrying cannot help.
23
+ * - `none`: requests carry no auth headers.
24
+ */
25
+ export type ApiAuthConfig = {
26
+ mode: "jwt";
27
+ apiKey?: string;
28
+ consumerId?: string;
29
+ jwtData?: JWTData;
30
+ } | {
31
+ mode: "api-key";
32
+ value: string;
33
+ header?: string;
34
+ } | {
35
+ mode: "none";
36
+ };
37
+ /**
38
+ * Legacy authentication options accepted by {@link AleoNetworkClient} and {@link RecordScanner}
39
+ * before explicit modes existed. Normalized by {@link normalizeAuthConfig}.
40
+ */
41
+ export interface LegacyAuthOptions {
42
+ auth?: ApiAuthConfig;
43
+ apiKey?: string | {
44
+ header: string;
45
+ value: string;
46
+ };
47
+ consumerId?: string;
48
+ jwtData?: JWTData;
49
+ }
50
+ /**
51
+ * Normalize legacy auth options into an explicit {@link ApiAuthConfig}.
52
+ *
53
+ * Rules, preserving the pre-mode behavior of both clients:
54
+ * - An explicit `auth` wins. Combining it with legacy fields throws — the caller's intent
55
+ * is ambiguous and guessing hides misconfiguration.
56
+ * - A string `apiKey` (with or without `consumerId`) or a bare `jwtData` selects `jwt` mode.
57
+ * - An `{ header, value }` apiKey selects `api-key` mode with that header. Combining it with
58
+ * a `consumerId` throws: keyed auth has no consumer and the pair would silently pick one.
59
+ * - Nothing configured selects `none`.
60
+ */
61
+ export declare function normalizeAuthConfig(options: LegacyAuthOptions): ApiAuthConfig;
62
+ /**
63
+ * ApiAuth resolves the auth headers each request must carry, owning the JWT mint/refresh
64
+ * lifecycle in `jwt` mode. Share one instance across clients that use the same credentials so
65
+ * only one minter exists and concurrent refreshes are deduplicated.
66
+ */
67
+ export declare class ApiAuth {
68
+ readonly config: ApiAuthConfig;
69
+ private readonly baseUrl;
70
+ private readonly transport;
71
+ private jwtData?;
72
+ private inFlight?;
73
+ private warned;
74
+ private readonly mintHeaders;
75
+ /**
76
+ * @param {ApiAuthConfig} config The auth mode and its material.
77
+ * @param {string} baseUrl API root origin; `/jwts/{consumerId}` lives here in `jwt` mode.
78
+ * @param {TransportFunction} transport Transport used for the JWT mint request.
79
+ * @param {Record<string, string>} [mintHeaders] Extra headers on the mint request (e.g. SDK telemetry).
80
+ */
81
+ constructor(config: ApiAuthConfig, baseUrl: string, transport?: TransportFunction, mintHeaders?: Record<string, string>);
82
+ /** The mode this instance authenticates with. */
83
+ get mode(): ApiAuthConfig["mode"];
84
+ /** Replace the stored JWT, e.g. one minted elsewhere. Only meaningful in `jwt` mode. */
85
+ setJwtData(jwtData: JWTData | undefined): void;
86
+ /** The stored JWT, when one exists. */
87
+ getJwtData(): JWTData | undefined;
88
+ /**
89
+ * The auth headers a request must carry, refreshing the JWT first when it is stale and a
90
+ * refresh is possible. Returns an empty object in `none` mode or when `jwt` mode lacks
91
+ * both a usable token and the material to mint one.
92
+ */
93
+ headers(): Promise<Record<string, string>>;
94
+ /**
95
+ * Refreshes the JWT by making a POST request to /jwts/{consumer_id}.
96
+ *
97
+ * @param {string} apiKey The API key to use for the refresh request.
98
+ * @param {string} consumerId The consumer ID for the JWT endpoint.
99
+ * @returns {Promise<JWTData>} The new JWT data.
100
+ */
101
+ private refreshJwt;
102
+ }
@@ -0,0 +1,102 @@
1
+ import { TransportFunction } from "./utils/utils.js";
2
+ /**
3
+ * Interface for the JWT data.
4
+ *
5
+ * @property jwt {string} The JWT token string.
6
+ * @property expiration {number} The expiration time of the JWT token in UNIX timestamp format (milliseconds).
7
+ */
8
+ export interface JWTData {
9
+ jwt: string;
10
+ expiration: number;
11
+ }
12
+ /** Default header carrying a provisioned API key (e.g. edge.provable.com). */
13
+ export declare const DEFAULT_API_KEY_HEADER = "X-API-Key";
14
+ /**
15
+ * Authentication configuration for Provable API services.
16
+ *
17
+ * Two live modes exist, matching the two gateways:
18
+ * - `jwt` (api.provable.com): the apiKey + consumerId pair mints a short-lived JWT at
19
+ * `/jwts/{consumerId}` which is sent as an `Authorization` header and refreshed near expiry.
20
+ * - `api-key` (edge.provable.com): a provisioned key is sent verbatim on every request in a
21
+ * header (default `X-API-Key`). There is no registration, minting, or refresh in this mode;
22
+ * a 401 means the key is invalid or revoked and retrying cannot help.
23
+ * - `none`: requests carry no auth headers.
24
+ */
25
+ export type ApiAuthConfig = {
26
+ mode: "jwt";
27
+ apiKey?: string;
28
+ consumerId?: string;
29
+ jwtData?: JWTData;
30
+ } | {
31
+ mode: "api-key";
32
+ value: string;
33
+ header?: string;
34
+ } | {
35
+ mode: "none";
36
+ };
37
+ /**
38
+ * Legacy authentication options accepted by {@link AleoNetworkClient} and {@link RecordScanner}
39
+ * before explicit modes existed. Normalized by {@link normalizeAuthConfig}.
40
+ */
41
+ export interface LegacyAuthOptions {
42
+ auth?: ApiAuthConfig;
43
+ apiKey?: string | {
44
+ header: string;
45
+ value: string;
46
+ };
47
+ consumerId?: string;
48
+ jwtData?: JWTData;
49
+ }
50
+ /**
51
+ * Normalize legacy auth options into an explicit {@link ApiAuthConfig}.
52
+ *
53
+ * Rules, preserving the pre-mode behavior of both clients:
54
+ * - An explicit `auth` wins. Combining it with legacy fields throws — the caller's intent
55
+ * is ambiguous and guessing hides misconfiguration.
56
+ * - A string `apiKey` (with or without `consumerId`) or a bare `jwtData` selects `jwt` mode.
57
+ * - An `{ header, value }` apiKey selects `api-key` mode with that header. Combining it with
58
+ * a `consumerId` throws: keyed auth has no consumer and the pair would silently pick one.
59
+ * - Nothing configured selects `none`.
60
+ */
61
+ export declare function normalizeAuthConfig(options: LegacyAuthOptions): ApiAuthConfig;
62
+ /**
63
+ * ApiAuth resolves the auth headers each request must carry, owning the JWT mint/refresh
64
+ * lifecycle in `jwt` mode. Share one instance across clients that use the same credentials so
65
+ * only one minter exists and concurrent refreshes are deduplicated.
66
+ */
67
+ export declare class ApiAuth {
68
+ readonly config: ApiAuthConfig;
69
+ private readonly baseUrl;
70
+ private readonly transport;
71
+ private jwtData?;
72
+ private inFlight?;
73
+ private warned;
74
+ private readonly mintHeaders;
75
+ /**
76
+ * @param {ApiAuthConfig} config The auth mode and its material.
77
+ * @param {string} baseUrl API root origin; `/jwts/{consumerId}` lives here in `jwt` mode.
78
+ * @param {TransportFunction} transport Transport used for the JWT mint request.
79
+ * @param {Record<string, string>} [mintHeaders] Extra headers on the mint request (e.g. SDK telemetry).
80
+ */
81
+ constructor(config: ApiAuthConfig, baseUrl: string, transport?: TransportFunction, mintHeaders?: Record<string, string>);
82
+ /** The mode this instance authenticates with. */
83
+ get mode(): ApiAuthConfig["mode"];
84
+ /** Replace the stored JWT, e.g. one minted elsewhere. Only meaningful in `jwt` mode. */
85
+ setJwtData(jwtData: JWTData | undefined): void;
86
+ /** The stored JWT, when one exists. */
87
+ getJwtData(): JWTData | undefined;
88
+ /**
89
+ * The auth headers a request must carry, refreshing the JWT first when it is stale and a
90
+ * refresh is possible. Returns an empty object in `none` mode or when `jwt` mode lacks
91
+ * both a usable token and the material to mint one.
92
+ */
93
+ headers(): Promise<Record<string, string>>;
94
+ /**
95
+ * Refreshes the JWT by making a POST request to /jwts/{consumer_id}.
96
+ *
97
+ * @param {string} apiKey The API key to use for the refresh request.
98
+ * @param {string} consumerId The consumer ID for the JWT endpoint.
99
+ * @returns {Promise<JWTData>} The new JWT data.
100
+ */
101
+ private refreshJwt;
102
+ }