UNPKG

dwnpm

Version:

Decentralized Registry Package Manager (DRPM) helps developers publish, install, find and manage Decentralized Packages (DPKs) published to Decentralized Web Nodes (DWNs). DRPM does this by looking up a Decentralized Identifier (DID) to find its DID docum

34 lines (26 loc) 1.28 kB
import { DwnResponseStatus } from '@web5/agent'; import { ResponseInfo } from './types.js'; export class ResponseUtils { static is2xx = (code: number) => code >= 200 && code <= 299; static is3xx = (code: number) => code >= 300 && code <= 399; static OK = (ok: boolean) => ok === true; static notOK = (ok: boolean) => !this.OK(ok); static codeSuccess = (statusCode: number) => this.is2xx(statusCode) || this.is3xx(statusCode); static codeFail = (statusCode: number) => !this.codeSuccess(statusCode); static statusOk = (status: string) => status === 'OK'; static statusNotOk = (status: string) => !this.statusOk(status); static dwnSuccess = ({status: {code, detail}}: DwnResponseStatus) => this.codeSuccess(code) || this.statusNotOk(detail); static dwnFail = (status: DwnResponseStatus) => !this.dwnSuccess(status); static success = ({ok, code, status}: ResponseInfo) => this.OK(ok) || this.codeSuccess(code) || this.statusOk(status); static fail = (info: ResponseInfo) => !this.success(info); static fetchSuccess = (response: Response) => this.OK(response.ok) || this.codeSuccess(response.status) || this.statusOk(response.statusText); static fetchFail = (response: Response) => !this.fetchSuccess(response); }