Skip to content

Commit

Permalink
docs: update docs & examples (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Feb 3, 2024
1 parent 8de859d commit a3639a2
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 228 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
A self-defined code generator can return an array of `string`.

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

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

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

interface Option {}

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

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

interface Option {}
Expand Down
6 changes: 3 additions & 3 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
独自定義のコードジェネレーターは`string`の配列を返すことができます。

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

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

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

interface Option {}

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

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

interface Option {}
Expand Down
98 changes: 61 additions & 37 deletions examples/apis/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//
// Generated by @himenon/openapi-typescript-code-generator v0.7.2
// Generated by @himenon/openapi-typescript-code-generator v1.0.2
//
// OpenApi : 3.0.3
//
// License : MIT
//

export namespace schemas {
export namespace Schemas {
export interface Author {
id: string;
/** author name */
Expand All @@ -20,19 +20,19 @@ export namespace schemas {
updatedAt: string;
}
}
export namespace responses {
export namespace Responses {
/** Get Books */
export namespace Books {
export interface Content {
"application/json": {
books: schemas.Book[];
books: Schemas.Book[];
};
}
}
}
export interface Response$getBooks$Status$200 {
"application/json": {
books: schemas.Book[];
books: Schemas.Book[];
};
}
export interface Parameter$searchBooks {
Expand All @@ -44,7 +44,7 @@ export interface Parameter$searchBooks {
}
export interface Response$searchBooks$Status$200 {
"application/json": {
books?: schemas.Book[];
books?: Schemas.Book[];
};
}
export type ResponseContentType$getBooks = keyof Response$getBooks$Status$200;
Expand All @@ -69,36 +69,60 @@ export namespace ErrorResponse {
export type getBooks = void;
export type searchBooks = void;
}
export interface ApiClient<RequestOption> {
request: <T = SuccessResponses>(
httpMethod: HttpMethod,
url: string,
headers: ObjectLike | any,
requestBody: ObjectLike | any,
queryParameters: QueryParameters | undefined,
options?: RequestOption,
) => Promise<T>;
export interface Encoding {
readonly contentType?: string;
headers?: Record<string, any>;
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
readonly explode?: boolean;
readonly allowReserved?: boolean;
}
export class Client<RequestOption> {
constructor(
private apiClient: ApiClient<RequestOption>,
private baseUrl: string,
) {}
public async getBooks(option?: RequestOption): Promise<Response$getBooks$Status$200["application/json"]> {
const url = this.baseUrl + `/get/books`;
const headers = {
Accept: "application/json",
};
return this.apiClient.request("GET", url, headers, undefined, undefined, option);
}
public async searchBooks(params: Params$searchBooks, option?: RequestOption): Promise<Response$searchBooks$Status$200["application/json"]> {
const url = this.baseUrl + `/search/books`;
const headers = {
Accept: "application/json",
};
const queryParameters: QueryParameters = {
filter: { value: params.parameter.filter, style: "deepObject", explode: true },
};
return this.apiClient.request("GET", url, headers, undefined, queryParameters, option);
}
export interface RequestArgs {
readonly httpMethod: HttpMethod;
readonly url: string;
headers: ObjectLike | any;
requestBody?: ObjectLike | any;
requestBodyEncoding?: Record<string, Encoding>;
queryParameters?: QueryParameters | undefined;
}
export interface ApiClient<RequestOption> {
request: <T = SuccessResponses>(requestArgs: RequestArgs, options?: RequestOption) => Promise<T>;
}
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
const _baseUrl = baseUrl.replace(/\/$/, "");
return {
getBooks: (option?: RequestOption): Promise<Response$getBooks$Status$200["application/json"]> => {
const url = _baseUrl + `/get/books`;
const headers = {
Accept: "application/json",
};
return apiClient.request(
{
httpMethod: "GET",
url,
headers,
},
option,
);
},
searchBooks: (params: Params$searchBooks, option?: RequestOption): Promise<Response$searchBooks$Status$200["application/json"]> => {
const url = _baseUrl + `/search/books`;
const headers = {
Accept: "application/json",
};
const queryParameters: QueryParameters = {
filter: { value: params.parameter.filter, style: "deepObject", explode: true },
};
return apiClient.request(
{
httpMethod: "GET",
url,
headers,
queryParameters: queryParameters,
},
option,
);
},
};
};
type ClientFunction<RequestOption> = typeof createClient<RequestOption>;
export type Client<RequestOption> = ReturnType<ClientFunction<RequestOption>>;
6 changes: 3 additions & 3 deletions examples/apis/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from "fs";

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

const main = () => {
const codeGenerator = new CodeGenerator("./spec/openapi.yml");
Expand Down
20 changes: 11 additions & 9 deletions examples/apis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
"private": true,
"description": "Demo",
"license": "MIT",
"type": "module",
"main": "index.js",
"scripts": {
"codegen": "yarn ts ./codegen.ts",
"ts": "ts-node -P tsconfig.json"
"codegen": "pnpm ts ./codegen.ts",
"ts": "node --no-warnings=ExperimentalWarning --experimental-specifier-resolution=node --loader ts-node/esm"
},
"dependencies": {
"@himenon/openapi-parameter-formatter": "0.3.1",
"axios": "1.2.2",
"superagent": "8.0.6"
"@himenon/openapi-typescript-code-generator": "link:../..",
"axios": "1.6.7",
"superagent": "8.1.2"
},
"devDependencies": {
"@types/node-fetch": "^2.6.2",
"@types/superagent": "^4.1.16",
"node-fetch": "^3.3.0",
"ts-node": "10.9.1",
"typescript": "4.9.4"
"@types/node-fetch": "^2.6.11",
"@types/superagent": "^8.1.3",
"node-fetch": "^3.3.2",
"ts-node": "10.9.2",
"typescript": "5.3.3"
}
}
Loading

0 comments on commit a3639a2

Please sign in to comment.