-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
238 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { ROUTE_METHODS } from "@/constants" | ||
import { | ||
DefaultBodyType, | ||
HttpHandler, | ||
HttpResponseResolver, | ||
Path, | ||
PathParams, | ||
http as originHttp, | ||
} from "msw" | ||
|
||
// 1. constant definition | ||
const HTTP_METHODS = ROUTE_METHODS.map((method) => | ||
method.toLowerCase() | ||
) as (keyof typeof originHttp)[] | ||
|
||
// 2. proxy method creation | ||
const createProxyMethod = < | ||
Params extends PathParams<keyof Params> = PathParams, | ||
RequestBodyType extends DefaultBodyType = DefaultBodyType, | ||
ResponseBodyType extends DefaultBodyType = undefined, | ||
RequestPath extends Path = Path, | ||
>( | ||
method: keyof typeof originHttp | ||
): HttpRequestHandler< | ||
Params, | ||
RequestBodyType, | ||
ResponseBodyType, | ||
RequestPath | ||
> => { | ||
return new Proxy(originHttp[method], { | ||
apply: (target, thisArg, argArray) => { | ||
const [url, responseHandlers] = argArray | ||
const responseHandlersWithPresets = (...rest: any) => | ||
responseHandlers(...rest) | ||
const result = Reflect.apply(target, thisArg, [ | ||
url, | ||
responseHandlersWithPresets, | ||
]) | ||
result.presets = (presets: HttpPreset[]) => | ||
Reflect.apply(target, thisArg, [ | ||
url, | ||
responseHandlersWithPresets, | ||
presets, | ||
]) | ||
return result | ||
}, | ||
}) as HttpRequestHandler< | ||
Params, | ||
RequestBodyType, | ||
ResponseBodyType, | ||
RequestPath | ||
> | ||
} | ||
|
||
// 3. type definition | ||
type HttpPreset = { | ||
status: number | ||
description: string | ||
response: any | ||
} | ||
|
||
type HttpRequestHandler< | ||
Params extends PathParams<keyof Params> = PathParams, | ||
RequestBodyType extends DefaultBodyType = DefaultBodyType, | ||
ResponseBodyType extends DefaultBodyType = undefined, | ||
RequestPath extends Path = Path, | ||
> = ( | ||
path: RequestPath, | ||
resolver: HttpResponseResolver<Params, RequestBodyType, ResponseBodyType> | ||
) => HttpHandler & { | ||
presets: (presets: HttpPreset[]) => HttpHandler | ||
} | ||
|
||
// 4. custom http object creation | ||
let http = originHttp as { | ||
[method in keyof typeof originHttp]: HttpRequestHandler | ||
} | ||
|
||
// 5. method assignment | ||
HTTP_METHODS.forEach((method: keyof typeof originHttp) => { | ||
http[method] = createProxyMethod(method) | ||
}) | ||
|
||
export { http } |
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,26 @@ | ||
import { twMerge } from "tailwind-merge" | ||
import { Input, InputProps } from "./input" | ||
import { Label } from "./label" | ||
|
||
type InputContainerProps = InputProps & { | ||
label: string | ||
containerClassName?: string | ||
} | ||
|
||
export const InputContainer = ({ | ||
containerClassName, | ||
label, | ||
...rest | ||
}: InputContainerProps) => { | ||
return ( | ||
<div className={twMerge("flex items-center", containerClassName)}> | ||
<Label | ||
htmlFor={rest.id} | ||
className="text-primary flex-shrink-0 font-semibold" | ||
> | ||
{label} | ||
</Label> | ||
<Input className={twMerge("ml-[12px]", rest.className)} {...rest} /> | ||
</div> | ||
) | ||
} |
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
Oops, something went wrong.