Skip to content

Commit cb4d68f

Browse files
authored
feat: Allow generating code without a prefix (#50)
* Allow generating code without a prefix Signed-off-by: Alexis Rico <[email protected]> * Use empty string instead of null Signed-off-by: Alexis Rico <[email protected]>
1 parent b1db5c1 commit cb4d68f

File tree

6 files changed

+170
-5
lines changed

6 files changed

+170
-5
lines changed

plugins/typescript/src/generators/generateFetchers.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,39 @@ describe("generateFetchers", () => {
7777
`);
7878
});
7979

80+
it("should genarate fetchers without prefix", async () => {
81+
const writeFile = jest.fn();
82+
83+
await generateFetchers(
84+
{
85+
openAPIDocument,
86+
writeFile,
87+
readFile: async () => "",
88+
existsFile: () => true,
89+
},
90+
{ ...config, filenamePrefix: "" }
91+
);
92+
93+
expect(writeFile.mock.calls[0][0]).toBe("components.ts");
94+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
95+
"/**
96+
* Generated by @openapi-codegen
97+
*
98+
* @version 1.0.0
99+
*/
100+
import { fetch } from \\"./fetcher\\";
101+
import type * as Schemas from \\"./petstoreSchemas\\";
102+
103+
export type ListPetsResponse = Schemas.Pet[];
104+
105+
/**
106+
* Get all the pets
107+
*/
108+
export const listPets = () => fetch<ListPetsResponse, undefined, {}, {}, {}>({ url: \\"/pets\\", method: \\"get\\" });
109+
"
110+
`);
111+
});
112+
80113
it("should generate fetcher with injected props", async () => {
81114
const writeFile = jest.fn();
82115

plugins/typescript/src/generators/generateFetchers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ts, { factory as f } from "typescript";
22
import * as c from "case";
3+
import { get } from "lodash";
34

45
import { ConfigBase, Context } from "./types";
56
import { PathItemObject } from "openapi3-ts";
@@ -13,7 +14,6 @@ import { getOperationTypes } from "../core/getOperationTypes";
1314
import { createNamedImport } from "../core/createNamedImport";
1415

1516
import { getFetcher } from "../templates/fetcher";
16-
import { get } from "lodash";
1717

1818
export type Config = ConfigBase & {
1919
/**
@@ -61,7 +61,8 @@ export const generateFetchers = async (context: Context, config: Config) => {
6161
.join("\n");
6262

6363
const filenamePrefix =
64-
c.snake(config.filenamePrefix || context.openAPIDocument.info.title) + "-";
64+
c.snake(config.filenamePrefix ?? context.openAPIDocument.info.title) + "-";
65+
6566
const formatFilename = config.filenameCase ? c[config.filenameCase] : c.camel;
6667

6768
const filename = formatFilename(filenamePrefix + "-components");

plugins/typescript/src/generators/generateReactQueryComponents.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("generateReactQueryComponents", () => {
3131
openAPIDocument,
3232
writeFile,
3333
existsFile: () => false, // customFetcher is not there
34+
readFile: async () => "",
3435
},
3536
config
3637
);
@@ -76,6 +77,7 @@ describe("generateReactQueryComponents", () => {
7677
openAPIDocument,
7778
writeFile,
7879
existsFile: () => true,
80+
readFile: async () => "",
7981
},
8082
config
8183
);
@@ -175,6 +177,7 @@ describe("generateReactQueryComponents", () => {
175177
openAPIDocument,
176178
writeFile,
177179
existsFile: () => true,
180+
readFile: async () => "",
178181
},
179182
config
180183
);
@@ -287,6 +290,7 @@ describe("generateReactQueryComponents", () => {
287290
openAPIDocument,
288291
writeFile,
289292
existsFile: () => true,
293+
readFile: async () => "",
290294
},
291295
{ ...config, injectedHeaders: ["breed"] }
292296
);
@@ -391,6 +395,7 @@ describe("generateReactQueryComponents", () => {
391395
openAPIDocument,
392396
writeFile,
393397
existsFile: () => true,
398+
readFile: async () => "",
394399
},
395400
config
396401
);
@@ -505,6 +510,7 @@ describe("generateReactQueryComponents", () => {
505510
openAPIDocument,
506511
writeFile,
507512
existsFile: () => true,
513+
readFile: async () => "",
508514
},
509515
config
510516
);
@@ -625,6 +631,7 @@ describe("generateReactQueryComponents", () => {
625631
openAPIDocument,
626632
writeFile,
627633
existsFile: () => true,
634+
readFile: async () => "",
628635
},
629636
config
630637
);
@@ -751,6 +758,7 @@ describe("generateReactQueryComponents", () => {
751758
openAPIDocument,
752759
writeFile,
753760
existsFile: () => true,
761+
readFile: async () => "",
754762
},
755763
config
756764
);
@@ -850,6 +858,7 @@ describe("generateReactQueryComponents", () => {
850858
openAPIDocument,
851859
writeFile,
852860
existsFile: () => true,
861+
readFile: async () => "",
853862
},
854863
config
855864
);
@@ -884,4 +893,104 @@ describe("generateReactQueryComponents", () => {
884893
"
885894
`);
886895
});
896+
897+
it("should build components without prefix", async () => {
898+
const writeFile = jest.fn();
899+
const openAPIDocument: OpenAPIObject = {
900+
openapi: "3.0.0",
901+
info: {
902+
title: "petshop",
903+
version: "1.0.0",
904+
},
905+
components: {
906+
requestBodies: {
907+
UpdatePetRequestBody: {
908+
content: {
909+
"application/json": {
910+
schema: {
911+
type: "object",
912+
properties: {
913+
name: {
914+
type: "string",
915+
},
916+
},
917+
},
918+
},
919+
},
920+
},
921+
},
922+
},
923+
paths: {
924+
"/pet/{pet_id}": {
925+
parameters: [
926+
{
927+
in: "path",
928+
name: "pet_id",
929+
schema: {
930+
type: "string",
931+
},
932+
required: true,
933+
},
934+
],
935+
put: {
936+
operationId: "updatePet",
937+
requestBody: {
938+
$ref: "#/components/requestBodies/UpdatePetRequestBody",
939+
},
940+
responses: {
941+
200: {
942+
content: {
943+
"application/json": {
944+
description: "Successful response",
945+
schema: {
946+
type: "string",
947+
},
948+
},
949+
},
950+
},
951+
},
952+
},
953+
},
954+
},
955+
};
956+
await generateReactQueryComponents(
957+
{
958+
openAPIDocument,
959+
writeFile,
960+
existsFile: () => true,
961+
readFile: async () => "",
962+
},
963+
{ ...config, filenamePrefix: "" }
964+
);
965+
966+
expect(writeFile.mock.calls[0][0]).toBe("components.ts");
967+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
968+
"/**
969+
* Generated by @openapi-codegen
970+
*
971+
* @version 1.0.0
972+
*/
973+
import * as reactQuery from \\"react-query\\";
974+
import { useContext, Context } from \\"./context\\";
975+
import { fetch } from \\"./fetcher\\";
976+
import type * as RequestBodies from \\"./petstoreRequestBodies\\";
977+
978+
export type UpdatePetPathParams = {
979+
petId: string;
980+
};
981+
982+
export type UpdatePetVariables = {
983+
body?: RequestBodies.UpdatePetRequestBody;
984+
pathParams: UpdatePetPathParams;
985+
} & Context[\\"fetcherOptions\\"];
986+
987+
export const fetchUpdatePet = (variables: UpdatePetVariables) => fetch<string, RequestBodies.UpdatePetRequestBody, {}, {}, UpdatePetPathParams>({ url: \\"/pet/{petId}\\", method: \\"put\\", ...variables });
988+
989+
export const useUpdatePet = (options?: Omit<reactQuery.UseMutationOptions<string, undefined, UpdatePetVariables>, \\"mutationFn\\">) => {
990+
const { fetcherOptions } = useContext();
991+
return reactQuery.useMutation<string, undefined, UpdatePetVariables>((variables: UpdatePetVariables) => fetchUpdatePet({ ...fetcherOptions, ...variables }), options);
992+
};
993+
"
994+
`);
995+
});
887996
});

plugins/typescript/src/generators/generateReactQueryComponents.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ts, { factory as f } from "typescript";
22
import * as c from "case";
3+
import { get } from "lodash";
34

45
import { ConfigBase, Context } from "./types";
56
import { OperationObject, PathItemObject } from "openapi3-ts";
@@ -14,7 +15,6 @@ import { createNamedImport } from "../core/createNamedImport";
1415

1516
import { getFetcher } from "../templates/fetcher";
1617
import { getContext } from "../templates/context";
17-
import { get } from "lodash";
1818

1919
export type Config = ConfigBase & {
2020
/**
@@ -65,7 +65,8 @@ export const generateReactQueryComponents = async (
6565
.join("\n");
6666

6767
const filenamePrefix =
68-
c.snake(config.filenamePrefix || context.openAPIDocument.info.title) + "-";
68+
c.snake(config.filenamePrefix ?? context.openAPIDocument.info.title) + "-";
69+
6970
const formatFilename = config.filenameCase ? c[config.filenameCase] : c.camel;
7071

7172
const filename = formatFilename(filenamePrefix + "-components");

plugins/typescript/src/generators/generateSchemaTypes.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ describe("generateSchemaTypes", () => {
9696
});
9797
});
9898

99+
describe("without filenamePrefix option", () => {
100+
it("should not have a prefix", () => {
101+
const writeFile = jest.fn();
102+
const readFile = jest.fn(() => Promise.resolve(""));
103+
generateSchemaTypes(
104+
{
105+
openAPIDocument: petstore,
106+
writeFile,
107+
readFile,
108+
existsFile: () => true,
109+
},
110+
{
111+
filenameCase: "camel",
112+
filenamePrefix: "",
113+
}
114+
);
115+
expect(writeFile.mock.calls[0][0]).toBe("schemas.ts");
116+
});
117+
});
118+
99119
describe("schemas file generation", () => {
100120
it("should generate the schemas file", async () => {
101121
const writeFile = jest.fn();

plugins/typescript/src/generators/generateSchemaTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export const generateSchemaTypes = async (
4646
.join("\n");
4747

4848
const filenamePrefix =
49-
c.snake(config.filenamePrefix || context.openAPIDocument.info.title) + "-";
49+
c.snake(config.filenamePrefix ?? context.openAPIDocument.info.title) + "-";
50+
5051
const formatFilename = config.filenameCase ? c[config.filenameCase] : c.camel;
5152
const files = {
5253
requestBodies: formatFilename(filenamePrefix + "-request-bodies"),

0 commit comments

Comments
 (0)