Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js): Scaffold ts phoenix client #4847

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions js/packages/phoenix-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# @arizeai/phoenix-client

This package provides a client for the Phoenix API.

It utililizes [openapi-ts](https://openapi-ts.pages.dev/) to generate the types from the Phoenix OpenAPI spec.

## Installation

```bash
# or yarn, pnpm, bun, etc...
npm install @arizeai/phoenix-client
```

## Configuration

The client will automatically read environment variables from your environment, if available.

The following environment variables are used:

- `PHOENIX_HOST` - The base URL of the Phoenix API.
- `PHOENIX_CLIENT_HEADERS` - Custom headers to add to all requests. A JSON stringified object.

```bash
PHOENIX_HOST=http://localhost:8000 PHOENIX_CLIENT_HEADERS='{"X-Custom-Header": "123"}' pnpx tsx examples/list_datasets.ts
# emits the following request:
# GET http://localhost:8000/v1/datasets
# headers: {
# "X-Custom-Header": "123",
# }
```

Alternatively, you can pass configuration options to the client directly, and they will be prioritized over environment variables and default values.

```ts
const phoenix = createClient({
options: {
baseUrl: "http://localhost:8000",
headers: {
"X-Custom-Header": "123",
},
},
});
```

## Examples

To run examples, install dependencies using `pnpm` and run:

```bash
pnpm install
pnpx tsx examples/list_datasets.ts
# change the file name to run other examples
```
22 changes: 22 additions & 0 deletions js/packages/phoenix-client/examples/list_datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createClient, Types } from "../src";

// baseUrl defaults to http://localhost:6006
const phoenix = createClient();

// Make GET request to /v1/datasets
// Available GET endpoints are available via auto-completion
// or by looking at Types["V1"]["paths"]
phoenix
.GET("/v1/datasets", { params: { query: { limit: 100 } } })
.then(({ data }) => data?.data ?? [])
.then(listDatasets);

// Extract Dataset type from OpenAPI spec schema
type Dataset = Types["V1"]["components"]["schemas"]["Dataset"];

// Process each dataset, using TypeScript types
function listDatasets(datasets: Dataset[]) {
datasets.forEach((dataset, index) => {
console.log(`${index + 1}. ${dataset.name} (${dataset.id})`);
});
}
38 changes: 38 additions & 0 deletions js/packages/phoenix-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@arizeai/phoenix-client",
"version": "0.0.0",
"description": "A client for the Phoenix API",
"main": "dist/src/index.js",
"module": "dist/esm/index.js",
"esnext": "dist/esnext/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist",
"src",
"package.json"
],
"scripts": {
"clean": "rimraf dist",
"prebuild": "pnpm run clean && pnpm run generate",
"generate": "openapi-typescript ../../../schemas/openapi.json -o ./src/__generated__/api/v1.d.ts",
"build": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"type:check": "tsc --noEmit",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ELv2",
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.14.11",
"jest": "^29.7.0",
"openapi-typescript": "^7.4.1",
"rimraf": "^5.0.9",
"tsx": "^4.19.1",
"typescript": "^5.5.4"
},
"dependencies": {
"openapi-fetch": "^0.12.2",
"zod": "^3.23.8"
}
}
Loading
Loading