@arizeai/phoenix-client
Version:
A client for the Phoenix API
65 lines • 2.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClient = exports.getMergedOptions = void 0;
const config_1 = require("./config");
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
/**
* Merge all configuration options according to priority:
* defaults < environment < explicit options
*
* Headers are simply replaced, not merged.
*
* You can call this function before instantiating the client if you need to retain access
* to the options that were passed in to the client.
*/
const getMergedOptions = ({ options = {}, getEnvironmentOptions = config_1.defaultGetEnvironmentOptions, } = {}) => {
const defaultOptions = (0, config_1.makeDefaultClientOptions)();
const environmentOptions = getEnvironmentOptions();
return Object.assign(Object.assign(Object.assign({}, defaultOptions), environmentOptions), options);
};
exports.getMergedOptions = getMergedOptions;
/**
* Middleware to take non-successful API calls throw instead of being swallowed
*/
const middleware = {
onResponse({ response }) {
if (!response.ok) {
// Will produce error messages like "https://example.org/api/v1/example: 404 Not Found".
throw new Error(`${response.url}: ${response.status} ${response.statusText}`);
}
},
};
/**
* Create a Phoenix client.
*
* The client is strongly typed and uses generated openapi types.
*
* @example
* ```ts
* import { createClient } from "@arize/phoenix-client";
*
* const client = createClient();
*
* const response = await client.GET("/v1/traces");
* // ^ path string is strongly typed, and completion works with autocomplete
* // path parameters, query parameters, and request body are also strongly typed based on the openapi spec,
* // the path, and the method.
* ```
*
* @param config - The configuration to use for the client.
* @param config.options - The options to use for [openapi-fetch.createOpenApiClient](https://github.com/openapi-ts/openapi-typescript/tree/main/packages/openapi-fetch).
* @param config.getEnvironmentOptions - The function to use to get the environment options. By default, a function that
* returns `process.env` is used.
* @returns The Phoenix client as a strongly typed [openapi-fetch](https://github.com/openapi-ts/openapi-typescript/tree/main/packages/openapi-fetch) client.
*/
const createClient = (config = {}) => {
const mergedOptions = (0, exports.getMergedOptions)(config);
const openApiClient = (0, openapi_fetch_1.default)(mergedOptions);
openApiClient.use(middleware);
return Object.assign(Object.assign({}, openApiClient), { config: mergedOptions });
};
exports.createClient = createClient;
//# sourceMappingURL=client.js.map