Skip to content

Commit 9ba8da4

Browse files
committed
simplify api generator
1 parent e9cee58 commit 9ba8da4

File tree

9 files changed

+61
-79
lines changed

9 files changed

+61
-79
lines changed

AspNetCoreExample.Generator/output/api/NotesApi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// TypeScriptContractGenerator's generated content
33
import { Guid } from './../DataTypes/Guid';
44
import { BlogEntry } from './../DataTypes/BlogEntry';
5-
import { ApiBase } from './../ApiBase/ApiBase';
5+
import { request } from './../ApiBase/ApiBase';
66
import { url } from './../ApiBase/ApiBase';
77

8-
export class NotesApi extends ApiBase implements INotesApi {
8+
export class NotesApi implements INotesApi {
99
addEntry(userId: Guid, entry: BlogEntry): Promise<void> {
10-
return this.makePostRequest(url`/v1/user/${userId}/blog`, entry);
10+
return request('POST', url`/v1/user/${userId}/blog`, entry);
1111
}
1212

1313
addEntries(userId: Guid, entries: BlogEntry[]): Promise<void> {
14-
return this.makePostRequest(url`/v1/user/${userId}/blog/batch`, entries);
14+
return request('POST', url`/v1/user/${userId}/blog/batch`, entries);
1515
}
1616

1717
};

AspNetCoreExample.Generator/output/api/UserApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
// TypeScriptContractGenerator's generated content
33
import { User } from './../DataTypes/User';
44
import { Guid } from './../DataTypes/Guid';
5-
import { ApiBase } from './../ApiBase/ApiBase';
5+
import { request } from './../ApiBase/ApiBase';
66
import { url } from './../ApiBase/ApiBase';
77

8-
export class UserApi extends ApiBase implements IUserApi {
8+
export class UserApi implements IUserApi {
99
createUser(user: User): Promise<void> {
10-
return this.makePostRequest(url`/v1/users`, user);
10+
return request('POST', url`/v1/users`, user);
1111
}
1212

1313
deleteUser(userId: Guid): Promise<void> {
14-
return this.makeDeleteRequest(url`/v1/users/${userId}`);
14+
return request('DELETE', url`/v1/users/${userId}`);
1515
}
1616

1717
getUser(userId: Guid): Promise<User> {
18-
return this.makeGetRequest(url`/v1/users/${userId}`);
18+
return request('GET', url`/v1/users/${userId}`);
1919
}
2020

2121
searchUsers(name: string): Promise<User[]> {
22-
return this.makeGetRequest(url`/v1/users?name=${name}`);
22+
return request('GET', url`/v1/users?name=${name}`);
2323
}
2424

2525
};

AspNetCoreExample.Generator/output/api/WeatherForecastApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
// TypeScriptContractGenerator's generated content
33
import { WeatherForecast } from './../DataTypes/WeatherForecast';
44
import { Guid } from './../DataTypes/Guid';
5-
import { ApiBase } from './../ApiBase/ApiBase';
5+
import { request } from './../ApiBase/ApiBase';
66
import { url } from './../ApiBase/ApiBase';
77

8-
export class WeatherForecastApi extends ApiBase implements IWeatherForecastApi {
8+
export class WeatherForecastApi implements IWeatherForecastApi {
99
get(): Promise<WeatherForecast[]> {
10-
return this.makeGetRequest(url`/WeatherForecast`);
10+
return request('GET', url`/WeatherForecast`);
1111
}
1212

1313
update(city: string, forecast: WeatherForecast): Promise<void> {
14-
return this.makePostRequest(url`/WeatherForecast/Update/${city}`, forecast);
14+
return request('POST', url`/WeatherForecast/Update/${city}`, forecast);
1515
}
1616

1717
reset(seed: number): Promise<void> {
18-
return this.makePostRequest(url`/Reset?seed=${seed}`);
18+
return request('POST', url`/Reset?seed=${seed}`);
1919
}
2020

2121
urlForDownload(city: string): string {
@@ -27,7 +27,7 @@ export class WeatherForecastApi extends ApiBase implements IWeatherForecastApi {
2727
}
2828

2929
newGuid(): Promise<Guid> {
30-
return this.makeGetRequest(url`/WeatherForecast/none`);
30+
return request('GET', url`/WeatherForecast/none`);
3131
}
3232

3333
};

AspNetCoreExample.Generator/output/apiBase/ApiBase.ts

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,14 @@
22

33
export const url = String.raw;
44

5-
export class ApiBase {
6-
public async makeGetRequest(url: string, body?: any): Promise<any> {
7-
const response = await fetch(url, {
8-
method: "GET",
9-
});
10-
return await response.json();
11-
}
12-
13-
public async makePostRequest(url: string, body?: any): Promise<any> {
14-
const response = await fetch(url, {
15-
method: "POST",
16-
body: body && JSON.stringify(body),
17-
});
18-
const textResult = await response.text();
19-
if (textResult !== "") {
20-
return JSON.parse(textResult);
21-
}
22-
}
23-
24-
public async makePutRequest(url: string, body?: any): Promise<any> {
25-
const response = await fetch(url, {
26-
method: "PUT",
27-
body: body && JSON.stringify(body),
28-
});
29-
const textResult = await response.text();
30-
if (textResult !== "") {
31-
return JSON.parse(textResult);
32-
}
33-
}
5+
export const request = async (method: string, url: string, body?: any): Promise<any> => {
6+
const response = await fetch(url, {
7+
method: method,
8+
body: body && JSON.stringify(body),
9+
});
3410

35-
public async makeDeleteRequest(url: string, body?: any): Promise<any> {
36-
const response = await fetch(url, {
37-
method: "DELETE",
38-
body: body && JSON.stringify(body),
39-
});
40-
const textResult = await response.text();
41-
if (textResult !== "") {
42-
return JSON.parse(textResult);
43-
}
11+
const textResult = await response.text();
12+
if (textResult !== "") {
13+
return JSON.parse(textResult);
4414
}
45-
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController
2+
{
3+
public class ApiBaseLocation
4+
{
5+
public string RequestMethodName { get; set; }
6+
public string UrlTagName { get; set; }
7+
public string Location { get; set; }
8+
}
9+
}

TypeScript.ContractGenerator/TypeBuilders/ApiController/ApiControllerTypeBuildingContext.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ public override void Initialize(ITypeGenerator typeGenerator)
2323
BuildAndImportType = t => typeGenerator.BuildAndImportType(Unit, t);
2424

2525
var baseApi = ApiCustomization.GetApiBase(Type);
26-
var urlTag = ApiCustomization.GetUrlTag(Type);
27-
28-
Unit.AddSymbolImport(baseApi.Name, baseApi.Location);
29-
Unit.AddSymbolImport(urlTag.Name, urlTag.Location);
26+
Unit.AddSymbolImport(baseApi.RequestMethodName, baseApi.Location);
27+
Unit.AddSymbolImport(baseApi.UrlTagName, baseApi.Location);
3028

3129
var apiName = ApiCustomization.GetApiClassName(Type);
3230
var interfaceName = ApiCustomization.GetApiInterfaceName(Type);
@@ -37,7 +35,6 @@ public override void Initialize(ITypeGenerator typeGenerator)
3735

3836
var apiClassDefinition = new TypeScriptClassDefinition
3937
{
40-
BaseClass = new TypeScriptTypeReference(baseApi.Name),
4138
ImplementedInterfaces = new TypeScriptType[] {new TypeScriptTypeReference(interfaceName)},
4239
};
4340

@@ -102,13 +99,17 @@ protected virtual TypeScriptReturnStatement CreateCall(IMethodInfo methodInfo)
10299
return new TypeScriptReturnStatement(routeExpression);
103100
}
104101

102+
var requestMethodName = ApiCustomization.GetApiBase(methodInfo.DeclaringType!).RequestMethodName;
103+
var verb = ApiCustomization.GetMethodVerb(methodInfo);
104+
var requestExpression = new TypeScriptVariableReference(requestMethodName);
105+
var methodExpression = (TypeScriptExpression)new TypeScriptStringLiteral(verb);
105106
var bodyExpression = ApiCustomization.GetMethodBodyExpression(methodInfo);
106107
var arguments = bodyExpression == null
107-
? new[] {routeExpression}
108-
: new[] {routeExpression, bodyExpression};
108+
? new[] {methodExpression, routeExpression}
109+
: new[] {methodExpression, routeExpression, bodyExpression};
109110

110111
return new TypeScriptReturnStatement(
111-
new TypeScriptMethodCallExpression(new TypeScriptThisReference(), ApiCustomization.GetMethodVerb(methodInfo), arguments)
112+
new TypeScriptFunctionCallExpression(requestExpression, arguments)
112113
);
113114
}
114115

TypeScript.ContractGenerator/TypeBuilders/ApiController/DefaultApiCustomization.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController
1616
{
1717
public class DefaultApiCustomization : IApiCustomization
1818
{
19-
public virtual TypeLocation GetApiBase(ITypeInfo type) => new TypeLocation
19+
public virtual ApiBaseLocation GetApiBase(ITypeInfo type) => new ApiBaseLocation
2020
{
21-
Name = "ApiBase",
22-
Location = "ApiBase/ApiBase",
23-
};
24-
25-
public virtual TypeLocation GetUrlTag(ITypeInfo type) => new TypeLocation
26-
{
27-
Name = "url",
21+
RequestMethodName = "request",
22+
UrlTagName = "url",
2823
Location = "ApiBase/ApiBase",
2924
};
3025

@@ -47,7 +42,7 @@ public virtual IParameterInfo[] GetMethodParameters(IMethodInfo methodInfo) =>
4742

4843
public virtual bool IsUrlMethod(IMethodInfo methodInfo)
4944
{
50-
return GetMethodVerb(methodInfo) == "makeGetRequest" && ResolveReturnType(methodInfo.ReturnType).Equals(TypeInfo.From(typeof(void)));
45+
return GetMethodVerb(methodInfo) == "GET" && ResolveReturnType(methodInfo.ReturnType).Equals(TypeInfo.From(typeof(void)));
5146
}
5247

5348
public virtual bool IsAsyncMethod(IMethodInfo methodInfo) => false;
@@ -57,19 +52,25 @@ public virtual string GetMethodVerb(IMethodInfo methodInfo)
5752
var attributes = methodInfo.GetAttributes(inherit : false);
5853

5954
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpGet)))
60-
return "makeGetRequest";
55+
return "GET";
6156

6257
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpPost)))
63-
return "makePostRequest";
58+
return "POST";
6459

6560
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpPut)))
66-
return "makePutRequest";
61+
return "PUT";
6762

6863
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpDelete)))
69-
return "makeDeleteRequest";
64+
return "DELETE";
7065

7166
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpPatch)))
72-
return "makePatchRequest";
67+
return "PATCH";
68+
69+
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpHead)))
70+
return "HEAD";
71+
72+
if (attributes.Any(x => x.HasName(KnownTypeNames.Attributes.HttpOptions)))
73+
return "OPTIONS";
7374

7475
throw new NotSupportedException($"Unresolved http verb for method {methodInfo.Name} at controller {methodInfo.DeclaringType?.Name}");
7576
}

TypeScript.ContractGenerator/TypeBuilders/ApiController/IApiCustomization.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController
77
{
88
public interface IApiCustomization
99
{
10-
public TypeLocation GetApiBase(ITypeInfo type);
11-
public TypeLocation GetUrlTag(ITypeInfo type);
10+
public ApiBaseLocation GetApiBase(ITypeInfo type);
1211

1312
string GetApiClassName(ITypeInfo type);
1413
string GetApiInterfaceName(ITypeInfo type);

TypeScript.ContractGenerator/TypeBuilders/ApiController/KnownTypeNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static class Attributes
2626
public const string HttpPut = "HttpPut";
2727
public const string HttpPatch = "HttpPatch";
2828
public const string HttpDelete = "HttpDelete";
29+
public const string HttpHead = "HttpHead";
30+
public const string HttpOptions = "HttpOptions";
2931
public const string FromBody = "FromBody";
3032
}
3133
}

0 commit comments

Comments
 (0)