Last active
November 26, 2018 14:58
-
-
Save andywer/6f0cf5abc199702ab5a01ee7f22f4745 to your computer and use it in GitHub Desktop.
Typing REST APIs with TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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