@pitcher/js-api 1.21.0-beta.4 → 1.21.0-beta.6

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.
package/js-api.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { camelCase, snakeCase, cloneDeep } from 'lodash-es';
1
+ import { camelCase, snakeCase, pickBy, cloneDeep, isEqual } from 'lodash-es';
2
2
 
3
3
  function _mergeNamespaces(n, m) {
4
4
  for (var i = 0; i < m.length; i++) {
@@ -2791,13 +2791,23 @@ const MAX_RETRIES = 5;
2791
2791
  const BASE_DELAY = 1e3;
2792
2792
  function paramsSerializer(params) {
2793
2793
  const searchParams = new URLSearchParams();
2794
- for (const key in params) {
2795
- if (Array.isArray(params[key])) {
2796
- params[key].forEach((val) => searchParams.append(key, val));
2794
+ function addParam(key, value) {
2795
+ if (value === void 0 || value === null) {
2796
+ return;
2797
+ }
2798
+ if (Array.isArray(value)) {
2799
+ value.forEach((val) => addParam(key, val));
2800
+ } else if (typeof value === "object" && !Array.isArray(value)) {
2801
+ for (const subKey in value) {
2802
+ addParam(`${key}[${subKey}]`, value[subKey]);
2803
+ }
2797
2804
  } else {
2798
- searchParams.append(key, params[key]);
2805
+ searchParams.append(key, value);
2799
2806
  }
2800
2807
  }
2808
+ for (const key in params) {
2809
+ addParam(key, params[key]);
2810
+ }
2801
2811
  return searchParams.toString();
2802
2812
  }
2803
2813
  async function fetchWithRetry(url, options, remainingRetries = MAX_RETRIES) {
@@ -2905,13 +2915,29 @@ function createHttpClient(origin, token) {
2905
2915
  };
2906
2916
  }
2907
2917
 
2918
+ function normalizeNetworkFilterParams(params) {
2919
+ return Object.entries(params).reduce(
2920
+ (acc, [key, value]) => {
2921
+ if (key.endsWith("__range") && Array.isArray(value)) {
2922
+ acc[key] = value.join(",");
2923
+ } else {
2924
+ acc[key] = value;
2925
+ }
2926
+ return acc;
2927
+ },
2928
+ {}
2929
+ );
2930
+ }
2931
+
2908
2932
  let env;
2909
2933
  let http;
2910
- const logInfo = (msg, ...rest) => console.info(`[online handlers]: ${msg}`, ...rest);
2911
- const check = () => {
2934
+ function check() {
2912
2935
  if (!env) throw new Error("Missing env");
2913
2936
  if (!http) throw new Error("Missing http");
2914
- };
2937
+ }
2938
+ function pickNonNullish(data) {
2939
+ return pickBy(data ?? {}, (v) => v !== void 0 && v !== null);
2940
+ }
2915
2941
  const setEnv = (e) => {
2916
2942
  env = e;
2917
2943
  http = createHttpClient(
@@ -2921,40 +2947,40 @@ const setEnv = (e) => {
2921
2947
  };
2922
2948
  const onlineRestApiHandlers = {
2923
2949
  get_canvases: async (params) => {
2924
- logInfo("online handler get_canvases called with:", params);
2925
2950
  check();
2926
- const { api_base_url, ...restParams } = params;
2927
- const res = await http.get("/canvases", { params: { ...restParams, instance_id: env.pitcher.instance.id } });
2951
+ const { api_base_url, filters = {}, ...restParams } = params;
2952
+ const res = await http.get("/canvases", {
2953
+ params: {
2954
+ instance_id: env.pitcher.instance.id,
2955
+ ...normalizeNetworkFilterParams(filters),
2956
+ ...pickNonNullish(restParams)
2957
+ }
2958
+ });
2928
2959
  return res;
2929
2960
  },
2930
2961
  get_canvas: async (params) => {
2931
- logInfo("online handler get_canvas called with:", params);
2932
2962
  check();
2933
2963
  const { id, ...restParams } = params;
2934
2964
  const res = await http.get(`/canvases/${params.id}`, { params: restParams });
2935
2965
  return res;
2936
2966
  },
2937
2967
  create_canvas: async (params) => {
2938
- logInfo("online handler create_canvas called with:", params);
2939
2968
  check();
2940
2969
  const res = await http.post("/canvases", { ...params, instance_id: env.pitcher.instance.id });
2941
2970
  return res;
2942
2971
  },
2943
2972
  update_canvas: async (params) => {
2944
- logInfo("online handler update_canvas called with:", params);
2945
2973
  check();
2946
2974
  const { id, ...restParams } = params;
2947
2975
  const res = await http.patch(`/canvases/${params.id}`, restParams);
2948
2976
  return res;
2949
2977
  },
2950
2978
  delete_canvas: async (params) => {
2951
- logInfo("online handler delete_canvas called with:", params);
2952
2979
  check();
2953
2980
  const res = await http.delete(`/canvases/${params.id}`);
2954
2981
  return res;
2955
2982
  },
2956
2983
  share_canvas: async (params) => {
2957
- logInfo("online handler share_canvas called with:", params);
2958
2984
  check();
2959
2985
  let link;
2960
2986
  try {
@@ -3400,8 +3426,12 @@ const dsr = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
3400
3426
  }, Symbol.toStringTag, { value: 'Module' }));
3401
3427
 
3402
3428
  let highLevelApi;
3429
+ let prevOptions;
3403
3430
  function usePitcherApi(options) {
3404
- if (!highLevelApi) highLevelApi = createHighLevelApi(options);
3431
+ if (!highLevelApi || !isEqual(options, prevOptions)) {
3432
+ highLevelApi = createHighLevelApi(options);
3433
+ prevOptions = options;
3434
+ }
3405
3435
  return highLevelApi;
3406
3436
  }
3407
3437
  const uiApi = Object.entries(
@@ -3444,7 +3474,7 @@ function getAvailableApis() {
3444
3474
  const location = window.parent.jsApiLocation;
3445
3475
  return location === "ui" ? ["ui", "impact"] : [location ?? "impact"];
3446
3476
  }
3447
- function useApi() {
3477
+ function useApi(options) {
3448
3478
  if (typeof window === "undefined") throw new Error("useApi: window is not defined");
3449
3479
  const location = window.parent.jsApiLocation;
3450
3480
  switch (location) {
@@ -3455,7 +3485,7 @@ function useApi() {
3455
3485
  case "dsr":
3456
3486
  return useDsr();
3457
3487
  case "impact":
3458
- return usePitcherApi();
3488
+ return usePitcherApi(options);
3459
3489
  default:
3460
3490
  throw new Error(`Unknown api for location: ${location}`);
3461
3491
  }