@rozoai/intent-common 0.1.2 → 0.1.4

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,12 +1,16 @@
1
1
  {
2
2
  "name": "@rozoai/intent-common",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Intent Pay shared types and utilities",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "RozoAI",
8
8
  "homepage": "https://github.com/RozoAI/intent-pay",
9
9
  "license": "BSD-2-Clause",
10
+ "files": [
11
+ "dist",
12
+ "README.md"
13
+ ],
10
14
  "devDependencies": {
11
15
  "@tsconfig/node20": "^20.1.4",
12
16
  "@types/tape": "^5.6.0",
package/src/api/base.ts DELETED
@@ -1,261 +0,0 @@
1
- /**
2
- * RozoAI API Configuration Constants
3
- */
4
- export const ROZO_API_URL = "https://intentapiv2.rozo.ai/functions/v1";
5
- export const NEW_ROZO_API_URL = "https://intentapiv4.rozo.ai/functions/v1";
6
- export const ROZO_API_TOKEN =
7
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZ4Y3Zmb2xobmNtdXZmYXp1cXViIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI4Mzg2NjYsImV4cCI6MjA2ODQxNDY2Nn0.B4dV5y_-zCMKSNm3_qyCbAvCPJmoOGv_xB783LfAVUA";
8
-
9
- // HTTP methods type
10
- export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
11
-
12
- // Request options type
13
- export interface RequestOptions {
14
- method?: HttpMethod;
15
- headers?: Record<string, string>;
16
- body?: any;
17
- params?: Record<string, string>;
18
- signal?: AbortSignal;
19
- }
20
-
21
- // Response type with generic data
22
- export interface ApiResponse<T = any> {
23
- data: T | null;
24
- error: Error | null;
25
- status: number | null;
26
- }
27
-
28
- // Request state for hooks (used in connectkit)
29
- export interface RequestState<T = any> extends ApiResponse<T> {
30
- isLoading: boolean;
31
- isError: boolean;
32
- isSuccess: boolean;
33
- }
34
-
35
- /**
36
- * API Version type
37
- */
38
- export type ApiVersion = "v1" | "v2";
39
-
40
- /**
41
- * API Configuration
42
- */
43
- export interface ApiConfig {
44
- baseUrl: string;
45
- apiToken: string;
46
- version?: ApiVersion;
47
- }
48
-
49
- // Default configuration (can be overridden via setApiConfig)
50
- // v2 is the default API version
51
- let apiConfig: Record<ApiVersion, ApiConfig> = {
52
- v1: { baseUrl: ROZO_API_URL, apiToken: ROZO_API_TOKEN, version: "v1" },
53
- v2: { baseUrl: NEW_ROZO_API_URL, apiToken: ROZO_API_TOKEN, version: "v2" },
54
- };
55
-
56
- // Current active API version
57
- let activeVersion: ApiVersion = "v2";
58
-
59
- /**
60
- * Gets the current active API configuration
61
- * @returns Current API configuration
62
- */
63
- export const getApiConfig = (): ApiConfig => {
64
- return apiConfig[activeVersion];
65
- };
66
-
67
- /**
68
- * Sets the API configuration (baseUrl, apiToken, and version)
69
- * @param config - Partial API configuration to override defaults
70
- * @example
71
- * ```typescript
72
- * // Use v2 API (default)
73
- * setApiConfig({ version: "v2" });
74
- *
75
- * // Use v1 API
76
- * setApiConfig({ version: "v1" });
77
- *
78
- * // Custom configuration for a specific version
79
- * setApiConfig({
80
- * baseUrl: "https://custom-api.com",
81
- * apiToken: "custom-token",
82
- * version: "v2"
83
- * });
84
- * ```
85
- */
86
- export const setApiConfig = (config: Partial<ApiConfig>): void => {
87
- if (config.version) {
88
- activeVersion = config.version;
89
-
90
- // Update the specific version's config if baseUrl or apiToken provided
91
- if (config.baseUrl || config.apiToken) {
92
- apiConfig[activeVersion] = {
93
- ...apiConfig[activeVersion],
94
- ...(config.baseUrl && { baseUrl: config.baseUrl }),
95
- ...(config.apiToken && { apiToken: config.apiToken }),
96
- };
97
- }
98
- } else {
99
- // Update current active version with provided config
100
- apiConfig[activeVersion] = {
101
- ...apiConfig[activeVersion],
102
- ...(config.baseUrl && { baseUrl: config.baseUrl }),
103
- ...(config.apiToken && { apiToken: config.apiToken }),
104
- };
105
- }
106
- };
107
-
108
- /**
109
- * Creates a URL with query parameters
110
- * @param url - Base URL
111
- * @param params - Query parameters
112
- * @returns Full URL with query parameters
113
- */
114
- const createUrl = (url: string, params?: Record<string, string>): string => {
115
- const config = getApiConfig();
116
- const fullUrl = url.startsWith("/")
117
- ? `${config.baseUrl}${url}`
118
- : `${config.baseUrl}/${url}`;
119
-
120
- if (!params) return fullUrl;
121
-
122
- const queryParams = new URLSearchParams();
123
- Object.entries(params).forEach(([key, value]) => {
124
- if (value !== undefined && value !== null) {
125
- queryParams.append(key, value);
126
- }
127
- });
128
-
129
- const queryString = queryParams.toString();
130
- return queryString ? `${fullUrl}?${queryString}` : fullUrl;
131
- };
132
-
133
- /**
134
- * Core fetch function for making API requests
135
- * @param url - API endpoint path
136
- * @param options - Request options
137
- * @returns Promise with response data
138
- */
139
- export const fetchApi = async <T = any>(
140
- url: string,
141
- options: RequestOptions = {}
142
- ): Promise<ApiResponse<T>> => {
143
- const { method = "GET", headers = {}, body, params, signal } = options;
144
-
145
- try {
146
- const fullUrl = createUrl(url, params);
147
-
148
- const config = getApiConfig();
149
- const requestHeaders: Record<string, string> = {
150
- "Content-Type": "application/json",
151
- ...headers,
152
- Authorization: `Bearer ${config.apiToken}`,
153
- };
154
-
155
- const requestOptions: {
156
- method: string;
157
- headers: Record<string, string>;
158
- signal?: AbortSignal;
159
- body?: string;
160
- } = {
161
- method,
162
- headers: requestHeaders,
163
- signal,
164
- };
165
-
166
- if (body && method !== "GET") {
167
- requestOptions.body = JSON.stringify(body);
168
- }
169
-
170
- const response = await fetch(fullUrl, requestOptions);
171
- const status = response.status;
172
-
173
- // Handle non-JSON responses
174
- const contentType = response.headers.get("content-type");
175
- let data: T | null = null;
176
-
177
- if (contentType && contentType.includes("application/json")) {
178
- data = (await response.json()) as T;
179
- } else if (contentType && contentType.includes("text/")) {
180
- data = (await response.text()) as unknown as T;
181
- }
182
-
183
- if (!response.ok) {
184
- throw new Error(data ? JSON.stringify(data) : response.statusText);
185
- }
186
-
187
- return { data, error: null, status };
188
- } catch (error) {
189
- return {
190
- data: null,
191
- error: error instanceof Error ? error : new Error(String(error)),
192
- status: null,
193
- };
194
- }
195
- };
196
-
197
- /**
198
- * API client with methods for different HTTP verbs
199
- */
200
- export const apiClient = {
201
- /**
202
- * GET request
203
- * @param url - API endpoint path
204
- * @param options - Request options
205
- * @returns Promise with response data
206
- */
207
- get: <T = any>(
208
- url: string,
209
- options: Omit<RequestOptions, "method" | "body"> = {}
210
- ) => fetchApi<T>(url, { ...options, method: "GET" }),
211
-
212
- /**
213
- * POST request
214
- * @param url - API endpoint path
215
- * @param body - Request body
216
- * @param options - Additional request options
217
- * @returns Promise with response data
218
- */
219
- post: <T = any>(
220
- url: string,
221
- body: any,
222
- options: Omit<RequestOptions, "method" | "body"> = {}
223
- ) => fetchApi<T>(url, { ...options, method: "POST", body }),
224
-
225
- /**
226
- * PUT request
227
- * @param url - API endpoint path
228
- * @param body - Request body
229
- * @param options - Additional request options
230
- * @returns Promise with response data
231
- */
232
- put: <T = any>(
233
- url: string,
234
- body: any,
235
- options: Omit<RequestOptions, "method" | "body"> = {}
236
- ) => fetchApi<T>(url, { ...options, method: "PUT", body }),
237
-
238
- /**
239
- * PATCH request
240
- * @param url - API endpoint path
241
- * @param body - Request body
242
- * @param options - Additional request options
243
- * @returns Promise with response data
244
- */
245
- patch: <T = any>(
246
- url: string,
247
- body: any,
248
- options: Omit<RequestOptions, "method" | "body"> = {}
249
- ) => fetchApi<T>(url, { ...options, method: "PATCH", body }),
250
-
251
- /**
252
- * DELETE request
253
- * @param url - API endpoint path
254
- * @param options - Request options
255
- * @returns Promise with response data
256
- */
257
- delete: <T = any>(
258
- url: string,
259
- options: Omit<RequestOptions, "method"> = {}
260
- ) => fetchApi<T>(url, { ...options, method: "DELETE" }),
261
- };
package/src/api/fee.ts DELETED
@@ -1,95 +0,0 @@
1
- import { getChainById } from "../chain";
2
- import { ApiResponse } from "./base";
3
- import { FeeType } from "./payment";
4
-
5
- /**
6
- * Fee response data type (success case)
7
- */
8
- export interface FeeResponseData {
9
- appId: string;
10
- amount: number;
11
- currency: string;
12
- type: string;
13
- fee: number;
14
- feePercentage: string;
15
- minimumFee: string;
16
- amountIn: number;
17
- amountOut: number;
18
- amount_out: number;
19
- amount_in: number;
20
- }
21
-
22
- /**
23
- * Fee error response data type
24
- */
25
- export interface FeeErrorData {
26
- error: string;
27
- message: string;
28
- received: number;
29
- maxAllowed: number;
30
- }
31
-
32
- /**
33
- * Fee request parameters
34
- */
35
- export interface GetFeeParams {
36
- appId?: string;
37
- currency?: string;
38
- toUnits: string;
39
- toChain: number;
40
- type: FeeType;
41
- }
42
-
43
- /**
44
- * Gets fee calculation for a payment amount
45
- * @param params - Fee calculation parameters (amount is required)
46
- * @returns Promise with fee response or error
47
- */
48
- export const getFee = async (
49
- params: GetFeeParams
50
- ): Promise<ApiResponse<FeeResponseData>> => {
51
- const { toUnits, appId, currency = "USDC", toChain, type } = params;
52
-
53
- try {
54
- const chain = getChainById(Number(toChain));
55
- const toChainName = chain.name.toLowerCase();
56
-
57
- const queryParams = new URLSearchParams({
58
- ...(appId ? { appId } : {}),
59
- ...(type === FeeType.ExactIn
60
- ? { type: "exactIn" }
61
- : { type: "exactOut" }),
62
- amount: toUnits,
63
- currency,
64
- tochain: toChainName,
65
- });
66
-
67
- const response = await fetch(
68
- `https://intentapi.rozo.ai/getFee?${queryParams.toString()}`
69
- );
70
-
71
- const data = (await response.json()) as FeeResponseData | FeeErrorData;
72
-
73
- // Check if response contains an error
74
- if (!response.ok || "error" in data) {
75
- const errorData = data as FeeErrorData;
76
- return {
77
- data: null,
78
- error: new Error(errorData.message || errorData.error),
79
- status: response.status,
80
- };
81
- }
82
-
83
- return {
84
- data: data as FeeResponseData,
85
- error: null,
86
- status: response.status,
87
- };
88
- } catch (error) {
89
- return {
90
- data: null,
91
- error: error instanceof Error ? error : new Error(String(error)),
92
- status: null,
93
- };
94
- }
95
- };