@coin-voyage/shared 2.3.5-beta.3 → 2.4.0-beta.0

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.
Files changed (57) hide show
  1. package/dist/api/client.d.ts +5 -1
  2. package/dist/api/client.js +42 -51
  3. package/dist/api/config.d.ts +6 -2
  4. package/dist/api/fetcher.d.ts +1 -1
  5. package/dist/api/fetcher.js +11 -5
  6. package/dist/chain/index.d.ts +3 -0
  7. package/dist/chain/index.js +3 -0
  8. package/dist/{common → currency}/currencies.js +6 -6
  9. package/dist/currency/index.d.ts +2 -0
  10. package/dist/currency/index.js +2 -0
  11. package/dist/{common → currency}/token-list.d.ts +2 -1
  12. package/dist/hooks/use-is-mobile.js +1 -1
  13. package/dist/payment/index.d.ts +1 -0
  14. package/dist/payment/index.js +1 -0
  15. package/dist/{common → payment}/payment-steps.d.ts +1 -1
  16. package/dist/{common → payment}/payment-steps.js +1 -1
  17. package/dist/schemas/index.d.ts +1 -0
  18. package/dist/schemas/index.js +1 -0
  19. package/dist/{common/validation.js → schemas/pay-order.js} +2 -2
  20. package/dist/types/api.d.ts +1 -1
  21. package/dist/types/index.d.ts +1 -0
  22. package/dist/types/index.js +1 -0
  23. package/dist/types/model.d.ts +2 -2
  24. package/dist/{common → types}/organization.d.ts +1 -1
  25. package/dist/{common → utils}/browser.d.ts +1 -1
  26. package/dist/utils/index.d.ts +7 -0
  27. package/dist/utils/index.js +7 -0
  28. package/package.json +48 -13
  29. package/dist/common/index.d.ts +0 -15
  30. package/dist/common/index.js +0 -15
  31. /package/dist/{common → chain}/address.d.ts +0 -0
  32. /package/dist/{common → chain}/address.js +0 -0
  33. /package/dist/{common → chain}/chain-explorer.d.ts +0 -0
  34. /package/dist/{common → chain}/chain-explorer.js +0 -0
  35. /package/dist/{common → chain}/chains.d.ts +0 -0
  36. /package/dist/{common → chain}/chains.js +0 -0
  37. /package/dist/{common → currency}/currencies.d.ts +0 -0
  38. /package/dist/{common → currency}/token-list.js +0 -0
  39. /package/dist/{common/validation.d.ts → schemas/pay-order.d.ts} +0 -0
  40. /package/dist/{common → types}/organization.js +0 -0
  41. /package/dist/{common → utils}/assert.d.ts +0 -0
  42. /package/dist/{common → utils}/assert.js +0 -0
  43. /package/dist/{common → utils}/browser.js +0 -0
  44. /package/dist/{common → utils}/debug.d.ts +0 -0
  45. /package/dist/{common → utils}/debug.js +0 -0
  46. /package/dist/{common → utils}/format.d.ts +0 -0
  47. /package/dist/{common → utils}/format.js +0 -0
  48. /package/dist/{common → utils}/i18n/index.d.ts +0 -0
  49. /package/dist/{common → utils}/i18n/index.js +0 -0
  50. /package/dist/{common → utils}/i18n/languages/en.d.ts +0 -0
  51. /package/dist/{common → utils}/i18n/languages/en.js +0 -0
  52. /package/dist/{common → utils}/local-storage.d.ts +0 -0
  53. /package/dist/{common → utils}/local-storage.js +0 -0
  54. /package/dist/{common → utils}/plural.d.ts +0 -0
  55. /package/dist/{common → utils}/plural.js +0 -0
  56. /package/dist/{common → utils}/time.d.ts +0 -0
  57. /package/dist/{common → utils}/time.js +0 -0
@@ -1,5 +1,6 @@
1
- import { type PayOrder, PayOrderMode } from "../types";
2
1
  import type { ClaimFeesRequest, ClaimFeesResponse, CreateWebhookRequest, GetFeeBalancesResponse, OrderStatusSocket, PaymentDetails, PaymentDetailsParams, PaymentMethodsResponse, PayOrderParams, PayOrderQuoteParams, RouteQuote, SwapDataRequest, SwapDataResponse, SwapQuoteRequest, SwapQuoteResponse, UpdateWebhookRequest, WebhookResponse } from "../types/api";
2
+ import { PayOrderMode } from "../types/enums";
3
+ import type { PayOrder } from "../types/model";
3
4
  import { type APIEnvironment } from "./config";
4
5
  import { type APIResponse } from "./fetcher";
5
6
  type Opts = {
@@ -17,6 +18,9 @@ export declare class ApiClient {
17
18
  version?: string;
18
19
  });
19
20
  private request;
21
+ private getErrorMessage;
22
+ private createBadRequestResponse;
23
+ private validatePayOrderParams;
20
24
  /**
21
25
  * Fetches a PayOrder by its ID.
22
26
  *
@@ -1,6 +1,7 @@
1
1
  import { createHmac } from "crypto";
2
- import { now, zPayOrder, zPayOrderMetadata } from "../common";
3
- import { PayOrderMode } from "../types";
2
+ import { now } from "../utils/time";
3
+ import { zPayOrder, zPayOrderMetadata } from "../schemas/pay-order";
4
+ import { PayOrderMode } from "../types/enums";
4
5
  import { WS_URL } from "./config";
5
6
  import { fetchApi } from "./fetcher";
6
7
  export class ApiClient {
@@ -25,6 +26,35 @@ export class ApiClient {
25
26
  },
26
27
  });
27
28
  }
29
+ getErrorMessage(error) {
30
+ if (error instanceof Error) {
31
+ return error.message;
32
+ }
33
+ return "An unknown error occurred";
34
+ }
35
+ createBadRequestResponse(path, error) {
36
+ return {
37
+ error: {
38
+ path,
39
+ statusCode: 400,
40
+ status: "Bad Request",
41
+ message: this.getErrorMessage(error),
42
+ },
43
+ };
44
+ }
45
+ validatePayOrderParams(params) {
46
+ const payOrderResult = zPayOrder.safeParse(params);
47
+ if (!payOrderResult.success) {
48
+ throw new Error(payOrderResult.error.issues.map((issue) => issue.message).join(", "));
49
+ }
50
+ if (!params.metadata) {
51
+ return;
52
+ }
53
+ const metadataResult = zPayOrderMetadata.safeParse(params.metadata);
54
+ if (!metadataResult.success) {
55
+ throw new Error(metadataResult.error.issues.map((issue) => issue.message).join(", "));
56
+ }
57
+ }
28
58
  /**
29
59
  * Fetches a PayOrder by its ID.
30
60
  *
@@ -84,17 +114,10 @@ export class ApiClient {
84
114
  */
85
115
  async createDepositPayOrder(params, opts) {
86
116
  try {
87
- return this.createPayOrder(params, PayOrderMode.DEPOSIT, undefined, opts);
117
+ return await this.createPayOrder(params, PayOrderMode.DEPOSIT, undefined, opts);
88
118
  }
89
119
  catch (error) {
90
- return {
91
- error: {
92
- path: `/pay-orders`,
93
- statusCode: 400,
94
- status: "Bad Request",
95
- message: error.message,
96
- },
97
- };
120
+ return this.createBadRequestResponse(`/pay-orders`, error);
98
121
  }
99
122
  }
100
123
  /**
@@ -140,17 +163,10 @@ export class ApiClient {
140
163
  async createSalePayOrder(params, apiSecret, opts) {
141
164
  try {
142
165
  const signature = this.generateAuthorizationSignature(apiSecret, "POST", "/pay-orders");
143
- return this.createPayOrder(params, PayOrderMode.SALE, signature, opts);
166
+ return await this.createPayOrder(params, PayOrderMode.SALE, signature, opts);
144
167
  }
145
168
  catch (error) {
146
- return {
147
- error: {
148
- path: `/pay-orders`,
149
- statusCode: 400,
150
- status: "Bad Request",
151
- message: error.message,
152
- },
153
- };
169
+ return this.createBadRequestResponse(`/pay-orders`, error);
154
170
  }
155
171
  }
156
172
  /**
@@ -195,20 +211,11 @@ export class ApiClient {
195
211
  * }
196
212
  */
197
213
  async createRefundPayOrder(payOrderId, params, apiSecret, opts) {
214
+ const path = `/pay-orders/${payOrderId}/refund`;
198
215
  try {
199
- const result = zPayOrder.safeParse(params);
200
- if (!result.success) {
201
- throw new Error(result.error.issues.map((e) => e.message).join(", "));
202
- }
203
- if (params.metadata) {
204
- const result = zPayOrderMetadata.safeParse(params.metadata);
205
- if (!result.success) {
206
- throw new Error(result.error.issues.map((e) => e.message).join(", "));
207
- }
208
- }
209
- const path = `/pay-orders/${payOrderId}/refund`;
216
+ this.validatePayOrderParams(params);
210
217
  const signature = this.generateAuthorizationSignature(apiSecret, "POST", path);
211
- return this.request({
218
+ return await this.request({
212
219
  path,
213
220
  options: {
214
221
  method: "POST",
@@ -218,27 +225,11 @@ export class ApiClient {
218
225
  });
219
226
  }
220
227
  catch (error) {
221
- return {
222
- error: {
223
- path: `/pay-orders/${payOrderId}/refund`,
224
- statusCode: 400,
225
- status: "Bad Request",
226
- message: error.message,
227
- },
228
- };
228
+ return this.createBadRequestResponse(path, error);
229
229
  }
230
230
  }
231
231
  async createPayOrder(params, mode, signature, opts) {
232
- const result = zPayOrder.safeParse(params);
233
- if (!result.success) {
234
- throw new Error(result.error.issues.map((e) => e.message).join(", "));
235
- }
236
- if (params.metadata) {
237
- const result = zPayOrderMetadata.safeParse(params.metadata);
238
- if (!result.success) {
239
- throw new Error(result.error.issues.map((e) => e.message).join(", "));
240
- }
241
- }
232
+ this.validatePayOrderParams(params);
242
233
  if ([PayOrderMode.SALE, PayOrderMode.REFUND].includes(mode) && !signature) {
243
234
  throw new Error(`Signature is required for ${mode} PayOrders`);
244
235
  }
@@ -377,7 +368,7 @@ export class ApiClient {
377
368
  * @returns {Promise<APIResponse<WebhookResponse[]>>} - List of webhooks wrapped in an API response.
378
369
  */
379
370
  async listWebhooks(apiSecret, opts) {
380
- const signature = this.generateAuthorizationSignature(apiSecret, "GET", "/organizations/webhooks");
371
+ const signature = this.generateAuthorizationSignature(apiSecret, "GET", "/webhooks");
381
372
  return this.request({
382
373
  path: "/webhooks",
383
374
  options: {
@@ -1,3 +1,7 @@
1
- export declare const API_URL: Record<string, string>;
2
- export declare const WS_URL: Record<string, string>;
1
+ export declare const API_URL: {
2
+ readonly production: "https://api.coinvoyage.io/v2";
3
+ readonly development: "https://acc-api.coinvoyage.io/v2";
4
+ readonly local: "http://localhost:8000/v2";
5
+ };
3
6
  export type APIEnvironment = keyof typeof API_URL;
7
+ export declare const WS_URL: Record<APIEnvironment, string>;
@@ -1,4 +1,4 @@
1
- import { APIEnvironment } from "./config";
1
+ import { type APIEnvironment } from "./config";
2
2
  type FetchApiOptions = {
3
3
  path: string;
4
4
  environment?: APIEnvironment;
@@ -1,14 +1,20 @@
1
1
  import { API_URL } from "./config";
2
+ function buildHeaders(options) {
3
+ const headers = new Headers(options?.headers);
4
+ if (!headers.has("Accept")) {
5
+ headers.set("Accept", "application/json");
6
+ }
7
+ if (options?.body !== undefined && !headers.has("Content-Type")) {
8
+ headers.set("Content-Type", "application/json");
9
+ }
10
+ return headers;
11
+ }
2
12
  export async function fetchApi(opts) {
3
13
  try {
4
14
  const baseUrl = API_URL[opts.environment || "production"];
5
15
  const response = await fetch(`${baseUrl}${opts.path}`, {
6
16
  ...opts.options,
7
- headers: {
8
- ...opts.options?.headers,
9
- "Content-Type": "application/json",
10
- "Accept-Encoding": "gzip, deflate, br",
11
- },
17
+ headers: buildHeaders(opts.options),
12
18
  });
13
19
  if (!response.ok) {
14
20
  const message = await response.text();
@@ -0,0 +1,3 @@
1
+ export * from "./address";
2
+ export * from "./chain-explorer";
3
+ export * from "./chains";
@@ -0,0 +1,3 @@
1
+ export * from "./address";
2
+ export * from "./chain-explorer";
3
+ export * from "./chains";
@@ -7,13 +7,13 @@ export const currencyRateUSD = {
7
7
  rateUSD: 1,
8
8
  };
9
9
  const data = [
10
- ["Euro", "", "EUR", 2],
11
- ["British Pound", "£", "GBP", 2],
10
+ ["Euro", "€", "EUR", 2],
11
+ ["British Pound", "£", "GBP", 2],
12
12
  ["New Taiwan Dollar", "NT$", "TWD", 0],
13
- ["Swiss Franc", "", "CHF", 2],
14
- ["Japanese Yen", "¥", "JPY", 0],
15
- ["Korean Won", "", "KRW", 0],
16
- ["Yuan", "¥", "CNY", 0],
13
+ ["Swiss Franc", "â‚£", "CHF", 2],
14
+ ["Japanese Yen", "Â¥", "JPY", 0],
15
+ ["Korean Won", "â‚©", "KRW", 0],
16
+ ["Yuan", "Â¥", "CNY", 0],
17
17
  ["Canadian Dollar", "C$", "CAD", 2],
18
18
  ["Australian Dollar", "A$", "AUD", 2],
19
19
  ["Singapore Dollar", "S$", "SGD", 2],
@@ -0,0 +1,2 @@
1
+ export * from "./currencies";
2
+ export * from "./token-list";
@@ -0,0 +1,2 @@
1
+ export * from "./currencies";
2
+ export * from "./token-list";
@@ -1,4 +1,5 @@
1
- import type { ChainId, Currency, CurrencyBase } from "../types";
1
+ import type { ChainId } from "../types/enums";
2
+ import type { Currency, CurrencyBase } from "../types/model";
2
3
  export interface Token {
3
4
  name: string;
4
5
  address?: string;
@@ -1,5 +1,5 @@
1
1
  import { useEffect, useState } from "react";
2
- import { isMobile } from "../common/browser";
2
+ import { isMobile } from "../utils/browser";
3
3
  export default function useIsMobile() {
4
4
  const [mobile, setMobile] = useState(isMobile());
5
5
  useEffect(() => {
@@ -0,0 +1 @@
1
+ export * from "./payment-steps";
@@ -0,0 +1 @@
1
+ export * from "./payment-steps";
@@ -1,4 +1,4 @@
1
- import { BitcoinPaymentData, type CryptoPaymentData, EVMPaymentData, type FiatPaymentData, type PaymentData, type PaymentStep, SolanaPaymentData, SuiPaymentData } from "../types";
1
+ import { type BitcoinPaymentData, type CryptoPaymentData, type EVMPaymentData, type FiatPaymentData, type PaymentData, type PaymentStep, type SolanaPaymentData, type SuiPaymentData } from "../types/model";
2
2
  export declare function getFiatPaymentStep(payment?: PaymentData): PaymentStep | undefined;
3
3
  export declare function getFiatPaymentData(payment?: PaymentData): FiatPaymentData | undefined;
4
4
  export declare function getCryptoDepositStep(payment?: PaymentData): PaymentStep | undefined;
@@ -1,4 +1,4 @@
1
- import { PaymentRail, StepKind, } from "../types";
1
+ import { PaymentRail, StepKind } from "../types/enums";
2
2
  export function getFiatPaymentStep(payment) {
3
3
  return payment?.steps.find((step) => {
4
4
  return (step.rail === PaymentRail.FIAT || step.kind === StepKind.KIND_STRIPE_ONRAMP) && step.data?.fiat;
@@ -0,0 +1 @@
1
+ export * from "./pay-order";
@@ -0,0 +1 @@
1
+ export * from "./pay-order";
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { ChainId } from "../types/enums";
3
- import { isBitcoinAddress, isEthereumAddress, isSolanaAddress, isSuiAddress, isTronAddress } from "./address";
4
- import { FIAT_CURRENCIES } from "./currencies";
3
+ import { isBitcoinAddress, isEthereumAddress, isSolanaAddress, isSuiAddress, isTronAddress } from "../chain/address";
4
+ import { FIAT_CURRENCIES } from "../currency/currencies";
5
5
  const knownKeys = ["items", "refund"];
6
6
  const customFieldSchema = z.union([z.string(), z.number(), z.boolean(), z.array(z.any()), z.object({}).loose()]).refine((val) => {
7
7
  if (typeof val === "string")
@@ -1,4 +1,4 @@
1
- import { FiatCurrency } from "../common/currencies";
1
+ import { FiatCurrency } from "../currency/currencies";
2
2
  import { ChainId, ChainType, PaymentMethod, PaymentRail, PayOrderStatus } from "./enums";
3
3
  import { PayOrderEvent } from "./events";
4
4
  import { CurrencyAmount, CurrencyBase, CurrencyWithAmount, FiatAmount, PaymentData, PayOrderMetadata, QuoteWithCurrency } from "./model";
@@ -3,4 +3,5 @@ export * from "./crypto";
3
3
  export * from "./enums";
4
4
  export * from "./events";
5
5
  export * from "./model";
6
+ export * from "./organization";
6
7
  export * from "./webhooks";
@@ -3,4 +3,5 @@ export * from "./crypto";
3
3
  export * from "./enums";
4
4
  export * from "./events";
5
5
  export * from "./model";
6
+ export * from "./organization";
6
7
  export * from "./webhooks";
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
- import { FiatCurrency } from "../common/currencies";
3
- import { zPayOrderMetadata } from "../common/validation";
2
+ import { FiatCurrency } from "../currency/currencies";
3
+ import { zPayOrderMetadata } from "../schemas/pay-order";
4
4
  import { ChainId, PaymentRail, PayOrderMode, PayOrderStatus, ProviderStatus, StepKind } from "./enums";
5
5
  export type PayOrder = {
6
6
  id: string;
@@ -1,4 +1,4 @@
1
- import { Currency } from "../types/model";
1
+ import { Currency } from "./model";
2
2
  export type SettlementCurrency = {
3
3
  receiving_address: string;
4
4
  } & Currency;
@@ -1,4 +1,4 @@
1
- declare const detectBrowser: () => import("detect-browser").Browser | "bot" | "node" | "react-native" | "";
1
+ declare const detectBrowser: () => "" | import("detect-browser").Browser | "bot" | "node" | "react-native";
2
2
  declare const detectOS: () => "" | import("detect-browser").OperatingSystem | NodeJS.Platform;
3
3
  declare const isIOS: () => boolean;
4
4
  declare const isAndroid: () => boolean;
@@ -0,0 +1,7 @@
1
+ export * from "./assert";
2
+ export * from "./browser";
3
+ export * from "./debug";
4
+ export * from "./format";
5
+ export * from "./local-storage";
6
+ export * from "./plural";
7
+ export * from "./time";
@@ -0,0 +1,7 @@
1
+ export * from "./assert";
2
+ export * from "./browser";
3
+ export * from "./debug";
4
+ export * from "./format";
5
+ export * from "./local-storage";
6
+ export * from "./plural";
7
+ export * from "./time";
package/package.json CHANGED
@@ -1,21 +1,41 @@
1
1
  {
2
2
  "name": "@coin-voyage/shared",
3
3
  "description": "Shared utilities for Coin Voyage",
4
- "version": "2.3.5-beta.3",
4
+ "version": "2.4.0-beta.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {
8
- "./common/chains": {
9
- "types": "./dist/common/chains.d.ts",
10
- "default": "./dist/common/chains.js"
8
+ "./api": {
9
+ "types": "./dist/api/index.d.ts",
10
+ "default": "./dist/api/index.js"
11
11
  },
12
- "./types/*": {
12
+ "./chain": {
13
+ "types": "./dist/chain/index.d.ts",
14
+ "default": "./dist/chain/index.js"
15
+ },
16
+ "./currency": {
17
+ "types": "./dist/currency/index.d.ts",
18
+ "default": "./dist/currency/index.js"
19
+ },
20
+ "./hooks": {
21
+ "types": "./dist/hooks/index.d.ts",
22
+ "default": "./dist/hooks/index.js"
23
+ },
24
+ "./payment": {
25
+ "types": "./dist/payment/index.d.ts",
26
+ "default": "./dist/payment/index.js"
27
+ },
28
+ "./schemas": {
29
+ "types": "./dist/schemas/index.d.ts",
30
+ "default": "./dist/schemas/index.js"
31
+ },
32
+ "./types": {
13
33
  "types": "./dist/types/index.d.ts",
14
34
  "default": "./dist/types/index.js"
15
35
  },
16
- "./*": {
17
- "types": "./dist/*/index.d.ts",
18
- "default": "./dist/*/index.js"
36
+ "./utils": {
37
+ "types": "./dist/utils/index.d.ts",
38
+ "default": "./dist/utils/index.js"
19
39
  }
20
40
  },
21
41
  "files": [
@@ -23,14 +43,29 @@
23
43
  ],
24
44
  "typesVersions": {
25
45
  "*": {
26
- "common/chains": [
27
- "dist/common/chains.d.ts"
46
+ "api": [
47
+ "dist/api/index.d.ts"
48
+ ],
49
+ "chain": [
50
+ "dist/chain/index.d.ts"
51
+ ],
52
+ "currency": [
53
+ "dist/currency/index.d.ts"
54
+ ],
55
+ "hooks": [
56
+ "dist/hooks/index.d.ts"
57
+ ],
58
+ "payment": [
59
+ "dist/payment/index.d.ts"
60
+ ],
61
+ "schemas": [
62
+ "dist/schemas/index.d.ts"
28
63
  ],
29
- "types/*": [
64
+ "types": [
30
65
  "dist/types/index.d.ts"
31
66
  ],
32
- "*": [
33
- "dist/*/index.d.ts"
67
+ "utils": [
68
+ "dist/utils/index.d.ts"
34
69
  ]
35
70
  }
36
71
  },
@@ -1,15 +0,0 @@
1
- export * from "./address";
2
- export * from "./assert";
3
- export * from "./browser";
4
- export * from "./chain-explorer";
5
- export * from "./chains";
6
- export * from "./currencies";
7
- export * from "./debug";
8
- export * from "./format";
9
- export * from "./local-storage";
10
- export * from "./organization";
11
- export * from "./payment-steps";
12
- export * from "./plural";
13
- export * from "./time";
14
- export * from "./validation";
15
- export * from "./token-list";
@@ -1,15 +0,0 @@
1
- export * from "./address";
2
- export * from "./assert";
3
- export * from "./browser";
4
- export * from "./chain-explorer";
5
- export * from "./chains";
6
- export * from "./currencies";
7
- export * from "./debug";
8
- export * from "./format";
9
- export * from "./local-storage";
10
- export * from "./organization";
11
- export * from "./payment-steps";
12
- export * from "./plural";
13
- export * from "./time";
14
- export * from "./validation";
15
- export * from "./token-list";
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes