@rozoai/intent-common 0.0.28-beta.2 → 0.0.28-beta.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.
@@ -0,0 +1,215 @@
1
+ /**
2
+ * RozoAI API Configuration Constants
3
+ */
4
+ export const ROZO_API_URL = "https://intentapiv2.rozo.ai/functions/v1";
5
+ export const ROZO_API_TOKEN =
6
+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZ4Y3Zmb2xobmNtdXZmYXp1cXViIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI4Mzg2NjYsImV4cCI6MjA2ODQxNDY2Nn0.B4dV5y_-zCMKSNm3_qyCbAvCPJmoOGv_xB783LfAVUA";
7
+
8
+ // HTTP methods type
9
+ export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
10
+
11
+ // Request options type
12
+ export interface RequestOptions {
13
+ method?: HttpMethod;
14
+ headers?: Record<string, string>;
15
+ body?: any;
16
+ params?: Record<string, string>;
17
+ signal?: AbortSignal;
18
+ }
19
+
20
+ // Response type with generic data
21
+ export interface ApiResponse<T = any> {
22
+ data: T | null;
23
+ error: Error | null;
24
+ status: number | null;
25
+ }
26
+
27
+ // Request state for hooks (used in connectkit)
28
+ export interface RequestState<T = any> extends ApiResponse<T> {
29
+ isLoading: boolean;
30
+ isError: boolean;
31
+ isSuccess: boolean;
32
+ }
33
+
34
+ /**
35
+ * API Configuration
36
+ */
37
+ export interface ApiConfig {
38
+ baseUrl: string;
39
+ apiToken: string;
40
+ }
41
+
42
+ // Default configuration (can be overridden via setApiConfig)
43
+ let apiConfig: ApiConfig = {
44
+ baseUrl: ROZO_API_URL,
45
+ apiToken: ROZO_API_TOKEN,
46
+ };
47
+
48
+ /**
49
+ * Set API configuration
50
+ * @param config - API configuration
51
+ */
52
+ export const setApiConfig = (config: Partial<ApiConfig>) => {
53
+ apiConfig = { ...apiConfig, ...config };
54
+ };
55
+
56
+ /**
57
+ * Get current API configuration
58
+ * @returns Current API configuration
59
+ */
60
+ export const getApiConfig = (): ApiConfig => {
61
+ return apiConfig;
62
+ };
63
+
64
+ /**
65
+ * Creates a URL with query parameters
66
+ * @param url - Base URL
67
+ * @param params - Query parameters
68
+ * @returns Full URL with query parameters
69
+ */
70
+ const createUrl = (url: string, params?: Record<string, string>): string => {
71
+ const fullUrl = url.startsWith("/")
72
+ ? `${apiConfig.baseUrl}${url}`
73
+ : `${apiConfig.baseUrl}/${url}`;
74
+
75
+ if (!params) return fullUrl;
76
+
77
+ const queryParams = new URLSearchParams();
78
+ Object.entries(params).forEach(([key, value]) => {
79
+ if (value !== undefined && value !== null) {
80
+ queryParams.append(key, value);
81
+ }
82
+ });
83
+
84
+ const queryString = queryParams.toString();
85
+ return queryString ? `${fullUrl}?${queryString}` : fullUrl;
86
+ };
87
+
88
+ /**
89
+ * Core fetch function for making API requests
90
+ * @param url - API endpoint path
91
+ * @param options - Request options
92
+ * @returns Promise with response data
93
+ */
94
+ export const fetchApi = async <T = any>(
95
+ url: string,
96
+ options: RequestOptions = {}
97
+ ): Promise<ApiResponse<T>> => {
98
+ const { method = "GET", headers = {}, body, params, signal } = options;
99
+
100
+ try {
101
+ const fullUrl = createUrl(url, params);
102
+
103
+ const requestHeaders: Record<string, string> = {
104
+ "Content-Type": "application/json",
105
+ ...headers,
106
+ Authorization: `Bearer ${apiConfig.apiToken}`,
107
+ };
108
+
109
+ const requestOptions: {
110
+ method: string;
111
+ headers: Record<string, string>;
112
+ signal?: AbortSignal;
113
+ body?: string;
114
+ } = {
115
+ method,
116
+ headers: requestHeaders,
117
+ signal,
118
+ };
119
+
120
+ if (body && method !== "GET") {
121
+ requestOptions.body = JSON.stringify(body);
122
+ }
123
+
124
+ const response = await fetch(fullUrl, requestOptions);
125
+ const status = response.status;
126
+
127
+ // Handle non-JSON responses
128
+ const contentType = response.headers.get("content-type");
129
+ let data: T | null = null;
130
+
131
+ if (contentType && contentType.includes("application/json")) {
132
+ data = (await response.json()) as T;
133
+ } else if (contentType && contentType.includes("text/")) {
134
+ data = (await response.text()) as unknown as T;
135
+ }
136
+
137
+ if (!response.ok) {
138
+ throw new Error(data ? JSON.stringify(data) : response.statusText);
139
+ }
140
+
141
+ return { data, error: null, status };
142
+ } catch (error) {
143
+ return {
144
+ data: null,
145
+ error: error instanceof Error ? error : new Error(String(error)),
146
+ status: null,
147
+ };
148
+ }
149
+ };
150
+
151
+ /**
152
+ * API client with methods for different HTTP verbs
153
+ */
154
+ export const apiClient = {
155
+ /**
156
+ * GET request
157
+ * @param url - API endpoint path
158
+ * @param options - Request options
159
+ * @returns Promise with response data
160
+ */
161
+ get: <T = any>(
162
+ url: string,
163
+ options: Omit<RequestOptions, "method" | "body"> = {}
164
+ ) => fetchApi<T>(url, { ...options, method: "GET" }),
165
+
166
+ /**
167
+ * POST request
168
+ * @param url - API endpoint path
169
+ * @param body - Request body
170
+ * @param options - Additional request options
171
+ * @returns Promise with response data
172
+ */
173
+ post: <T = any>(
174
+ url: string,
175
+ body: any,
176
+ options: Omit<RequestOptions, "method" | "body"> = {}
177
+ ) => fetchApi<T>(url, { ...options, method: "POST", body }),
178
+
179
+ /**
180
+ * PUT request
181
+ * @param url - API endpoint path
182
+ * @param body - Request body
183
+ * @param options - Additional request options
184
+ * @returns Promise with response data
185
+ */
186
+ put: <T = any>(
187
+ url: string,
188
+ body: any,
189
+ options: Omit<RequestOptions, "method" | "body"> = {}
190
+ ) => fetchApi<T>(url, { ...options, method: "PUT", body }),
191
+
192
+ /**
193
+ * PATCH request
194
+ * @param url - API endpoint path
195
+ * @param body - Request body
196
+ * @param options - Additional request options
197
+ * @returns Promise with response data
198
+ */
199
+ patch: <T = any>(
200
+ url: string,
201
+ body: any,
202
+ options: Omit<RequestOptions, "method" | "body"> = {}
203
+ ) => fetchApi<T>(url, { ...options, method: "PATCH", body }),
204
+
205
+ /**
206
+ * DELETE request
207
+ * @param url - API endpoint path
208
+ * @param options - Request options
209
+ * @returns Promise with response data
210
+ */
211
+ delete: <T = any>(
212
+ url: string,
213
+ options: Omit<RequestOptions, "method"> = {}
214
+ ) => fetchApi<T>(url, { ...options, method: "DELETE" }),
215
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * API client utilities for RozoAI Intent Pay
3
+ *
4
+ * This module provides core API client functionality for making HTTP requests
5
+ * to the RozoAI payment API. It includes:
6
+ * - Base API client with support for GET, POST, PUT, PATCH, DELETE
7
+ * - Payment API functions and types
8
+ * - Type-safe interfaces for requests and responses
9
+ *
10
+ * Note: React hooks for these APIs are available in the @rozoai/intent-pay package.
11
+ */
12
+
13
+ export * from "./base";
14
+ export * from "./payment";
@@ -0,0 +1,112 @@
1
+ import { apiClient, ApiResponse } from "./base";
2
+
3
+ /**
4
+ * Payment display information
5
+ */
6
+ export interface PaymentDisplay {
7
+ intent: string;
8
+ paymentValue: string;
9
+ currency: string;
10
+ }
11
+
12
+ /**
13
+ * Payment destination information
14
+ */
15
+ export interface PaymentDestination {
16
+ destinationAddress?: string;
17
+ chainId: string;
18
+ amountUnits: string;
19
+ tokenSymbol: string;
20
+ tokenAddress?: string;
21
+ txHash?: string | null;
22
+ }
23
+
24
+ /**
25
+ * Payment source information
26
+ */
27
+ export interface PaymentSource {
28
+ sourceAddress?: string;
29
+ [key: string]: unknown;
30
+ }
31
+
32
+ /**
33
+ * Payment request data type
34
+ */
35
+ export interface PaymentRequestData {
36
+ appId: string;
37
+ display: PaymentDisplay;
38
+ destination: PaymentDestination;
39
+ externalId?: string;
40
+ metadata?: Record<string, unknown>;
41
+ [key: string]: unknown;
42
+ }
43
+
44
+ /**
45
+ * Payment response data type
46
+ */
47
+ export interface PaymentResponseData {
48
+ id: string;
49
+ status: "payment_unpaid" | string;
50
+ createdAt: string;
51
+ display: {
52
+ intent: string;
53
+ currency: string;
54
+ paymentValue?: string;
55
+ };
56
+ source: PaymentSource | null;
57
+ destination: {
58
+ destinationAddress: string;
59
+ txHash: string | null;
60
+ chainId: string;
61
+ amountUnits: string;
62
+ tokenSymbol: string;
63
+ tokenAddress: string;
64
+ };
65
+ metadata: {
66
+ daimoOrderId?: string;
67
+ intent: string;
68
+ items: unknown[];
69
+ payer: Record<string, unknown>;
70
+ appId: string;
71
+ orderDate: string;
72
+ webhookUrl: string;
73
+ provider: string;
74
+ receivingAddress: string;
75
+ memo: string | null;
76
+ payinchainid: string;
77
+ payintokenaddress: string;
78
+ preferredChain: string;
79
+ preferredToken: string;
80
+ preferredTokenAddress: string;
81
+ source_tx_hash?: string;
82
+ [key: string]: unknown;
83
+ };
84
+ url: string;
85
+ [key: string]: unknown;
86
+ }
87
+
88
+ /**
89
+ * Creates a new payment
90
+ * @param paymentData - Payment data to send
91
+ * @returns Promise with payment response
92
+ */
93
+ export const createRozoPayment = (
94
+ paymentData: PaymentRequestData
95
+ ): Promise<ApiResponse<PaymentResponseData>> => {
96
+ return apiClient.post<PaymentResponseData>("/payment-api", paymentData);
97
+ };
98
+
99
+ /**
100
+ * Gets payment details by ID
101
+ * @param paymentId - Payment ID
102
+ * @returns Promise with payment response
103
+ */
104
+ export const getRozoPayment = (
105
+ paymentId: string
106
+ ): Promise<ApiResponse<PaymentResponseData>> => {
107
+ const isMugglePay = paymentId.includes("mugglepay_order");
108
+ const endpoint = isMugglePay
109
+ ? `payment-api/${paymentId}`
110
+ : `payment/id/${paymentId}`;
111
+ return apiClient.get<PaymentResponseData>(endpoint);
112
+ };