Skip to content

Commit

Permalink
fix: engines fields (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Mar 31, 2022
1 parent 5cd7b7e commit 4ea37e1
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
browser: true,
es2021: true,
},
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:import/typescript", "prettier/@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:import/typescript"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
Expand Down
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Since the parameters extracted from OpenAPI can be used freely, it can be used f
## Installation

```bash
npm i -D @himenon/openapi-typescript-code-generator
# or
pnpm i -D @himenon/openapi-typescript-code-generator
# or
yarn add -D @himenon/openapi-typescript-code-generator
```

Expand Down Expand Up @@ -118,13 +122,11 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
/** Write the definition of the Code Template here. */
const customGenerator: Types.CodeGenerator.CustomGenerator<{}> = {
/** .... */
}
};

const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const code = codeGenerator.generateCode([
customGenerator,
]);
const code = codeGenerator.generateCode([customGenerator]);

fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
```
Expand All @@ -150,7 +152,7 @@ const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.
const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

### Define using the information extracted from OpenAPI Schema
Expand All @@ -161,19 +163,18 @@ See Type definitions for available parameters.
```ts
import * as Types from "@himenon/openapi-typescript-code-generator/types";

interface Option {
}
interface Option {}

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): string[] => {
return payload.map((params) => {
return payload.map(params => {
return `function ${params.operationId}() { console.log("${params.comment}") }`;
})
});
};

const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

### Define any Data Types Format
Expand Down Expand Up @@ -236,9 +237,9 @@ The typedef generated by this will look like this

```ts
export namespace Schemas {
export type Binary = Blob;
export type IntOrString = number | string;
export type AandB = A & B;
export type Binary = Blob;
export type IntOrString = number | string;
export type AandB = A & B;
}
```

Expand All @@ -251,25 +252,27 @@ You can directly use the API of TypeScript AST or use the wrapper API of TypeScr
import * as Types from "@himenon/openapi-typescript-code-generator/types";
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/api";

interface Option {
}
interface Option {}

const factory = TsGenerator.Factory.create();

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): Types.CodeGenerator.IntermediateCode[] => {
return payload.map((params) => {
const generator: Types.CodeGenerator.GenerateFunction<Option> = (
payload: Types.CodeGenerator.Params[],
option,
): Types.CodeGenerator.IntermediateCode[] => {
return payload.map(params => {
return factory.InterfaceDeclaration.create({
export: true,
name: params.functionName,
members: [],
})
})
});
});
};

const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

## API
Expand Down
39 changes: 19 additions & 20 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ import type * as Types from "@himenon/openapi-typescript-code-generator/types";
/** ここにCode Templateの定義を記述してください */
const customGenerator: Types.CodeGenerator.CustomGenerator<{}> = {
/** .... */
}
};

const codeGenerator = new CodeGenerator("your/openapi/spec.yml");

const code = codeGenerator.generateCode([
customGenerator,
]);
const code = codeGenerator.generateCode([customGenerator]);

fs.writeFileSync("output/file/name", code, { encoding: "utf-8" });
```
Expand All @@ -148,7 +146,7 @@ const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.
const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

### OpenAPI Schema から抽出した情報を利用した定義をする
Expand All @@ -159,19 +157,18 @@ const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
```ts
import * as Types from "@himenon/openapi-typescript-code-generator/types";

interface Option {
}
interface Option {}

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): string[] => {
return payload.map((params) => {
return payload.map(params => {
return `function ${params.operationId}() { console.log("${params.comment}") }`;
})
});
};

const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

### 任意の Data Types Format を定義する
Expand Down Expand Up @@ -234,9 +231,9 @@ const codeGenerator = new CodeGenerator(inputFilename, option);

```ts
export namespace Schemas {
export type Binary = Blob;
export type IntOrString = number | string;
export type AandB = A & B;
export type Binary = Blob;
export type IntOrString = number | string;
export type AandB = A & B;
}
```

Expand All @@ -249,25 +246,27 @@ TypeScript AST の API を利用したコードの拡張が可能です。
import * as Types from "@himenon/openapi-typescript-code-generator/types";
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/api";

interface Option {
}
interface Option {}

const factory = TsGenerator.Factory.create();

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): Types.CodeGenerator.IntermediateCode[] => {
return payload.map((params) => {
const generator: Types.CodeGenerator.GenerateFunction<Option> = (
payload: Types.CodeGenerator.Params[],
option,
): Types.CodeGenerator.IntermediateCode[] => {
return payload.map(params => {
return factory.InterfaceDeclaration.create({
export: true,
name: params.functionName,
members: [],
})
})
});
});
};

const customGenerator: Types.CodeGenerator.CustomGenerator<Option> = {
generator: generator,
option: {},
}
};
```

## API
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@
"typescript": "4.2.3"
},
"engines": {
"node": ">=16",
"npm": "forbidden, use pnpm",
"pnpm": ">=6",
"yarn": "forbidden, use pnpm"
"pnpm": ">=6"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 4ea37e1

Please sign in to comment.