Skip to content

Commit 8f9f0fb

Browse files
committed
Export schema types through the components["schemas"]
Until this change, it had attempted to export the concrete type by `#getText()` but it doesn't work certainly as expected in some cases. So this commit changes it to export them through `components` module. Signed-off-by: moznion <moznion@mail.moznion.net>
1 parent 83c851f commit 8f9f0fb

File tree

2 files changed

+21
-104
lines changed

2 files changed

+21
-104
lines changed

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function generateClientCode(
6868
"// DO NOT EDIT THIS FILE MANUALLY.",
6969
"// See Also: https://github.com/moznion/openapi-fetch-gen",
7070
`import createClient, { type ClientOptions } from "openapi-fetch";`,
71-
`import type { paths } from "./${schemaFileName}"; // generated by openapi-typescript`,
71+
`import type { components, paths } from "./${schemaFileName}"; // generated by openapi-typescript`,
7272
"",
7373
clientClass,
7474
"",
@@ -92,10 +92,13 @@ function generateSchemaTypes(
9292
for (const schema of schemasType.getProperties()) {
9393
const schemaType = schema.getValueDeclaration()?.getType();
9494
if (schemaType) {
95-
const schemaName = (
96-
schema.getName() === "Client" ? "ClientSchema" : schema.getName()
95+
const schemaName = schema.getName();
96+
const schemaTypeName = (
97+
schemaName + (schemaName === "Client" ? "Schema" : "")
9798
).replaceAll(/-/g, "_");
98-
types.push(`export type ${schemaName} = ${schemaType.getText()};`);
99+
types.push(
100+
`export type ${schemaTypeName} = components["schemas"]["${schema.getName()}"];`,
101+
);
99102
}
100103
}
101104
}

src/test_resources/expected_client.ts

Lines changed: 14 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// DO NOT EDIT THIS FILE MANUALLY.
33
// See Also: https://github.com/moznion/openapi-fetch-gen
44
import createClient, { type ClientOptions } from "openapi-fetch";
5-
import type { paths } from "./schema.d.ts"; // generated by openapi-typescript
5+
import type { components, paths } from "./schema.d.ts"; // generated by openapi-typescript
66

77
export class Client<HT extends Record<string, string>> {
88
private readonly client;
@@ -237,102 +237,16 @@ export class Client<HT extends Record<string, string>> {
237237
}
238238
}
239239

240-
export type Error = { code: number; message: string };
241-
export type User = {
242-
id: string;
243-
name: string;
244-
email: string;
245-
membershipType: "REGULAR" | "PREMIUM" | "STUDENT";
246-
registeredAt: string;
247-
address?: {
248-
postalCode?: string;
249-
street: string;
250-
city: string;
251-
country: string;
252-
};
253-
};
254-
export type Address = {
255-
postalCode?: string;
256-
street: string;
257-
city: string;
258-
country: string;
259-
};
260-
export type UserCreate = {
261-
name: string;
262-
email: string;
263-
membershipType: "REGULAR" | "PREMIUM" | "STUDENT";
264-
address?: {
265-
postalCode?: string;
266-
street: string;
267-
city: string;
268-
country: string;
269-
};
270-
};
271-
export type UserUpdate = {
272-
name: string;
273-
email: string;
274-
membershipType: "REGULAR" | "PREMIUM" | "STUDENT";
275-
address?: {
276-
postalCode?: string;
277-
street: string;
278-
city: string;
279-
country: string;
280-
};
281-
} & { id: string };
282-
export type UserPatch = {
283-
name?: string;
284-
email?: string;
285-
membershipType?: "REGULAR" | "PREMIUM" | "STUDENT";
286-
address?: {
287-
postalCode?: string;
288-
street: string;
289-
city: string;
290-
country: string;
291-
};
292-
};
293-
export type UserList = {
294-
id: string;
295-
name: string;
296-
email: string;
297-
membershipType: "REGULAR" | "PREMIUM" | "STUDENT";
298-
registeredAt: string;
299-
address?: {
300-
postalCode?: string;
301-
street: string;
302-
city: string;
303-
country: string;
304-
};
305-
}[];
306-
export type UserPage = {
307-
total: number;
308-
page: number;
309-
pageSize: number;
310-
items: {
311-
id: string;
312-
name: string;
313-
email: string;
314-
membershipType: "REGULAR" | "PREMIUM" | "STUDENT";
315-
registeredAt: string;
316-
address?: {
317-
postalCode?: string;
318-
street: string;
319-
city: string;
320-
country: string;
321-
};
322-
}[];
323-
};
324-
export type Loan = {
325-
loanId: string;
326-
book: { isbn: string; title: string; author: string };
327-
borrowedAt: string;
328-
dueAt: string;
329-
};
330-
export type Book = { isbn: string; title: string; author: string };
331-
export type BulkJobStatus = {
332-
jobId: string;
333-
status: "PENDING" | "PROCESSING" | "COMPLETED" | "FAILED";
334-
processed: number;
335-
total: number;
336-
};
337-
export type ClientSchema = { id?: string };
338-
export type schema_Something = { id?: string };
240+
export type Error = components["schemas"]["Error"];
241+
export type User = components["schemas"]["User"];
242+
export type Address = components["schemas"]["Address"];
243+
export type UserCreate = components["schemas"]["UserCreate"];
244+
export type UserUpdate = components["schemas"]["UserUpdate"];
245+
export type UserPatch = components["schemas"]["UserPatch"];
246+
export type UserList = components["schemas"]["UserList"];
247+
export type UserPage = components["schemas"]["UserPage"];
248+
export type Loan = components["schemas"]["Loan"];
249+
export type Book = components["schemas"]["Book"];
250+
export type BulkJobStatus = components["schemas"]["BulkJobStatus"];
251+
export type ClientSchema = components["schemas"]["Client"];
252+
export type schema_Something = components["schemas"]["schema-Something"];

0 commit comments

Comments
 (0)