Skip to content

Commit

Permalink
chore: Trim the trailing slash of the baseUrl with constructor (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Aug 11, 2021
1 parent 9cdc602 commit 4d62643
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 49 deletions.
8 changes: 7 additions & 1 deletion src/code-templates/api-client/ApiClientClass/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export const create = (factory: TsGenerator.Factory.Type, members: ts.ClassEleme
return factory.ClassDeclaration.create({
name: "Client",
export: true,
members,
members: [
factory.PropertyDeclaration.create({
modifiers: [ts.factory.createModifier(ts.SyntaxKind.PrivateKeyword)],
name: "baseUrl",
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
})
,...members],
typeParameterDeclaration: [
factory.TypeParameterDeclaration.create({
name: "RequestOption",
Expand Down
21 changes: 19 additions & 2 deletions src/code-templates/api-client/ApiClientClass/Constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,33 @@ export const create = (factory: TsGenerator.Factory.Type): ts.ConstructorDeclara
}),
});
const parameter2 = factory.ParameterDeclaration.create({
modifiers: "private",
name: "baseUrl",
type: factory.TypeNode.create({
type: "string",
}),
});

return factory.ConstructorDeclaration.create({
parameters: [parameter1, parameter2],
body: factory.Block.create({
statements: [],
statements: [
factory.ExpressionStatement.create({
expression: factory.BinaryExpression.create({
left: factory.PropertyAccessExpression.create({
expression: "this",
name: "baseUrl",
}),
operator: "=",
right: factory.CallExpression.create({
expression: factory.PropertyAccessExpression.create({
expression: "baseUrl",
name: "replace",
}),
argumentsArray: [factory.RegularExpressionLiteral.create({ text: "/\\/$/" }), factory.StringLiteral.create({ text: "" })],
}),
}),
}),
],
multiLine: false,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ const generateUrlVariableStatement = (
factory.VariableDeclaration.create({
name: "url",
initializer: factory.BinaryExpression.create({
left: factory.CallExpression.create({
expression: factory.PropertyAccessExpression.create({
expression: factory.PropertyAccessExpression.create({
name: "baseUrl",
expression: "this",
}),
name: "replace",
}),
argumentsArray: [factory.RegularExpressionLiteral.create({ text: "/\\\/$/" }), factory.StringLiteral.create({ text: "" })],
left: factory.PropertyAccessExpression.create({
name: "baseUrl",
expression: "this",
}),
operator: "+",
right: Utils.generateTemplateExpression(factory, urlTemplate),
Expand Down
1 change: 1 addition & 0 deletions src/internal/TsGenerator/factory/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ts from "typescript";

const operators = {
"+": ts.SyntaxKind.PlusToken,
"=": ts.SyntaxKind.EqualsToken,
} as const;

export interface Params {
Expand Down
20 changes: 20 additions & 0 deletions src/internal/TsGenerator/factory/ExpressionStatement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ts from "typescript";

export interface Params$Create {
expression: ts.Expression;
}

export interface Factory {
create: (params: Params$Create) => ts.ExpressionStatement;
}

export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.ExpressionStatement => {
const node = factory.createExpressionStatement(params.expression);
return node;
};

export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
};
34 changes: 34 additions & 0 deletions src/internal/TsGenerator/factory/PropertyDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ts from "typescript";

export interface Params {
decorators?: readonly ts.Decorator[] | undefined;
modifiers: readonly ts.Modifier[] | undefined;
name: string | ts.PropertyName;
questionOrExclamationToken?: ts.QuestionToken | ts.ExclamationToken | undefined;
type: ts.TypeNode | undefined;
initializer?: ts.Expression | undefined;
}

export interface Factory {
create: (params: Params) => ts.PropertyDeclaration;
}

export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (
params: Params,
): ts.PropertyDeclaration => {
const node = factory.createPropertyDeclaration(
params.decorators,
params.modifiers,
params.name,
params.questionOrExclamationToken,
params.type,
params.initializer,
);
return node;
};

export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
};
6 changes: 6 additions & 0 deletions src/internal/TsGenerator/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as CallExpression from "./CallExpression";
import * as ClassDeclaration from "./ClassDeclaration";
import * as ConstructorDeclaration from "./ConstructorDeclaration";
import * as ElementAccessExpression from "./ElementAccessExpression";
import * as ExpressionStatement from "./ExpressionStatement";
import * as FunctionTypeNode from "./FunctionTypeNode";
import * as Identifier from "./Identifier";
import * as IndexedAccessTypeNode from "./IndexedAccessTypeNode";
Expand All @@ -21,6 +22,7 @@ import * as ParameterDeclaration from "./ParameterDeclaration";
import * as PropertyAccessExpression from "./PropertyAccessExpression";
import * as PropertyAssignment from "./PropertyAssignment";
import * as PropertySignature from "./PropertySignature";
import * as PropertyDeclaration from "./PropertyDeclaration";
import * as RegularExpressionLiteral from "./RegularExpressionLiteral";
import * as ReturnStatement from "./ReturnStatement";
import * as StringLiteral from "./StringLiteral";
Expand Down Expand Up @@ -50,6 +52,7 @@ export interface Type {
TypeOperatorNode: TypeOperatorNode.Factory;
Namespace: Namespace.Factory;
PropertySignature: PropertySignature.Factory;
PropertyDeclaration: PropertyDeclaration.Factory;
RegularExpressionLiteral: RegularExpressionLiteral.Factory;
TypeAliasDeclaration: TypeAliasDeclaration.Factory;
TypeNode: TypeNode.Factory;
Expand All @@ -76,6 +79,7 @@ export interface Type {
PropertyAssignment: PropertyAssignment.Factory;
ObjectLiteralExpression: ObjectLiteralExpression.Factory;
ElementAccessExpression: ElementAccessExpression.Factory;
ExpressionStatement: ExpressionStatement.Factory;
CallExpression: CallExpression.Factory;
StringLiteral: StringLiteral.Factory;
FunctionTypeNode: FunctionTypeNode.Factory;
Expand All @@ -94,6 +98,7 @@ export const create = (): Type => {
IndexedAccessTypeNode: IndexedAccessTypeNode.make(context),
Namespace: Namespace.make(context),
PropertySignature: PropertySignature.make(context),
PropertyDeclaration: PropertyDeclaration.make(context),
TypeAliasDeclaration: TypeAliasDeclaration.make(context),
TypeNode: TypeNode.make(context),
LiteralTypeNode: LiteralTypeNode.make(context),
Expand Down Expand Up @@ -122,6 +127,7 @@ export const create = (): Type => {
PropertyAssignment: PropertyAssignment.make(context),
ObjectLiteralExpression: ObjectLiteralExpression.make(context),
ElementAccessExpression: ElementAccessExpression.make(context),
ExpressionStatement: ExpressionStatement.make(context),
CallExpression: CallExpression.make(context),
StringLiteral: StringLiteral.make(context),
FunctionTypeNode: FunctionTypeNode.make(context),
Expand Down
15 changes: 8 additions & 7 deletions test/__tests__/__snapshots__/spit-code-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ 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) { }
private baseUrl: string;
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
/**
* operationId: getIncludeLocalReference
* Request URI: /get/IncludeLocalReference
*/
public async getIncludeLocalReference(params: Params$getIncludeLocalReference, option?: RequestOption): Promise<Response$getIncludeLocalReference$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeLocalReference\`;
const url = this.baseUrl + \`/get/IncludeLocalReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -125,7 +126,7 @@ export class Client<RequestOption> {
* Request URI: /get/IncludeRemoteReference
*/
public async getIncludeRemoteReference(params: Params$getIncludeRemoteReference, option?: RequestOption): Promise<void> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeRemoteReference\`;
const url = this.baseUrl + \`/get/IncludeRemoteReference\`;
const headers = {
\\"Content-Type\\": \\"application/json\\"
};
Expand All @@ -139,7 +140,7 @@ export class Client<RequestOption> {
* Request URI: /FullRemoteReference
*/
public async getFullRemoteReference(params: Params$getFullRemoteReference, option?: RequestOption): Promise<Response$getFullRemoteReference$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/FullRemoteReference\`;
const url = this.baseUrl + \`/FullRemoteReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -153,7 +154,7 @@ export class Client<RequestOption> {
* Request URI: /get/reference/items
*/
public async getReferenceItems(option?: RequestOption): Promise<Response$getReferenceItems$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/reference/items\`;
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -164,7 +165,7 @@ export class Client<RequestOption> {
* Request URI: /get/books/{id}
*/
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -175,7 +176,7 @@ export class Client<RequestOption> {
* Request URI: /get/books/{id}
*/
public async deleteBook(params: Params$deleteBook, option?: RequestOption): Promise<Response$deleteBook$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
Expand Down
33 changes: 18 additions & 15 deletions test/__tests__/__snapshots__/template-only-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ 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) { }
private baseUrl: string;
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
public async getIncludeLocalReference(params: Params$getIncludeLocalReference, option?: RequestOption): Promise<Response$getIncludeLocalReference$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeLocalReference\`;
const url = this.baseUrl + \`/get/IncludeLocalReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -69,7 +70,7 @@ export class Client<RequestOption> {
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public async getIncludeRemoteReference(params: Params$getIncludeRemoteReference, option?: RequestOption): Promise<void> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeRemoteReference\`;
const url = this.baseUrl + \`/get/IncludeRemoteReference\`;
const headers = {
\\"Content-Type\\": \\"application/json\\"
};
Expand All @@ -79,7 +80,7 @@ export class Client<RequestOption> {
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.replace(/\\\\/$/, \\"\\") + \`/FullRemoteReference\`;
const url = this.baseUrl + \`/FullRemoteReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -89,21 +90,21 @@ export class Client<RequestOption> {
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.replace(/\\\\/$/, \\"\\") + \`/get/reference/items\`;
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public async deleteBook(params: Params$deleteBook, option?: RequestOption): Promise<Response$deleteBook$Status$200[\\"application/json\\"]> {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
Expand Down Expand Up @@ -170,9 +171,10 @@ 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) { }
private baseUrl: string;
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
public getIncludeLocalReference(params: Params$getIncludeLocalReference, option?: RequestOption): Response$getIncludeLocalReference$Status$200[\\"application/json\\"] {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeLocalReference\`;
const url = this.baseUrl + \`/get/IncludeLocalReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -182,7 +184,7 @@ export class Client<RequestOption> {
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public getIncludeRemoteReference(params: Params$getIncludeRemoteReference, option?: RequestOption): void {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/IncludeRemoteReference\`;
const url = this.baseUrl + \`/get/IncludeRemoteReference\`;
const headers = {
\\"Content-Type\\": \\"application/json\\"
};
Expand All @@ -192,7 +194,7 @@ export class Client<RequestOption> {
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.replace(/\\\\/$/, \\"\\") + \`/FullRemoteReference\`;
const url = this.baseUrl + \`/FullRemoteReference\`;
const headers = {
Accept: \\"application/json\\"
};
Expand All @@ -202,21 +204,21 @@ export class Client<RequestOption> {
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public getReferenceItems(option?: RequestOption): Response$getReferenceItems$Status$200[\\"application/json\\"] {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/reference/items\`;
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public getBookById(params: Params$getBookById, option?: RequestOption): Response$getBookById$Status$200[\\"application/json\\"] {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public deleteBook(params: Params$deleteBook, option?: RequestOption): Response$deleteBook$Status$200[\\"application/json\\"] {
const url = this.baseUrl.replace(/\\\\/$/, \\"\\") + \`/get/books/\${params.parameter.id}\`;
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
const headers = {
Accept: \\"application/json\\"
};
Expand Down Expand Up @@ -254,7 +256,8 @@ 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) { }
private baseUrl: string;
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
}
"
`;
Loading

0 comments on commit 4d62643

Please sign in to comment.