Nswag: Generated code breaks after update to TypeScript 2.6.1

Created on 2 Nov 2017  路  14Comments  路  Source: RicoSuter/NSwag

Hi,

Since we've updated to TypeScript 2.6.1 the generated output breaks our build. This is due to the headers in the output which don't conform to the new RequestInit interface in lib.dom.ts. In the old version it accepted a <any> type for the headers property but now it only accepts <Headers | string[][]>.

For now we can simply solve this by reverting to TypeScript version 2.5.3.

Is there a way to influence the way the headers get generated?

bug

All 14 comments

Can you show me an example what is generated and what is expected? Is there a way to generate it so that it works with both versions?

Currently we have something like:

headers: {
  "Content-Type": "text/plain",
  "Content-Length": content.length.toString(),
  "X-Custom-Header": "ProcessThisImmediately",
};

what if we just add new Headers like, this should fix it and also work with older versions?

headers: new Headers({
  "Content-Type": "text/plain",
  "Content-Length": content.length.toString(),
  "X-Custom-Header": "ProcessThisImmediately",
});

And we are talking only about the fetch/aurelia template?

Yep, sorry I didn't mention that, we're using the fetch template

I'm wondering why it currently works without new Headers?

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers

The documentation does not mention that it is allowed to directly assign an object...

I'm not sure, maybe it's a legacy thing?

Works like a charm!

Thank you for the speedy solution.

Ok, thanks for testing. I hope you can live with the CI artifacts for now, the current version of NSwag has a lot of changes so I have to heavily test it before I can publicly release it...

I guess this fix is not compatible to TypeScript 2.4 and earlier. We are still using TypeScript 2.4.2 and ran into an issue. Since NSwag 11.12.2.0 all generated header like Content-Type and Accept are lost. So our RestApi is throwing 401.

Issue with TypeScript 2.4.2

        let options_ = <RequestInit>{
            method: "GET",
            headers: new Headers({
                "Content-type": "application/json",
                "Accept": "application/json"
            })
        };

No Issue with TypeScript 2.4.2

        let options_ = <RequestInit>{
            method: "GET",
            headers: {
                "Content-type": "application/json",
                "Accept": "application/json"
            }
        };

How is this possible? Is this a compiler feature?

Why would this not work in older TS versions? It's a browser/language feature and not a TS feature:

image

Assigning an object {} to header is not specified by MDN, but supported by the browser.
We are using an fetch Decorator to set the token. In the old version, setting headers.Authorization works fine.

const fetchDecorator = {
  fetch(url: any, init?: RequestInit): Promise<Response> {
    const token = 'Bearer ' + identityManager.getToken();
    if (init) {
      init.headers.Authorization = token;
      init.credentials = 'include';
    }
    return window.fetch(url, init);
  }
};

Now we have to use the headers.set method, to inject the token.

const fetchDecorator = {
  fetch(url: any, init?: RequestInit): Promise<Response> {
    const token = 'Bearer ' + identityManager.getToken();
    if (init) {
      const headers = init.headers as Headers;
      headers.set('Authorization', token);
      init.credentials = 'include';
    }
  return window.fetch(url, init);
  }
};
Was this page helpful?
0 / 5 - 0 ratings