diff --git a/README.md b/README.md index 3bad655..9dfd711 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,6 @@ const { data, pending, refresh, error } = await useJsonPlaceholderData('po ## Multiple API Endpoints -> ℹ️ Using the `endpoints` module option will invalidate other module options of the default API endpoint. - You may want to connect multiple APIs to your Nuxt application. Utilize the `endpoints` module option for this use-case, expecting a record of API endpoint configurations with the following type: ```ts @@ -105,6 +103,8 @@ type ApiPartyEndpoints = Record< > ``` +> ℹ️ You can keep the default endpoint as well. + The key of each item will intrinsically be used as the API name. A custom `url`, as well as optionally `token` and `headers` can be set in the endpoint details configuration: ```ts @@ -145,7 +145,7 @@ export default defineNuxtConfig({ -`string` +`string | undefined` @@ -153,7 +153,7 @@ export default defineNuxtConfig({ For example, if you set it to `foo`, the composables will be called `$foo` and `useFooData`. -Default value: `party` +Default value: `undefined` @@ -165,7 +165,7 @@ Default value: `party` -`string` +`string | undefined` @@ -185,7 +185,7 @@ Default value: `process.env.API_PARTY_BASE_URL` -`string` +`string | undefined` @@ -205,7 +205,7 @@ Default value: `process.env.API_PARTY_TOKEN` -`Record` +`Record | undefined` @@ -249,14 +249,14 @@ type ApiPartyEndpoints = Record< token?: string headers?: Record } -> +> | undefined ``` **Multiple API endpoints** -This will create multiple API composables and invalidate the `name`, `url`, `token` and `headers` module options of the default endpoint. +This will create multiple API composables for the given endpoint configurations. You can keep the default endpoint as well. Default value: `{}` diff --git a/src/module.ts b/src/module.ts index d7f7e3f..39e6594 100644 --- a/src/module.ts +++ b/src/module.ts @@ -8,8 +8,6 @@ export interface ModuleOptions { * * @remarks * For example, if you set it to `foo`, the composables will be called `$foo` and `useFooData` - * - * @default 'party' */ name?: string @@ -54,7 +52,7 @@ export interface ModuleOptions { * Multiple API endpoints * * @remarks - * This will create multiple API composables and invalidate the `name`, `url`, `token` and `headers` module options of the default endpoint + * This will create multiple API composables for the given endpoint configurations. You can keep the default endpoint as well. * * @default {} */ @@ -77,7 +75,7 @@ export default defineNuxtModule({ }, }, defaults: { - name: 'party', + name: undefined, url: process.env.API_PARTY_BASE_URL as string, token: process.env.API_PARTY_TOKEN as string, headers: {}, @@ -89,7 +87,10 @@ export default defineNuxtModule({ const getRawComposableName = (endpointId: string) => `$${camelCase(endpointId)}` const getDataComposableName = (endpointId: string) => `use${pascalCase(endpointId)}Data` - if (!hasMultipleEndpoints) { + if (!hasMultipleEndpoints && !options.name) + logger.error('No name provided for the API. Please set the `name` option.') + + if (options.name) { // Make sure API base URL is set if (!options.url) logger.error('Missing `API_PARTY_BASE_URL` in `.env` file') @@ -99,7 +100,7 @@ export default defineNuxtModule({ logger.warn('Missing `API_PARTY_TOKEN` in `.env` file for bearer authentication and custom headers in module options. Are you sure your API doesn\'t require authentication? If so, you may not need this module.') // Add default endpoint to collection of endpoints - options.endpoints![options.name!] = { + options.endpoints![options.name] = { url: options.url!, token: options.token, headers: options.headers, @@ -148,12 +149,13 @@ export const ${getDataComposableName(i)} = (...args) => _useApiData('${i}', ...a addTemplate({ filename: 'api-party.d.ts', getContents() { - return endpointKeys.map(i => ` -import { $Api } from '${resolve('runtime/composables/$api')}' -import { UseApiData } from '${resolve('runtime/composables/useApiData')}' + return ` +import type { $Api } from '${resolve('runtime/composables/$api')}' +import type { UseApiData } from '${resolve('runtime/composables/useApiData')}' +${endpointKeys.map(i => ` export declare const ${getRawComposableName(i)}: $Api export declare const ${getDataComposableName(i)}: UseApiData -`.trimStart()).join('') +`.trimStart()).join('')}`.trimStart() }, }) },