型システムを利用し、漏れがないようにレスポンスの処理が可能なRest API Client。 仕様書に定義されているエラーレスポンスについては個別にハンドリング可能。
import {
Router,
SuccessResponse,
ExpectedErrorResponse,
HttpCodes,
RESTClient,
UnexpectedErrorResponse,
isSucess,
isExpectedError,
isUnexpectedError
} from '../lib/RESTClient';
interface IResponse {
// response JSON schema here ...
}
const StatusCodes = [
HttpCodes.NotFound,
HttpCodes.InternalServerError,
] as const;
type StatusCodesTuple = typeof StatusCodes;
type TStatusCodes = StatusCodesTuple[number];
type TRouter = Router<IResponse, TStatusCodes>;
type TResponse = SuccessResponse<IResponse>;
const client = RestClient("client name", "https://your/api/url_base")
// In your async func
const router: TRouter = await client.get<IResponse, TStatusCodes>("/your/api/path", StatusCodes);
const route = router.routing({
success: (response: TResponse) => {
// post process here
},
expectedError: {
404: (response: TExpectedErrorResponse) => {
// error handling here ...
},
500: (response: TExpectedErrorResponse) => {
// error handling here ...
}
},
unexpectedError: (response: UnexpectedErrorResponse) => {
// error handling here ...
}
});
if (isSucess(route)) {
route.func(route.response);
}else if (isExpectedError(route)) {
route.func(route.response);
}else if (isUnexpectedError(route)) {
route.func(route.response);
} else {
throw Error("Invalid route")
};