Skip to content

Commit 9040452

Browse files
committed
v5.0.0, add support for Scryfall's new agent restriction
Won't know if this actually works until Scryfall starts rejecting requests, though
1 parent ffa2678 commit 9040452

File tree

9 files changed

+45
-9
lines changed

9 files changed

+45
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v5.0.0 (August 8th, 2024)
2+
- Added support for setting the agent name due to [Scryfall's new requirements](https://scryfall.com/blog/user-agent-and-accept-header-now-required-on-the-api-225) via `Scry.setAgent(name, version)`(./DOCUMENTATION.md#scrysetagent-agent-string-version-string-void-). All applications using scryfall-sdk that are not running on clientside in the browser must provide an agent name & version.
3+
14
# v4.2.1 (December 9th, 2023)
25
- Fixed `MagicEmitters` (IE, what's returned by `Cards.search()`) erroring if not every possible event has a listener registered, thanks to [#69](https://github.com/ChiriVulpes/scryfall-sdk/pull/69) by [lordnox](https://github.com/lordnox).
36

DOCUMENTATION.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
## Table of Contents
22
- [Usage](#usage-)
3+
- [`Scry.setAgent (agent: string, version: string): void;`](#scrysetagent-agent-string-version-string-void-)
34
- [Cards](#cards-)
45
- [`Cards.byId (id: string): Promise<Card>;`](#cardsbyid-id-string-promisecard-)
56
- [`Cards.byName (name: string, set?: string, fuzzy = false): Promise<Card>;`](#cardsbyname-name-string-set-string-fuzzy--false-promisecard-)
6-
- [`Cards.bySet (code: string, collectorNumber: string | number, lang?: string): Promise<Card>;`](#cardsbyset-setcode-string-collectornumber-string--number-lang-string-promisecard-)
7+
- [`Cards.bySet (setCode: string, collectorNumber: string | number, lang?: string): Promise<Card>;`](#cardsbyset-setcode-string-collectornumber-string--number-lang-string-promisecard-)
78
- [`Cards.byMultiverseId (id: number): Promise<Card>;` ](#cardsbymultiverseid-id-number-promisecard-)
89
- [`Cards.byMtgoId (id: number): Promise<Card>;` ](#cardsbymtgoid-id-number-promisecard-)
910
- [`Cards.byArenaId (id: number): Promise<Card>;` ](#cardsbyarenaid-id-number-promisecard-)
@@ -86,7 +87,12 @@ import Scry = require("scryfall-sdk");
8687
### Using a node.js version older than v18?
8788
Install the [`axios`](https://www.npmjs.com/package/axios) dependency alongside scryfall-sdk and it will automatically use it.
8889

89-
90+
### `Scry.setAgent (agent: string, version: string): void;` [🡅](#table-of-contents)
91+
> [!IMPORTANT]
92+
> Scryfall *requires* that all applications provide an agent, except if they are executing on clientside in a browser.
93+
```ts
94+
Scry.setAgent("MyAmazingAppName", "1.0.0");
95+
```
9096

9197
## Cards [🡅](#table-of-contents)
9298

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2017-2023 Chiri Vulpes
1+
Copyright 2017-2024 Chiri Vulpes
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A Node.js SDK for [Scryfall](https://scryfall.com/docs/api) written in Typescript.
77

8-
As of [October 23rd, 2023](./CHANGELOG.md), all features described in the [Scryfall documentation](https://scryfall.com/docs/api) are supported. If you see something that isn't supported, make an issue!
8+
As of [August 8th, 2024](./CHANGELOG.md), all features described in the [Scryfall documentation](https://scryfall.com/docs/api) should be supported. If you see something that isn't, make an issue!
99

1010

1111
## Installation
@@ -30,11 +30,18 @@ const prints = await chalice.getPrints();
3030
console.log(prints.length); // 7
3131
```
3232

33-
This module supports all features of Scryfall, along with automatically paginating through results, downloading bulk data streams, etc. There's also documentation on every single part of the SDK:
34-
33+
> [!IMPORTANT]
34+
> Scryfall [requires](https://scryfall.com/docs/api#required-headers) that all applications provide an agent, except if they are executing from web browser JavaScript.
35+
>
36+
> If this is true for your application, you must set your agent before making any requests:
37+
> ```ts
38+
> Scry.setAgent("MyAmazingAppName", "1.0.0");
39+
> ```
3540
3641
## [Full Documentation](./DOCUMENTATION.md)
37-
### [Scryfall-SDK Equivalents for Scryfall Routes](./ROUTES.md)
42+
scryfall-sdk supports all features of Scryfall, along with automatically paginating through results, downloading bulk data streams, and more. See the [documentation](./DOCUMENTATION.md) for information on everything you can do.
43+
44+
Know the endpoint you want, but not sure what it looks like in scryfall-sdk? Well, you're in luck: [Scryfall-SDK Equivalents for Scryfall Routes](./ROUTES.md)
3845
3946
4047
## Contributing
@@ -59,4 +66,4 @@ gulp watch
5966

6067
## MIT License
6168

62-
[Copyright 2017-2022 Chiri Vulpes](./LICENSE)
69+
[Copyright 2017-2024 Chiri Vulpes](./LICENSE)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scryfall-sdk",
3-
"version": "4.2.2",
3+
"version": "5.0.0",
44
"main": "./out/Scry.js",
55
"types": "./out/Scry.d.ts",
66
"description": "A Node.js SDK for https://scryfall.com/docs/api written in Typescript.",

src/Scry.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ Cards["Scry"] = exports;
2525
Sets["Scry"] = exports;
2626

2727

28+
/**
29+
* Sets the name and version of this agent. This is required for all non-browser applications.
30+
*/
31+
export function setAgent (agent: string, version: string) {
32+
MagicQuerier.agent = `${agent}/${version}`;
33+
}
34+
2835
/**
2936
* Sets the API calls to retry if they fail, for any reason.
3037
* @param attempts The number of attempts that can be made (includes the initial call).

src/api/BulkData.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ class BulkData extends MagicQuerier {
9494
} else {
9595
const result = await fetch(definition.download_uri, {
9696
method: "GET",
97+
headers: {
98+
...!MagicQuerier.agent ? undefined : {
99+
'User-Agent': MagicQuerier.agent,
100+
},
101+
Accept: "*/*",
102+
},
97103
});
98104

99105
return result.body;

src/tests/Main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { Card, ENDPOINT_FILE_1, RESOURCE_GENERIC_CARD_BACK, SymbologyTransformer
99
import Cached from "../util/Cached";
1010
import MagicQuerier from "../util/MagicQuerier";
1111

12+
Scry.setAgent("ScryfallSDKTests", "1.0");
13+
1214
const expect = chai.expect;
1315
chai.use(chaiAsPromised);
1416

src/util/MagicQuerier.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default class MagicQuerier {
6363

6464
public static lastQuery = 0;
6565
public static retry: RetryStrategy = { attempts: 1 };
66+
public static agent?: string;
6667
public static timeout = defaultRequestTimeout;
6768
public static requestCount = 0;
6869

@@ -149,6 +150,10 @@ export default class MagicQuerier {
149150
body: JSON.stringify(post),
150151
headers: {
151152
'Content-Type': 'application/json',
153+
...!MagicQuerier.agent ? undefined : {
154+
'User-Agent': MagicQuerier.agent,
155+
},
156+
Accept: "*/*",
152157
},
153158
method: post ? "POST" : "GET",
154159
});

0 commit comments

Comments
 (0)