Skip to content

Commit

Permalink
feat: rename magic to codegenSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jul 27, 2023
1 parent 56133a1 commit 90eb489
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion demo/.stl-codegen/api/posts/create.ts
Expand Up @@ -9,7 +9,7 @@ export const Query: z.ZodTypeAny = z.object({
.optional(),
});
export const Body: z.ZodTypeAny = z.object({ body: z.string() });
export const post__api_posts: any = {
export const POST__api_posts: any = {
query: z.lazy(() => Query),
body: z.lazy(() => Body),
response: z.lazy(() => Models.PostType),
Expand Down
2 changes: 1 addition & 1 deletion demo/.stl-codegen/api/posts/retrieve.ts
Expand Up @@ -17,7 +17,7 @@ export const Query: z.ZodTypeAny = z.object({
export const Path: z.ZodTypeAny = z.object({
post: z.lazy(() => Models.PostLoader),
});
export const get__api_posts_$post$: any = {
export const GET__api_posts_$post$: any = {
query: z.lazy(() => Query),
path: z.lazy(() => Path),
response: z.lazy(() => Models.PostType),
Expand Down
4 changes: 2 additions & 2 deletions demo/.stl-codegen/index.ts
@@ -1,6 +1,6 @@
export const typeSchemas = {
"POST /api/posts": () =>
import("./api/posts/create.js").then((mod) => mod.post__api_posts),
import("./api/posts/create.js").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.js").then((mod) => mod.GET__api_posts_$post$),
};
2 changes: 1 addition & 1 deletion demo/api/posts/models.ts
Expand Up @@ -40,7 +40,7 @@ export class PostLoader extends PrismaModelLoader {
model = prisma.post;
}

export const Post = stl.magic<PostType>(PostTypeSchema);
export const Post = stl.codegenSchema<PostType>(PostTypeSchema);

export const PostSelection = Post.selection();

Expand Down
15 changes: 8 additions & 7 deletions packages/cli/src/index.ts
Expand Up @@ -258,7 +258,7 @@ async function evaluate(
const ctx = new ConvertTypeContext(baseCtx, file);
const imports = new Map<string, ImportInfo>();
const namespacedImports = new Map<string, NamespaceImportInfo>();
let hasMagicCall = false;
let hasCodegenSchemaCall = false;

for (const callExpression of file.getDescendantsOfKind(
ts.SyntaxKind.CallExpression
Expand All @@ -271,7 +271,8 @@ async function evaluate(

const methodName = symbol.getEscapedName();

if (!(methodName === "magic" || methodName === "endpoint")) continue;
if (!(methodName === "codegenSchema" || methodName === "endpoint"))
continue;

if (methodName == "endpoint") {
const call = preprocessEndpoint(callExpression);
Expand Down Expand Up @@ -313,10 +314,10 @@ async function evaluate(
continue;
}

// Handle stl.magic call
// Handle stl.codegenSchema call
const typeRefArguments = callExpression.getTypeArguments();
if (typeRefArguments.length != 1) continue;
hasMagicCall = true;
hasCodegenSchemaCall = true;

const [typeArgument] = typeRefArguments;

Expand Down Expand Up @@ -368,7 +369,7 @@ async function evaluate(
}
}

// remove all arguments to magic function
// remove all arguments to codegenSchema function
for (
let argumentLength = callExpression.getArguments().length;
argumentLength > 0;
Expand Down Expand Up @@ -396,7 +397,7 @@ async function evaluate(
processModuleIdentifiers(fileInfo);
}

if (!hasMagicCall) continue;
if (!hasCodegenSchemaCall) continue;

// Get the imports needed for the current file, any
if (fileInfo) {
Expand Down Expand Up @@ -505,7 +506,7 @@ async function evaluate(
for (const { filePath, line, column, diagnostics } of callDiagnostics) {
output.push(
chalk.magenta(
`While processing magic call at ${Path.relative(
`While processing codegenSchema call at ${Path.relative(
".",
filePath
)}:${line}:${column}:`
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/test_project/src/index.ts
Expand Up @@ -14,13 +14,13 @@ export enum EnumTest {

export class X {}

// stl.magic<X>(__class_X);
// stl.codegenSchema<X>(__class_X);

// stl.magic<ExternalInterface>(__symbol_ExternalInterface);
// stl.codegenSchema<ExternalInterface>(__symbol_ExternalInterface);

export class Test {}

// stl.magic<Test>(__class_Test);
// stl.codegenSchema<Test>(__class_Test);

type BadType = {
a: string;
Expand All @@ -29,13 +29,13 @@ type BadType = {

type Mapped = Partial<{ a: string }>;

// stl.magic<Partial<{a: string}>>(z.object({ a: z.string().optional() }));
// stl.codegenSchema<Partial<{a: string}>>(z.object({ a: z.string().optional() }));

type InThisFile = {
id: string;
};

stl.magic<{ nested: EnumTest; nested2: TestEnumAdditional }>(
stl.codegenSchema<{ nested: EnumTest; nested2: TestEnumAdditional }>(
z.object({
nested: z.lazy(() => EnumTestSchema),
nested2: z.lazy(() => TestEnumAdditionalSchema),
Expand All @@ -44,7 +44,7 @@ stl.magic<{ nested: EnumTest; nested2: TestEnumAdditional }>(

// stl.endpoint({
// endpoint: "GET /users",
// response: stl.magic<InThisFile>(__symbol_InThisFile),
// response: stl.codegenSchema<InThisFile>(__symbol_InThisFile),
// handler: (request, ctx) => {
// throw new Error("dummy");
// },
Expand Down
16 changes: 8 additions & 8 deletions packages/stainless/src/stl.ts
Expand Up @@ -397,9 +397,9 @@ export type CreateStlOptions<Plugins extends AnyPlugins> = {
*/
plugins: Plugins;
/**
* Injects generated magic schemas into the `stl` instance.
* Injects generated schemas into the `stl` instance.
* `typeSchemas` is exported from the module where you're configured
* magic schemas to generate in. By default, this is `stl-api-gen`.
* codegen schemas to generate in. By default, this is `stl-api-gen`.
*/
typeSchemas?: TypeSchemas;
};
Expand Down Expand Up @@ -678,7 +678,7 @@ export class Stl<Plugins extends AnyPlugins> {
) {
if (!this.typeSchemas) {
throw new Error(
"Failed to provide `typeSchemas` to stl instance while using magic schemas"
"Failed to provide `typeSchemas` to stl instance while using codegen schemas"
);
}
try {
Expand Down Expand Up @@ -974,17 +974,17 @@ export class Stl<Plugins extends AnyPlugins> {
* For more details on advanced conversion functionality,
* including adding custom validation and transformation logic
* to generated schemas, see the
* [magic type schema docs](https://stainlessapi.com/stl/schemas/schemas-from-types.md).
* [codegen schema docs](https://stainlessapi.com/stl/schemas/schemas-from-types.md).
*
* ## Example
* ```ts
* // invoke like this
* const partialSchema = stl.magic<Partial<{a: string}>>();
* const partialSchema = stl.codegenSchema<Partial<{a: string}>>();
* // `stl` converts this to
* const partialSchema = stl.magic<Partial<{a: string}>>(z.object({a: z.string().optional()}));
* const partialSchema = stl.codegenSchema<Partial<{a: string}>>(z.object({a: z.string().optional()}));
* ```
*/
magic<T>(schema: z.ZodTypeAny): t.toZod<T> {
codegenSchema<T>(schema: z.ZodTypeAny): t.toZod<T> {
return schema as any;
}

Expand Down Expand Up @@ -1012,7 +1012,7 @@ export class Stl<Plugins extends AnyPlugins> {
* For more details on advanced conversion functionality,
* including adding custom validation and transformation logic
* to generated schemas, see the
* [magic type schema docs](https://stainlessapi.com/stl/schemas/schemas-from-types.md).
* [codegen schema docs](https://stainlessapi.com/stl/schemas/schemas-from-types.md).
*
* ## Example
* ```ts
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-to-zod/src/convertType.ts
Expand Up @@ -204,7 +204,7 @@ export interface ImportInfo {
* `symbol`.
*/
importFromUserFile?: boolean;
/** Whether this import shouldn't be imported into a user file when using `stl.magic`. */
/** Whether this import shouldn't be imported into a user file when using `stl.codegenSchema`. */
excludeFromUserFile?: boolean;
}

Expand Down

0 comments on commit 90eb489

Please sign in to comment.