-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for additional response resolver info (#35)
- Loading branch information
1 parent
ad8d2d1
commit 07fa9b0
Showing
8 changed files
with
139 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-msw": minor | ||
--- | ||
|
||
Restructured the library to add support for additional response resolver info. The enhanced `ResponseResolver` type and `ResponseResolverInfo` are available as exports. |
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
export { createOpenApiHttp } from "../src/openapi-http.js"; | ||
export type { HttpOptions, OpenApiHttpHandlers } from "../src/openapi-http.js"; | ||
export type { AnyApiSpec, HttpMethod } from "../src/api-spec.js"; | ||
|
||
export { | ||
createOpenApiHttp, | ||
type HttpHandlerFactory, | ||
type HttpOptions, | ||
type OpenApiHttpHandlers, | ||
} from "../src/openapi-http.js"; | ||
|
||
export type { | ||
AnyApiSpec, | ||
HttpHandlerFactory, | ||
HttpMethod, | ||
} from "../src/type-helpers.js"; | ||
ResponseResolver, | ||
ResponseResolverInfo, | ||
} from "../src/response-resolver.js"; |
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,57 @@ | ||
import type { AsyncResponseResolverReturnType, http } from "msw"; | ||
import type { | ||
AnyApiSpec, | ||
HttpMethod, | ||
PathParams, | ||
RequestBody, | ||
ResponseBody, | ||
} from "./api-spec.js"; | ||
|
||
/** Response resolver that gets provided to HTTP handler factories. */ | ||
export type ResponseResolver< | ||
ApiSpec extends AnyApiSpec, | ||
Path extends keyof ApiSpec, | ||
Method extends HttpMethod, | ||
> = ( | ||
info: ResponseResolverInfo<ApiSpec, Path, Method>, | ||
) => AsyncResponseResolverReturnType<ResponseBody<ApiSpec, Path, Method>>; | ||
|
||
/** Response resolver info that extends MSW's resolver info with additional functionality. */ | ||
export interface ResponseResolverInfo< | ||
ApiSpec extends AnyApiSpec, | ||
Path extends keyof ApiSpec, | ||
Method extends HttpMethod, | ||
> extends MSWResponseResolverInfo<ApiSpec, Path, Method> {} | ||
|
||
/** Wraps MSW's resolver function to provide additional info to a given resolver. */ | ||
export function createResolverWrapper< | ||
ApiSpec extends AnyApiSpec, | ||
Path extends keyof ApiSpec, | ||
Method extends HttpMethod, | ||
>( | ||
resolver: ResponseResolver<ApiSpec, Path, Method>, | ||
): MSWResponseResolver<ApiSpec, Path, Method> { | ||
return (info) => { | ||
return resolver(info); | ||
}; | ||
} | ||
|
||
/** MSW response resolver info that is made type-safe through an api spec. */ | ||
type MSWResponseResolverInfo< | ||
ApiSpec extends AnyApiSpec, | ||
Path extends keyof ApiSpec, | ||
Method extends HttpMethod, | ||
> = Parameters<MSWResponseResolver<ApiSpec, Path, Method>>[0]; | ||
|
||
/** MSW response resolver function that is made type-safe through an api spec. */ | ||
export type MSWResponseResolver< | ||
ApiSpec extends AnyApiSpec, | ||
Path extends keyof ApiSpec, | ||
Method extends HttpMethod, | ||
> = Parameters< | ||
typeof http.all< | ||
PathParams<ApiSpec, Path, Method>, | ||
RequestBody<ApiSpec, Path, Method>, | ||
ResponseBody<ApiSpec, Path, Method> | ||
> | ||
>[1]; |
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,7 @@ | ||
/** Converts a type to string while preserving string literal types. */ | ||
export type Stringify<Value> = Value extends string ? Value : string; | ||
|
||
/** Converts a object values to their {@link Stringify} value. */ | ||
export type ConvertToStringified<Params> = { | ||
[Name in keyof Params]: Stringify<Required<Params>[Name]>; | ||
}; |
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