Skip to content

Commit 8f3762e

Browse files
committed
ApiEndpointsSchema
1 parent dccea1f commit 8f3762e

File tree

12 files changed

+64
-116
lines changed

12 files changed

+64
-116
lines changed

pkgs/typed-api-spec/src/core/openapi/openapi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ToOpenApiResponse,
1818
} from "./spec";
1919
import { StandardSchemaV1 } from "@standard-schema/spec";
20-
import { SSAnyApiResponse } from "../ss";
20+
import { ApiResponseSchema } from "../schema";
2121
import { toJsonSchemaApiEndpoints } from "../jsonschema";
2222

2323
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -26,7 +26,7 @@ export type SSOpenApiEndpoints = {
2626
[Path in string]: SSOpenApiEndpoint;
2727
};
2828
export type SSOpenApiEndpoint = DefineOpenApiEndpoint<SSOpenApiSpec>;
29-
export type SSAnyOpenApiResponse = ToOpenApiResponse<SSAnyApiResponse>;
29+
export type SSAnyOpenApiResponse = ToOpenApiResponse<ApiResponseSchema>;
3030
export type SSAnyOpenApiResponses =
3131
DefineOpenApiResponses<SSAnyOpenApiResponse>;
3232

pkgs/typed-api-spec/src/core/ss.t-test.ts pkgs/typed-api-spec/src/core/schema.t-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ToValidators,
99
Validators,
1010
} from ".";
11-
import { SSApiEndpoints } from "./ss";
11+
import { ApiEndpointsSchema } from "./schema";
1212

1313
const SSResponse = z.object({ a: z.string() });
1414
const SSEndpoints = {
@@ -20,7 +20,7 @@ const SSEndpoints = {
2020
},
2121
},
2222
},
23-
} satisfies SSApiEndpoints;
23+
} satisfies ApiEndpointsSchema;
2424

2525
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2626
type ToSSValidatorsTestCases = [

pkgs/typed-api-spec/src/core/ss.test.ts pkgs/typed-api-spec/src/core/schema.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, assert } from "vitest";
2-
import { SSApiEndpoints } from "./ss";
2+
import { ApiEndpointsSchema } from "./schema";
33
import * as v from "valibot";
44
import {
55
newValidator,
@@ -26,7 +26,7 @@ describe("newSSValidator", () => {
2626
},
2727
},
2828
},
29-
} satisfies SSApiEndpoints;
29+
} satisfies ApiEndpointsSchema;
3030

3131
const validReqInput = {
3232
path: "/",

pkgs/typed-api-spec/src/core/ss.ts pkgs/typed-api-spec/src/core/schema.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,41 @@ export type InferOrUndefined<T> = T extends StandardSchemaV1
1414
: undefined;
1515

1616
// -- spec --
17-
export type SSApiEndpoints = { [Path in string]: SSApiEndpoint };
18-
export type SSApiEndpoint = Partial<Record<Method, SSApiSpec>>;
19-
export type SSApiSpec<
17+
export type ApiEndpointsSchema = { [Path in string]: ApiEndpointSchema };
18+
export type ApiEndpointSchema = Partial<Record<Method, ApiSpecSchema>>;
19+
export type ApiSpecSchema<
2020
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2121
ParamKeys extends string = string,
2222
Params extends AnyStandardSchemaV1 = AnyStandardSchemaV1,
2323
Query extends AnyStandardSchemaV1 = AnyStandardSchemaV1,
2424
Body extends AnyStandardSchemaV1 = AnyStandardSchemaV1,
2525
RequestHeaders extends AnyStandardSchemaV1 = AnyStandardSchemaV1,
26-
Responses extends SSAnyApiResponses = SSAnyApiResponses,
26+
Responses extends ApiResponsesSchema = ApiResponsesSchema,
2727
> = BaseApiSpec<Params, Query, Body, RequestHeaders, Responses>;
28-
export type SSAnyApiResponse = DefineResponse<
28+
export type ApiResponseSchema = DefineResponse<
2929
StandardSchemaV1,
3030
StandardSchemaV1
3131
>;
32-
export type SSAnyApiResponses = DefineApiResponses<SSAnyApiResponse>;
32+
export type ApiResponsesSchema = DefineApiResponses<ApiResponseSchema>;
3333

3434
// -- converter --
35-
export type ToApiEndpoints<E extends SSApiEndpoints> = {
35+
export type ToApiEndpoints<E extends ApiEndpointsSchema> = {
3636
[Path in keyof E & string]: ToApiEndpoint<E, Path>;
3737
};
38-
export type ToApiEndpoint<E extends SSApiEndpoints, Path extends keyof E> = {
38+
export type ToApiEndpoint<
39+
E extends ApiEndpointsSchema,
40+
Path extends keyof E,
41+
> = {
3942
[M in keyof E[Path] & Method]: ToApiSpec<NonNullable<E[Path][M]>>;
4043
};
41-
export type ToApiSpec<ZAS extends SSApiSpec> = {
44+
export type ToApiSpec<ZAS extends ApiSpecSchema> = {
4245
query: InferOrUndefined<ZAS["query"]>;
4346
params: InferOrUndefined<ZAS["params"]>;
4447
body: InferOrUndefined<ZAS["body"]>;
4548
headers: InferOrUndefined<ZAS["headers"]>;
4649
responses: ToApiResponses<ZAS["responses"]>;
4750
};
48-
export type ToApiResponses<AR extends SSAnyApiResponses> = {
51+
export type ToApiResponses<AR extends ApiResponsesSchema> = {
4952
[SC in keyof AR & StatusCode]: {
5053
body: InferOrUndefined<NonNullable<AR[SC]>["body"]>;
5154
headers: InferOrUndefined<NonNullable<AR[SC]>["headers"]>;

pkgs/typed-api-spec/src/core/validator/validate.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
MethodInvalidError,
1212
newMethodInvalidError,
1313
} from "../spec";
14-
import { SSAnyApiResponses, SSApiSpec } from "../ss";
15-
import { SSApiEndpoints } from "../ss";
14+
import { ApiResponsesSchema, ApiSpecSchema } from "../schema";
15+
import { ApiEndpointsSchema } from "../schema";
1616
import {
1717
AnySpecValidator,
1818
listDefinedRequestApiSpecKeys,
@@ -39,7 +39,7 @@ export type Validator<V extends AnyStandardSchemaV1 | undefined> =
3939
: undefined;
4040

4141
export type Validators<
42-
AS extends SSApiSpec,
42+
AS extends ApiSpecSchema,
4343
// FIXME
4444
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4545
ParamKeys extends string,
@@ -58,15 +58,15 @@ export type ResponseValidators<
5858
Headers extends SS | undefined,
5959
> = ResponseSpecValidator<Validator<Body>, Validator<Headers>>;
6060
export type ToSSResponseValidators<
61-
Responses extends SSAnyApiResponses | undefined,
61+
Responses extends ApiResponsesSchema | undefined,
6262
SC extends number,
6363
> = ResponseValidators<
64-
Responses extends SSAnyApiResponses
64+
Responses extends ApiResponsesSchema
6565
? SC extends keyof Responses
6666
? ApiResBody<Responses, SC>
6767
: undefined
6868
: undefined,
69-
Responses extends SSAnyApiResponses
69+
Responses extends ApiResponsesSchema
7070
? SC extends keyof Responses
7171
? ApiResHeaders<Responses, SC> extends SS
7272
? ApiResHeaders<Responses, SC>
@@ -76,12 +76,12 @@ export type ToSSResponseValidators<
7676
>;
7777

7878
export type ToValidators<
79-
E extends SSApiEndpoints,
79+
E extends ApiEndpointsSchema,
8080
Path extends string,
8181
M extends string,
8282
> = Path extends keyof E
8383
? M extends keyof E[Path] & Method
84-
? E[Path][M] extends SSApiSpec
84+
? E[Path][M] extends ApiSpecSchema
8585
? Validators<E[Path][M], string>
8686
: Record<string, never>
8787
: Record<string, never>
@@ -159,7 +159,7 @@ type ValidatorInputPathNotFoundError = ReturnType<
159159
*
160160
* @param endpoints API endpoints
161161
*/
162-
export const newValidator = <E extends SSApiEndpoints>(endpoints: E) => {
162+
export const newValidator = <E extends ApiEndpointsSchema>(endpoints: E) => {
163163
const req = <Path extends string, M extends string>(
164164
input: SpecValidatorGeneratorRawInput<Path, M>,
165165
): Result<ToValidators<E, Path, M>, ValidatorInputError> => {

pkgs/typed-api-spec/src/express/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { newValidator, StatusCode, ToValidators } from "../core";
1616
import { ParsedQs } from "qs";
1717
import { AnySpecValidator } from "../core/validator/request";
1818
import { StandardSchemaV1 } from "@standard-schema/spec";
19-
import { SSApiEndpoints, ToApiEndpoints } from "../core/ss";
19+
import { ApiEndpointsSchema, ToApiEndpoints } from "../core/schema";
2020

2121
/**
2222
* Express Request Handler, but with more strict type information.
@@ -36,7 +36,7 @@ export type Handler<
3636
) => void;
3737

3838
export type ToHandler<
39-
E extends SSApiEndpoints,
39+
E extends ApiEndpointsSchema,
4040
Path extends keyof E & string,
4141
M extends Method,
4242
> = Handler<
@@ -48,7 +48,7 @@ export type ToHandler<
4848
>
4949
>;
5050

51-
export type ToHandlers<E extends SSApiEndpoints> = {
51+
export type ToHandlers<E extends ApiEndpointsSchema> = {
5252
[Path in keyof E & string]: {
5353
[M in Method]: ToHandler<E, Path, M>;
5454
};
@@ -93,7 +93,7 @@ export type RouterT<
9393
};
9494

9595
// eslint-disable-next-line @typescript-eslint/no-explicit-any
96-
export const validatorMiddleware = <const E extends SSApiEndpoints>(
96+
export const validatorMiddleware = <const E extends ApiEndpointsSchema>(
9797
pathMap: E,
9898
) => {
9999
return (_req: Request, res: Response, next: NextFunction) => {
@@ -229,7 +229,7 @@ export const asAsync = <Router extends IRouter | RouterT<any, any>>(
229229
* })
230230
* ```
231231
*/
232-
export const typed = <const Endpoints extends SSApiEndpoints>(
232+
export const typed = <const Endpoints extends ApiEndpointsSchema>(
233233
pathMap: Endpoints,
234234
router: Router,
235235
): RouterT<Endpoints> => {

pkgs/typed-api-spec/src/express/ss.ts

-62
This file was deleted.

pkgs/typed-api-spec/src/express/valibot.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import {
1616
newValidatorPathNotFoundError,
1717
Validators,
1818
} from "../core/validator/validate";
19-
import { SSApiEndpoints, SSApiSpec } from "../core/ss";
19+
import { ApiEndpointsSchema, ApiSpecSchema } from "../core/schema";
2020

2121
type SSValidateLocals<
22-
AS extends SSApiSpec,
22+
AS extends ApiSpecSchema,
2323
ParamKeys extends string,
2424
> = ValidateLocals<Validators<AS, ParamKeys>>;
2525

@@ -62,7 +62,7 @@ describe("valibot", () => {
6262
},
6363
},
6464
},
65-
} satisfies SSApiEndpoints;
65+
} satisfies ApiEndpointsSchema;
6666
// const { req: reqValidator } = newSSValidator(pathMap);
6767
// const middleware = validatorMiddleware(reqValidator);
6868
const middleware = validatorMiddleware(pathMap);
@@ -339,7 +339,7 @@ describe("valibot", () => {
339339
},
340340
},
341341
},
342-
} satisfies SSApiEndpoints;
342+
} satisfies ApiEndpointsSchema;
343343

344344
it("ok", async () => {
345345
const app = newApp();
@@ -467,7 +467,7 @@ describe("valibot", () => {
467467
},
468468
},
469469
},
470-
} satisfies SSApiEndpoints;
470+
} satisfies ApiEndpointsSchema;
471471

472472
// eslint-disable-next-line @typescript-eslint/no-unused-vars
473473
const getHandler: ToHandlers<typeof pathMap>["/users"]["get"] = async (

pkgs/typed-api-spec/src/express/zod.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import {
1616
newValidatorPathNotFoundError,
1717
Validators,
1818
} from "../core/validator/validate";
19-
import { SSApiEndpoints, SSApiSpec } from "../core/ss";
19+
import { ApiEndpointsSchema, ApiSpecSchema } from "../core/schema";
2020

2121
type SSValidateLocals<
22-
AS extends SSApiSpec,
22+
AS extends ApiSpecSchema,
2323
ParamKeys extends string,
2424
> = ValidateLocals<Validators<AS, ParamKeys>>;
2525

@@ -55,7 +55,7 @@ describe("validatorMiddleware", () => {
5555
},
5656
},
5757
},
58-
} satisfies SSApiEndpoints;
58+
} satisfies ApiEndpointsSchema;
5959

6060
const middleware = validatorMiddleware(pathMap);
6161
const next = vi.fn();
@@ -199,7 +199,7 @@ describe("validatorMiddleware", () => {
199199
expect(next).toHaveBeenCalled();
200200
expect(res.locals.validate).toEqual(expect.any(Function));
201201
const locals = res.locals as SSValidateLocals<
202-
SSApiSpec,
202+
ApiSpecSchema,
203203
ParseUrlParams<"">
204204
>;
205205
const validate = locals.validate(req as Request);
@@ -281,7 +281,7 @@ describe("typed", () => {
281281
},
282282
},
283283
},
284-
} satisfies SSApiEndpoints;
284+
} satisfies ApiEndpointsSchema;
285285

286286
it("ok", async () => {
287287
const app = newApp();
@@ -427,7 +427,7 @@ describe("Handler", () => {
427427
},
428428
},
429429
},
430-
} satisfies SSApiEndpoints;
430+
} satisfies ApiEndpointsSchema;
431431

432432
// eslint-disable-next-line @typescript-eslint/no-unused-vars
433433
const getHandler: ToHandlers<typeof pathMap>["/users"]["get"] = async (

0 commit comments

Comments
 (0)