Skip to content

Commit

Permalink
Merge pull request #79 from stainless-api/mcasey/ignore-base-path-case
Browse files Browse the repository at this point in the history
Do not format base paths
  • Loading branch information
matt-casey authored Sep 24, 2024
2 parents 33871c4 + b2627ff commit 8131b65
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"pkg-up": "~3.1.0",
"resolve": "^1.22.8",
"ts-morph": "^19.0.0",
"ts-to-zod": "github:stainless-api/stl-api#ts-to-zod-0.0.5"
"ts-to-zod": "github:stainless-api/stl-api#ts-to-zod-0.0.4"
},
"devDependencies": {
"@swc/core": "^1.3.100",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dedent-js": "^1.0.1",
"lodash": "^4.17.21",
"prettier": "^3.3.3",
"stainless": "github:stainless-api/stl-api#stainless-0.1.3",
"stainless": "github:stainless-api/stl-api#stainless-0.1.4",
"typescript": "^5.3.2",
"zod-to-ts": "^1.2.0"
},
Expand Down
37 changes: 37 additions & 0 deletions packages/client/src/core/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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", () => {
Expand Down Expand Up @@ -31,6 +32,42 @@ describe("API Client", () => {
});
});

describe("long base paths", () => {
let client: Client<
MockAPI.APIWithCustomBasePathAPI,
MockAPI.APIWithCustomBasePathConfig
>;
let mockFetch: typeof fetch;

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

afterEach(() => {
vi.restoreAllMocks();
});

it("preserves base path, including casing", async () => {
const dogs = await client.dogs.list();
expect(mockFetch).toHaveBeenCalledWith(
"/api/camelCase/kebab-case/v2/dogs",
{
method: "GET",
}
);
expect(dogs).toStrictEqual([{ name: "Fido", color: "red" }]);
});
});

describe("fetch calls", () => {
let client: Client<MockAPI.API, MockAPI.Config>;
let mockFetch: typeof fetch;
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/core/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ function makeUrl(callPath: string[], outputCase: "camel" | "kebab" = "kebab") {
async function makeRequest(
config: ClientConfig<string>,
action: string,
callPath: string[],
[basePath, ...callPath]: string[],
body?: unknown
) {
const method = inferHTTPMethod(action, body);
const url = makeUrl(callPath, config.urlCase);
const url = `${basePath}/${makeUrl(callPath, config.urlCase)}`;
const fetchFn = config.fetch ?? fetch;
const options: RequestInit =
method !== "GET" && body !== undefined
Expand Down
16 changes: 16 additions & 0 deletions packages/client/src/test-util/api-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 All @@ -27,6 +28,9 @@ export async function mockFetchImplementation(
case "GET /api/dogs/fido/dog-treats":
payload = [{ yummy: true }];
break;
case "GET /api/camelCase/kebab-case/v2/dogs":
payload = [{ name: "Fido", color: "red" }];
break;
case "GET /api/dogs":
throw new Error("Expected to throw");
case "PATCH /api/dogs/fido/dog-treats/treatId":
Expand Down Expand Up @@ -69,6 +73,18 @@ 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;
28 changes: 28 additions & 0 deletions packages/client/src/test-util/long-base-path-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Stl, z } from "stainless";

export const complicatedBasePath = "/api/camelCase/kebab-case/v2" as const;

const stl = new Stl({ plugins: {} });

const dog = z.object({
name: z.string(),
color: z.string(),
});

const listDogs = stl.endpoint({
endpoint: `GET ${complicatedBasePath}/dogs`,
response: dog.array(),
handler: async (_params, _context) => {
return [
{ name: "Shiro", color: "black" },
{ name: "baby!", color: "black" },
];
},
});

export const dogs = stl.resource({
summary: "Dogs",
actions: {
list: listDogs,
},
});
Loading

0 comments on commit 8131b65

Please sign in to comment.