|
| 1 | +# Using the generated code |
| 2 | + |
| 3 | +> The examples below refer to the [Pet Store OpenAPI 3.0 schema](https://petstore3.swagger.io/). |
| 4 | +
|
| 5 | +## Basic example: using a single controller |
| 6 | + |
| 7 | +In the most basic scenario, you may need just a single **controller** from the generated code. In this case, the code is as simple as this: |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { petController as createPetController } from "./src/generated/petstore.json/paths/PetController"; |
| 11 | +import { Pet } from "./src/generated/petstore.json/components/schemas/Pet"; |
| 12 | + |
| 13 | +// Creating a controller, see the "HTTP Clients" wiki page for more details |
| 14 | +const petController = createPetController({ httpClient: fetchHttpClient }); |
| 15 | + |
| 16 | +// The returned object is guaranteed to be a valid `Pet` |
| 17 | +const createdPet: Promise<Pet> = petController.addPet({ |
| 18 | + body: { |
| 19 | + // The parameters are statically typed, IntelliSense works, too |
| 20 | + name: "Spotty", |
| 21 | + photoUrls: [], |
| 22 | + }, |
| 23 | +}); |
| 24 | +``` |
| 25 | + |
| 26 | +## The `controllers` object |
| 27 | + |
| 28 | +In most projects, the generated code includes more than one controller. Sometimes it's handy to have a single entry points to all of them - for this purpose, the `controllers` object is created by the generator: |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { controllers } from "./src/generated/petstore.json/paths/paths"; |
| 32 | + |
| 33 | +const api = controllers({ httpClient: fetchHttpClient }); |
| 34 | +const pets = api.petController.findPetsByStatus({ |
| 35 | + query: { status: some("available") }, |
| 36 | +}); |
| 37 | +// api.userController and api.storeController are also available |
| 38 | +``` |
| 39 | + |
| 40 | +## Plugging different HTTP clients |
| 41 | + |
| 42 | +Generated functions may be instructed to return any generic type with one or two type arguments, for example `Promise<Response>` or `Observable<Either<Error, Response>>`. The return type is specified by providing a corresponding **client**. In the example below, providing an `rxjsHttpClient` makes the `petController` return RxJS's `Observable`: |
| 43 | + |
| 44 | +```typescript |
| 45 | +import { Observable } from "rxjs"; |
| 46 | + |
| 47 | +// Now create another controller returning an RxJS stream |
| 48 | +const petRxjsController = createPetController({ httpClient: rxjsHttpClient }); |
| 49 | +const createdPet$: Observable<Pet> = petRxjsController.addPet({ |
| 50 | + body: { |
| 51 | + name: "Spotty", |
| 52 | + photoUrls: [], |
| 53 | + }, |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +The list of bundled clients and more information can be found in the [Clients](./clients.md) page. |
| 58 | + |
| 59 | +## Using [`RemoteData`](https://github.com/devexperts/remote-data-ts) |
| 60 | + |
| 61 | +The codegen provides first class support for the `RemoteData<Error, Response>` type, making it easier to build complex logic on top of the generated controllers. |
| 62 | + |
| 63 | +```typescript |
| 64 | +const petRDController = createPetController({ httpClient: liveDataHttpClient }); |
| 65 | +/** |
| 66 | + * `LiveData<E, A> = Observable<RemoteData<E, A>>` |
| 67 | + * |
| 68 | + * Emits `pending` when the request is started, |
| 69 | + * then `success(Pet)` or `failure(Error)` upon completion. |
| 70 | + */ |
| 71 | +const createdPet$: LiveData<Error, Pet> = petRDController.addPet({ |
| 72 | + body: { |
| 73 | + name: "Spotty", |
| 74 | + photoUrls: [], |
| 75 | + }, |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Validation utils |
| 80 | + |
| 81 | +Each schema defined in the spec produces a TS type and an `io-ts` codec, which can be used for runtime type checking in the application code: |
| 82 | + |
| 83 | +```typescript |
| 84 | +import { either } from "fp-ts"; |
| 85 | +import { pipe } from "fp-ts/function"; |
| 86 | +import { User, UserIO } from "./src/generated/petstore.json/components/schemas/User"; |
| 87 | + |
| 88 | +pipe( |
| 89 | + UserIO.decode(JSON.parse(localStorage.getItem('PetStore.user'))), |
| 90 | + either.fold( |
| 91 | + error => { |
| 92 | + console.log('The user record is not valid'); |
| 93 | + }, |
| 94 | + (user: User) => { |
| 95 | + console.log(`Was previously logged in as: ${user.email}`); |
| 96 | + } |
| 97 | + ) |
| 98 | +); |
| 99 | +``` |
| 100 | + |
| 101 | +Learn more on the `Either` type: [Getting started with fp-ts: Either vs Validation](https://dev.to/gcanti/getting-started-with-fp-ts-either-vs-validation-5eja) |
| 102 | + |
0 commit comments