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-validators",
"@basketry/typescript-http-client"
],
"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
โ โ โโโ date-utils.ts
โ โ โโโ http-client.ts <-- generated
โ โ โโโ sanitizers.ts
โ โ โโโ validators.ts
โ โ โโโ types.ts
โ โโโ index.ts
โโโ .gitignore
โโโ basketry.config.json
โโโ package.json
โโโ petstore.json
โโโ README.md
Optionsโ
This generator depends on the @basketry/typescript
and @basketry/typescript-validators
generator 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
and @basketry/typescript-validators
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 dateUtils from "./date-utils.ts";
import * as sanitizers from "./sanitizers.ts";
import * as validators from "./validators.ts";
import * as types from "./types.ts";
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.
dateUtilsImportPath
โ
- Type:
string
- specifies the path for the date-utils import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-validators",
"@basketry/typescript-http-client"
],
"output": "src",
"options": {
"typescriptHttpClient": {
"dateUtilsImportPath": "@petstore/sdk/lib/date-utils"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as dateUtils from "@petstore/sdk/lib/date-utils";
sanitizersImportPath
โ
- Type:
string
- specifies the path for the sanitizers import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-validators",
"@basketry/typescript-http-client"
],
"output": "src",
"options": {
"typescriptHttpClient": {
"sanitizersImportPath": "@petstore/sdk/lib/sanitizers"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as sanitizers from "@petstore/sdk/lib/sanitizers";
validatorsImportPath
โ
- Type:
string
- specifies the path for the validators import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-validators",
"@basketry/typescript-http-client"
],
"output": "src",
"options": {
"typescriptHttpClient": {
"validatorsImportPath": "@petstore/sdk/lib/validators"
}
}
}
This option as configured will now emit the following code at the top of the generated file:
import * as validators from "@petstore/sdk/lib/validators";
typesImportPath
โ
- Type:
string
- specifies the path for the types import.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-validators",
"@basketry/typescript-http-client"
],
"output": "src",
"options": {
"typescriptHttpClient": {
"typesImportPath": "@petstore/sdk/lib/types"
}
}
}
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";
includeAuthSchemes
โ
- Type:
boolean
- specifies whether authentication scheme options should be defined in the client class constructors. If set totrue
, the client class constructors will accept anauth
object that can be used to authenticate requests. If set tofalse
or not defined, the client class constructors will not accept anauth
object.
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": [
"@basketry/typescript",
"@basketry/typescript-validators",
"@basketry/typescript-http-client"
],
"output": "src",
"options": {
"typescriptHttpClient": {
"includeAuthSchemes": true
}
}
}
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;