1
import { RESTDataSource } from "apollo-datasource-rest"
2
import { ScryfallModels } from "../models"
3

4
export class Scryfall extends RESTDataSource {
5 0
  baseURL = `https://api.scryfall.com/`
6

7 0
  cardById = async ({ id, ...params }: { id: string }) => {
8 0
    const card = await this.get(`cards/${id}`, { ...params })
9 0
    return new ScryfallModels.Card(card)
10
  }
11

12 1
  cards = async ({ page = 1, ...params }) => {
13 0
    const { data, ...rest } = await this.get(`cards`, { page, ...params })
14

15 0
    return {
16
      page,
17
      ...rest,
18
      data: data.map(
19
        (card: ConstructorParameters<typeof ScryfallModels.Card>) =>
20 0
          new ScryfallModels.Card(card)
21
      )
22
    }
23
  }
24

25 0
  cardsBySet = async ({
26
    code,
27 1
    page = 1,
28
    ...params
29
  }: {
30
    code: string
31
    page: number
32
  }) => {
33 0
    const { data, ...rest } = await this.get(`cards/search`, {
34
      q: `set:${code}`,
35
      page,
36
      ...params
37
    })
38

39 0
    return {
40
      page,
41
      ...rest,
42
      data: data.map(
43
        (card: ConstructorParameters<typeof ScryfallModels.Card>) =>
44 0
          new ScryfallModels.Card(card)
45
      )
46
    }
47
  }
48

49 0
  setByCode = async ({ code, ...params }: { code: string }) => {
50 0
    const set = await this.get(`sets/${code}`, { ...params })
51 0
    return new ScryfallModels.Set(set)
52
  }
53

54 1
  sets = async (params = {}) => {
55 0
    const { data, ...rest } = await this.get(`sets`, params)
56 0
    return {
57
      ...rest,
58
      data: data.map(
59
        (set: ConstructorParameters<typeof ScryfallModels.Set>) =>
60 0
          new ScryfallModels.Set(set)
61
      )
62
    }
63
  }
64

65 0
  rulings = ({ id, ...params }: { id: string }) =>
66 0
    this.get(`cards/${id}/rulings`, { ...params })
67

68 0
  searchCards = async ({
69
    q,
70 1
    page = 1,
71
    ...params
72
  }: {
73
    q: string
74
    page: number
75
  }) => {
76 0
    const { data, ...rest } = await this.get(`cards/search`, {
77
      q,
78
      page,
79
      ...params
80
    })
81

82 0
    return {
83
      page,
84
      ...rest,
85
      data: data.map(
86
        (card: ConstructorParameters<typeof ScryfallModels.Card>) =>
87 0
          new ScryfallModels.Card(card)
88
      )
89
    }
90
  }
91
}

Read our documentation on viewing source code .

Loading