{"version":3,"sources":["../src/response.ts"],"sourcesContent":["/* eslint-disable max-classes-per-file */\r\ninterface RawRemoteError {\r\n  /**\r\n   * code > 0 才是合法的 Error;\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  code: number\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  name: string\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  message: string\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteError extends Error {\r\n  /**\r\n   * code > 0 才是合法的 Error;\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  code = 1\r\n\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  constructor(message: string) {\r\n    super(message)\r\n    this.name = 'RemoteError'\r\n  }\r\n\r\n  toJson(): RawRemoteError {\r\n    return {\r\n      code: this.code,\r\n      name: this.name,\r\n      message: this.message,\r\n    }\r\n  }\r\n\r\n  toString() {\r\n    return JSON.stringify(this.toJson())\r\n  }\r\n\r\n  valueOf() {\r\n    return `${this.name} [${this.code}]: ${this.message}`\r\n  }\r\n\r\n  static fromError(err: unknown) {\r\n    if (typeof err === 'string') {\r\n      return new RemoteError(err)\r\n    }\r\n    if (!err) {\r\n      return new RemoteError('Unknown error')\r\n    }\r\n    const json = err as Partial<RawRemoteError>\r\n    const error = new RemoteError(\r\n      typeof json.message === 'string' ? json.message : 'Unknown error'\r\n    )\r\n    if (typeof json.code === 'number' && json.code > 0) {\r\n      error.code = json.code\r\n    }\r\n    if (json.name && typeof json.name === 'string') {\r\n      error.name = json.name\r\n    }\r\n    return error\r\n  }\r\n\r\n  static isRemoteError(data: unknown): data is RawRemoteError {\r\n    const json = data as Partial<RawRemoteError>\r\n    return (\r\n      !!json &&\r\n      typeof json === 'object' &&\r\n      typeof json.code === 'number' &&\r\n      json.code > 0 &&\r\n      typeof json.name === 'string' &&\r\n      typeof json.message === 'string'\r\n    )\r\n  }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteTimeoutError extends RemoteError {\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  static code = 2\r\n\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  code = 2\r\n\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  constructor(message: string) {\r\n    super(message)\r\n    this.name = 'RemoteTimeoutError'\r\n  }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteNotFoundError extends RemoteError {\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  static code = 3\r\n\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  code = 3\r\n\r\n  /**\r\n   * name 和 message 都是 human readable 的字符串,\r\n   * 仅用于调试和展示,\r\n   * 程序逻辑判断请使用 code\r\n   */\r\n  constructor(message: string) {\r\n    super(message)\r\n    this.name = 'RemoteNotFoundError'\r\n  }\r\n}\r\n\r\nexport const response = {\r\n  success<T>(data: T) {\r\n    return {\r\n      code: 0,\r\n      data,\r\n    } as const\r\n  },\r\n\r\n  error(error: RemoteError) {\r\n    return error.toJson()\r\n  },\r\n}\r\n"],"mappings":"AA4BO,MAAMA,UAAoB,KAAM,CAOrC,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,aACd,CAEA,QAAyB,CACvB,MAAO,CACL,KAAM,KAAK,KACX,KAAM,KAAK,KACX,QAAS,KAAK,OAChB,CACF,CAEA,UAAW,CACT,OAAO,KAAK,UAAU,KAAK,OAAO,CAAC,CACrC,CAEA,SAAU,CACR,MAAO,GAAG,KAAK,IAAI,KAAK,KAAK,IAAI,MAAM,KAAK,OAAO,EACrD,CAEA,OAAO,UAAUC,EAAc,CAC7B,GAAI,OAAOA,GAAQ,SACjB,OAAO,IAAIF,EAAYE,CAAG,EAE5B,GAAI,CAACA,EACH,OAAO,IAAIF,EAAY,eAAe,EAExC,MAAMG,EAAOD,EACPE,EAAQ,IAAIJ,EAChB,OAAOG,EAAK,SAAY,SAAWA,EAAK,QAAU,eACpD,EACA,OAAI,OAAOA,EAAK,MAAS,UAAYA,EAAK,KAAO,IAC/CC,EAAM,KAAOD,EAAK,MAEhBA,EAAK,MAAQ,OAAOA,EAAK,MAAS,WACpCC,EAAM,KAAOD,EAAK,MAEbC,CACT,CAEA,OAAO,cAAcC,EAAuC,CAC1D,MAAMF,EAAOE,EACb,MACE,CAAC,CAACF,GACF,OAAOA,GAAS,UAChB,OAAOA,EAAK,MAAS,UACrBA,EAAK,KAAO,GACZ,OAAOA,EAAK,MAAS,UACrB,OAAOA,EAAK,SAAY,QAE5B,CACF,CAOO,MAAMG,UAA2BN,CAAY,CAMlD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,oBACd,CACF,CAOO,MAAMM,UAA4BP,CAAY,CAMnD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,qBACd,CACF,CAEO,MAAMO,EAAW,CACtB,QAAWH,EAAS,CAClB,MAAO,CACL,KAAM,EACN,KAAAA,CACF,CACF,EAEA,MAAMD,EAAoB,CACxB,OAAOA,EAAM,OAAO,CACtB,CACF","names":["RemoteError","message","err","json","error","data","RemoteTimeoutError","RemoteNotFoundError","response"]}