Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
enkot committed Nov 11, 2023
1 parent 99185ed commit 394c409
Show file tree
Hide file tree
Showing 25 changed files with 3,415 additions and 1,837 deletions.
10 changes: 5 additions & 5 deletions docs/content/1.setup/1.quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export default defineNuxtConfig({
openFetch: {
clients: {
pets: {
fetchOptions: {
baseURL: 'https://petstore3.swagger.io/api/v3'
}
baseURL: 'https://petstore3.swagger.io/api/v3'
}
}
}
Expand All @@ -56,15 +54,17 @@ nuxt.config.ts

```vue
<script setup lang="ts">
const { data, error } = await usePetsFetch('/pet/{petId}', {
const { $petsFetch } = useNuxtApp()
const data = await $petsFetch('/pet/{petId}', {
path: {
petId
}
})
// or
const data = await $petsFetch('/pet/{petId}', {
const { data, error } = await usePetsFetch('/pet/{petId}', {
path: {
petId
}
Expand Down
10 changes: 6 additions & 4 deletions docs/content/1.setup/2.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ export default defineNuxtConfig({
| **Key** | **Type** | **Default** | **Description** |
| ---------------------------- | ---------- | --------------------- | ---------------------------------------------------------------------------------------------------- |
| `openAPITS` | `OpenAPITSOptions` | | OpenAPI TS [options](https://openapi-ts.pages.dev/node/#options) |
| `addPlugin` | `boolean` | `true` | If enable default plugin to create fetch clients |
| **Clients** | | | |
| `clients` | `Record<string, OpenFetchClientOptions>` | `{}` | Configures your OpenAPI clients. |
| `clients.[client]` | `OpenFetchClientOptions` | | Client options |
| `clients.[client].schema` | `OpenAPI3Schema` | | Local or remote schema file (YAML or JSON) |
| `clients.[client].functionSuffix` | `string` | `'fetch'` | Suffix for generated fetch composables/utils ('fetch' -> 'use\[Client\]Fetch'). |
| `clients.[client].fetchOptions` | `OpenFetchOptions` | | Serializable [ofetch](https://github.com/unjs/ofetch) options, except `method` and `params` |
| `clients.[client].baseURL` | `string` | | Base URL for the request. |
| `clients.[client].headers` | `HeadersInit` | | Request headers. |
| `clients.[client].query` | `SearchParameters` | | Adds query search params to URL |
| `clients.[client].schema` | `OpenAPI3Schema` | | Local or remote schema file (YAML or JSON) |


34 changes: 30 additions & 4 deletions docs/content/1.setup/3.schemas.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Schemas

By default NuxtOpenFetch will search for OpenAPI schemas in `/openapi/[client]` directory, where `[client]` is the name specified in the Nuxt config (f.e. `pets`):
By default NuxtOpenFetch searches for OpenAPI schemas in `/openapi/[client]` directory, where `[client]` is the name specified in the Nuxt config (f.e. `pets`):

```ts [nuxt.config.ts]
export default defineNuxtConfig({
openFetch: {
clients: {
// `$petsFetch`/`usePetsFetch`/`useLazyPetsFetch` will be generated, but without default options
pets: {}
pets: {
baseURL: 'https://petstore3.swagger.io/api/v3/'
}
}
}
})
Expand All @@ -20,7 +21,7 @@ openapi/
nuxt.config.ts
```

You can also manually specify schema for each client using [schema](/setup/configuration) option in Nuxt config.
You can manually specify schema for each client using [schema](/setup/configuration) option in Nuxt config.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
Expand All @@ -35,3 +36,28 @@ export default defineNuxtConfig({
}
})
```

Client can also be configured using `runtimeConfig` or environment variables:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
openFetch: {
clients: {
pets: {}
}
},
runtimeConfig: {
openFetch: {
pets: {
schema: 'https://petstore3.swagger.io/api/v3/openapi.json',
baseURL: 'https://petstore3.swagger.io/api/v3/'
},
},
},
})
```
or:
```sh
NUXT_OPEN_FETCH_PETS_SCHEMA=https://petstore3.swagger.io/api/v3/openapi.json
NUXT_OPEN_FETCH_PETS_BASE_URL=https://petstore3.swagger.io/api/v3/
```
6 changes: 1 addition & 5 deletions docs/content/1.setup/4.proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export default defineNuxtConfig({
openFetch: {
clients: {
pets: {
fetchOptions: {
baseURL: '/petsProxy'
}
baseURL: '/petsProxy'
}
}
},
Expand Down Expand Up @@ -39,5 +37,3 @@ export default defineNuxtConfig({
}
})
```

That's it :) You can use fetch composables/utils as usual, but now all the requests will go to Nuxt server first and then proxied to the target server.
31 changes: 0 additions & 31 deletions docs/content/2.composables/3.useOpenFetchOptions.md

This file was deleted.

2 changes: 2 additions & 0 deletions docs/content/3.utils/1.$[client]Fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ It has the same API as Nuxt's [$fetch](https://nuxt.com/docs/api/utils/dollarfet

```vue
<script setup lang="ts">
const { $petsFetch } = useNuxtApp()
const data = await $petsFetch('/pet/{petId}', {
path: {
petId: 12
Expand Down
39 changes: 0 additions & 39 deletions docs/content/3.utils/2.createOpenFetchClient.md

This file was deleted.

39 changes: 0 additions & 39 deletions docs/content/3.utils/3.createUseLazyOpenFetchClient.md

This file was deleted.

39 changes: 0 additions & 39 deletions docs/content/3.utils/4.createUseOpenFetchClient.md

This file was deleted.

35 changes: 35 additions & 0 deletions docs/content/4.advanced/1.custom-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Custom plugin

**NuxtOpenFetch** uses Nuxt plugin to provide fetch clients to the app.
If you need to add non-serializable fetch options which can't be specified in Nuxt config, such as `onRequest`, `onResponse` etc, you can create custom Nuxt plugin.

First you need disable default plugin:
```ts
export default defineNuxtConfig({
openFetch: {
disablePlugin: true,
// ...
},
})
```

For example if you need to add logging on each request:
```ts
export default defineNuxtPlugin(() => {
const { public: { openFetch: clients }} = useRuntimeConfig()

return {
provide: Object.fromEntries(Object.entries(clients).map(([name, client]) => [
`${name}Fetch`,
createOpenFetch((options) => ({
...clients.pets,
...options,
onRequest(ctx) {
console.log('My logging', ctx.request)
return options.onRequest?.(ctx)
}
}))
]))
}
})
```
2 changes: 2 additions & 0 deletions docs/content/4.advanced/_dir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: 'Advanced'
icon: heroicons-outline:bookmark-alt
16 changes: 4 additions & 12 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
<script setup lang="ts">
const { execute } = await usePetsFetch('/pet/{petId}', {
const { execute, data } = await usePetsFetch('/pet/{petId}', {
path: {
petId: 1,
}
petId: 12,
},
immediate: false,
})
// const result = await $petsFetch('/pet', {
// method: 'post',
// body: {
// name: 'david',
// photoUrls: ['https://www.baidu.com']
// }
// })
</script>

<template>
Expand Down
17 changes: 16 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
export default defineNuxtConfig({
modules: ['../src/module'],
extends: ['./pets'],
devtools: { enabled: true }
devtools: { enabled: false },
openFetch: {
clients: {
foo: {
baseURL: '/petsProxy'
},
},
},
runtimeConfig: {
openFetch: {
pets: {
baseURL: '/petsProxy',
},
},
},
})
Loading

0 comments on commit 394c409

Please sign in to comment.