@perses-dev/core 0.51.0-rc.1 → 0.51.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ async function fetch(...args) {
41
41
  if (response.ok === false) {
42
42
  const contentType = response.headers.get('content-type');
43
43
  if (contentType?.includes('application/json')) {
44
- const json = await response.json();
44
+ const json = await response.clone().json();
45
45
  if (json.error) {
46
46
  throw new UserFriendlyError(json.error, response.status);
47
47
  }
@@ -49,7 +49,7 @@ async function fetch(...args) {
49
49
  throw new UserFriendlyError(json.message, response.status);
50
50
  }
51
51
  }
52
- const text = await response.text();
52
+ const text = await response.clone().text();
53
53
  if (text) {
54
54
  throw new UserFriendlyError(text, response.status);
55
55
  }
@@ -59,8 +59,10 @@ async function fetch(...args) {
59
59
  }
60
60
  async function fetchJson(...args) {
61
61
  const response = await fetch(...args);
62
- const json = await response.json();
63
- return json;
62
+ if (!response.ok) {
63
+ throw new FetchError(response);
64
+ }
65
+ return await response.json();
64
66
  }
65
67
  class FetchError extends Error {
66
68
  status;
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,wBAAsB,KAAK,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBvF;AAED;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAIvF;AAED,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAM,YAAW,WAAW;IAC1D,MAAM,EAAE,MAAM,CAAC;gBACH,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC;CAKzC;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAM,YAAW,WAAW;IACjE,MAAM,EAAE,MAAM,CAAC;gBACH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAK5C"}
1
+ {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,wBAAsB,KAAK,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBvF;AAED;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAMvF;AAED,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAM,YAAW,WAAW;IAC1D,MAAM,EAAE,MAAM,CAAC;gBACH,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC;CAKzC;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAM,YAAW,WAAW;IACjE,MAAM,EAAE,MAAM,CAAC;gBACH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAK5C"}
@@ -17,7 +17,7 @@
17
17
  if (response.ok === false) {
18
18
  const contentType = response.headers.get('content-type');
19
19
  if (contentType?.includes('application/json')) {
20
- const json = await response.json();
20
+ const json = await response.clone().json();
21
21
  if (json.error) {
22
22
  throw new UserFriendlyError(json.error, response.status);
23
23
  }
@@ -25,7 +25,7 @@
25
25
  throw new UserFriendlyError(json.message, response.status);
26
26
  }
27
27
  }
28
- const text = await response.text();
28
+ const text = await response.clone().text();
29
29
  if (text) {
30
30
  throw new UserFriendlyError(text, response.status);
31
31
  }
@@ -39,8 +39,10 @@
39
39
  * decoded body.
40
40
  */ export async function fetchJson(...args) {
41
41
  const response = await fetch(...args);
42
- const json = await response.json();
43
- return json;
42
+ if (!response.ok) {
43
+ throw new FetchError(response);
44
+ }
45
+ return await response.json();
44
46
  }
45
47
  /**
46
48
  * Error thrown when fetch returns a non-200 response.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/fetch.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n/**\n * Calls `global.fetch` and determines which type of error to show for non-200 responses.\n */\nexport async function fetch(...args: Parameters<typeof global.fetch>): Promise<Response> {\n const response = await global.fetch(...args);\n if (response.ok === false) {\n const contentType = response.headers.get('content-type');\n if (contentType?.includes('application/json')) {\n const json = await response.json();\n if (json.error) {\n throw new UserFriendlyError(json.error, response.status);\n }\n if (json.message) {\n throw new UserFriendlyError(json.message, response.status);\n }\n }\n\n const text = await response.text();\n if (text) {\n throw new UserFriendlyError(text, response.status);\n }\n throw new FetchError(response);\n }\n return response;\n}\n\n/**\n * Calls `global.fetch` and throws a `FetchError` on non-200 responses, but also\n * decodes the response body as JSON, casting it to type `T`. Returns the\n * decoded body.\n */\nexport async function fetchJson<T>(...args: Parameters<typeof global.fetch>): Promise<T> {\n const response = await fetch(...args);\n const json: T = await response.json();\n return json;\n}\n\nexport interface StatusError extends Error {\n status: number;\n}\n\n/**\n * Error thrown when fetch returns a non-200 response.\n */\nexport class FetchError extends Error implements StatusError {\n status: number;\n constructor(response: Readonly<Response>) {\n super(`${response.status} ${response.statusText}`);\n this.status = response.status;\n Object.setPrototypeOf(this, FetchError.prototype);\n }\n}\n\n/**\n * General error type for an error that has a message that is OK to show to the end user.\n */\nexport class UserFriendlyError extends Error implements StatusError {\n status: number;\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n Object.setPrototypeOf(this, UserFriendlyError.prototype);\n }\n}\n"],"names":["fetch","args","response","global","ok","contentType","headers","get","includes","json","error","UserFriendlyError","status","message","text","FetchError","fetchJson","Error","constructor","statusText","Object","setPrototypeOf","prototype"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;CAEC,GACD,OAAO,eAAeA,MAAM,GAAGC,IAAqC;IAClE,MAAMC,WAAW,MAAMC,OAAOH,KAAK,IAAIC;IACvC,IAAIC,SAASE,EAAE,KAAK,OAAO;QACzB,MAAMC,cAAcH,SAASI,OAAO,CAACC,GAAG,CAAC;QACzC,IAAIF,aAAaG,SAAS,qBAAqB;YAC7C,MAAMC,OAAO,MAAMP,SAASO,IAAI;YAChC,IAAIA,KAAKC,KAAK,EAAE;gBACd,MAAM,IAAIC,kBAAkBF,KAAKC,KAAK,EAAER,SAASU,MAAM;YACzD;YACA,IAAIH,KAAKI,OAAO,EAAE;gBAChB,MAAM,IAAIF,kBAAkBF,KAAKI,OAAO,EAAEX,SAASU,MAAM;YAC3D;QACF;QAEA,MAAME,OAAO,MAAMZ,SAASY,IAAI;QAChC,IAAIA,MAAM;YACR,MAAM,IAAIH,kBAAkBG,MAAMZ,SAASU,MAAM;QACnD;QACA,MAAM,IAAIG,WAAWb;IACvB;IACA,OAAOA;AACT;AAEA;;;;CAIC,GACD,OAAO,eAAec,UAAa,GAAGf,IAAqC;IACzE,MAAMC,WAAW,MAAMF,SAASC;IAChC,MAAMQ,OAAU,MAAMP,SAASO,IAAI;IACnC,OAAOA;AACT;AAMA;;CAEC,GACD,OAAO,MAAMM,mBAAmBE;IAC9BL,OAAe;IACfM,YAAYhB,QAA4B,CAAE;QACxC,KAAK,CAAC,GAAGA,SAASU,MAAM,CAAC,CAAC,EAAEV,SAASiB,UAAU,EAAE;QACjD,IAAI,CAACP,MAAM,GAAGV,SAASU,MAAM;QAC7BQ,OAAOC,cAAc,CAAC,IAAI,EAAEN,WAAWO,SAAS;IAClD;AACF;AAEA;;CAEC,GACD,OAAO,MAAMX,0BAA0BM;IACrCL,OAAe;IACfM,YAAYL,OAAe,EAAED,MAAc,CAAE;QAC3C,KAAK,CAACC;QACN,IAAI,CAACD,MAAM,GAAGA;QACdQ,OAAOC,cAAc,CAAC,IAAI,EAAEV,kBAAkBW,SAAS;IACzD;AACF"}
1
+ {"version":3,"sources":["../../src/utils/fetch.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n/**\n * Calls `global.fetch` and determines which type of error to show for non-200 responses.\n */\nexport async function fetch(...args: Parameters<typeof global.fetch>): Promise<Response> {\n const response = await global.fetch(...args);\n if (response.ok === false) {\n const contentType = response.headers.get('content-type');\n if (contentType?.includes('application/json')) {\n const json = await response.clone().json();\n if (json.error) {\n throw new UserFriendlyError(json.error, response.status);\n }\n if (json.message) {\n throw new UserFriendlyError(json.message, response.status);\n }\n }\n\n const text = await response.clone().text();\n if (text) {\n throw new UserFriendlyError(text, response.status);\n }\n throw new FetchError(response);\n }\n return response;\n}\n\n/**\n * Calls `global.fetch` and throws a `FetchError` on non-200 responses, but also\n * decodes the response body as JSON, casting it to type `T`. Returns the\n * decoded body.\n */\nexport async function fetchJson<T>(...args: Parameters<typeof global.fetch>): Promise<T> {\n const response = await fetch(...args);\n if (!response.ok) {\n throw new FetchError(response);\n }\n return await response.json();\n}\n\nexport interface StatusError extends Error {\n status: number;\n}\n\n/**\n * Error thrown when fetch returns a non-200 response.\n */\nexport class FetchError extends Error implements StatusError {\n status: number;\n constructor(response: Readonly<Response>) {\n super(`${response.status} ${response.statusText}`);\n this.status = response.status;\n Object.setPrototypeOf(this, FetchError.prototype);\n }\n}\n\n/**\n * General error type for an error that has a message that is OK to show to the end user.\n */\nexport class UserFriendlyError extends Error implements StatusError {\n status: number;\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n Object.setPrototypeOf(this, UserFriendlyError.prototype);\n }\n}\n"],"names":["fetch","args","response","global","ok","contentType","headers","get","includes","json","clone","error","UserFriendlyError","status","message","text","FetchError","fetchJson","Error","constructor","statusText","Object","setPrototypeOf","prototype"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;CAEC,GACD,OAAO,eAAeA,MAAM,GAAGC,IAAqC;IAClE,MAAMC,WAAW,MAAMC,OAAOH,KAAK,IAAIC;IACvC,IAAIC,SAASE,EAAE,KAAK,OAAO;QACzB,MAAMC,cAAcH,SAASI,OAAO,CAACC,GAAG,CAAC;QACzC,IAAIF,aAAaG,SAAS,qBAAqB;YAC7C,MAAMC,OAAO,MAAMP,SAASQ,KAAK,GAAGD,IAAI;YACxC,IAAIA,KAAKE,KAAK,EAAE;gBACd,MAAM,IAAIC,kBAAkBH,KAAKE,KAAK,EAAET,SAASW,MAAM;YACzD;YACA,IAAIJ,KAAKK,OAAO,EAAE;gBAChB,MAAM,IAAIF,kBAAkBH,KAAKK,OAAO,EAAEZ,SAASW,MAAM;YAC3D;QACF;QAEA,MAAME,OAAO,MAAMb,SAASQ,KAAK,GAAGK,IAAI;QACxC,IAAIA,MAAM;YACR,MAAM,IAAIH,kBAAkBG,MAAMb,SAASW,MAAM;QACnD;QACA,MAAM,IAAIG,WAAWd;IACvB;IACA,OAAOA;AACT;AAEA;;;;CAIC,GACD,OAAO,eAAee,UAAa,GAAGhB,IAAqC;IACzE,MAAMC,WAAW,MAAMF,SAASC;IAChC,IAAI,CAACC,SAASE,EAAE,EAAE;QAChB,MAAM,IAAIY,WAAWd;IACvB;IACA,OAAO,MAAMA,SAASO,IAAI;AAC5B;AAMA;;CAEC,GACD,OAAO,MAAMO,mBAAmBE;IAC9BL,OAAe;IACfM,YAAYjB,QAA4B,CAAE;QACxC,KAAK,CAAC,GAAGA,SAASW,MAAM,CAAC,CAAC,EAAEX,SAASkB,UAAU,EAAE;QACjD,IAAI,CAACP,MAAM,GAAGX,SAASW,MAAM;QAC7BQ,OAAOC,cAAc,CAAC,IAAI,EAAEN,WAAWO,SAAS;IAClD;AACF;AAEA;;CAEC,GACD,OAAO,MAAMX,0BAA0BM;IACrCL,OAAe;IACfM,YAAYL,OAAe,EAAED,MAAc,CAAE;QAC3C,KAAK,CAACC;QACN,IAAI,CAACD,MAAM,GAAGA;QACdQ,OAAOC,cAAc,CAAC,IAAI,EAAEV,kBAAkBW,SAAS;IACzD;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perses-dev/core",
3
- "version": "0.51.0-rc.1",
3
+ "version": "0.51.1",
4
4
  "description": "Core functionality consumed by both the Perses UI and plugins",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/perses/perses/blob/main/README.md",