Skip to content

Commit c6951ac

Browse files
committed
log-client: update to new log-api
1 parent 7986b6d commit c6951ac

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

packages/log-client/__mocks__/mock-server.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { type paths } from '@lightmill/log-api';
32
import {
43
http,
54
HttpResponse,
@@ -10,6 +9,7 @@ import {
109
import { setupServer, SetupServerApi } from 'msw/node';
1110
import type { IsNever, RequiredKeysOf } from 'type-fest';
1211
import { test, vi, type Mock } from 'vitest';
12+
import { type paths } from '../src/generated/openapi.js';
1313
import { apiMediaType } from '../src/utils.js';
1414

1515
export type ApiMediaType = typeof apiMediaType;
@@ -32,14 +32,18 @@ type PathMethod<Path extends keyof paths> = Extract<
3232
type ApiRequestBody<
3333
Path extends keyof paths,
3434
Method extends PathMethod<Path>,
35-
> = paths extends {
36-
[P in Path]: {
37-
[M in Method]: {
38-
requestBody: { content: { [K in ApiMediaType]: infer R } };
39-
};
40-
};
41-
}
42-
? R
35+
> = paths extends { [P in Path]: { [M in Method]: infer Route } }
36+
? Route extends {
37+
requestBody: { content: { [K in ApiMediaType]: infer Content } };
38+
}
39+
? Content
40+
: Route extends { requestBody?: never }
41+
? undefined
42+
: Route extends {
43+
requestBody?: { content: { [K in ApiMediaType]: infer Content } };
44+
}
45+
? Content | undefined
46+
: never
4347
: never;
4448

4549
type ApiRequestPathParams<

packages/log-client/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"cross-env": "^7.0.3",
2626
"msw": "^2.7.3",
2727
"onchange": "^7.1.0",
28+
"openapi-typescript": "^7.8.0",
2829
"type-fest": "^4.39.1",
2930
"typescript": "catalog:",
3031
"vitest": "catalog:"
@@ -34,7 +35,9 @@
3435
},
3536
"scripts": {
3637
"test": "cross-env NODE_ENV=test vitest",
37-
"build": "tsc -b tsconfig.build.json",
38+
"generate-openapi-types": "openapi-typescript node_modules/@lightmill/log-api/dist/openapi.yaml --output src/generated/openapi.ts",
39+
"build-ts": "tsc -b tsconfig.build.json",
40+
"build": "pnpm run generate-openapi-types && pnpm run build-ts",
3841
"prepublish": "pnpm run build"
3942
},
4043
"keywords": [

packages/log-client/src/client.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { components, paths } from '@lightmill/log-api';
21
import createClient from 'openapi-fetch';
2+
import type { components, paths } from './generated/openapi.js';
33
import { anyLogSerializer } from './log-serializer.js';
44
import { LightmillLogger } from './logger.js';
55
import type {
@@ -207,13 +207,12 @@ export class LightmillClient<ClientLog extends LogBase = AnyLog> {
207207
let response = await this.#fetchClient.GET('/experiments', {
208208
params: { query: { 'filter[name]': experimentName } },
209209
});
210-
if (response.data != null) return response.data.data[0];
211-
if (response.response.status === 404) return null;
212-
let error = response.error.errors[0];
213-
throw new Error(
214-
error.detail ??
215-
`Could not fetch experiment: server returned ${error.code}`,
216-
);
210+
// Checking the length isn't strictly necessary, but it makes the
211+
// intention clearer, and let typescript know that it may return null.
212+
if (response.data != null) {
213+
return response.data.data.length > 0 ? response.data.data[0] : null;
214+
}
215+
throw new RequestError(response);
217216
}
218217

219218
async #getRunFromName(
@@ -231,12 +230,12 @@ export class LightmillClient<ClientLog extends LogBase = AnyLog> {
231230
},
232231
},
233232
});
234-
if (response.data != null) return response.data.data[0];
235-
if (response.response.status === 404) return null;
236-
let error = response.error.errors[0];
237-
throw new Error(
238-
error.detail ?? `Could not fetch run: server returned ${error.code}`,
239-
);
233+
// Checking the length isn't strictly necessary, but it makes the
234+
// intention clearer, and let typescript know that it may return null.
235+
if (response.data != null) {
236+
return response.data.data.length > 0 ? response.data.data[0] : null;
237+
}
238+
throw new RequestError(response);
240239
}
241240

242241
async #getRunIdFromName(
@@ -355,5 +354,5 @@ export class LightmillClient<ClientLog extends LogBase = AnyLog> {
355354
}
356355
}
357356

358-
type ExperimentResource = components['schemas']['Experiment.Resource'];
359-
type LogResource = components['schemas']['Log.Resource'];
357+
type ExperimentResource = components['schemas']['ExperimentResource'];
358+
type LogResource = components['schemas']['LogResource'];

packages/log-client/src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { paths } from '@lightmill/log-api';
21
import type { Client as FetchClient } from 'openapi-fetch';
32
import type { JsonValue } from 'type-fest';
3+
import type { paths } from './generated/openapi.js';
44
import { Subject } from './subject.ts';
55
import type { LogValuesSerializer, RunStatus } from './types.js';
66
import { apiMediaType, RequestError } from './utils.js';

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)