Skip to content

Commit a3639a2

Browse files
authored
docs: update docs & examples (#123)
1 parent 8de859d commit a3639a2

15 files changed

+259
-228
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
329329
A self-defined code generator can return an array of `string`.
330330

331331
```ts
332-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
332+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
333333

334334
interface Option {
335335
showLog?: boolean;
@@ -354,7 +354,7 @@ The self-defined code generator can accept parameters extracted from OpenAPI Sch
354354
See Type definitions for available parameters.
355355

356356
```ts
357-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
357+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
358358

359359
interface Option {}
360360

@@ -442,7 +442,7 @@ You can extend your code using the API of TypeScript AST.
442442
You can directly use the API of TypeScript AST or use the wrapper API of TypeScript AST provided by this library.
443443

444444
```ts
445-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
445+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
446446
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/dist/api";
447447

448448
interface Option {}

docs/ja/README-ja.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
325325
独自定義のコードジェネレーターは`string`の配列を返すことができます。
326326

327327
```ts
328-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
328+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
329329

330330
interface Option {
331331
showLog?: boolean;
@@ -350,7 +350,7 @@ const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
350350
利用可能なパラメーターは型定義を参照してください。
351351

352352
```ts
353-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
353+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
354354

355355
interface Option {}
356356

@@ -438,7 +438,7 @@ TypeScript AST の API を利用したコードの拡張が可能です。
438438
直接 TypeScript の AST の API を利用したり、本ライブラリが提供する TypeScript AST のラッパー API を利用できます。
439439

440440
```ts
441-
import * as Types from "@himenon/openapi-typescript-code-generator/types";
441+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
442442
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/dist/api";
443443

444444
interface Option {}

examples/apis/client.ts

+61-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
2-
// Generated by @himenon/openapi-typescript-code-generator v0.7.2
2+
// Generated by @himenon/openapi-typescript-code-generator v1.0.2
33
//
44
// OpenApi : 3.0.3
55
//
66
// License : MIT
77
//
88

9-
export namespace schemas {
9+
export namespace Schemas {
1010
export interface Author {
1111
id: string;
1212
/** author name */
@@ -20,19 +20,19 @@ export namespace schemas {
2020
updatedAt: string;
2121
}
2222
}
23-
export namespace responses {
23+
export namespace Responses {
2424
/** Get Books */
2525
export namespace Books {
2626
export interface Content {
2727
"application/json": {
28-
books: schemas.Book[];
28+
books: Schemas.Book[];
2929
};
3030
}
3131
}
3232
}
3333
export interface Response$getBooks$Status$200 {
3434
"application/json": {
35-
books: schemas.Book[];
35+
books: Schemas.Book[];
3636
};
3737
}
3838
export interface Parameter$searchBooks {
@@ -44,7 +44,7 @@ export interface Parameter$searchBooks {
4444
}
4545
export interface Response$searchBooks$Status$200 {
4646
"application/json": {
47-
books?: schemas.Book[];
47+
books?: Schemas.Book[];
4848
};
4949
}
5050
export type ResponseContentType$getBooks = keyof Response$getBooks$Status$200;
@@ -69,36 +69,60 @@ export namespace ErrorResponse {
6969
export type getBooks = void;
7070
export type searchBooks = void;
7171
}
72-
export interface ApiClient<RequestOption> {
73-
request: <T = SuccessResponses>(
74-
httpMethod: HttpMethod,
75-
url: string,
76-
headers: ObjectLike | any,
77-
requestBody: ObjectLike | any,
78-
queryParameters: QueryParameters | undefined,
79-
options?: RequestOption,
80-
) => Promise<T>;
72+
export interface Encoding {
73+
readonly contentType?: string;
74+
headers?: Record<string, any>;
75+
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
76+
readonly explode?: boolean;
77+
readonly allowReserved?: boolean;
8178
}
82-
export class Client<RequestOption> {
83-
constructor(
84-
private apiClient: ApiClient<RequestOption>,
85-
private baseUrl: string,
86-
) {}
87-
public async getBooks(option?: RequestOption): Promise<Response$getBooks$Status$200["application/json"]> {
88-
const url = this.baseUrl + `/get/books`;
89-
const headers = {
90-
Accept: "application/json",
91-
};
92-
return this.apiClient.request("GET", url, headers, undefined, undefined, option);
93-
}
94-
public async searchBooks(params: Params$searchBooks, option?: RequestOption): Promise<Response$searchBooks$Status$200["application/json"]> {
95-
const url = this.baseUrl + `/search/books`;
96-
const headers = {
97-
Accept: "application/json",
98-
};
99-
const queryParameters: QueryParameters = {
100-
filter: { value: params.parameter.filter, style: "deepObject", explode: true },
101-
};
102-
return this.apiClient.request("GET", url, headers, undefined, queryParameters, option);
103-
}
79+
export interface RequestArgs {
80+
readonly httpMethod: HttpMethod;
81+
readonly url: string;
82+
headers: ObjectLike | any;
83+
requestBody?: ObjectLike | any;
84+
requestBodyEncoding?: Record<string, Encoding>;
85+
queryParameters?: QueryParameters | undefined;
86+
}
87+
export interface ApiClient<RequestOption> {
88+
request: <T = SuccessResponses>(requestArgs: RequestArgs, options?: RequestOption) => Promise<T>;
10489
}
90+
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
91+
const _baseUrl = baseUrl.replace(/\/$/, "");
92+
return {
93+
getBooks: (option?: RequestOption): Promise<Response$getBooks$Status$200["application/json"]> => {
94+
const url = _baseUrl + `/get/books`;
95+
const headers = {
96+
Accept: "application/json",
97+
};
98+
return apiClient.request(
99+
{
100+
httpMethod: "GET",
101+
url,
102+
headers,
103+
},
104+
option,
105+
);
106+
},
107+
searchBooks: (params: Params$searchBooks, option?: RequestOption): Promise<Response$searchBooks$Status$200["application/json"]> => {
108+
const url = _baseUrl + `/search/books`;
109+
const headers = {
110+
Accept: "application/json",
111+
};
112+
const queryParameters: QueryParameters = {
113+
filter: { value: params.parameter.filter, style: "deepObject", explode: true },
114+
};
115+
return apiClient.request(
116+
{
117+
httpMethod: "GET",
118+
url,
119+
headers,
120+
queryParameters: queryParameters,
121+
},
122+
option,
123+
);
124+
},
125+
};
126+
};
127+
type ClientFunction<RequestOption> = typeof createClient<RequestOption>;
128+
export type Client<RequestOption> = ReturnType<ClientFunction<RequestOption>>;

examples/apis/codegen.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as fs from "fs";
22

3-
import { CodeGenerator } from "../lib"; // = @himenon/openapi-typescript-code-generator
4-
import * as Templates from "../lib/templates"; // = @himenon/openapi-typescript-code-generator/templates
5-
import * as Types from "../lib/types"; // = @himenon/openapi-typescript-code-generator/types
3+
import { CodeGenerator } from "@himenon/openapi-typescript-code-generator";
4+
import * as Templates from "@himenon/openapi-typescript-code-generator/dist/templates";
5+
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";
66

77
const main = () => {
88
const codeGenerator = new CodeGenerator("./spec/openapi.yml");

examples/apis/package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
"private": true,
55
"description": "Demo",
66
"license": "MIT",
7+
"type": "module",
78
"main": "index.js",
89
"scripts": {
9-
"codegen": "yarn ts ./codegen.ts",
10-
"ts": "ts-node -P tsconfig.json"
10+
"codegen": "pnpm ts ./codegen.ts",
11+
"ts": "node --no-warnings=ExperimentalWarning --experimental-specifier-resolution=node --loader ts-node/esm"
1112
},
1213
"dependencies": {
1314
"@himenon/openapi-parameter-formatter": "0.3.1",
14-
"axios": "1.2.2",
15-
"superagent": "8.0.6"
15+
"@himenon/openapi-typescript-code-generator": "link:../..",
16+
"axios": "1.6.7",
17+
"superagent": "8.1.2"
1618
},
1719
"devDependencies": {
18-
"@types/node-fetch": "^2.6.2",
19-
"@types/superagent": "^4.1.16",
20-
"node-fetch": "^3.3.0",
21-
"ts-node": "10.9.1",
22-
"typescript": "4.9.4"
20+
"@types/node-fetch": "^2.6.11",
21+
"@types/superagent": "^8.1.3",
22+
"node-fetch": "^3.3.2",
23+
"ts-node": "10.9.2",
24+
"typescript": "5.3.3"
2325
}
2426
}

0 commit comments

Comments
 (0)