scryfall-api 4.0.0-beta.2 → 4.0.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/README.md CHANGED
@@ -39,21 +39,26 @@ bun add scryfall-api
39
39
 
40
40
  In the documentation below, requiring the package is assumed.
41
41
 
42
- ```js
42
+ ```ts
43
43
  import scryfall from 'scryfall-api';
44
- ```
45
44
 
45
+ scryfall.Cards.byName('Black Lotus').then(console.log);
46
+ ```
46
47
 
47
48
  **With CommonJS**
48
49
 
49
50
  ```js
50
51
  const scryfall = require('scryfall-api');
52
+
53
+ scryfall.Cards.byName('Black Lotus').then(console.log);
51
54
  ```
52
55
 
53
56
  **If you only need Cards**
54
57
 
55
- ```js
58
+ ```ts
56
59
  import { Cards } from 'scryfall-api';
60
+
61
+ Cards.byName('Black Lotus').then(console.log);
57
62
  ```
58
63
 
59
64
  Link to [full documentation.](./DOCUMENTATION.md)
@@ -20,15 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/scryfall-api.ts
21
21
  var scryfall_api_exports = {};
22
22
  __export(scryfall_api_exports, {
23
- CardIdentifierBuilder: () => card_identifier_default,
24
- Cards: () => card_default,
25
- Catalogs: () => catalog_default,
23
+ CardIdentifierBuilder: () => CardIdentifierApi,
24
+ Cards: () => card_api_default,
25
+ Catalogs: () => catalog_api_default,
26
26
  InvalidScryfallArgumentError: () => InvalidScryfallArgumentError,
27
27
  MagicPageResult: () => MagicPageResult,
28
- Rulings: () => ruling_default,
28
+ Rulings: () => ruling_api_default,
29
29
  ScryfallError: () => ScryfallError,
30
- Sets: () => set_default,
31
- SymbologyApi: () => symbology_default,
30
+ Sets: () => set_api_default,
31
+ SymbologyApi: () => symbology_api_default,
32
32
  UnknownScryfallError: () => UnknownScryfallError
33
33
  });
34
34
  module.exports = __toCommonJS(scryfall_api_exports);
@@ -72,68 +72,12 @@ async function debounce() {
72
72
  }
73
73
  }
74
74
  function createDebounceFetcher(fetcher2 = fetch) {
75
- return async function debounceFetcher(...args) {
75
+ return async function debounceFetcher(...arguments_) {
76
76
  await debounce();
77
- return fetcher2(...args);
77
+ return fetcher2(...arguments_);
78
78
  };
79
79
  }
80
80
 
81
- // src/fetcher/page.result.ts
82
- var MagicPageResult = class {
83
- constructor(apiPath, query) {
84
- this.apiPath = apiPath;
85
- this.query = query;
86
- this.#hasMore = true;
87
- this.#count = 0;
88
- }
89
- #count;
90
- #hasMore;
91
- get count() {
92
- return this.#count;
93
- }
94
- get hasMore() {
95
- return this.#hasMore;
96
- }
97
- async all() {
98
- const r = [];
99
- while (this.#hasMore) {
100
- const result = await this.next();
101
- r.push(...result);
102
- }
103
- return r;
104
- }
105
- async get(limit) {
106
- const r = [];
107
- while (this.#hasMore) {
108
- const result = await this.next();
109
- r.push(...result);
110
- if (r.length === limit) {
111
- return r;
112
- }
113
- if (r.length > limit) {
114
- return r.slice(0, limit);
115
- }
116
- }
117
- return r;
118
- }
119
- async next() {
120
- const results = await fetcher(this.apiPath, this.query);
121
- this.#hasMore = results?.has_more ?? false;
122
- this.#count = Number.parseInt(`${results?.total_cards ?? 0}`, 10);
123
- if (this.#hasMore) {
124
- this.setNextPage();
125
- }
126
- return results?.data ?? [];
127
- }
128
- async page(page) {
129
- this.query.page = page;
130
- return this.next();
131
- }
132
- setNextPage() {
133
- this.query.page += 1;
134
- }
135
- };
136
-
137
81
  // src/fetcher/retry.fetcher.ts
138
82
  var defaultCanRetry = async (response) => {
139
83
  const object = await response.json();
@@ -144,11 +88,11 @@ var defaultCanRetry = async (response) => {
144
88
  const code = "code" in object && typeof object.code === "string" ? object.code : void 0;
145
89
  return code !== "not_found" && code !== "bad_request";
146
90
  };
147
- function createRetryFetcher({ maxAttempts = 3, canRetry = defaultCanRetry } = {}, fetcher2 = fetch) {
91
+ function createRetryFetcher({ canRetry = defaultCanRetry, maxAttempts = 3 } = {}, fetcher2 = fetch) {
148
92
  let retries = 0;
149
- return async function retryFetcher(...args) {
93
+ return async function retryFetcher(...arguments_) {
150
94
  retries++;
151
- const response = await fetcher2(...args);
95
+ const response = await fetcher2(...arguments_);
152
96
  if (response.ok) {
153
97
  return response;
154
98
  }
@@ -162,44 +106,42 @@ function createRetryFetcher({ maxAttempts = 3, canRetry = defaultCanRetry } = {}
162
106
  if (code && details && status) {
163
107
  throw new ScryfallError(code, details, status, type, warnings);
164
108
  }
165
- throw new UnknownScryfallError(`Request failed with status ${response.status}`);
109
+ throw new UnknownScryfallError(`Request failed with status ${response.status.toFixed(0)}`);
166
110
  }
167
111
  if (canRetry(response)) {
168
- return retryFetcher(...args);
112
+ return retryFetcher(...arguments_);
169
113
  }
170
114
  if (code && details && status) {
171
115
  throw new ScryfallError(code, details, status, type, warnings);
172
116
  }
173
- throw new UnknownScryfallError(`Request failed with status ${response.status}`);
117
+ throw new UnknownScryfallError(`Request failed with status ${response.status.toFixed(0)}`);
174
118
  };
175
119
  }
176
120
 
177
- // src/fetcher/index.ts
121
+ // src/fetcher/fetcher.ts
178
122
  var simpleFetcher = createDebounceFetcher(createRetryFetcher());
179
123
  var endpoint = "https://api.scryfall.com";
180
124
  var DATE_REGEXP = /^\d{4}-\d{2}-\d{2}?$/;
181
125
  var dateParser = (_key, value) => {
182
- if (typeof value === "string") {
183
- if (DATE_REGEXP.test(value)) {
184
- return new Date(value);
185
- }
126
+ if (typeof value === "string" && DATE_REGEXP.test(value)) {
127
+ return new Date(value);
186
128
  }
187
129
  return value;
188
130
  };
189
- async function fetcher(apiPath, params, body) {
131
+ async function fetcher(apiPath, parameters, body) {
190
132
  let path = "";
191
133
  if (typeof apiPath === "number" || typeof apiPath === "string") {
192
134
  path = apiPath;
193
135
  } else if (apiPath) {
194
136
  path = apiPath.join("/");
195
137
  }
196
- const url = new URL(`${endpoint}/${path}`);
197
- if (params) {
198
- Object.entries(params).forEach(([key, value]) => {
138
+ const url = new URL(`${endpoint}/${String(path)}`);
139
+ if (parameters) {
140
+ for (const [key, value] of Object.entries(parameters)) {
199
141
  if (value !== void 0) {
200
- url.searchParams.append(key, `${value}`);
142
+ url.searchParams.append(key, value.toString());
201
143
  }
202
- });
144
+ }
203
145
  }
204
146
  try {
205
147
  const init = {
@@ -214,19 +156,75 @@ async function fetcher(apiPath, params, body) {
214
156
  const response = await simpleFetcher(url, init);
215
157
  const content = await response.text();
216
158
  return JSON.parse(content, dateParser);
217
- } catch (e) {
218
- if (e instanceof ScryfallError) {
219
- if (e.warnings) {
220
- console.warn(e.warnings);
159
+ } catch (error) {
160
+ if (error instanceof ScryfallError) {
161
+ if (error.warnings) {
162
+ console.warn(error.warnings);
221
163
  }
222
- if (e.code === "not_found" || e.code === "bad_request") {
164
+ if (error.code === "not_found" || error.code === "bad_request") {
223
165
  return void 0;
224
166
  }
225
167
  }
226
- throw e;
168
+ throw error;
227
169
  }
228
170
  }
229
171
 
172
+ // src/fetcher/page.result.ts
173
+ var MagicPageResult = class {
174
+ constructor(apiPath, query) {
175
+ this.apiPath = apiPath;
176
+ this.query = query;
177
+ this.#hasMore = true;
178
+ this.#count = 0;
179
+ }
180
+ #count;
181
+ #hasMore;
182
+ get count() {
183
+ return this.#count;
184
+ }
185
+ get hasMore() {
186
+ return this.#hasMore;
187
+ }
188
+ async all() {
189
+ const r = [];
190
+ while (this.#hasMore) {
191
+ const result = await this.next();
192
+ r.push(...result);
193
+ }
194
+ return r;
195
+ }
196
+ async get(limit) {
197
+ const r = [];
198
+ while (this.#hasMore) {
199
+ const result = await this.next();
200
+ r.push(...result);
201
+ if (r.length === limit) {
202
+ return r;
203
+ }
204
+ if (r.length > limit) {
205
+ return r.slice(0, limit);
206
+ }
207
+ }
208
+ return r;
209
+ }
210
+ async next() {
211
+ const results = await fetcher(this.apiPath, this.query);
212
+ this.#hasMore = results?.has_more ?? false;
213
+ this.#count = Number.parseInt(results?.total_cards?.toFixed(0) ?? "0", 10);
214
+ if (this.#hasMore) {
215
+ this.setNextPage();
216
+ }
217
+ return results?.data ?? [];
218
+ }
219
+ async page(page) {
220
+ this.query.page = page;
221
+ return this.next();
222
+ }
223
+ setNextPage() {
224
+ this.query.page += 1;
225
+ }
226
+ };
227
+
230
228
  // src/api/card/card.api.ts
231
229
  var CardApi = class {
232
230
  /*
@@ -292,8 +290,8 @@ var CardApi = class {
292
290
  }
293
291
  async collection(...identifiers) {
294
292
  const cards2 = [];
295
- for (let i = 0; i < identifiers.length; i += 75) {
296
- const collectionSection = { identifiers: identifiers.slice(i, i + 75) };
293
+ for (let index = 0; index < identifiers.length; index += 75) {
294
+ const collectionSection = { identifiers: identifiers.slice(index, index + 75) };
297
295
  const result = await fetcher(
298
296
  "cards/collection",
299
297
  void 0,
@@ -316,9 +314,6 @@ var CardApi = class {
316
314
  var cards = new CardApi();
317
315
  var card_api_default = cards;
318
316
 
319
- // src/api/card/index.ts
320
- var card_default = card_api_default;
321
-
322
317
  // src/api/card-identifier/card-identifier.api.ts
323
318
  var CardIdentifierApi = class {
324
319
  static byId(id) {
@@ -343,85 +338,94 @@ var CardIdentifierApi = class {
343
338
  return { oracle_id: id };
344
339
  }
345
340
  static bySet(set2, collectorNumber) {
346
- return { collector_number: `${collectorNumber}`, set: set2 };
341
+ return { collector_number: String(collectorNumber), set: set2 };
347
342
  }
348
343
  };
349
344
 
350
- // src/api/card-identifier/index.ts
351
- var card_identifier_default = CardIdentifierApi;
352
-
353
345
  // src/api/catalog/catalog.api.ts
354
346
  var CatalogApi = class {
355
347
  async artifactTypes() {
356
- return (await fetcher("catalog/artifact-types"))?.data ?? [];
348
+ const response = await fetcher("catalog/artifact-types");
349
+ return response?.data ?? [];
357
350
  }
358
351
  async artistNames() {
359
- return (await fetcher("catalog/artist-names"))?.data ?? [];
352
+ const response = await fetcher("catalog/artist-names");
353
+ return response?.data ?? [];
360
354
  }
361
355
  async cardNames() {
362
- return (await fetcher("catalog/card-names"))?.data ?? [];
356
+ const response = await fetcher("catalog/card-names");
357
+ return response?.data ?? [];
363
358
  }
364
359
  async creatureTypes() {
365
- return (await fetcher("catalog/creature-types"))?.data ?? [];
360
+ const response = await fetcher("catalog/creature-types");
361
+ return response?.data ?? [];
366
362
  }
367
363
  async enchantmentTypes() {
368
- return (await fetcher("catalog/enchantment-types"))?.data ?? [];
364
+ const response = await fetcher("catalog/enchantment-types");
365
+ return response?.data ?? [];
369
366
  }
370
367
  async landTypes() {
371
- return (await fetcher("catalog/land-types"))?.data ?? [];
368
+ const response = await fetcher("catalog/land-types");
369
+ return response?.data ?? [];
372
370
  }
373
371
  async loyalties() {
374
- return (await fetcher("catalog/loyalties"))?.data ?? [];
372
+ const response = await fetcher("catalog/loyalties");
373
+ return response?.data ?? [];
375
374
  }
376
375
  async planeswalkerTypes() {
377
- return (await fetcher("catalog/planeswalker-types"))?.data ?? [];
376
+ const response = await fetcher("catalog/planeswalker-types");
377
+ return response?.data ?? [];
378
378
  }
379
379
  async powers() {
380
- return (await fetcher("catalog/powers"))?.data ?? [];
380
+ const response = await fetcher("catalog/powers");
381
+ return response?.data ?? [];
381
382
  }
382
383
  async spellTypes() {
383
- return (await fetcher("catalog/spell-types"))?.data ?? [];
384
+ const response = await fetcher("catalog/spell-types");
385
+ return response?.data ?? [];
384
386
  }
385
387
  async toughnesses() {
386
- return (await fetcher("catalog/toughnesses"))?.data ?? [];
388
+ const response = await fetcher("catalog/toughnesses");
389
+ return response?.data ?? [];
387
390
  }
388
391
  async watermarks() {
389
- return (await fetcher("catalog/watermarks"))?.data ?? [];
392
+ const response = await fetcher("catalog/watermarks");
393
+ return response?.data ?? [];
390
394
  }
391
395
  async wordBank() {
392
- return (await fetcher("catalog/word-bank"))?.data ?? [];
396
+ const response = await fetcher("catalog/word-bank");
397
+ return response?.data ?? [];
393
398
  }
394
399
  };
395
400
  var catalog = new CatalogApi();
396
401
  var catalog_api_default = catalog;
397
402
 
398
- // src/api/catalog/index.ts
399
- var catalog_default = catalog_api_default;
400
-
401
403
  // src/api/ruling/ruling.api.ts
402
404
  var RulingApi = class {
403
405
  async byArenaId(id) {
404
- return (await fetcher(["cards/arena", id, "rulings"]))?.data ?? [];
406
+ const response = await fetcher(["cards/arena", id, "rulings"]);
407
+ return response?.data ?? [];
405
408
  }
406
409
  async byId(id) {
407
- return (await fetcher(["cards", id, "rulings"]))?.data ?? [];
410
+ const response = await fetcher(["cards", id, "rulings"]);
411
+ return response?.data ?? [];
408
412
  }
409
413
  async byMtgoId(id) {
410
- return (await fetcher(["cards/mtgo", id, "rulings"]))?.data ?? [];
414
+ const response = await fetcher(["cards/mtgo", id, "rulings"]);
415
+ return response?.data ?? [];
411
416
  }
412
417
  async byMultiverseId(id) {
413
- return (await fetcher(["cards/multiverse", id, "rulings"]))?.data ?? [];
418
+ const response = await fetcher(["cards/multiverse", id, "rulings"]);
419
+ return response?.data ?? [];
414
420
  }
415
421
  async bySet(setCode, collectorNumber) {
416
- return (await fetcher(["cards", setCode, `${collectorNumber}`, "rulings"]))?.data ?? [];
422
+ const response = await fetcher(["cards", setCode, String(collectorNumber), "rulings"]);
423
+ return response?.data ?? [];
417
424
  }
418
425
  };
419
426
  var rulings = new RulingApi();
420
427
  var ruling_api_default = rulings;
421
428
 
422
- // src/api/ruling/index.ts
423
- var ruling_default = ruling_api_default;
424
-
425
429
  // src/api/set/set.api.ts
426
430
  var SetApi = class {
427
431
  async all() {
@@ -441,9 +445,6 @@ var SetApi = class {
441
445
  var set = new SetApi();
442
446
  var set_api_default = set;
443
447
 
444
- // src/api/set/index.ts
445
- var set_default = set_api_default;
446
-
447
448
  // src/api/symbology/symbology.api.ts
448
449
  var SymbologyApi = class {
449
450
  async all() {
@@ -458,10 +459,10 @@ var SymbologyApi = class {
458
459
  cost: shorthand
459
460
  });
460
461
  return cost ?? {
461
- cost: "",
462
- colors: [],
463
462
  cmc: 0,
464
463
  colorless: false,
464
+ colors: [],
465
+ cost: "",
465
466
  monocolored: false,
466
467
  multicolored: false,
467
468
  object: "mana_cost"
@@ -470,9 +471,6 @@ var SymbologyApi = class {
470
471
  };
471
472
  var symbology = new SymbologyApi();
472
473
  var symbology_api_default = symbology;
473
-
474
- // src/api/symbology/index.ts
475
- var symbology_default = symbology_api_default;
476
474
  // Annotate the CommonJS export names for ESM import in node:
477
475
  0 && (module.exports = {
478
476
  CardIdentifierBuilder,