Skip to content

Commit

Permalink
Update examples.md (#1419)
Browse files Browse the repository at this point in the history
add example of using getter for dynamic header
  • Loading branch information
WickyNilliams authored Nov 2, 2023
1 parent 06c04a0 commit bebf08c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/src/content/docs/openapi-fetch/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ client.GET("/some-authenticated-url", {
});
```

### Vanilla JS getter

You can also use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get" target="_blank" rel="noopener noreferrer">getter</a>:

```ts
// src/lib/api/index.ts
import createClient from "openapi-fetch";
import { paths } from "./v1";

let authToken: string | undefined = undefined;
someAuthMethod().then((newToken) => (authToken = newToken));

export default createClient<paths>({
baseUrl: "https://myapi.dev/v1/",
headers: {
get Authorization() {
return authToken ? `Bearer ${authToken}` : undefined
}
}
});
```

```ts
// src/some-other-file.ts
import client from "./lib/api";

client.GET("/some-authenticated-url", {
/**/
});
```

## Frameworks

openapi-fetch is simple vanilla JS that can be used in any project. But sometimes the implementation in a framework may come with some prior art that helps you get the most out of your usage.
Expand Down

0 comments on commit bebf08c

Please sign in to comment.