react-api-adts
Version:
Helpful algebraic data types for API handling in React apps.
29 lines (28 loc) • 1.39 kB
TypeScript
import { AxiosResponse } from "axios";
import { AuthErrorDetails, FutureResult, ValidErrorDetails } from "./result";
export declare class Encoder<T> {
contentType: string;
encode: (obj: T) => ArrayBuffer;
constructor(contentType: string, encode: (obj: T) => ArrayBuffer);
}
export declare class Decoder<T> {
contentType: string;
decode: (input: ArrayBuffer) => T;
constructor(contentType: string, decode: (input: ArrayBuffer) => T);
map<S>(fun: (t: T) => S): Decoder<S>;
}
export declare function ackDecoder(): Decoder<any>;
export declare class API<V> {
urlPrefix: string;
validErrorDecoder: Decoder<ValidErrorDetails>;
authErrorDecoder: Decoder<AuthErrorDetails>;
timeoutMillis: number;
constructor(urlPrefix: string, validErrorDecoder: Decoder<ValidErrorDetails>, authErrorDecoder: Decoder<AuthErrorDetails>, timeoutMillis: number);
makeFullUrl(url: string): string;
makeAcceptHeader(successType: string): string;
handleResponse<T>(resp: Promise<AxiosResponse>, successParser: Decoder<T>): FutureResult<T>;
post<S, T>(url: string, s: S, encoder: Encoder<S>, decoder: Decoder<T>): FutureResult<T>;
put<S, T>(url: string, s: S, encoder: Encoder<S>, decoder: Decoder<T>): FutureResult<T>;
get<T>(url: string, decoder: Decoder<T>): FutureResult<T>;
delete<T>(url: string, decoder: Decoder<T>): FutureResult<T>;
}