Skip to content

Added better types along with throwing errors - #1

Merged
TheBuilderJR merged 1 commit into
telemetry-sh:mainfrom
kalradivyanshu:main
Aug 21, 2024
Merged

TheBuilderJR merged 1 commit into
telemetry-sh:mainfrom
kalradivyanshu:main

Conversation

@kalradivyanshu

@kalradivyanshu kalradivyanshu commented Aug 21, 2024 •

Copy link
Copy Markdown
Contributor
const data = {
  city: "paris",
  price: 42,
  timestamp: new Date().toISOString()
};

async function logData() {
  try {
    const response = await telemetry.log("uber_rides", data);
    console.log("Log response:", response);
  } catch (error) {
    console.error("Error logging data:", error);
  }
}

logData();

This will actually not throw an error if the log failed, since fetch only throws error if network fails, so 500 error will get ignored.

To fix this added:

if (responseData.status === "error") throw new Error(responseData.message);

to both query and log.

Also added better typings for both query and log functions:

async query<T>(
    query: string,
    options: Record<string, any> = {},
  ): Promise<TelemetryQueryResponse<T>>

Where:

export type TelemetryQueryResponse<T> = {
  data: T[];
  status: "success";
  key_order: string[];
};

So now query can be used:

type uber_data = {
  city: string;
  price: number;
};

const response = await telemetry.query<uber_data>("SELECT city, price from uber_rides");

//response.data is of type uber_data[]

@TheBuilderJR

Copy link
Copy Markdown
Contributor

This is awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants