Skip to content

Commit

Permalink
Refine validator (#142)
Browse files Browse the repository at this point in the history
* Fix variable name

* Refine zod request validator

* createRequestValidator

* createResponseValidator

* createValidator and add test

* add test for validator method and path

* Add valibot validator test

* WIP: still have valibot type error and zod runtime test error

* Valibot以外のエラーを直した

* Fix valibot related errors

* Refactor
  • Loading branch information
mpppk authored Dec 10, 2024
1 parent 0a16c64 commit 85bea66
Show file tree
Hide file tree
Showing 16 changed files with 988 additions and 458 deletions.
57 changes: 48 additions & 9 deletions src/core/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export type CaseInsensitive<S extends string> = S | Uppercase<S> | Lowercase<S>;
export type CaseInsensitiveMethod = Method | Uppercase<Method>;
export const isMethod = (x: unknown): x is Method =>
Method.includes(x as Method);
export interface MethodInvalidError {
error: "MethodInvalid";
actual: string;
}
export const newMethodInvalidError = (method: string): MethodInvalidError => ({
error: "MethodInvalid",
actual: method,
});

export type ApiEndpoint = Partial<Record<Method, ApiSpec>>;
export type AnyApiEndpoint = Partial<Record<Method, AnyApiSpec>>;
Expand All @@ -42,6 +50,19 @@ export type UnknownApiEndpoints = {
[Path in string]: Partial<Record<Method, UnknownApiSpec>>;
};

export const apiSpecRequestKeys = Object.freeze([
"query",
"params",
"body",
"headers",
] as const);
export type ApiSpecRequestKey = (typeof apiSpecRequestKeys)[number];
export const apiSpecResponseKeys = Object.freeze(["body", "headers"] as const);
export type ApiSpecResponseKey = (typeof apiSpecResponseKeys)[number];
export const apiSpecKeys = Object.freeze([
...apiSpecRequestKeys,
"responses",
] as const);
export interface BaseApiSpec<
Params,
Query,
Expand Down Expand Up @@ -94,13 +115,13 @@ type AsJsonApiSpec<AS extends ApiSpec> = Omit<AS, "headers" | "resHeaders"> & {
};

export type ApiP<
E extends ApiEndpoints,
Path extends (keyof E & string) | C.AnyE,
M extends Method,
E extends AnyApiEndpoints,
Path extends string | C.AnyE,
M extends string,
P extends keyof ApiSpec,
> = Path extends keyof E & string
? E[Path] extends ApiEndpoint
? M extends Method
? E[Path] extends AnyApiEndpoint
? M extends keyof E[Path] & Method
? E[Path][M] extends ApiSpec<ParseUrlParams<Path>>
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
E[Path][M][P] extends Record<string, any> | string
Expand All @@ -111,6 +132,24 @@ export type ApiP<
: undefined
: undefined;

// TODO use InternalError instead of undefined
export type ApiResponses<
E extends AnyApiEndpoints,
Path extends string | C.AnyE,
M extends string,
> = Path extends keyof E & string
? E[Path] extends AnyApiEndpoint
? M extends keyof E[Path] & Method
? E[Path][M] extends AnyApiSpec
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
E[Path][M]["responses"] extends AnyApiResponses
? E[Path][M]["responses"]
: undefined
: undefined
: undefined
: undefined
: undefined;

export type ApiHasP<
E extends ApiEndpoints,
Path extends keyof E & string,
Expand All @@ -126,13 +165,13 @@ export type ApiHasP<
: never
: never;

export type ApiRes<
export type ApiResBody<
AResponses extends AnyApiResponses,
SC extends keyof AResponses & StatusCode,
SC extends keyof AResponses,
> = AResponses[SC] extends AnyResponse ? AResponses[SC]["body"] : undefined;
export type ApiResHeaders<
AResponses extends AnyApiResponses,
SC extends keyof AResponses & StatusCode,
SC extends keyof AResponses,
> = AResponses[SC] extends AnyResponse
? AResponses[SC]["headers"]
: Record<string, never>;
Expand All @@ -145,7 +184,7 @@ export type DefineResponse<Body, Headers> = { body: Body; headers?: Headers };
export type AnyResponse = DefineResponse<any, any>;
export type ApiClientResponses<AResponses extends AnyApiResponses> = {
[SC in keyof AResponses & StatusCode]: ClientResponse<
ApiRes<AResponses, SC>,
ApiResBody<AResponses, SC>,
SC,
"json",
ApiResHeaders<AResponses, SC>
Expand Down
34 changes: 0 additions & 34 deletions src/core/validate.test.ts

This file was deleted.

178 changes: 0 additions & 178 deletions src/core/validate.ts

This file was deleted.

Loading

0 comments on commit 85bea66

Please sign in to comment.