Skip to content

Commit

Permalink
fix(cli): module generation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jul 28, 2023
1 parent d36b8f7 commit bef0ebb
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 116 deletions.
6 changes: 3 additions & 3 deletions demo/.stl-codegen/index.ts
@@ -1,8 +1,8 @@
export const typeSchemas = {
"GET /api/params/{id}": () =>
import("./api/params/retrieve.js").then((mod) => mod.GET__api_params_$id$),
import("./api/params/retrieve").then((mod) => mod.GET__api_params_$id$),
"POST /api/posts": () =>
import("./api/posts/create.js").then((mod) => mod.POST__api_posts),
import("./api/posts/create").then((mod) => mod.POST__api_posts),
"GET /api/posts/{post}": () =>
import("./api/posts/retrieve.js").then((mod) => mod.GET__api_posts_$post$),
import("./api/posts/retrieve").then((mod) => mod.GET__api_posts_$post$),
};
10 changes: 8 additions & 2 deletions packages/cli/src/index.ts
Expand Up @@ -42,7 +42,7 @@ import {
preprocessEndpoint,
} from "./endpointPreprocess";

import { program as argParser } from "commander";
import { program as argParser, Option } from "commander";
import {
convertPathToImport,
isSymbolStlMethod,
Expand All @@ -64,6 +64,11 @@ argParser.option(
"the directory in which to generate schemas",
".stl-codegen"
);
argParser.addOption(
new Option("-m, --module-type")
.choices(["module", "commonjs"])
.default("commonjs")
);
argParser.allowExcessArguments(false);

interface CallDiagnostics {
Expand Down Expand Up @@ -118,6 +123,7 @@ async function main() {
},
rootPath,
zPackage: "stainless",
moduleType: options.moduleType,
} as const;
const generationConfig = createGenerationConfig(generationOptions);

Expand Down Expand Up @@ -604,7 +610,7 @@ async function evaluate(
convertPathToImport(
generatePath(file.getFilePath(), generationConfig)
)
)}.js`
)}${generationConfig.moduleType === "module" ? ".js" : ""}`
),
]
);
Expand Down
17 changes: 10 additions & 7 deletions packages/ts-to-zod/src/convertType.ts
Expand Up @@ -311,13 +311,16 @@ export function prefixValueWithModule(
escapedName?: string
): ts.Expression {
const currentFileInfo = ctx.getFileInfo(currentFilePath);
return ctx.generateInUserFile ||
(!importFromUserFile && currentFilePath === valueFilePath)
? factory.createIdentifier(escapedName || name)
: factory.createPropertyAccessExpression(
getModuleIdentifier(currentFileInfo, valueFilePath, importFromUserFile),
name
);
if (ctx.generateInUserFile) {
return factory.createIdentifier(escapedName || name);
} else if (!importFromUserFile && currentFilePath === valueFilePath) {
return factory.createIdentifier(name);
} else {
return factory.createPropertyAccessExpression(
getModuleIdentifier(currentFileInfo, valueFilePath, importFromUserFile),
name
);
}
}

interface GeneratedSchema {
Expand Down
3 changes: 3 additions & 0 deletions packages/ts-to-zod/src/filePathConfig.ts
Expand Up @@ -15,6 +15,7 @@ export interface GenOptions {
rootPath: string;
/** the package from where to import the `z` constant */
zPackage?: string;
moduleType?: "commonjs" | "module";
}

// produced via the _
Expand All @@ -24,6 +25,7 @@ export interface GenerationConfig {
rootPath: string;
suffix?: string;
zPackage?: string;
moduleType: "commonjs" | "module";
}

export function createGenerationConfig(options: GenOptions): GenerationConfig {
Expand Down Expand Up @@ -52,5 +54,6 @@ export function createGenerationConfig(options: GenOptions): GenerationConfig {
suffix,
rootPath: options.rootPath,
zPackage: options.zPackage,
moduleType: options.moduleType || "commonjs",
};
}
304 changes: 207 additions & 97 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Expand Up @@ -2,4 +2,5 @@ packages:
- "packages/*"
- "packages/cli/test_project"
- "demo"
- "todomvc"
- "docs"
4 changes: 2 additions & 2 deletions todomvc/.stl-codegen/index.ts
@@ -1,5 +1,5 @@
export const typeSchemas = {
"GET /items/:item": () =>
import("./src/index.js").then((mod) => mod.GET__items_$58item),
"POST /items": () => import("./src/index.js").then((mod) => mod.POST__items),
import("./src/index").then((mod) => mod.GET__items_$58item),
"POST /items": () => import("./src/index").then((mod) => mod.POST__items),
};
2 changes: 1 addition & 1 deletion todomvc/.stl-codegen/src/index.ts
Expand Up @@ -5,7 +5,7 @@ export const ItemLoader: z.ZodTypeAny = z
.uuid()
.stlTransform(new Index.ItemLoader().transform);
export const RetrievePathParams: z.ZodTypeAny = z.object({
item: z.lazy(() => ItemLoaderSchema),
item: z.lazy(() => ItemLoader),
});
export const Item: z.ZodTypeAny = z.object({
completed: z.boolean(),
Expand Down
4 changes: 0 additions & 4 deletions todomvc/src/index.ts
@@ -1,9 +1,5 @@
import express, { Express, Request, Response } from "express";
import { Stl, NotFoundError, z } from "stainless";
import {
addStlEndpointToExpress,
stlExpressRouteHandler,
} from "@stl-api/express";
import * as crypto from "crypto";

import { ItemLoader as ItemLoaderSchema } from "../.stl-codegen/src/index";
Expand Down

0 comments on commit bef0ebb

Please sign in to comment.