Skip to content

Instantly share code, notes, and snippets.

@andywer
Last active November 26, 2018 14:58
Show Gist options
  • Select an option

  • Save andywer/6f0cf5abc199702ab5a01ee7f22f4745 to your computer and use it in GitHub Desktop.

Select an option

Save andywer/6f0cf5abc199702ab5a01ee7f22f4745 to your computer and use it in GitHub Desktop.
Typing REST APIs with TypeScript
import { Responses as PublisherAPIResponses } from "@satoshipay/publisher-service-api"
import axios from "axios"
import { URL } from "url"
import config from "../config"
export async function getPublisherById (id: string) {
const url = new URL(`/${id}`, config.publisherServiceUrl).toString()
return await axios.get(url) as PublisherAPIResponses["GET /*"]
}
// In theory we could even abstract that into a function of its own:
// function queryPublisherService<PublisherAPIPath extends keyof PublisherAPI> (
// path: PublisherAPIPath,
// pathParams?: string[],
// options?: AxiosRequestOptions
// )
// Benefit: A little bit nicer to use
// Drawback: A bit less explicit, the late path params substitution looks less elegant & seems unnecessarily complex
interface StatusLiveResponse {}
interface GetPublisherResponse {
publisher: Publisher,
tier2Application: Tier2Application
}
export type Responses = {
"GET /status/live": StatusLiveResponse,
// Idea:
"GET /*": GetPublisherResponse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment