Skip to content

Commit a2d0e25

Browse files
committed
Make client base path behave as expected
1 parent 4ebcb60 commit a2d0e25

File tree

6 files changed

+26
-58
lines changed

6 files changed

+26
-58
lines changed

packages/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stl-api/client",
3-
"version": "0.0.1-alpha.40",
3+
"version": "0.0.1-alpha.41",
44
"description": "primary client package for stl-api",
55
"author": "[email protected]",
66
"license": "ISC",

packages/client/src/codegen/generate-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,13 @@ function makeTypes(
319319

320320
export async function generateOutput<API extends APIConfig>(
321321
api: API,
322-
config: ClientConfig<API["basePath"]>,
322+
config: ClientConfig,
323323
installLocation: string = "@stl-api/client",
324324
reactQueryAlias: string = "@tanstack/react-query"
325325
) {
326326
const resources = getResources(api.resources);
327327
const endpoints = getEndpoints(resources);
328-
const apiMap = nestEndpoints(endpoints, config.basePath);
328+
const apiMap = nestEndpoints(endpoints, api.basePath);
329329
const output = makeTypes(
330330
apiMap,
331331
api,

packages/client/src/core/api-client.test.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import { makeClientWithInferredTypes } from "./api-client";
33
import { Client } from "./api-client-types";
44
import * as MockAPI from "../test-util/api-server";
5-
import { complicatedBasePath } from "../test-util/long-base-path-api";
65

76
describe("API Client", () => {
87
describe("configuration options", () => {
9-
let client: Client<MockAPI.API, MockAPI.Config>;
8+
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
109
let mockFetch: typeof fetch;
1110

1211
beforeEach(() => {
@@ -16,7 +15,9 @@ describe("API Client", () => {
1615
basePath: "/api" as const,
1716
urlCase: "camel",
1817
};
19-
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
18+
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
19+
config
20+
);
2021
});
2122

2223
afterEach(() => {
@@ -33,22 +34,24 @@ describe("API Client", () => {
3334
});
3435

3536
describe("long base paths", () => {
36-
let client: Client<
37-
MockAPI.APIWithCustomBasePathAPI,
38-
MockAPI.APIWithCustomBasePathConfig
39-
>;
37+
const longBasePath = "/api/camelCase/kebab-case/v2" as const;
38+
type LongBasePath = typeof longBasePath;
39+
type ClientConfigWithLongBasePath = {
40+
basePath: LongBasePath;
41+
};
42+
let client: Client<MockAPI.API, ClientConfigWithLongBasePath>;
4043
let mockFetch: typeof fetch;
4144

4245
beforeEach(() => {
4346
mockFetch = vi.fn(MockAPI.mockFetchImplementation);
4447
const config = {
4548
fetch: mockFetch,
46-
basePath: complicatedBasePath,
49+
basePath: longBasePath,
4750
urlCase: "camel",
4851
};
4952
client = makeClientWithInferredTypes<
50-
MockAPI.APIWithCustomBasePathAPI,
51-
MockAPI.APIWithCustomBasePathConfig
53+
MockAPI.API,
54+
ClientConfigWithLongBasePath
5255
>(config);
5356
});
5457

@@ -69,13 +72,15 @@ describe("API Client", () => {
6972
});
7073

7174
describe("fetch calls", () => {
72-
let client: Client<MockAPI.API, MockAPI.Config>;
75+
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
7376
let mockFetch: typeof fetch;
7477

7578
beforeEach(() => {
7679
mockFetch = vi.fn(MockAPI.mockFetchImplementation);
7780
const config = { fetch: mockFetch, basePath: "/api" as const };
78-
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
81+
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
82+
config
83+
);
7984
});
8085

8186
afterEach(() => {
@@ -138,15 +143,17 @@ describe("API Client", () => {
138143
});
139144

140145
describe("`useMethod` style call", () => {
141-
let client: Client<MockAPI.API, MockAPI.Config>;
146+
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
142147
let mockFetch: typeof fetch;
143148

144149
beforeEach(() => {
145150
mockFetch = vi
146151
.fn(fetch)
147152
.mockImplementation(MockAPI.mockFetchImplementation);
148153
const config = { fetch: mockFetch, basePath: "/api" as const };
149-
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
154+
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
155+
config
156+
);
150157
});
151158

152159
afterEach(() => {

packages/client/src/core/api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function createClientProxy(
226226
export function makeClientWithInferredTypes<
227227
API extends APIConfig,
228228
/** Unfortunately this cannot be infered from the parameter since the API generic needs to be specified */
229-
Config extends ClientConfig<API["basePath"]>
229+
Config extends ClientConfig
230230
>(config: Config): Client<API, Config> {
231231
return createClientProxy(config, [config.basePath]) as Client<API, Config>;
232232
}

packages/client/src/test-util/api-server.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Stl } from "stainless";
22
import { cats } from "../test-util/cat-api";
33
import { dogs } from "../test-util/dog-api";
4-
import * as LongBasePath from "../test-util/long-base-path-api";
54
import { users } from "../test-util/user-api";
65
import { dogTreats } from "../test-util/dog-treat-api";
76

@@ -75,17 +74,7 @@ export const nestedApi = stl.api({
7574
},
7675
},
7776
});
78-
export const customBasePathApi = stl.api({
79-
basePath: LongBasePath.complicatedBasePath,
80-
resources: {
81-
dogs: LongBasePath.dogs,
82-
},
83-
});
8477

85-
export type APIWithCustomBasePathAPI = typeof customBasePathApi;
86-
export type APIWithCustomBasePathConfig = {
87-
basePath: APIWithCustomBasePathAPI["basePath"];
88-
};
8978
export type API = typeof api;
9079
export const config = { basePath: "/api" } as const;
91-
export type Config = typeof config;
80+
export type ClientConfig = typeof config;

packages/client/src/test-util/long-base-path-api.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)