RicoSuter/NSwag

NSwag Typescript compiler: API is not getting called on a PUT function

Open

#2350 opened on Aug 13, 2019

View on GitHub
 (6 comments) (0 reactions) (0 assignees)C# (6,291 stars) (1,189 forks)batch import
help wantedproject: NSwag.CodeGeneration.TypeScripttype: bug

Description

update(accountId: string, command: UpdateAccountCommand, version: string): Observable<FileResponse> {
        let url_ = this.baseUrl + "/v{version}/Accounts/{accountId}";
        if (accountId === undefined || accountId === null)
            throw new Error("The parameter 'accountId' must be defined.");
        url_ = url_.replace("{accountId}", encodeURIComponent("" + accountId)); 
        if (version === undefined || version === null)
            throw new Error("The parameter 'version' must be defined.");
        url_ = url_.replace("{version}", encodeURIComponent("" + version)); 
        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(command);

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "application/json", 
                "Accept": "application/octet-stream"
            })
        };

        return this.http.request("put", url_, options_).pipe(_observableMergeMap((response_ : any) => {
            return this.processUpdate(response_);
        })).pipe(_observableCatch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.processUpdate(<any>response_);
                } catch (e) {
                    return <Observable<FileResponse>><any>_observableThrow(e);
                }
            } else
                return <Observable<FileResponse>><any>_observableThrow(response_);
        }));
    }

    protected processUpdate(response: HttpResponseBase): Observable<FileResponse> {
        const status = response.status;
        const responseBlob = 
            response instanceof HttpResponse ? response.body : 
            (<any>response).error instanceof Blob ? (<any>response).error : undefined;

        let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }};
        if (status === 200 || status === 206) {
            const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
            const fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
            const fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
            return _observableOf({ fileName: fileName, data: <any>responseBlob, status: status, headers: _headers });
        } else if (status !== 200 && status !== 204) {
            return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            }));
        }
        return _observableOf<FileResponse>(<any>null);
    }

This is the code for the update method and here is what I call in my angular component:

const form = this.companyForm.value;
    const account: UpdateAccountCommand = new UpdateAccountCommand();
    account.init({
      company: form.company.compName,
      status: 1,
      address: form.company.address,
      address2: form.company.address2,
      city: form.company.city,
      region: form.company.state,
      postalCode: form.company.postal,
      country: form.company.country,
      mainPhone: form.company.phone,
      defaultDateFormat: form.defaults.defaultDate,
      defaultTimeFormat: form.defaults.defaultTime,
      defaultDateTimeFormat: null,
      defaultCurrencyFormat: form.defaults.defaultCurrency,
      defaultNumberFormat: form.defaults.defaultNumber,
      pcFirstName: form.primaryContact.firstName,
      pcLastName: form.primaryContact.lastName,
      pcEmail: form.primaryContact.email,
      pcPhone: form.primaryContact.phone,
      bcFirstName: null,
      bcLastName: null,
      bcEmail: null,
      bcPhone: null
    });
    this.acctService.update(this.refService.getAcctId(), account, '1').subscribe(result => {
      console.log(result);
    },
    error => {
      console.log(error);
    });

This is what get returned:

{fileName: undefined, data: Blob, status: 200, headers: {…}}

The problem is that the function is acting like an API call has been made and it returns the HttpResponse, but the API hasn't been called. In chrome, the "Network" tab in Dev Tools doesn't show that a request has been made either. I checked my interceptor to verify that the call is reaching that and it is. I'm not sure why the call is returning an HttpResponse if a call was never made. Could you give me some insight as to why this would be? Thanks in advance!

Contributor guide