Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Generate TypeScript strict mode compatible code. #3

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

Merged
merged 1 commit into from
Jan 25, 2018
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
26 changes: 13 additions & 13 deletions example/ts/exampleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ type IFetch = HttpClientUtility.IFetch;
type IFetchRequest = HttpClientUtility.IFetchRequest;

class ExampleApiHttpClient implements IExampleApi {
constructor(fetch: IFetch, baseUri: string) {
constructor(fetch: IFetch, baseUri?: string) {
if (typeof fetch !== 'function') {
throw new TypeError('fetch must be a function.');
}
Expand Down Expand Up @@ -366,7 +366,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetWidgetsResponse = null;
let value: IGetWidgetsResponse | null = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should this be IGetWidgetsResponse? instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's not valid TypeScript. ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! 😊

if (result.json) {
if (status === 200) {
value = result.json;
Expand All @@ -393,7 +393,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: ICreateWidgetResponse = null;
let value: ICreateWidgetResponse | null = null;
if (result.json) {
if (status === 201) {
value = { widget: result.json };
Expand Down Expand Up @@ -422,7 +422,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetWidgetResponse = null;
let value: IGetWidgetResponse | null = null;
if (result.json) {
if (status === 200) {
value = { widget: result.json };
Expand Down Expand Up @@ -456,7 +456,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IDeleteWidgetResponse = null;
let value: IDeleteWidgetResponse | null = null;
if (result.json) {
if (status === 204) {
value = {};
Expand Down Expand Up @@ -487,7 +487,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IEditWidgetResponse = null;
let value: IEditWidgetResponse | null = null;
if (result.json) {
if (status === 200) {
value = { widget: result.json };
Expand All @@ -514,7 +514,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetWidgetBatchResponse = null;
let value: IGetWidgetBatchResponse | null = null;
if (result.json) {
if (status === 200) {
value = { results: result.json };
Expand Down Expand Up @@ -543,7 +543,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetWidgetWeightResponse = null;
let value: IGetWidgetWeightResponse | null = null;
if (result.json) {
if (status === 200) {
value = result.json;
Expand All @@ -569,7 +569,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetPreferenceResponse = null;
let value: IGetPreferenceResponse | null = null;
if (result.json) {
if (status === 200) {
value = { value: result.json };
Expand Down Expand Up @@ -597,7 +597,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: ISetPreferenceResponse = null;
let value: ISetPreferenceResponse | null = null;
if (result.json) {
if (status === 200) {
value = { value: result.json };
Expand All @@ -619,7 +619,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IGetInfoResponse = null;
let value: IGetInfoResponse | null = null;
if (result.json) {
if (status === 200) {
value = result.json;
Expand All @@ -641,7 +641,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: INotRestfulResponse = null;
let value: INotRestfulResponse | null = null;
if (result.json) {
if (status === 200) {
value = {};
Expand All @@ -664,7 +664,7 @@ class ExampleApiHttpClient implements IExampleApi {
return fetchResponse(this._fetch, this._baseUri + uri, fetchRequest)
.then(result => {
const status = result.response.status;
let value: IKitchenResponse = null;
let value: IKitchenResponse | null = null;
if (result.json) {
if (status === 200) {
value = {};
Expand Down
4 changes: 2 additions & 2 deletions src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
code.WriteLine();
using (code.Block($"class {capModuleName}HttpClient" + IfTypeScript($" implements I{capModuleName}") + " {", "}"))
{
using (code.Block("constructor(fetch" + IfTypeScript(": IFetch") + ", baseUri" + IfTypeScript(": string") + ") {", "}"))
using (code.Block("constructor(fetch" + IfTypeScript(": IFetch") + ", baseUri" + IfTypeScript("?: string") + ") {", "}"))
{
using (code.Block("if (typeof fetch !== 'function') {", "}"))
code.WriteLine("throw new TypeError('fetch must be a function.');");
Expand Down Expand Up @@ -192,7 +192,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
using (code.Block(".then(result => {", "});"))
{
code.WriteLine("const status = result.response.status;");
code.WriteLine("let value" + IfTypeScript($": I{capMethodName}Response") + " = null;");
code.WriteLine("let value" + IfTypeScript($": I{capMethodName}Response | null") + " = null;");
using (code.Block($"if (result.json) {{", "}"))
{
var validResponses = httpMethodInfo.ValidResponses;
Expand Down