@0xtrails/api 0.10.4 → 0.11.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xtrails/api",
3
- "version": "0.10.4",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "description": "Trails API Client SDK",
6
6
  "repository": "https://github.com/0xsequence/trails/tree/master/packages/trails-api",
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Union types for API shapes that differ between v1 and v1_5.
3
+ * Allows typechecking against both response shapes.
4
+ * TrailsClient is v1 (primary) plus v1_5-only methods (shim).
5
+ */
6
+ import type * as V1 from "./trails-api.gen.js"
7
+ import type * as V1_5 from "./trails-api-v1_5.gen.js"
8
+
9
+ /** v1 primary; v1_5-only methods shimmed in; commit/execute accept union request types */
10
+ export type TrailsClient = Omit<
11
+ V1.TrailsClient,
12
+ "commitIntent" | "executeIntent"
13
+ > &
14
+ Pick<
15
+ V1_5.TrailsClient,
16
+ "getIntentConfig" | "getSupportedIntentProtocolVersions"
17
+ > & {
18
+ commitIntent(
19
+ req: CommitIntentRequest,
20
+ headers?: object,
21
+ signal?: AbortSignal,
22
+ ): Promise<CommitIntentResponse>
23
+ executeIntent(
24
+ req: ExecuteIntentRequest,
25
+ headers?: object,
26
+ signal?: AbortSignal,
27
+ ): Promise<ExecuteIntentResponse>
28
+ }
29
+
30
+ export type TrailsContracts = V1.TrailsContracts | V1_5.TrailsContracts
31
+ /** Union; augmented so optional intentProtocolVersion is always readable (v2 has it, v1 does not). */
32
+ export type Intent = (V1.Intent | V1_5.Intent) & {
33
+ intentProtocolVersion?: V1_5.IntentProtocolVersion
34
+ }
35
+ /** Union; augmented so optional v1_5-only fields are readable. */
36
+ export type DepositSignature = (V1.DepositSignature | V1_5.DepositSignature) & {
37
+ intentSignature?: string
38
+ userNonce?: number
39
+ }
40
+ export type DepositIntentEntry = V1.DepositIntentEntry | V1_5.DepositIntentEntry
41
+ export type QuoteIntentRequest = V1.QuoteIntentRequest | V1_5.QuoteIntentRequest
42
+ /** Request with union Intent so callers don't need to cast */
43
+ export type CommitIntentRequest = { intent: Intent }
44
+ /** Request with union DepositSignature so callers don't need to cast */
45
+ export type ExecuteIntentRequest = {
46
+ intentId: string
47
+ depositTransactionHash?: string
48
+ depositSignature?: DepositSignature
49
+ }
50
+ export type GetIntentResponse = V1.GetIntentResponse | V1_5.GetIntentResponse
51
+ export type CommitIntentResponse =
52
+ | V1.CommitIntentResponse
53
+ | V1_5.CommitIntentResponse
54
+ export type ExecuteIntentResponse =
55
+ | V1.ExecuteIntentResponse
56
+ | V1_5.ExecuteIntentResponse
57
+ export type IntentReceipt = V1.IntentReceipt | V1_5.IntentReceipt
58
+ export type WaitIntentReceiptResponse =
59
+ | V1.WaitIntentReceiptResponse
60
+ | V1_5.WaitIntentReceiptResponse
61
+ export type IntentHistory = V1.IntentHistory | V1_5.IntentHistory
package/src/index.ts CHANGED
@@ -1,6 +1,39 @@
1
+ export type {
2
+ CommitIntentRequest,
3
+ CommitIntentResponse,
4
+ DepositIntentEntry,
5
+ DepositSignature,
6
+ ExecuteIntentRequest,
7
+ ExecuteIntentResponse,
8
+ GetIntentResponse,
9
+ Intent,
10
+ IntentHistory,
11
+ IntentReceipt,
12
+ QuoteIntentRequest,
13
+ TrailsClient,
14
+ TrailsContracts,
15
+ WaitIntentReceiptResponse,
16
+ } from "./api-types.js"
1
17
  export * from "./trails-api.gen.js"
2
-
18
+ export type {
19
+ GetIntentConfigRequest,
20
+ GetIntentConfigResponse,
21
+ GetSupportedIntentProtocolVersionsResponse,
22
+ } from "./trails-api-v1_5.gen.js"
23
+ export { IntentProtocolVersion, IntentSource } from "./trails-api-v1_5.gen.js"
24
+ import type {
25
+ CommitIntentRequest,
26
+ CommitIntentResponse,
27
+ ExecuteIntentRequest,
28
+ ExecuteIntentResponse,
29
+ } from "./api-types.js"
3
30
  import { Trails as BaseTrails } from "./trails-api.gen.js"
31
+ import type {
32
+ GetIntentConfigRequest,
33
+ GetIntentConfigResponse,
34
+ GetSupportedIntentProtocolVersionsResponse,
35
+ } from "./trails-api-v1_5.gen.js"
36
+ import { Trails as TrailsV1_5 } from "./trails-api-v1_5.gen.js"
4
37
 
5
38
  export interface TrailsApiOptions {
6
39
  apiKey?: string
@@ -8,14 +41,16 @@ export interface TrailsApiOptions {
8
41
  hostname?: string
9
42
  }
10
43
 
44
+ // Use v1 client as base, shim v2 methods in
45
+
11
46
  export class TrailsApi extends BaseTrails {
47
+ private _v1_5: TrailsV1_5
48
+
12
49
  constructor(apiKey: string, options?: TrailsApiOptions) {
13
50
  const customFetch = (
14
51
  input: RequestInfo,
15
52
  init?: RequestInit,
16
53
  ): Promise<Response> => {
17
- // automatically include jwt and access key auth header to requests
18
- // if its been set on the api client
19
54
  const headers: { [key: string]: any } = {}
20
55
 
21
56
  if (options?.jwt && options?.jwt.length > 0) {
@@ -26,7 +61,6 @@ export class TrailsApi extends BaseTrails {
26
61
  headers["X-Access-Key"] = apiKey
27
62
  }
28
63
 
29
- // before the request is made
30
64
  init = init || {}
31
65
  init.headers = { ...init.headers, ...headers }
32
66
 
@@ -38,10 +72,17 @@ export class TrailsApi extends BaseTrails {
38
72
  ? options?.hostname
39
73
  : "https://trails-api.sequence.app"
40
74
 
41
- super(
42
- hostname.endsWith("/") ? hostname.slice(0, -1) : hostname,
43
- customFetch,
44
- )
75
+ const baseUrl = hostname.endsWith("/") ? hostname.slice(0, -1) : hostname
76
+ super(baseUrl, customFetch)
77
+ this._v1_5 = new TrailsV1_5(baseUrl, customFetch)
78
+
79
+ // Capture and re-bind commit/executeIntent
80
+ const baseCommitIntent = this.commitIntent.bind(this)
81
+ const baseExecuteIntent = this.executeIntent.bind(this)
82
+ this.commitIntent = (req, headers?, signal?) =>
83
+ baseCommitIntent(req, headers, signal)
84
+ this.executeIntent = (req, headers?, signal?) =>
85
+ baseExecuteIntent(req, headers, signal)
45
86
  }
46
87
 
47
88
  getHostname(): string {
@@ -51,4 +92,34 @@ export class TrailsApi extends BaseTrails {
51
92
  getFetch(): any {
52
93
  return this.fetch
53
94
  }
95
+
96
+ // Accept union request types (CommitIntentRequest / ExecuteIntentRequest from api-types)
97
+ declare commitIntent: (
98
+ req: CommitIntentRequest,
99
+ headers?: object,
100
+ signal?: AbortSignal,
101
+ ) => Promise<CommitIntentResponse>
102
+
103
+ declare executeIntent: (
104
+ req: ExecuteIntentRequest,
105
+ headers?: object,
106
+ signal?: AbortSignal,
107
+ ) => Promise<ExecuteIntentResponse>
108
+
109
+ // V2-specific methods (shim)
110
+
111
+ getIntentConfig(
112
+ req: GetIntentConfigRequest,
113
+ headers?: object,
114
+ signal?: AbortSignal,
115
+ ): Promise<GetIntentConfigResponse> {
116
+ return this._v1_5.getIntentConfig(req, headers, signal)
117
+ }
118
+
119
+ getSupportedIntentProtocolVersions(
120
+ headers?: object,
121
+ signal?: AbortSignal,
122
+ ): Promise<GetSupportedIntentProtocolVersionsResponse> {
123
+ return this._v1_5.getSupportedIntentProtocolVersions(headers, signal)
124
+ }
54
125
  }