|
1 | 1 | import {
|
| 2 | + AnyResponse, |
2 | 3 | ApiResBody,
|
3 | 4 | ApiResHeaders,
|
4 |
| - ApiResponses, |
5 | 5 | BaseApiSpec,
|
6 | 6 | DefineApiResponses,
|
7 | 7 | DefineResponse,
|
8 | 8 | Method,
|
9 | 9 | StatusCode,
|
10 | 10 | } from "../core";
|
11 | 11 | import {
|
12 |
| - createValidator, |
| 12 | + checkValidatorsInput, |
13 | 13 | Validator,
|
14 | 14 | ValidatorInputError,
|
15 | 15 | } from "../core/validator/validate";
|
16 | 16 | import { Result } from "../utils";
|
17 | 17 | import {
|
| 18 | + AnySpecValidator, |
| 19 | + listDefinedRequestApiSpecKeys, |
18 | 20 | SpecValidator,
|
19 | 21 | SpecValidatorGeneratorRawInput,
|
20 | 22 | } from "../core/validator/request";
|
21 | 23 | import {
|
| 24 | + AnyResponseSpecValidator, |
| 25 | + listDefinedResponseApiSpecKeys, |
22 | 26 | ResponseSpecValidator,
|
23 |
| - ResponseSpecValidatorGeneratorRawInput, |
| 27 | + ResponseSpecValidatorGeneratorInput, |
24 | 28 | } from "../core/validator/response";
|
25 | 29 | import { StandardSchemaV1 } from "@standard-schema/spec";
|
26 | 30 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -118,46 +122,69 @@ export type ToApiResponses<AR extends SSAnyApiResponses> = {
|
118 | 122 | };
|
119 | 123 | };
|
120 | 124 |
|
121 |
| -type SSRequestValidatorsGenerator<E extends SSApiEndpoints> = < |
122 |
| - Path extends string, |
123 |
| - M extends string, |
124 |
| ->( |
125 |
| - input: SpecValidatorGeneratorRawInput<Path, M>, |
126 |
| -) => Result<ToSSValidators<E, Path, M>, ValidatorInputError>; |
127 |
| -type SSResponseValidatorsGenerator<E extends SSApiEndpoints> = < |
128 |
| - Path extends string, |
129 |
| - M extends string, |
130 |
| - SC extends number, |
131 |
| ->( |
132 |
| - input: ResponseSpecValidatorGeneratorRawInput<Path, M, SC>, |
133 |
| -) => Result< |
134 |
| - ToSSResponseValidators<ApiResponses<E, Path, M>, SC>, |
135 |
| - ValidatorInputError |
136 |
| ->; |
137 |
| - |
138 | 125 | /**
|
139 | 126 | * Create a new validator for the given endpoints.
|
140 | 127 | *
|
141 | 128 | * @param endpoints API endpoints
|
142 | 129 | */
|
143 | 130 | export const newSSValidator = <E extends SSApiEndpoints>(endpoints: E) => {
|
144 |
| - return createValidator( |
145 |
| - endpoints, |
146 |
| - async (spec: SSApiSpec, input, key) => { |
147 |
| - let r = spec[key]!["~standard"].validate(input[key]); |
148 |
| - if (r instanceof Promise) r = await r; |
149 |
| - return r; |
150 |
| - }, |
151 |
| - async (spec: SSApiSpec, input, key) => { |
152 |
| - const schema = spec["responses"][input.statusCode as StatusCode]?.[key]; |
153 |
| - let r = schema!["~standard"].validate(input[key]); |
154 |
| - if (r instanceof Promise) r = await r; |
155 |
| - return r; |
156 |
| - }, |
157 |
| - ) as { |
158 |
| - req: SSRequestValidatorsGenerator<E>; |
159 |
| - res: SSResponseValidatorsGenerator<E>; |
| 131 | + const req = ( |
| 132 | + input: SpecValidatorGeneratorRawInput<string, string>, |
| 133 | + ): Result<AnySpecValidator, ValidatorInputError> => { |
| 134 | + const { data: vInput, error } = checkValidatorsInput(endpoints, input); |
| 135 | + if (error) { |
| 136 | + return Result.error(error); |
| 137 | + } |
| 138 | + const validators: AnySpecValidator = {}; |
| 139 | + const spec = endpoints[vInput.path][vInput.method]!; |
| 140 | + listDefinedRequestApiSpecKeys(spec).forEach((key) => { |
| 141 | + validators[key] = async () => { |
| 142 | + let r = spec[key]!["~standard"].validate(input[key]); |
| 143 | + if (r instanceof Promise) r = await r; |
| 144 | + return r; |
| 145 | + }; |
| 146 | + }); |
| 147 | + return Result.data(validators); |
| 148 | + }; |
| 149 | + const res = ( |
| 150 | + input: ResponseSpecValidatorGeneratorInput<string, Method, StatusCode>, |
| 151 | + ): Result<AnyResponseSpecValidator, ValidatorInputError> => { |
| 152 | + const { data: vInput, error } = checkValidatorsInput(endpoints, input); |
| 153 | + if (error) { |
| 154 | + return Result.error(error); |
| 155 | + } |
| 156 | + const validator: AnySpecValidator = {}; |
| 157 | + const spec = endpoints[vInput.path][vInput.method]!; |
| 158 | + const response = |
| 159 | + spec?.responses?.[input.statusCode as StatusCode] ?? ({} as AnyResponse); |
| 160 | + listDefinedResponseApiSpecKeys(response).forEach((key) => { |
| 161 | + validator[key] = async () => { |
| 162 | + const schema = spec["responses"][input.statusCode as StatusCode]?.[key]; |
| 163 | + let r = schema!["~standard"].validate(input[key]); |
| 164 | + if (r instanceof Promise) r = await r; |
| 165 | + return r; |
| 166 | + }; |
| 167 | + }); |
| 168 | + return Result.data(validator); |
160 | 169 | };
|
| 170 | + return { req, res }; |
| 171 | + // return createValidator( |
| 172 | + // endpoints, |
| 173 | + // async (spec: SSApiSpec, input, key) => { |
| 174 | + // let r = spec[key]!["~standard"].validate(input[key]); |
| 175 | + // if (r instanceof Promise) r = await r; |
| 176 | + // return r; |
| 177 | + // }, |
| 178 | + // async (spec: SSApiSpec, input, key) => { |
| 179 | + // const schema = spec["responses"][input.statusCode as StatusCode]?.[key]; |
| 180 | + // let r = schema!["~standard"].validate(input[key]); |
| 181 | + // if (r instanceof Promise) r = await r; |
| 182 | + // return r; |
| 183 | + // }, |
| 184 | + // ) as { |
| 185 | + // req: SSRequestValidatorsGenerator<E>; |
| 186 | + // res: SSResponseValidatorsGenerator<E>; |
| 187 | + // }; |
161 | 188 | };
|
162 | 189 |
|
163 | 190 | // const toResult = <T>(
|
|
0 commit comments