http.ts
This file
Path
packages/kit/src/http.ts
25 lines · 722 bytes · TypeScript
Last changed by
History of this fileTake it
1import { fetchTransport, isSuccess } from "./transport.ts";2import type { Transport } from "./transport.ts";34/** A GET's outcome: the body on a success, the status otherwise. */5export type Got =6 | { readonly ok: true; readonly text: string }7 | { readonly ok: false; readonly status: number };89/**10 * The HTTP helpers new code uses. A failed request is a value the caller11 * handles, never an exception.12 */13export const http = {14 /** GETs `url`. */15 get: async (16 url: string,17 transport: Transport = fetchTransport18 ): Promise<Got> => {19 const reply = await transport(url);2021 return isSuccess(reply.status)22 ? { ok: true, text: reply.text }23 : { ok: false, status: reply.status };24 },25};