Skip to content

Commit 5d44346

Browse files
committed
feat: rename route to staticRoute
1 parent ad2373c commit 5d44346

File tree

9 files changed

+27
-21
lines changed

9 files changed

+27
-21
lines changed

.changeset/perfect-turkeys-explode.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ddadaal/next-typed-api-routes-runtime": minor
3+
"@ddadaal/next-typed-api-routes-cli": minor
4+
---
5+
6+
rename `route` to `staticRoute`. Also export `route` for backward compatibility

example/src/apis/api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-len */
22

3-
import { fromApi, fromTypeboxRoute, fromZodRoute } from "@ddadaal/next-typed-api-routes-runtime/lib/client";
3+
import { fromStaticRoute, fromTypeboxRoute, fromZodRoute } from "@ddadaal/next-typed-api-routes-runtime/lib/client";
44
import { join } from "path";
55
import type { LoginSchema } from "src/pages/api/login/[username]";
66
import type { RegisterSchema } from "src/pages/api/register/index";
@@ -13,8 +13,8 @@ const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
1313

1414

1515
export const api = {
16-
login: fromApi<LoginSchema>("GET", join(basePath, "/api/login/[username]")),
17-
register: fromApi<RegisterSchema>("POST", join(basePath, "/api/register")),
16+
login: fromStaticRoute<LoginSchema>("GET", join(basePath, "/api/login/[username]")),
17+
register: fromStaticRoute<RegisterSchema>("POST", join(basePath, "/api/register")),
1818
typeboxRoute: fromTypeboxRoute<typeof TypeboxRouteSchema>("POST", join(basePath, "/api/typeboxRoute/[test]")),
1919
zodRoute: fromZodRoute<typeof ZodRouteSchema>("POST", join(basePath, "/api/zodRoute/[test]")),
2020
};

example/src/pages/api/login/[username].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2-
import { route } from "@ddadaal/next-typed-api-routes-runtime";
2+
import { staticRoute } from "@ddadaal/next-typed-api-routes-runtime";
33

44
/** You can use types anywhere. Recommend to use import type */
55
import type { LoginInfo } from "../../../models/LoginInfo";
@@ -19,7 +19,7 @@ export interface LoginSchema {
1919

2020
}
2121

22-
export default route<LoginSchema>("LoginSchema", (req) => {
22+
export default staticRoute<LoginSchema>("LoginSchema", (req) => {
2323
const { password, username } = req.query;
2424

2525
if (username === password) {

example/src/pages/api/register/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2-
import { route } from "@ddadaal/next-typed-api-routes-runtime";
2+
import { staticRoute } from "@ddadaal/next-typed-api-routes-runtime";
33

44
export interface RegisterSchema {
55
method: "POST";
@@ -19,7 +19,7 @@ export interface RegisterSchema {
1919

2020
}
2121

22-
export default route<RegisterSchema>("RegisterSchema", (req) => {
22+
export default staticRoute<RegisterSchema>("RegisterSchema", (req) => {
2323
const { password, username } = req.body;
2424

2525
return { 200: { token: username + password } };

packages/cli/src/generateClients.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface Import {
2424
export const ApiTypes = {
2525
zod: { libImport: "fromZodRoute", typeFormat: (typeName: string) => `typeof ${typeName}` },
2626
typebox: { libImport: "fromTypeboxRoute", typeFormat: (typeName: string) => `typeof ${typeName}` },
27-
ajv: { libImport: "fromApi", typeFormat: (typeName: string) => typeName },
27+
static: { libImport: "fromStaticRoute", typeFormat: (typeName: string) => typeName },
2828
};
2929

3030
export type ApiType = keyof typeof ApiTypes;
@@ -117,7 +117,7 @@ async function getApiObject(
117117
found = {
118118
typeName: interfaceName,
119119
method: s.type.literal.text,
120-
apiType: "ajv",
120+
apiType: "static",
121121
};
122122

123123
break;

packages/runtime/src/fetch/fetch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export function jsonFetch<T extends AnySchema>(
219219

220220
export type JsonFetch = typeof jsonFetch;
221221

222-
export function fromApi<TSchema extends AnySchema>(method: HttpMethod, url: string) {
222+
export function fromStaticRoute<TSchema extends AnySchema>(method: HttpMethod, url: string) {
223223
return function(
224224
args: RequestArgs<TSchema>,
225225
signal?: AbortSignal,
@@ -241,10 +241,10 @@ export function fromApi<TSchema extends AnySchema>(method: HttpMethod, url: stri
241241
}
242242

243243
export function fromZodRoute<TSchema extends ZodRouteSchema>(method: HttpMethod, url: string) {
244-
return fromApi<ZodRouteSchemaToSchema<TSchema>>(method, url);
244+
return fromStaticRoute<ZodRouteSchemaToSchema<TSchema>>(method, url);
245245
}
246246

247247
export function fromTypeboxRoute<TSchema extends TypeboxRouteSchema>(method: HttpMethod, url: string) {
248-
return fromApi<TypeboxRouteSchemaToSchema<TSchema>>(method, url);
248+
return fromStaticRoute<TypeboxRouteSchemaToSchema<TSchema>>(method, url);
249249
}
250250

packages/runtime/src/route/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { createValidatorsFromSchema, route, Validator } from "./ajvRoute";
1+
export { createValidatorsFromStaticSchema, staticRoute as route, staticRoute, StaticValidator } from "./staticRoute";
22
export { typeboxRoute, TypeboxRouteSchema, typeboxRouteSchema, TypeboxRouteSchemaToSchema } from "./typeboxRoute";
33
export { zodRoute, zodRouteSchema } from "./zodRoute";
44
export { Type } from "@sinclair/typebox";

packages/runtime/src/route/ajvRoute.ts renamed to packages/runtime/src/route/staticRoute.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ajvOptions, createAjv } from "./ajv";
88
import { OrPromise, returnError, Serializer, ValueOf } from "./utils";
99

1010

11-
export interface Validator {
11+
export interface StaticValidator {
1212
query?: ValidateFunction;
1313
body?: ValidateFunction;
1414
responseSerializers?: Map<string, Serializer>;
@@ -19,7 +19,7 @@ interface SchemaFileContent {
1919
routes: Record<string, SchemaObject>;
2020
}
2121

22-
export function createValidatorsFromSchema(schemas: SchemaFileContent) {
22+
export function createValidatorsFromStaticSchema(schemas: SchemaFileContent) {
2323

2424
const ajv = createAjv();
2525

@@ -29,7 +29,7 @@ export function createValidatorsFromSchema(schemas: SchemaFileContent) {
2929
}
3030

3131
// compile validatiors
32-
const routeValiators = new Map<string, Validator>();
32+
const routeValiators = new Map<string, StaticValidator>();
3333

3434
for (const [name, schema] of Object.entries(schemas.routes)) {
3535
const query = schema.properties.query && ajv.compile(schema.properties.query);
@@ -58,11 +58,11 @@ export function createValidatorsFromSchema(schemas: SchemaFileContent) {
5858
}
5959

6060

61-
let validators: Map<string, Validator>;
61+
let validators: Map<string, StaticValidator>;
6262

6363

6464

65-
export function route<S extends AnySchema>(
65+
export function staticRoute<S extends AnySchema>(
6666
schemaName: string,
6767
handler: (
6868
req: Omit<NextApiRequest, "body"> & {
@@ -75,7 +75,7 @@ export function route<S extends AnySchema>(
7575

7676
if (!validators) {
7777
const schemas = JSON.parse(fs.readFileSync("./api-routes-schemas.json", "utf8"));
78-
validators = createValidatorsFromSchema(schemas);
78+
validators = createValidatorsFromStaticSchema(schemas);
7979
}
8080

8181
const validator = validators.get(schemaName);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createValidatorsFromSchema } from "../src/route/ajvRoute";
1+
import { createValidatorsFromStaticSchema } from "../src/route/staticRoute";
22
import schemas from "./schemas.json";
33

44
it("should correctly generate schemas", () => {
5-
createValidatorsFromSchema(schemas);
5+
createValidatorsFromStaticSchema(schemas);
66
});

0 commit comments

Comments
 (0)