HTTP Client
Installβ
- npm
- yarn
- pnpm
npm install @basketry/typescript-http-client
yarn add @basketry/typescript-http-client
pnpm add @basketry/typescript-http-client
Basic Usageβ
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"options": {
"httpClient": { "validation": "zod" }
},
"output": "src"
}
File Structureβ
This generator will create an http-client.ts
file that contains an HTTP client for the API. This client will have methods for each operation defined in the source schema.
my-project/
βββ node_modules/
βββ src/
β βββ v1/ <-- generated
β β βββ dtos
β β β βββ mappers.ts
β β β βββ README.md
β β β βββ types.ts
β β βββ http-client.ts <-- generated
β β βββ schemas.ts
β β βββ types.ts
β βββ index.ts
βββ .gitignore
βββ basketry.config.json
βββ package.json
βββ petstore.json
βββ README.md
Optionsβ
This generator depends on the @basketry/typescript
, @basketry/typescript-dtos
, and @basketry/zod
generators and all of their applied options will also apply to files emitted by this generator.
See:
This generator depends on files generated by the @basketry/typescript
, @basketry/typescript-dtos
, and @basketry/zod
generators. In most cases, those files will exist within the same directory as the generated http-client.ts
file.
By default, the generated code will import its dependencies as follows:
import * as dtos from "./dtos/types";
import * as mappers from "./dtos/mappers";
import * as schemas from "./schemas";
import * as types from "./types";
If those modules are located in a different directory, you can use the following options to specify custom import paths. These paths can be any string that you would normally require
or import
. This can be useful when generated dependencies are distributed as part of a library or package.
dtosImportPath
β
- Type:
string
- specifies the path for the DTO import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"output": "src",
"options": {
"httpClient": {
"dtosImportPath": "@petstore/sdk/lib/dtos/types",
"validation": "zod"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as mappers from "@petstore/sdk/lib/dtos/types";
mappersImportPath
β
- Type:
string
- specifies the path for the mappers import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"output": "src",
"options": {
"httpClient": {
"mappersImportPath": "@petstore/sdk/lib/dtos/mappers",
"validation": "zod"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as mappers from "@petstore/sdk/lib/dtos/mappers";
schemasImportPath
β
- Type:
string
- specifies the path for the Zod schemas import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"output": "src",
"options": {
"httpClient": {
"schemasImportPath": "@petstore/sdk/lib/schemas",
"validation": "zod"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as schemas from "@petstore/sdk/lib/schemas";
typesImportPath
β
- Type:
string
- specifies the path for the types import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"output": "src",
"options": {
"httpClient": {
"typesImportPath": "@petstore/sdk/lib/types",
"validation": "zod"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as types from "@petstore/sdk/lib/types";
validation
β
- Type:
zod
ornative
- specifies the validation strategy to use for the HTTP Client.zod
will become the default value in basketry 0.2βunitl then it needs to be specified explicitly.
Native validation is deprecated and will not receive updates. Please use the Zod Generator instead.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"output": "src",
"options": {
"httpClient": {
"validation": "zod"
}
}
}
Examplesβ
You can use the following examples to see how to use the generated HTTP Client. In both the browser and Node environments, you can pass any method that implements the fetch
interface to the service class. You can use the native fetch
method, a wrapped version of fetch
, or a custom library of your choosing.
Browserβ
import { HttpPetService, PetService } from "./v1/http-client";
const petService: PetService = new HttpPetService(window.fetch.bind(window), {
root: "https://api.example.com/v1",
});
export default petService;
Note that if you pass window.fetch
to a service class, you need to rebind the fetch
method to the window
object.
If you don't, the fetch
method will fail to execute in the browser environment. No request will be sent, and the console will display the error "TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation".
Nodeβ
import { HttpPetService, PetService } from "./v1/http-client";
const petService: PetService = new PetService(fetch, {
root: "https://api.example.com/v1",
});
export default petService;