-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: $myApi composables in Nitro routes
- Loading branch information
1 parent
ed12751
commit cd8481f
Showing
14 changed files
with
145 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// import { $jsonPlaceholder } from '#nuxt-api-party/server' | ||
|
||
export default defineEventHandler(async () => { | ||
const response = await $jsonPlaceholder('posts/1') | ||
return response | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { headersToObject, resolvePath } from '../utils' | ||
import type { ModuleOptions } from '../../module' | ||
import type { ApiFetchOptions } from '../composables/$api' | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
export function _$api<T = any>( | ||
endpointId: string, | ||
path: string, | ||
opts: ApiFetchOptions = {}, | ||
): Promise<T> { | ||
const { pathParams, query, headers, method, body, ...fetchOptions } = opts | ||
const { apiParty } = useRuntimeConfig() | ||
const endpoints = (apiParty as unknown as ModuleOptions).endpoints || {} | ||
const endpoint = endpoints[endpointId] | ||
|
||
return globalThis.$fetch<T>(resolvePath(path, pathParams), { | ||
...fetchOptions, | ||
baseURL: endpoint.url, | ||
method, | ||
query: { | ||
...endpoint.query, | ||
...query, | ||
}, | ||
headers: { | ||
...(endpoint.token && { Authorization: `Bearer ${endpoint.token}` }), | ||
...endpoint.headers, | ||
...headersToObject(headers), | ||
}, | ||
body, | ||
}) as Promise<T> | ||
} |
Oops, something went wrong.