-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nitro): automatically type middleware/api routes (#708)
- Loading branch information
Showing
4 changed files
with
73 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { FetchRequest, FetchOptions, FetchResponse } from 'ohmyfetch' | ||
|
||
// An interface to extend in a local project | ||
export declare interface InternalApi { } | ||
|
||
export declare type ValueOf<C> = C extends Record<any, any> ? C[keyof C] : never | ||
|
||
export declare type MatchedRoutes<Route extends string> = ValueOf<{ | ||
// exact match, prefix match or root middleware | ||
[key in keyof InternalApi]: Route extends key | `${key}/${string}` | '/' ? key : never | ||
}> | ||
|
||
export declare type MiddlewareOf<Route extends string> = Exclude<InternalApi[MatchedRoutes<Route>], Error | void> | ||
|
||
export declare type TypedInternalResponse<Route, Default> = | ||
Default extends string | boolean | number | null | void | object | ||
// Allow user overrides | ||
? Default | ||
: Route extends string | ||
? MiddlewareOf<Route> extends never | ||
// Bail if only types are Error or void (for example, from middleware) | ||
? Default | ||
: MiddlewareOf<Route> | ||
: Default | ||
|
||
export declare interface $Fetch { | ||
<T = unknown, R extends FetchRequest = FetchRequest> (request: R, opts?: FetchOptions): Promise<TypedInternalResponse<R, T>> | ||
raw<T = unknown, R extends FetchRequest = FetchRequest> (request: R, opts?: FetchOptions): Promise<FetchResponse<TypedInternalResponse<R, T>>> | ||
} | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var $fetch: $Fetch | ||
namespace NodeJS { | ||
interface Global { | ||
$fetch: $Fetch | ||
} | ||
} | ||
} | ||
|
||
export default {} |
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ declare module '@nuxt/kit' { | |
} | ||
} | ||
|
||
export * from './fetch' | ||
export * from '../dist' |
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