@cycleplatform/api-client-typescript 0.1.4 → 0.1.7

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/src/index.ts CHANGED
@@ -34,3 +34,28 @@ export const client = new Proxy(baseClient, {
34
34
  return newClient[key];
35
35
  },
36
36
  });
37
+
38
+ type ParamVal = string | number | Record<string, string | number> | string[];
39
+ type Params<T extends string, K extends ParamVal> = Partial<Record<T, K>>;
40
+
41
+ /** Converts any Cycle query parameters into a URL search string */
42
+ export function querySerializer<T extends string, K extends ParamVal>(
43
+ params: Params<T, K>
44
+ ): string {
45
+ const nv = Object.entries(params).reduce((acc, [k, v]) => {
46
+ if (typeof v === "string") {
47
+ acc[k] = v;
48
+ return acc;
49
+ }
50
+
51
+ if (v && typeof v === "object") {
52
+ Object.keys(v).forEach(
53
+ (sk) => (acc[`${k}[${sk}]`] = (v as Record<string, string>)[sk])
54
+ );
55
+ }
56
+
57
+ return acc;
58
+ }, {} as Record<string, string>);
59
+
60
+ return new URLSearchParams(nv).toString();
61
+ }