import type { Equal } from "@/src/util/type-util" import type { Extract200JSON, ExtractMethodResponseStatusContentJSON, } from "@/src/index" import { createHTTPClient } from "../index" import { it, expectTypeOf, describe } from "vitest" import type { GetArgs, PostArgs, PutArgs, PatchArgs, DelArgs, } from "../core-type" const client = createHTTPClient({ baseURL: "https://httpbin.org/anything" }) async function getPets() { return await client.get("/pets/{id}", { params: { id: 0 }, }) } async function postPet() { return await client.post("/pets", { name: "dog", tag: "pet" }) } async function deletePet() { return await client.delete<"/pets/{id}", 204>("/pets/{id}", { params: { id: 1 }, __is_config: true, }) } async function getPet() { return await client.get("/pets/{id}", { params: { id: 0 } }) } // ----------------------- type test ----------------------- type cases = [ Equal< Awaited>["data"], Extract200JSON<"get", "/pets/{id}"> >, Equal< Awaited>["data"], ExtractMethodResponseStatusContentJSON<"post", 200, "/pets"> >, Equal< Awaited>["data"], ExtractMethodResponseStatusContentJSON<"delete", 204, "/pets/{id}"> >, Equal< Awaited>["data"], Extract200JSON<"get", "/pets/{id}"> >, ] extends [true, true, true, true] ? true : false const testType: cases = true describe("test core types", () => { it("response type should match", () => { expectTypeOf(testType).toExtend() }) it("shoud match 204", () => { const data: ExtractMethodResponseStatusContentJSON< "delete", 204, "/pets/{id}" > = null as never expectTypeOf(data).toEqualTypeOf(data) }) it("shoud match 200", () => { const data: ExtractMethodResponseStatusContentJSON< "get", 200, "/pets/{id}" > = { id: 1, name: "dog" } expectTypeOf(data).toEqualTypeOf(data) }) it("shoud match 400", () => { const data: ExtractMethodResponseStatusContentJSON<"post", 400, "/pets"> = { code: 400, message: "", } expectTypeOf(data).toEqualTypeOf(data) }) }) describe("api-typing-meta 路径参数测试", () => { it("应该支持各种请求方法的 params 和 query 参数", () => { const argsGet: GetArgs<"/pets/paramsOrQuery/{testParam}"> = [ "/pets/paramsOrQuery/{testParam}", { query: {}, params: { testParam: "" } }, ] const argsPost: PostArgs<"/pets/paramsOrQuery/{testParam}"> = [ "/pets/paramsOrQuery/{testParam}", { query: {}, params: { testParam: "" }, __is_config: true }, ] const argsPut: PutArgs<"/pets/paramsOrQuery/{testParam}"> = [ "/pets/paramsOrQuery/{testParam}", { query: {}, params: { testParam: "" }, __is_config: true }, ] const argsPatch: PatchArgs<"/pets/paramsOrQuery/{testParam}"> = [ "/pets/paramsOrQuery/{testParam}", { query: {}, params: { testParam: "" }, __is_config: true }, ] expectTypeOf(argsGet).toExtend>() expectTypeOf(argsPost).toExtend< PostArgs<"/pets/paramsOrQuery/{testParam}"> >() expectTypeOf(argsPut).toExtend>() expectTypeOf(argsPatch).toExtend< PatchArgs<"/pets/paramsOrQuery/{testParam}"> >() }) // 测试宠物API的GET请求参数 it("应该支持 GET 请求的参数类型", () => { // GET /pets/{id} type PetByIdPathGet = "/pets/{id}" const argsWithPathParam: GetArgs = [ "/pets/{id}", { params: { id: 123, }, }, ] expectTypeOf(argsWithPathParam).toExtend>() }) // 测试宠物API的POST请求参数 it("应该支持 POST 请求的参数类型", () => { // POST /pets type PetsPathPost = "/pets" const newPet = { name: "Fluffy", tag: "cat", } const args: PostArgs = ["/pets", newPet] expectTypeOf(args).toExtend>() }) // 测试宠物API的PUT请求参数 it("应该支持 PUT 请求的参数类型", () => { // PUT /pets/{id} type PetUpdatePath = "/pets/{id}" const updatedPet = { name: "NewName", tag: "updated", } const args: PutArgs = [ "/pets/{id}", updatedPet, { params: { id: 456, }, }, ] expectTypeOf(args).toExtend>() }) // 测试宠物API的PATCH请求参数 it("应该支持 PATCH 请求的参数类型", () => { // PATCH /pets/{id} type PetPatchPath = "/pets/{id}" const patchData = { name: "PatchedName", } const args: PatchArgs = [ "/pets/{id}", patchData, { params: { id: 789, }, }, ] expectTypeOf(args).toExtend>() }) // 测试宠物API的DELETE请求参数 it("应该支持 DELETE 请求的参数类型", () => { // 通过路径参数删除 type PetDeletePath = "/pets/{id}" const deleteByPathArgs: DelArgs = [ "/pets/{id}", { params: { id: 999, }, __is_config: true, }, ] expectTypeOf(deleteByPathArgs).toExtend>() // 通过查询参数删除 type PetDeleteByQueryPath = "/pets" const deleteByQueryArgs: DelArgs = [ "/pets", { query: { id: 888, }, __is_config: true, }, ] expectTypeOf(deleteByQueryArgs).toExtend>() }) })