@stellar-snaps/sdk 0.3.0 → 0.3.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/dist/index.js CHANGED
@@ -23,8 +23,62 @@ function _interopNamespace(e) {
23
23
 
24
24
  var StellarSdk__namespace = /*#__PURE__*/_interopNamespace(StellarSdk);
25
25
 
26
+ // src/errors.ts
27
+ var StellarSnapError = class extends Error {
28
+ constructor(message, code) {
29
+ super(message);
30
+ this.code = code;
31
+ this.name = "StellarSnapError";
32
+ }
33
+ };
34
+ var InvalidAddressError = class extends StellarSnapError {
35
+ constructor(address) {
36
+ super(`Invalid Stellar address: ${address}`, "INVALID_ADDRESS");
37
+ this.name = "InvalidAddressError";
38
+ }
39
+ };
40
+ var InvalidAmountError = class extends StellarSnapError {
41
+ constructor(amount) {
42
+ super(`Invalid amount: ${amount}. Must be a positive number.`, "INVALID_AMOUNT");
43
+ this.name = "InvalidAmountError";
44
+ }
45
+ };
46
+ var InvalidAssetError = class extends StellarSnapError {
47
+ constructor(message) {
48
+ super(message, "INVALID_ASSET");
49
+ this.name = "InvalidAssetError";
50
+ }
51
+ };
52
+ var InvalidUriError = class extends StellarSnapError {
53
+ constructor(message) {
54
+ super(message, "INVALID_URI");
55
+ this.name = "InvalidUriError";
56
+ }
57
+ };
58
+ var SnapNotFoundError = class extends StellarSnapError {
59
+ constructor(snapId) {
60
+ super(`Snap not found: ${snapId}`, "SNAP_NOT_FOUND");
61
+ this.name = "SnapNotFoundError";
62
+ }
63
+ };
64
+ var SnapUnauthorizedError = class extends StellarSnapError {
65
+ constructor(message = "Unauthorized") {
66
+ super(message, "SNAP_UNAUTHORIZED");
67
+ this.name = "SnapUnauthorizedError";
68
+ }
69
+ };
70
+ var SnapApiError = class extends StellarSnapError {
71
+ constructor(message, statusCode) {
72
+ super(message, "SNAP_API_ERROR");
73
+ this.statusCode = statusCode;
74
+ this.name = "SnapApiError";
75
+ }
76
+ };
77
+
26
78
  // src/create-snap.ts
79
+ var DEFAULT_BASE_URL = "https://stellar-snaps.vercel.app";
27
80
  async function createSnap(options) {
81
+ const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
28
82
  const {
29
83
  creator,
30
84
  title,
@@ -36,20 +90,19 @@ async function createSnap(options) {
36
90
  memo,
37
91
  memoType = "MEMO_TEXT",
38
92
  network = "testnet",
39
- imageUrl,
40
- serverUrl = "https://stellarsnaps.com"
93
+ imageUrl
41
94
  } = options;
42
95
  if (!creator || creator.length !== 56 || !creator.startsWith("G")) {
43
- throw new Error("Invalid creator address");
96
+ throw new InvalidAddressError(creator || "");
44
97
  }
45
98
  if (!title || title.trim().length === 0) {
46
- throw new Error("Title is required");
99
+ throw new SnapApiError("Title is required");
47
100
  }
48
101
  if (!destination || destination.length !== 56 || !destination.startsWith("G")) {
49
- throw new Error("Invalid destination address");
102
+ throw new InvalidAddressError(destination || "");
50
103
  }
51
104
  if (assetCode !== "XLM" && !assetIssuer) {
52
- throw new Error("Asset issuer is required for non-XLM assets");
105
+ throw new SnapApiError("Asset issuer is required for non-XLM assets");
53
106
  }
54
107
  const body = {
55
108
  creator,
@@ -64,50 +117,62 @@ async function createSnap(options) {
64
117
  network,
65
118
  imageUrl
66
119
  };
67
- const response = await fetch(`${serverUrl}/api/snaps`, {
120
+ const response = await fetch(`${baseUrl}/api/snaps`, {
68
121
  method: "POST",
69
- headers: {
70
- "Content-Type": "application/json"
71
- },
122
+ headers: { "Content-Type": "application/json" },
72
123
  body: JSON.stringify(body)
73
124
  });
74
125
  if (!response.ok) {
75
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
76
- throw new Error(error.error || `Failed to create snap: ${response.status}`);
126
+ const data = await response.json().catch(() => ({ error: "Unknown error" }));
127
+ const message = data.error || `Failed to create snap: ${response.status}`;
128
+ throw new SnapApiError(message, response.status);
77
129
  }
78
130
  const snap = await response.json();
79
131
  return {
80
132
  id: snap.id,
81
- url: `${serverUrl}/s/${snap.id}`,
133
+ url: `${baseUrl}/s/${snap.id}`,
82
134
  snap
83
135
  };
84
136
  }
85
137
  async function getSnap(id, options = {}) {
86
- const { serverUrl = "https://stellarsnaps.com" } = options;
87
- const response = await fetch(`${serverUrl}/api/snaps/${id}`);
138
+ const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
139
+ const response = await fetch(`${baseUrl}/api/snap/${id}`);
88
140
  if (response.status === 404) {
89
- return null;
141
+ throw new SnapNotFoundError(id);
90
142
  }
91
143
  if (!response.ok) {
92
- throw new Error(`Failed to fetch snap: ${response.status}`);
144
+ const data = await response.json().catch(() => ({}));
145
+ const message = data.error || `Failed to fetch snap: ${response.status}`;
146
+ throw new SnapApiError(message, response.status);
93
147
  }
94
148
  return response.json();
95
149
  }
96
150
  async function listSnaps(creator, options = {}) {
97
- const { serverUrl = "https://stellarsnaps.com" } = options;
98
- const response = await fetch(`${serverUrl}/api/snaps?creator=${encodeURIComponent(creator)}`);
151
+ const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
152
+ const response = await fetch(
153
+ `${baseUrl}/api/snaps?creator=${encodeURIComponent(creator)}`
154
+ );
99
155
  if (!response.ok) {
100
- throw new Error(`Failed to list snaps: ${response.status}`);
156
+ const data = await response.json().catch(() => ({}));
157
+ const message = data.error || `Failed to list snaps: ${response.status}`;
158
+ throw new SnapApiError(message, response.status);
101
159
  }
102
160
  return response.json();
103
161
  }
104
- async function deleteSnap(id, options = {}) {
105
- const { serverUrl = "https://stellarsnaps.com" } = options;
106
- const response = await fetch(`${serverUrl}/api/snaps/${id}`, {
107
- method: "DELETE"
108
- });
109
- if (!response.ok && response.status !== 404) {
110
- throw new Error(`Failed to delete snap: ${response.status}`);
162
+ async function deleteSnap(id, creator, options = {}) {
163
+ const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
164
+ const url = `${baseUrl}/api/snaps?id=${encodeURIComponent(id)}&creator=${encodeURIComponent(creator)}`;
165
+ const response = await fetch(url, { method: "DELETE" });
166
+ if (response.status === 404) {
167
+ throw new SnapNotFoundError(id);
168
+ }
169
+ if (response.status === 403) {
170
+ throw new SnapUnauthorizedError("You do not have permission to delete this snap");
171
+ }
172
+ if (!response.ok) {
173
+ const data = await response.json().catch(() => ({}));
174
+ const message = data.error || `Failed to delete snap: ${response.status}`;
175
+ throw new SnapApiError(message, response.status);
111
176
  }
112
177
  }
113
178
 
@@ -317,39 +382,6 @@ var HORIZON_URLS = {
317
382
  testnet: "https://horizon-testnet.stellar.org"
318
383
  };
319
384
 
320
- // src/errors.ts
321
- var StellarSnapError = class extends Error {
322
- constructor(message, code) {
323
- super(message);
324
- this.code = code;
325
- this.name = "StellarSnapError";
326
- }
327
- };
328
- var InvalidAddressError = class extends StellarSnapError {
329
- constructor(address) {
330
- super(`Invalid Stellar address: ${address}`, "INVALID_ADDRESS");
331
- this.name = "InvalidAddressError";
332
- }
333
- };
334
- var InvalidAmountError = class extends StellarSnapError {
335
- constructor(amount) {
336
- super(`Invalid amount: ${amount}. Must be a positive number.`, "INVALID_AMOUNT");
337
- this.name = "InvalidAmountError";
338
- }
339
- };
340
- var InvalidAssetError = class extends StellarSnapError {
341
- constructor(message) {
342
- super(message, "INVALID_ASSET");
343
- this.name = "InvalidAssetError";
344
- }
345
- };
346
- var InvalidUriError = class extends StellarSnapError {
347
- constructor(message) {
348
- super(message, "INVALID_URI");
349
- this.name = "InvalidUriError";
350
- }
351
- };
352
-
353
385
  // src/create-transaction-snap.ts
354
386
  function createTransactionSnap(options) {
355
387
  const { xdr, network = "public", message, callback, pubkey } = options;
@@ -1166,6 +1198,9 @@ exports.NETWORK_PASSPHRASES = NETWORK_PASSPHRASES2;
1166
1198
  exports.POSTGRES_SCHEMA = POSTGRES_SCHEMA;
1167
1199
  exports.SHORTENER_DOMAINS = SHORTENER_DOMAINS;
1168
1200
  exports.SQLITE_SCHEMA = SQLITE_SCHEMA;
1201
+ exports.SnapApiError = SnapApiError;
1202
+ exports.SnapNotFoundError = SnapNotFoundError;
1203
+ exports.SnapUnauthorizedError = SnapUnauthorizedError;
1169
1204
  exports.StellarSnapError = StellarSnapError;
1170
1205
  exports.addDomain = addDomain;
1171
1206
  exports.buildPaymentTransaction = buildPaymentTransaction;