Skip to content

Commit ce3bdfd

Browse files
committed
feat(cli): add basepath var and extra imports
1 parent 9a99519 commit ce3bdfd

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

.changeset/friendly-radios-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ddadaal/next-typed-api-routes-cli": minor
3+
---
4+
5+
add option to customize basepath and extra imports

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ module.exports = {
159159
}
160160
```
161161

162+
You can also use `--basePathVar` option of `ntar` cli to customize the value of base path.
163+
164+
For example, when running `npx ntar client --basePathVar publicConfig.BASE_PATH`, the client file will look like:
165+
166+
```tsx
167+
import { fromApi } from "@ddadaal/next-typed-api-routes-runtime/lib/client";
168+
import { join } from "path";
169+
170+
import type { LoginSchema } from "src/pages/api/login/[username]";
171+
172+
// the value of basePath is the same as the basePathVar option
173+
const basePath = publicConfig.BASE_PATH;
174+
175+
export const api = {
176+
login: fromApi<LoginSchema>("GET", join(basePath, "/api/login/[username]")),
177+
};
178+
```
179+
180+
If your `basePath` declaration needs imports, you can also use `extraImports` cli option to add extra imports into the client file. Multiple `extraImports` options can be specified.
181+
162182
# Tips
163183

164184
- All schemas and used models must have globally unique name

example/src/apis/api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import { fromApi } from "@ddadaal/next-typed-api-routes-runtime/lib/client";
44
import { join } from "path";
5+
6+
57
import type { LoginSchema } from "src/pages/api/login/[username]";
68
import type { RegisterSchema } from "src/pages/api/register/index";
79

10+
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
11+
812

913
export const api = {
10-
login: fromApi<LoginSchema>("GET", join(process.env.NEXT_PUBLIC_BASE_PATH || "", "/api/login/[username]")),
11-
register: fromApi<RegisterSchema>("POST", join(process.env.NEXT_PUBLIC_BASE_PATH || "", "/api/register")),
14+
login: fromApi<LoginSchema>("GET", join(basePath, "/api/login/[username]")),
15+
register: fromApi<RegisterSchema>("POST", join(basePath, "/api/register")),
1216
};
17+

packages/cli/src/generateClients.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,17 @@ export interface GenerateApiClientsArgs {
107107
apiRoutesPath?: string;
108108
fetchImport?: string;
109109
apiObjectName?: string;
110+
basePathVar?: string;
111+
extraImports?: string[],
110112
}
111113

112114
export async function generateClients({
113115
apiFilePath = "src/apis/api.ts",
114116
apiRoutesPath = "src/pages/api",
115117
fetchImport = "@ddadaal/next-typed-api-routes-runtime/lib/client",
116118
apiObjectName = "api",
119+
basePathVar = "process.env.NEXT_PUBLIC_BASE_PATH || \"\"",
120+
extraImports = [],
117121
}: GenerateApiClientsArgs) {
118122

119123
if (!apiRoutesPath.endsWith("/")) {
@@ -128,12 +132,14 @@ export async function generateClients({
128132

129133
await getApiObject(apiRoutesPath, "", endpoints, imports);
130134

135+
const basePathVarDeclaration = `const basePath = ${basePathVar};`;
136+
131137
// use string instead of ts factories to easily style the code and reduce complexity
132138
const apiObjDeclaration = `
133139
export const ${apiObjectName} = {
134140
${endpoints.map((e) =>
135141
// eslint-disable-next-line max-len
136-
` ${e.schemaName}: fromApi<${e.interfaceName}>("${e.method}", join(process.env.NEXT_PUBLIC_BASE_PATH || "", "${e.url}")),`,
142+
` ${e.schemaName}: fromApi<${e.interfaceName}>("${e.method}", join(basePath, "${e.url}")),`,
137143
).join(EOL)}
138144
};
139145
`;
@@ -152,6 +158,10 @@ import { join } from "path";
152158
fetchApiImportDeclaration +
153159
EOL + EOL +
154160
importDeclarations +
161+
EOL + EOL
162+
extraImports.join(EOL) +
163+
EOL + EOL +
164+
basePathVarDeclaration +
155165
EOL + EOL +
156166
apiObjDeclaration;
157167

packages/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ yargs(hideBin(process.argv))
2727
default: "@ddadaal/next-typed-api-routes-runtime/lib/client",
2828
},
2929
apiObjectName: { type: "string", default: "api" },
30+
basePathVar: { type: "string", default: "process.env.NEXT_PUBLIC_BASE_PATH || \"\"" },
31+
extraImports: { type: "array", default: []},
3032
});
3133
}, (argv) => {
3234
generateClients(argv);

0 commit comments

Comments
 (0)