dw-innovation / dw-kid2-graph-frontend-prototype

dw-kid2-graph-frontend-prototype.vercel.app
MIT License
0 stars 0 forks source link

`isAvailable` should return reason #120

Open nikonikoniko opened 2 years ago

nikonikoniko commented 2 years ago

currently, isAvailable functions return an Observable<boolean>. This is not helpful if we need to figure out why a service is not available.

I suggest for it to return Observable<[boolean, status]> or [Observable<boolean>, Observable<status>]

where

type status = 
  "missing config" 
  | "server offline" 
  | "no data to operate on"
  | `unexpected error ${e}`
nikonikoniko commented 2 years ago
status$ = YoutubeDownloader.isAvailavble() -> Observable<boolean>
type ServiceStatus = 
   // service ready
   { status: 200, message: string } |  
   //  no data to operate on
   { status: 418, message: "no data to operate on" }
   // missing cfg   
   { status: 401, message: "missig config" as const } |
   // server offline
   { status: 500, message: "cannot reach server" }

const status$ = YoutubeDownloader.status() -> Observable<ServiceStatus>
nikonikoniko commented 2 years ago
nikonikoniko commented 2 years ago
const serviceStatus = {
  // service ready
  ok: { status: 200, message: "ok lets go" },
  //  no data to operate on
  nodata: { status: 418, message: "no data to operate on" },
  // missing cfg
  noconfig: { status: 401, message: "missig config" },
  // server offline
  noserver: { status: 500, message: "cannot reach server" }
} as const;

type StatusType = keyof typeof serviceStatus;
type ServiceStatus = typeof serviceStatus[StatusType];

const status = (dataNodes: Types.Data.Data[], config: Types.Config.PartialConfig): Observable<ServiceStatus> => {
  log.debug(`creating status observable`);

  if (!config.youtube_downloader?.api_url) return of(serviceStatus.noconfig);