Skip to content

Commit

Permalink
feat: allow to keep default endpoint with custom ones
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Nov 30, 2022
1 parent 8cae593 commit aeeb2f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ const { data, pending, refresh, error } = await useJsonPlaceholderData<Post>('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
Expand All @@ -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
Expand Down Expand Up @@ -145,15 +145,15 @@ export default defineNuxtConfig({

</td><td valign="top">

`string`
`string | undefined`

</td><td valign="top">

**API name used for composables**

For example, if you set it to `foo`, the composables will be called `$foo` and `useFooData`.

Default value: `party`
Default value: `undefined`

</td>
</tr>
Expand All @@ -165,7 +165,7 @@ Default value: `party`

</td><td valign="top">

`string`
`string | undefined`

</td><td valign="top">

Expand All @@ -185,7 +185,7 @@ Default value: `process.env.API_PARTY_BASE_URL`

</td><td valign="top">

`string`
`string | undefined`

</td><td valign="top">

Expand All @@ -205,7 +205,7 @@ Default value: `process.env.API_PARTY_TOKEN`

</td><td valign="top">

`Record<string, string>`
`Record<string, string> | undefined`

</td><td valign="top">

Expand Down Expand Up @@ -249,14 +249,14 @@ type ApiPartyEndpoints = Record<
token?: string
headers?: Record<string, string>
}
>
> | undefined
```
</td><td valign="top">
**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: `{}`
Expand Down
22 changes: 12 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {}
*/
Expand All @@ -77,7 +75,7 @@ export default defineNuxtModule<ModuleOptions>({
},
},
defaults: {
name: 'party',
name: undefined,
url: process.env.API_PARTY_BASE_URL as string,
token: process.env.API_PARTY_TOKEN as string,
headers: {},
Expand All @@ -89,7 +87,10 @@ export default defineNuxtModule<ModuleOptions>({
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')
Expand All @@ -99,7 +100,7 @@ export default defineNuxtModule<ModuleOptions>({
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,
Expand Down Expand Up @@ -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()
},
})
},
Expand Down

0 comments on commit aeeb2f3

Please sign in to comment.