Skip to content

Commit

Permalink
refactor: update test code generator (#33)
Browse files Browse the repository at this point in the history
* refactor: update test code generator
* test: update snapshot code test
  • Loading branch information
Himenon authored Apr 6, 2021
1 parent 1992416 commit 631af46
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 57 deletions.
4 changes: 0 additions & 4 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { copyPackageSet } from "./tools/copyPackageSet";
import { generateExportsField } from "./tools/dualPackageSupport";
import { shell } from "./tools/shell";
import { cherryPick } from "./tools/cherry-pick";
import * as fs from "fs";


const main = async () => {
await Promise.all([
Expand All @@ -16,8 +14,6 @@ const main = async () => {

await cherryPick({ inputDir: "../src", cwd: "./lib", typesDir: "./$types", cjsDir: "./$cjs", esmDir: "./$esm" });

const outputList = fs.readdirSync("./lib");

const exportsFiled = generateExportsField("./src", {
directory: {
import: "./$esm",
Expand Down
77 changes: 53 additions & 24 deletions scripts/testCodeGen.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,77 @@
import * as fs from "fs";
import { posix as path } from "path";

import { CodeGenerator } from "../lib";
import * as Templates from "../lib/templates";

const gen = (name: string, enableValidate = true): void => {
fs.mkdirSync("test/code", { recursive: true });
const codeGenerator = new CodeGenerator(`test/${name}/index.yml`);
const code = codeGenerator.generateTypeDefinition<Templates.ApiClient.Option>({
generator: Templates.ApiClient.generator,
option: {
sync: false,
},
});
if (enableValidate) {
const writeText = (filename: string, text: string): void => {
fs.mkdirSync(path.dirname(filename), { recursive: true });
fs.writeFileSync(filename, text, { encoding: "utf-8" });
console.log(`Generate Code : ${filename}`);
};

const generateTypedefCodeOnly = (inputFilename: string, outputFilename: string, isValidate: boolean) => {
const codeGenerator = new CodeGenerator(inputFilename);
if (isValidate) {
codeGenerator.validate({
logger: { displayLogLines: 1 },
});
}
fs.writeFileSync(`test/code/${name}.ts`, code, { encoding: "utf-8" });
console.log(`Generate Code : test/code/${name}.ts`);
const code = codeGenerator.generateTypeDefinition();
writeText(outputFilename, code);
};

const genSyncMode = (name: string, enableValidate = true): void => {
const codeGenerator = new CodeGenerator(`test/${name}/index.yml`);
fs.mkdirSync("test/code", { recursive: true });
if (enableValidate) {
const generateTemplateCodeOnly = (
inputFilename: string,
outputFilename: string,
isValidate: boolean,
option: Templates.ApiClient.Option,
): void => {
const codeGenerator = new CodeGenerator(inputFilename);
if (isValidate) {
codeGenerator.validate({
logger: { displayLogLines: 1 },
});
}
const code = codeGenerator.generateCode<Templates.ApiClient.Option>({
generator: Templates.ApiClient.generator,
option: option,
});

writeText(outputFilename, code);
};

const generateTypedefWithTemplateCode = (
inputFilename: string,
outputFilename: string,
isValidate: boolean,
option: Templates.ApiClient.Option,
): void => {
const codeGenerator = new CodeGenerator(inputFilename);
if (isValidate) {
codeGenerator.validate({
logger: { displayLogLines: 1 },
});
}
const code = codeGenerator.generateTypeDefinition<Templates.ApiClient.Option>({
generator: Templates.ApiClient.generator,
option: {
sync: true,
},
option: option,
});
fs.writeFileSync(`test/code/sync-${name}.ts`, code, { encoding: "utf-8" });
console.log(`Generate Code : test/code/sync-${name}.ts`);

writeText(outputFilename, code);
};

const main = () => {
gen("api.test.domain");
gen("infer.domain", false);
genSyncMode("api.test.domain");
generateTypedefCodeOnly("test/api.test.domain/index.yml", "test/code/typedef-only/api.test.domain.ts", true);
generateTypedefCodeOnly("test/infer.domain/index.yml", "test/code/typedef-only/infer.domain.ts", false);

generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/api.test.domain.ts", true, { sync: false });
generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/sync-api.test.domain.ts", true, { sync: true });
generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/template-only/infer.domain.ts", false, { sync: true });

generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/api.test.domain.ts", true, { sync: false });
generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/sync-api.test.domain.ts", true, { sync: true });
generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/typedef-with-template/infer.domain.ts", false, { sync: false });
};

main();
212 changes: 212 additions & 0 deletions test/__tests__/__snapshots__/template-only-test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Template Only api.test.domain 1`] = `
"//
// Generated by @himenon/openapi-typescript-code-generator
//
// OpenApi : 3.1.0
//
// License : MIT
//
export type ResponseContentType$getIncludeLocalReference = keyof Response$getIncludeLocalReference$Status$200;
export interface Params$getIncludeLocalReference {
parameter: Parameter$getIncludeLocalReference;
}
export type RequestContentType$getIncludeRemoteReference = keyof RequestBody$getIncludeRemoteReference;
export interface Params$getIncludeRemoteReference {
parameter: Parameter$getIncludeRemoteReference;
requestBody: RequestBody$getIncludeRemoteReference[\\"application/json\\"];
}
export type ResponseContentType$getFullRemoteReference = keyof Response$getFullRemoteReference$Status$200;
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
export interface ObjectLike {
[key: string]: any;
}
export interface QueryParameter {
value: any;
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
explode: boolean;
}
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type getReferenceItems = 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 class Client<RequestOption> {
constructor(private apiClient: ApiClient<RequestOption>, private baseUrl: string) { }
public async getIncludeLocalReference(params: Params$getIncludeLocalReference, option?: RequestOption): Promise<Response$getIncludeLocalReference$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/get/IncludeLocalReference\`;
const headers = {
Accept: \\"application/json\\"
};
const queryParameters: QueryParameters = {
StringQuery: { value: params.parameter.StringQuery, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public async getIncludeRemoteReference(params: Params$getIncludeRemoteReference, option?: RequestOption): Promise<void> {
const url = this.baseUrl + \`/get/IncludeRemoteReference\`;
const headers = {
\\"Content-Type\\": \\"application/json\\"
};
const queryParameters: QueryParameters = {
IncludeRemoteReference: { value: params.parameter.IncludeRemoteReference, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, params.requestBody, queryParameters, option);
}
public async getFullRemoteReference(params: Params$getFullRemoteReference, option?: RequestOption): Promise<Response$getFullRemoteReference$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/FullRemoteReference\`;
const headers = {
Accept: \\"application/json\\"
};
const queryParameters: QueryParameters = {
FullRemoteReferenceQuery: { value: params.parameter.FullRemoteReferenceQuery, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public async getReferenceItems(option?: RequestOption): Promise<Response$getReferenceItems$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
}
"
`;
exports[`Template Only async-api.test.domain 1`] = `
"//
// Generated by @himenon/openapi-typescript-code-generator
//
// OpenApi : 3.1.0
//
// License : MIT
//
export type ResponseContentType$getIncludeLocalReference = keyof Response$getIncludeLocalReference$Status$200;
export interface Params$getIncludeLocalReference {
parameter: Parameter$getIncludeLocalReference;
}
export type RequestContentType$getIncludeRemoteReference = keyof RequestBody$getIncludeRemoteReference;
export interface Params$getIncludeRemoteReference {
parameter: Parameter$getIncludeRemoteReference;
requestBody: RequestBody$getIncludeRemoteReference[\\"application/json\\"];
}
export type ResponseContentType$getFullRemoteReference = keyof Response$getFullRemoteReference$Status$200;
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
export interface ObjectLike {
[key: string]: any;
}
export interface QueryParameter {
value: any;
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
explode: boolean;
}
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type getReferenceItems = void;
}
export interface ApiClient<RequestOption> {
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => T;
}
export class Client<RequestOption> {
constructor(private apiClient: ApiClient<RequestOption>, private baseUrl: string) { }
public getIncludeLocalReference(params: Params$getIncludeLocalReference, option?: RequestOption): Response$getIncludeLocalReference$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/get/IncludeLocalReference\`;
const headers = {
Accept: \\"application/json\\"
};
const queryParameters: QueryParameters = {
StringQuery: { value: params.parameter.StringQuery, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public getIncludeRemoteReference(params: Params$getIncludeRemoteReference, option?: RequestOption): void {
const url = this.baseUrl + \`/get/IncludeRemoteReference\`;
const headers = {
\\"Content-Type\\": \\"application/json\\"
};
const queryParameters: QueryParameters = {
IncludeRemoteReference: { value: params.parameter.IncludeRemoteReference, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, params.requestBody, queryParameters, option);
}
public getFullRemoteReference(params: Params$getFullRemoteReference, option?: RequestOption): Response$getFullRemoteReference$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/FullRemoteReference\`;
const headers = {
Accept: \\"application/json\\"
};
const queryParameters: QueryParameters = {
FullRemoteReferenceQuery: { value: params.parameter.FullRemoteReferenceQuery, explode: false }
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public getReferenceItems(option?: RequestOption): Response$getReferenceItems$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
}
"
`;
exports[`Template Only infer.domain 1`] = `
"//
// Generated by @himenon/openapi-typescript-code-generator
//
// OpenApi : 3.1.0
//
// License : MIT
//
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
export interface ObjectLike {
[key: string]: any;
}
export interface QueryParameter {
value: any;
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
explode: boolean;
}
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = void;
export namespace ErrorResponse { }
export interface ApiClient<RequestOption> {
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => T;
}
export class Client<RequestOption> {
constructor(private apiClient: ApiClient<RequestOption>, private baseUrl: string) { }
}
"
`;
Loading

0 comments on commit 631af46

Please sign in to comment.