Skip to content

Commit

Permalink
Merge pull request #82 from stainless-api/mcasey/fix-client-base-path
Browse files Browse the repository at this point in the history
Correct issues with how the client base path interacted with the API config base path
  • Loading branch information
matt-casey authored Sep 25, 2024
2 parents 4ebcb60 + a2d0e25 commit a8209b7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stl-api/client",
"version": "0.0.1-alpha.40",
"version": "0.0.1-alpha.41",
"description": "primary client package for stl-api",
"author": "[email protected]",
"license": "ISC",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/codegen/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,13 @@ function makeTypes(

export async function generateOutput<API extends APIConfig>(
api: API,
config: ClientConfig<API["basePath"]>,
config: ClientConfig,
installLocation: string = "@stl-api/client",
reactQueryAlias: string = "@tanstack/react-query"
) {
const resources = getResources(api.resources);
const endpoints = getEndpoints(resources);
const apiMap = nestEndpoints(endpoints, config.basePath);
const apiMap = nestEndpoints(endpoints, api.basePath);
const output = makeTypes(
apiMap,
api,
Expand Down
35 changes: 21 additions & 14 deletions packages/client/src/core/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { makeClientWithInferredTypes } from "./api-client";
import { Client } from "./api-client-types";
import * as MockAPI from "../test-util/api-server";
import { complicatedBasePath } from "../test-util/long-base-path-api";

describe("API Client", () => {
describe("configuration options", () => {
let client: Client<MockAPI.API, MockAPI.Config>;
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
let mockFetch: typeof fetch;

beforeEach(() => {
Expand All @@ -16,7 +15,9 @@ describe("API Client", () => {
basePath: "/api" as const,
urlCase: "camel",
};
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
config
);
});

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

describe("long base paths", () => {
let client: Client<
MockAPI.APIWithCustomBasePathAPI,
MockAPI.APIWithCustomBasePathConfig
>;
const longBasePath = "/api/camelCase/kebab-case/v2" as const;
type LongBasePath = typeof longBasePath;
type ClientConfigWithLongBasePath = {
basePath: LongBasePath;
};
let client: Client<MockAPI.API, ClientConfigWithLongBasePath>;
let mockFetch: typeof fetch;

beforeEach(() => {
mockFetch = vi.fn(MockAPI.mockFetchImplementation);
const config = {
fetch: mockFetch,
basePath: complicatedBasePath,
basePath: longBasePath,
urlCase: "camel",
};
client = makeClientWithInferredTypes<
MockAPI.APIWithCustomBasePathAPI,
MockAPI.APIWithCustomBasePathConfig
MockAPI.API,
ClientConfigWithLongBasePath
>(config);
});

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

describe("fetch calls", () => {
let client: Client<MockAPI.API, MockAPI.Config>;
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
let mockFetch: typeof fetch;

beforeEach(() => {
mockFetch = vi.fn(MockAPI.mockFetchImplementation);
const config = { fetch: mockFetch, basePath: "/api" as const };
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
config
);
});

afterEach(() => {
Expand Down Expand Up @@ -138,15 +143,17 @@ describe("API Client", () => {
});

describe("`useMethod` style call", () => {
let client: Client<MockAPI.API, MockAPI.Config>;
let client: Client<MockAPI.API, MockAPI.ClientConfig>;
let mockFetch: typeof fetch;

beforeEach(() => {
mockFetch = vi
.fn(fetch)
.mockImplementation(MockAPI.mockFetchImplementation);
const config = { fetch: mockFetch, basePath: "/api" as const };
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.Config>(config);
client = makeClientWithInferredTypes<MockAPI.API, MockAPI.ClientConfig>(
config
);
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/core/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function createClientProxy(
export function makeClientWithInferredTypes<
API extends APIConfig,
/** Unfortunately this cannot be infered from the parameter since the API generic needs to be specified */
Config extends ClientConfig<API["basePath"]>
Config extends ClientConfig
>(config: Config): Client<API, Config> {
return createClientProxy(config, [config.basePath]) as Client<API, Config>;
}
Expand Down
13 changes: 1 addition & 12 deletions packages/client/src/test-util/api-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Stl } from "stainless";
import { cats } from "../test-util/cat-api";
import { dogs } from "../test-util/dog-api";
import * as LongBasePath from "../test-util/long-base-path-api";
import { users } from "../test-util/user-api";
import { dogTreats } from "../test-util/dog-treat-api";

Expand Down Expand Up @@ -75,17 +74,7 @@ export const nestedApi = stl.api({
},
},
});
export const customBasePathApi = stl.api({
basePath: LongBasePath.complicatedBasePath,
resources: {
dogs: LongBasePath.dogs,
},
});

export type APIWithCustomBasePathAPI = typeof customBasePathApi;
export type APIWithCustomBasePathConfig = {
basePath: APIWithCustomBasePathAPI["basePath"];
};
export type API = typeof api;
export const config = { basePath: "/api" } as const;
export type Config = typeof config;
export type ClientConfig = typeof config;
28 changes: 0 additions & 28 deletions packages/client/src/test-util/long-base-path-api.ts

This file was deleted.

0 comments on commit a8209b7

Please sign in to comment.