Giter Club home page Giter Club logo

api-types's Introduction

Scryfall API types

This library documents the definitive comprehensive typings of the Scryfall API for use in Typescript & Javascript projects.

This library uses semver for versioning. These versions only describe this library, not the Scryfall API as a whole.

Examples

Fetching a card

// CommonJS module syntax
const { ScryfallCard } = require("@scryfall/api-types");

// ES module syntax
import { ScryfallCard } from "@scryfall/api-types";

// Fetch a card
const response = await fetch("https://api.scryfall.com/cards/iko/275");
const godzilla: ScryfallCard.Any = await response.json();

Fetching the list of sets

// CommonJS module syntax
const { ScryfallList } = require("@scryfall/api-types");

// ES module syntax
import { ScryfallList } from "@scryfall/api-types";

// Fetch the list of all setss
const response = await fetch("https://api.scryfall.com/sets");
const sets: ScryfallList.Sets = await response.json();

Type narrowing on a card

import { ScryfallCard, ScryfallLayout } from "@scryfall/api-types";

// This card might be of any type.
// You cannot access `mysteryCard.card_faces`, because it might not have that property.
const mysteryCard: ScryfallCard.Any = getCard();

// You can narrow by layout:
if (mysteryCard.layout === ScryfallLayout.Transform) {
  // It's a transforming DFC!
  // You can access transform-specific fields in this scope, or assign it to a variable.
  const faces = anyCard.card_faces;
  const transform: ScryfallCard.Transform = mysteryCard;
}

// You can also narrow by property:
if ("card_faces" in anyCard) {
  // It's *some* kind of multi-faced card.
  // You can now access the card_faces property.
  // You'll need to do some further type narrowing to know more about what's in the card.
  const faces = anyCard.card_faces;
  const multifaced: ScryfallCard.AnyMultiFaced = anyCard;
} else {
  const sfc: ScryfallCard.AnySingleFaced = anyCard;
}

Usage

Each type and enum exported by this library corresponds to a Scryfall API object and its values.

Points of interest:

If the API provides an object, this library provides it as well. (If it doesn't, please tell us!)

All primary types and values are prefixed with Scryfall to avoid conflict with the standard library (e.g. Object, Error, Set) and to minimise conflict with your other libraries and dependencies (e.g. Color, LanguageCode). If we didn't have the prefix you'd be forced to append one yourself one on import, so we've defaulted to including it.

Enum fields are described both in terms of an enum and a set of strings in order to give you the option of interacting with that field with either one.

api-types's People

Contributors

scarletcs avatar crookedneighbor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.