Browse Source

fix(apps/hermes/client): use camelCase for streaming params (#1679)

* fix(apps/hermes/client): use camelCase for streaming params

* remove console log

* refactor

* use hex as example
Daniel Chew 1 year ago
parent
commit
2d3e4c6893

+ 1 - 1
apps/hermes/client/js/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@pythnetwork/hermes-client",
-  "version": "1.0.0",
+  "version": "1.0.1",
   "description": "Pyth Hermes Client",
   "author": {
     "name": "Pyth Data Association"

+ 5 - 3
apps/hermes/client/js/src/HermesClient.ts

@@ -1,6 +1,7 @@
 import EventSource from "eventsource";
 import { schemas } from "./zodSchemas";
 import { z } from "zod";
+import { camelToSnakeCaseObject } from "./utils";
 
 // Accessing schema objects
 export type AssetType = z.infer<typeof schemas.AssetType>;
@@ -193,8 +194,8 @@ export class HermesClient {
     options?: {
       encoding?: EncodingType;
       parsed?: boolean;
-      allow_unordered?: boolean;
-      benchmarks_only?: boolean;
+      allowUnordered?: boolean;
+      benchmarksOnly?: boolean;
     }
   ): Promise<EventSource> {
     const url = new URL("/v2/updates/price/stream", this.baseURL);
@@ -203,7 +204,8 @@ export class HermesClient {
     });
 
     if (options) {
-      this.appendUrlSearchParams(url, options);
+      const transformedOptions = camelToSnakeCaseObject(options);
+      this.appendUrlSearchParams(url, transformedOptions);
     }
 
     return new EventSource(url.toString());

+ 6 - 1
apps/hermes/client/js/src/examples/HermesClient.ts

@@ -45,7 +45,12 @@ async function run() {
   console.log(priceUpdates);
 
   // Streaming price updates
-  const eventSource = await connection.getPriceUpdatesStream(priceIds);
+  const eventSource = await connection.getPriceUpdatesStream(priceIds, {
+    encoding: "hex",
+    parsed: true,
+    allowUnordered: true,
+    benchmarksOnly: true,
+  });
 
   eventSource.onmessage = (event) => {
     console.log("Received price update:", event.data);

+ 13 - 0
apps/hermes/client/js/src/utils.ts

@@ -0,0 +1,13 @@
+function camelToSnakeCase(str: string): string {
+  return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
+}
+
+export function camelToSnakeCaseObject(
+  obj: Record<string, string | boolean>
+): Record<string, string | boolean> {
+  const result: Record<string, string | boolean> = {};
+  Object.keys(obj).forEach((key) => {
+    result[camelToSnakeCase(key)] = obj[key];
+  });
+  return result;
+}

+ 2 - 2
package-lock.json

@@ -48,7 +48,7 @@
     },
     "apps/hermes/client/js": {
       "name": "@pythnetwork/hermes-client",
-      "version": "1.0.0",
+      "version": "1.0.1",
       "license": "Apache-2.0",
       "dependencies": {
         "eventsource": "^2.0.2",
@@ -54903,7 +54903,7 @@
     },
     "target_chains/ethereum/sdk/js": {
       "name": "@pythnetwork/pyth-evm-js",
-      "version": "1.51.0",
+      "version": "1.52.0",
       "license": "Apache-2.0",
       "dependencies": {
         "@pythnetwork/price-service-client": "*",