Skip to content

Commit

Permalink
feat: configurable API proxy base path (closes #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jan 2, 2024
1 parent 5c2f3ac commit 2f2a72a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
9 changes: 9 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,14 @@ interface ModuleOptions {
* Global options for openapi-typescript
*/
openAPITS?: OpenAPITSOptions

server?: {
/**
* The API base path for the Nuxt server handler
*
* @default '__api_party'
*/
basePath?: string
}
}
```
16 changes: 15 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { relative } from 'pathe'
import { defu } from 'defu'
import { joinURL } from 'ufo'
import { camelCase, pascalCase } from 'scule'
import { addImportsSources, addServerHandler, addTemplate, createResolver, defineNuxtModule, tryResolveModule, useLogger } from '@nuxt/kit'
import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
Expand Down Expand Up @@ -68,6 +69,15 @@ export interface ModuleOptions {
* Global options for openapi-typescript
*/
openAPITS?: OpenAPITSOptions

server?: {
/**
* The API base route for the Nuxt server handler
*
* @default '__api_party'
*/
basePath?: string
}
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -82,6 +92,9 @@ export default defineNuxtModule<ModuleOptions>({
endpoints: {},
client: false,
openAPITS: {},
server: {
basePath: '__api_party',
},
},
async setup(options, nuxt) {
const moduleName = 'nuxt-api-party'
Expand Down Expand Up @@ -125,6 +138,7 @@ export default defineNuxtModule<ModuleOptions>({
),
),
client: false,
server: resolvedOptions.server,
},
)

Expand Down Expand Up @@ -152,7 +166,7 @@ export default defineNuxtModule<ModuleOptions>({

// Add Nuxt server route to proxy the API request server-side
addServerHandler({
route: '/api/__api_party/:endpointId',
route: joinURL('/api', options.server!.basePath!, ':endpointId'),
method: 'post',
handler: resolve('runtime/server/handler'),
})
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/$api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { hash } from 'ohash'
import { joinURL } from 'ufo'
import type { NitroFetchOptions } from 'nitropack'
import { headersToObject, resolvePathParams, serializeMaybeEncodedBody } from '../utils'
import { isFormData } from '../formData'
Expand Down Expand Up @@ -116,7 +117,7 @@ export function _$api<T = any>(
}) as Promise<T>

const serverFetcher = async () =>
(await globalThis.$fetch<T>(`/api/__api_party/${endpointId}`, {
(await globalThis.$fetch<T>(joinURL('/api', apiParty.server.basePath!, endpointId), {
...fetchOptions,
method: 'POST',
body: {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { computed, reactive } from 'vue'
import { hash } from 'ohash'
import { joinURL } from 'ufo'
import type { FetchError } from 'ofetch'
import type { NitroFetchOptions } from 'nitropack'
import type { WatchSource } from 'vue'
Expand Down Expand Up @@ -200,7 +201,7 @@ export function _useApiData<T = any>(
}
else {
result = (await globalThis.$fetch<T>(
`/api/__api_party/${endpointId}`,
joinURL('/api', apiParty.server.basePath!, endpointId),
{
..._fetchOptions,
signal: controller.signal,
Expand Down

0 comments on commit 2f2a72a

Please sign in to comment.