Skip to content

Commit

Permalink
fix: props type error.
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuki committed Feb 26, 2023
1 parent 5c16474 commit 753094d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
13 changes: 7 additions & 6 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createLogger } from "./logger";
import type { FourzeRouteMeta, MetaInstance } from "./shared/meta";
import { injectMeta } from "./shared/meta";
import type {
DefaultData,
FourzeApp, FourzeBaseRoute,
FourzeHandle,
FourzeMiddleware,
Expand Down Expand Up @@ -43,14 +44,14 @@ type FourzeRouteChain<Methods extends string = RequestMethod> = {
* only once
*/
[method in Methods]: {
<Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta>(
<Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta>(
options: Omit<FourzeRouteOptions<Props, Meta>, "path" | "method">,
handle: FourzeHandle<Result, Props, Meta>
): FourzeRouteChain<Exclude<Methods, method>> & {
route: FourzeRouter["route"]
}

<Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta>(
<Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta>(
handle: FourzeHandle<Result, Props, Meta>
): FourzeRouteChain<Exclude<Methods, method>> & {
route: FourzeRouter["route"]
Expand All @@ -68,15 +69,15 @@ export interface FourzeRouter

route(path: string): FourzeRouteChain

route<Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta>(path: string, method: RequestMethod, options: FourzeRouteOptions<Props, Meta>, handle: FourzeHandle<Result, Props, Meta>): this
route<Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta>(path: string, method: RequestMethod, options: FourzeRouteOptions<Props, Meta>, handle: FourzeHandle<Result, Props, Meta>): this

route(path: string, method: RequestMethod, handle: FourzeHandle): this

route<Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta>(path: string, options: FourzeRouteOptions<Props, Meta>, handle: FourzeHandle<Result, Props, Meta>): this
route<Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta>(path: string, options: FourzeRouteOptions<Props, Meta>, handle: FourzeHandle<Result, Props, Meta>): this

route(path: string, handle: FourzeHandle): this

route<Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta>(route: FourzeBaseRoute<Result, Props, Meta>): this
route<Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta>(route: FourzeBaseRoute<Result, Props, Meta>): this

route(route: FourzeBaseRoute[]): this

Expand Down Expand Up @@ -191,7 +192,7 @@ export function defineRouter(
} as FourzeRouteChain;

for (const method of FOURZE_METHODS) {
chain[method] = (...args: [FourzeRouteOptions, FourzeHandle] | [FourzeHandle]) => {
chain[method] = (...args: [FourzeRouteOptions<Record<string, any>>, FourzeHandle] | [FourzeHandle]) => {
const { handle, options } = overload({
options: {
type: Object
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/shared/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MaybePromise, MaybeRegex } from "maybe-types";
import { overload } from "../utils";
import type { FourzeContextOptions, FourzeServiceContext } from "./context";
import type { FourzeAppMeta, FourzeRouteMeta, MetaInstance } from "./meta";
import type { ObjectProps, PropType } from "./props";
import type { DefaultData, ObjectProps, PropType } from "./props";
import type { FourzeRequest } from "./request";
import type { FourzeResponse } from "./response";

Expand All @@ -12,7 +12,7 @@ const FOURZE_MIDDLEWARE_SYMBOL = Symbol("FOURZE_MIDDLEWARE_SYMBOL");
export type FourzeNext<T = any> = () => MaybePromise<T>;

export type FourzeHandle<
R = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta
R = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta
> = (
request: FourzeRequest<Props, Meta>,
response: FourzeResponse
Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/shared/props.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { isArray, isConstructor, isFunction, isPlainObject, isUndef } from "../utils";
import { FourzeError } from "./error";

export type DefaultData = Record<string, unknown>;
export interface GlobalProps extends Record<string, any> {}

export type DefaultData = Record<string, any>;

export type PropIn = "body" | "query" | "path";

export type ExtractPropTypes<
P extends Record<string, any>, In extends PropIn | "any" = "any", O = In extends PropIn ? Pick<P, InKeys<P, In>> : P
> = {
export declare type ExtractPropTypes<O> = {
[K in keyof Pick<O, RequiredKeys<O>>]: InferPropType<O[K]>;
} & {
[K in keyof Pick<O, OptionalKeys<O>>]?: InferPropType<O[K]>;
} & DefaultData;
} & GlobalProps;

export type ExtractPropTypesWithIn<
O, In extends PropIn
> = ExtractPropTypes<Pick<O, InKeys<O, In>>>;

export type ExtractDefaultPropTypes<P extends Record<string, any>> = {
[K in keyof Pick<P, DefaultKeys<P>>]: InferPropType<P[K]>;
Expand Down Expand Up @@ -251,7 +255,7 @@ export function normalizeProps<T>(
}

export function withDefaults<
T = Record<string, any>, P extends ObjectProps<T> = ObjectProps <T>, D = ExtractPropTypes<P>, Defaults = ExtractDefaultPropTypes<P>
T = Record<string, any>, P extends ObjectProps<T> = ObjectProps<T>, D = ExtractPropTypes<P>, Defaults = ExtractDefaultPropTypes<P>
>(props: Partial<Defaults> & Omit<D, keyof Defaults>, propsOptions: P, propIn?: PropIn): D {
if (Array.isArray(propsOptions)) {
return props as D;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/shared/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import qs from "query-string";
import type { PolyfillHeaderInit } from "../polyfill";
import { decodeFormData, flatHeaders, getHeaderValue } from "../polyfill";
import { isString, isUint8Array, normalize } from "../utils";
import type { ExtractPropTypes, ObjectProps } from "./props";
import type { DefaultData, ExtractPropTypes, ExtractPropTypesWithIn, ObjectProps } from "./props";
import { validateProps, withDefaults } from "./props";
import type { FourzeRoute } from "./route";
import type { FourzeRouteMeta } from "./meta";
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface FourzeRequestOptions {
}

export interface FourzeRequest<
Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta, Data = ExtractPropTypes<Props>, Query = ExtractPropTypes<Props, "query">, Body = ExtractPropTypes<Props, "body">, Params = ExtractPropTypes<Props, "path">
Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta, Data = ExtractPropTypes<Props>, Query = ExtractPropTypesWithIn<Props, "query">, Body = ExtractPropTypesWithIn<Props, "body">, Params = ExtractPropTypesWithIn<Props, "path">
> extends IncomingMessage {
url: string
method: string
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/shared/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolves } from "../utils";
import type { ObjectProps } from "./props";
import type { DefaultData, ObjectProps } from "./props";
import type { RequestMethod } from "./request";
import { FOURZE_METHODS } from "./request";
import type { FourzeRouteMeta } from "./meta";
Expand All @@ -14,7 +14,7 @@ export interface FourzeRouteOptions<Props extends ObjectProps = ObjectProps, Met
}

export interface FourzeBaseRoute<
Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta
Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta
> {
path: string
method?: RequestMethod
Expand All @@ -24,7 +24,7 @@ export interface FourzeBaseRoute<
}

export interface FourzeRoute<
Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta
Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta
> extends FourzeBaseRoute<Result, Props, Meta> {
readonly [FOURZE_ROUTE_SYMBOL]: true
readonly props: Props
Expand All @@ -34,7 +34,7 @@ export interface FourzeRoute<
export type FourzeRouteGenerator<This> = {
[K in RequestMethod]: {
<
Result = any, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta
Result = any, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta
>(
path: string,
options: FourzeRouteOptions<Props, Meta>,
Expand All @@ -53,14 +53,14 @@ const REQUEST_PATH_REGEX = new RegExp(
"i"
);

export function defineRouteProps<Props extends ObjectProps = ObjectProps>(
export function defineRouteProps<Props extends ObjectProps = DefaultData>(
props: Props
) {
return props;
}

export function defineRoute<
Result = unknown, Props extends ObjectProps = ObjectProps, Meta = FourzeRouteMeta
Result = unknown, Props extends ObjectProps = DefaultData, Meta = FourzeRouteMeta
>(
route: FourzeBaseRoute<Result, Props, Meta> & { base?: string }
): FourzeRoute<Result, Props, Meta> {
Expand Down
10 changes: 10 additions & 0 deletions playgrounds/vite/src/fourze.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import "@fourze/core";
declare module "@fourze/core" {
interface FourzeRouteMeta{
title?: string
}
interface GlobalProps {
title?: string
}
}
5 changes: 2 additions & 3 deletions playgrounds/vite/src/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ export default defineRouter((router) => {
props: {
file: {
type: PolyfillFile,
required: true,
in: "body"
required: true
}
}
},
async (req) => {
const file = req.body.file;
const file = req.data.file;
if (!fs.existsSync(path.resolve(__dirname, ".tmp"))) {
fs.mkdirSync(path.resolve(__dirname, ".tmp"));
}
Expand Down

0 comments on commit 753094d

Please sign in to comment.