Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: code format #128

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/apis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>,
const _baseUrl = baseUrl.replace(/\/$/, "");
return {
getBooks: (option?: RequestOption): Promise<Response$getBooks$Status$200["application/json"]> => {
const url = _baseUrl + `/get/books`;
const url = `${_baseUrl}/get/books`;
const headers = {
Accept: "application/json",
};
Expand All @@ -105,7 +105,7 @@ export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>,
);
},
searchBooks: (params: Params$searchBooks, option?: RequestOption): Promise<Response$searchBooks$Status$200["application/json"]> => {
const url = _baseUrl + `/search/books`;
const url = `${_baseUrl}/search/books`;
const headers = {
Accept: "application/json",
};
Expand Down
2 changes: 1 addition & 1 deletion examples/apis/sample-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
options?: RequestOption,
): Promise<any> => {
const query = generateQueryString(queryParameters);
const requestUrl = query ? url + "?" + encodeURI(query) : url;
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
const response = await axios.default.request({
url: requestUrl,
method: convertHttpMethodToAxiosMethod(httpMethod),
Expand Down
2 changes: 1 addition & 1 deletion examples/apis/sample-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
options?: RequestOption,
): Promise<any> => {
const query = generateQueryString(queryParameters);
const requestUrl = query ? url + "?" + encodeURI(query) : url;
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
console.log({
httpMethod,
url,
Expand Down
2 changes: 1 addition & 1 deletion examples/apis/sample-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
options?: RequestOption,
): Promise<any> => {
const query = generateQueryString(queryParameters);
const requestUrl = query ? url + "?" + encodeURI(query) : url;
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
const response = await fetch(requestUrl, {
body: JSON.stringify(requestBody),
headers,
Expand Down
2 changes: 1 addition & 1 deletion examples/apis/sample-superagent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
options?: RequestOption,
): Promise<any> => {
const query = generateQueryString(queryParameters);
const requestUrl = query ? url + "?" + encodeURI(query) : url;
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;

return new Promise((resolve, reject) => {
const agent = Superagent;
Expand Down
2 changes: 1 addition & 1 deletion examples/readme-sample/ast-code-template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TsGenerator } from "@himenon/openapi-typescript-code-generator/dist/api";
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";

interface Option {}
type Option = {};

const factory = TsGenerator.Factory.create();

Expand Down
2 changes: 1 addition & 1 deletion examples/readme-sample/text-base-code-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface Option {
}

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[], option): string[] => {
if (option && option.showLog) {
if (option?.showLog) {
console.log("show log message");
}
return ["Hello world"];
Expand Down
2 changes: 1 addition & 1 deletion examples/readme-sample/use-extract-schema-params.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Types from "@himenon/openapi-typescript-code-generator/dist/types";

interface Option {}
type Option = {};

const generator: Types.CodeGenerator.GenerateFunction<Option> = (payload: Types.CodeGenerator.Params[]): string[] => {
return payload.map(params => {
Expand Down
2 changes: 1 addition & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts", ".tsx"],
testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"],
transformIgnorePatterns: [`/node_modules/(?!|dot-prop)`],
transformIgnorePatterns: ["/node_modules/(?!|dot-prop)"],
transform: {
"^.+\\.(t|j)sx?$": "@swc/jest",
},
Expand Down
2 changes: 1 addition & 1 deletion jest.snapshot.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts", ".tsx"],
testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"],
transformIgnorePatterns: [`/node_modules/(?!|dot-prop)`],
transformIgnorePatterns: ["/node_modules/(?!|dot-prop)"],
transform: {
"^.+\\.(t|j)sx?$": [
"@swc/jest",
Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/shell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execa, ExecaChildProcess } from "execa";
import { ExecaChildProcess, execa } from "execa";

export const shell = (command: string, cwd: string = process.cwd()): ExecaChildProcess<string> => {
return execa(command, {
Expand Down
2 changes: 1 addition & 1 deletion src/code-templates/_shared/ApiClientArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const createHeaders = (factory: TsGenerator.Factory.Type, { convertedParams }: C
members.push(
factory.PropertySignature.create({
readOnly: false,
name: `Accept`,
name: "Accept",
optional: false,
type: factory.TypeReferenceNode.create({ name: "U" }),
}),
Expand Down
14 changes: 7 additions & 7 deletions src/code-templates/_shared/ApiClientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import ts from "typescript";

import type { TsGenerator } from "../../api";
import type { CodeGenerator } from "../../types";
import type { Option } from "./types";
import type { MethodType } from "./MethodBody/types";
import type { Option } from "./types";

const httpMethodList: string[] = ["GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"];

Expand Down Expand Up @@ -190,7 +190,7 @@ export const create = (
}),
});

const successResponseNames = list.map(item => item.convertedParams.responseSuccessNames).flat();
const successResponseNames = list.flatMap(item => item.convertedParams.responseSuccessNames);

const errorResponseNamespace = factory.Namespace.create({
export: true,
Expand Down Expand Up @@ -238,7 +238,7 @@ export const create = (
name: "RequestArgs",
members: [
factory.PropertySignature.create({
name: `httpMethod`,
name: "httpMethod",
readOnly: true,
optional: false,
type: factory.TypeReferenceNode.create({ name: "HttpMethod" }),
Expand All @@ -250,25 +250,25 @@ export const create = (
type: factory.TypeReferenceNode.create({ name: "string" }),
}),
factory.PropertySignature.create({
name: `headers`,
name: "headers",
readOnly: false,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBody`,
name: "requestBody",
readOnly: false,
optional: true,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBodyEncoding`,
name: "requestBodyEncoding",
readOnly: false,
optional: true,
type: factory.TypeReferenceNode.create({ name: "Record<string, Encoding>" }),
}),
factory.PropertySignature.create({
name: `queryParameters`,
name: "queryParameters",
optional: true,
readOnly: false,
type: factory.UnionTypeNode.create({
Expand Down
2 changes: 1 addition & 1 deletion src/code-templates/_shared/MethodBody/CallRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import ts from "typescript";
import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import * as Utils from "../utils";
import type { MethodType } from "./types";
import { createEncodingMap } from "./createEncodingMap";
import type { MethodType } from "./types";

export interface Params {
httpMethod: string;
Expand Down
2 changes: 1 addition & 1 deletion src/code-templates/_shared/MethodBody/PathParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import ts from "typescript";

import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import * as Utils from "../utils";
import { escapeText2 as escapeText } from "../../../utils";
import * as Utils from "../utils";
import type { MethodType } from "./types";

export const isPathParameter = (params: any): params is CodeGenerator.PickedParameter => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,76 +45,76 @@ describe("PathParameter Test", () => {
return getText(expression);
};
test("generateUrlTemplateExpression", () => {
expect(generate("/{a}", [{ in: "path", name: "a", required: true }])).toEqual("`/${params.parameter.a}`;" + EOL);
expect(generate("/{a}/", [{ in: "path", name: "a", required: true }])).toEqual("`/${params.parameter.a}/`;" + EOL);
expect(generate("/a/{b}", [{ in: "path", name: "b", required: true }])).toEqual("`/a/${params.parameter.b}`;" + EOL);
expect(generate("/a/{b}/", [{ in: "path", name: "b", required: true }])).toEqual("`/a/${params.parameter.b}/`;" + EOL);
expect(generate("/a/{b}/c", [{ in: "path", name: "b", required: true }])).toEqual("`/a/${params.parameter.b}/c`;" + EOL);
expect(generate("/a/{b}/c/", [{ in: "path", name: "b", required: true }])).toEqual("`/a/${params.parameter.b}/c/`;" + EOL);
expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toEqual("`/a/b/${params.parameter.c}`;" + EOL);
expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toEqual("`/a/b/${params.parameter.c}`;" + EOL);
expect(generate("/a/b/{c}/", [{ in: "path", name: "c", required: true }])).toEqual("`/a/b/${params.parameter.c}/`;" + EOL);
expect(generate("/a/b/{c}.json", [{ in: "path", name: "c", required: true }])).toEqual("`/a/b/${params.parameter.c}.json`;" + EOL);
expect(generate("/{a}", [{ in: "path", name: "a", required: true }])).toEqual(`\`/\${params.parameter.a}\`;${EOL}`);
expect(generate("/{a}/", [{ in: "path", name: "a", required: true }])).toEqual(`\`/\${params.parameter.a}/\`;${EOL}`);
expect(generate("/a/{b}", [{ in: "path", name: "b", required: true }])).toEqual(`\`/a/\${params.parameter.b}\`;${EOL}`);
expect(generate("/a/{b}/", [{ in: "path", name: "b", required: true }])).toEqual(`\`/a/\${params.parameter.b}/\`;${EOL}`);
expect(generate("/a/{b}/c", [{ in: "path", name: "b", required: true }])).toEqual(`\`/a/\${params.parameter.b}/c\`;${EOL}`);
expect(generate("/a/{b}/c/", [{ in: "path", name: "b", required: true }])).toEqual(`\`/a/\${params.parameter.b}/c/\`;${EOL}`);
expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toEqual(`\`/a/b/\${params.parameter.c}\`;${EOL}`);
expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toEqual(`\`/a/b/\${params.parameter.c}\`;${EOL}`);
expect(generate("/a/b/{c}/", [{ in: "path", name: "c", required: true }])).toEqual(`\`/a/b/\${params.parameter.c}/\`;${EOL}`);
expect(generate("/a/b/{c}.json", [{ in: "path", name: "c", required: true }])).toEqual(`\`/a/b/\${params.parameter.c}.json\`;${EOL}`);
expect(generate("/{a}.json/{a}.json/{a}.json", [{ in: "path", name: "a", required: true }])).toEqual(
"`/${params.parameter.a}.json/${params.parameter.a}.json/${params.parameter.a}.json`;" + EOL,
`\`/\${params.parameter.a}.json/\${params.parameter.a}.json/\${params.parameter.a}.json\`;${EOL}`,
);
expect(generate("/.json.{a}.json/{a}.json.{a}", [{ in: "path", name: "a", required: true }])).toEqual(
"`/.json.${params.parameter.a}.json/${params.parameter.a}.json.${params.parameter.a}`;" + EOL,
`\`/.json.\${params.parameter.a}.json/\${params.parameter.a}.json.\${params.parameter.a}\`;${EOL}`,
);

expect(
generate("/{a}/{b}", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "b", required: true },
]),
).toBe("`/${params.parameter.a}/${params.parameter.b}`;" + EOL);
).toBe(`\`/\${params.parameter.a}/\${params.parameter.b}\`;${EOL}`);
expect(
generate("/{a}/{b}/", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "b", required: true },
]),
).toBe("`/${params.parameter.a}/${params.parameter.b}/`;" + EOL);
).toBe(`\`/\${params.parameter.a}/\${params.parameter.b}/\`;${EOL}`);
expect(
generate("/{a}/{b}/c", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "b", required: true },
]),
).toBe("`/${params.parameter.a}/${params.parameter.b}/c`;" + EOL);
).toBe(`\`/\${params.parameter.a}/\${params.parameter.b}/c\`;${EOL}`);
expect(
generate("/{a}/{b}/c/", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "b", required: true },
]),
).toBe("`/${params.parameter.a}/${params.parameter.b}/c/`;" + EOL);
).toBe(`\`/\${params.parameter.a}/\${params.parameter.b}/c/\`;${EOL}`);
expect(
generate("/{a}/b/{c}", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "c", required: true },
]),
).toBe("`/${params.parameter.a}/b/${params.parameter.c}`;" + EOL);
).toBe(`\`/\${params.parameter.a}/b/\${params.parameter.c}\`;${EOL}`);
expect(
generate("/{a}/b/{c}/", [
{ in: "path", name: "a", required: true },
{ in: "path", name: "c", required: true },
]),
).toBe("`/${params.parameter.a}/b/${params.parameter.c}/`;" + EOL);
).toBe(`\`/\${params.parameter.a}/b/\${params.parameter.c}/\`;${EOL}`);
expect(
generate("/a/{b}/{c}", [
{ in: "path", name: "b", required: true },
{ in: "path", name: "c", required: true },
]),
).toBe("`/a/${params.parameter.b}/${params.parameter.c}`;" + EOL);
).toBe(`\`/a/\${params.parameter.b}/\${params.parameter.c}\`;${EOL}`);
expect(
generate("/a/{b}/{c}/", [
{ in: "path", name: "b", required: true },
{ in: "path", name: "c", required: true },
]),
).toBe("`/a/${params.parameter.b}/${params.parameter.c}/`;" + EOL);
).toBe(`\`/a/\${params.parameter.b}/\${params.parameter.c}/\`;${EOL}`);
expect(
generate("/a/{b}...{c}/", [
{ in: "path", name: "b", required: true },
{ in: "path", name: "c", required: true },
]),
).toBe("`/a/${params.parameter.b}...${params.parameter.c}/`;" + EOL);
).toBe(`\`/a/\${params.parameter.b}...\${params.parameter.c}/\`;${EOL}`);
});
});
10 changes: 5 additions & 5 deletions src/code-templates/_shared/MethodBody/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as CallRequest from "./CallRequest";
import * as HeaderParameter from "./HeaderParameter";
import * as PathParameter from "./PathParameter";
import * as QueryParameter from "./QueryParameter";
import type { MethodType } from "./types";
import { createEncodingMap } from "./createEncodingMap";
import type { MethodType } from "./types";

export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, methodType: MethodType): ts.Statement[] => {
const statements: ts.Statement[] = [];
Expand All @@ -27,7 +27,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
if (convertedParams.has2OrMoreRequestContentTypes) {
initialHeaderObject["Content-Type"] = {
type: "variable",
value: `params.headers.Content-Type`,
value: "params.headers.Content-Type",
};
} else if (convertedParams.requestFirstContentType) {
initialHeaderObject["Content-Type"] = {
Expand All @@ -36,12 +36,12 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
};
}
if (convertedParams.has2OrMoreSuccessResponseContentTypes) {
initialHeaderObject["Accept"] = {
initialHeaderObject.Accept = {
type: "variable",
value: `params.headers.Accept`,
value: "params.headers.Accept",
};
} else if (convertedParams.successResponseFirstContentType) {
initialHeaderObject["Accept"] = {
initialHeaderObject.Accept = {
type: "string",
value: convertedParams.successResponseFirstContentType,
};
Expand Down
Loading
Loading