Skip to content

Feature/angular #944

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 22 commits into from
Jan 28, 2022
Merged

Feature/angular #944

merged 22 commits into from
Jan 28, 2022

Conversation

ferdikoomen
Copy link
Owner

Creating the PR for the Angular client (WIP) needs some changes on the error handling.
See #935 (comment) for some discussions on the work so far

@codecov
Copy link

codecov bot commented Jan 27, 2022

Codecov Report

Merging #944 (71fd3ed) into master (f8a3729) will decrease coverage by 0.09%.
The diff coverage is 72.72%.

❗ Current head 71fd3ed differs from pull request most recent head ff1cb27. Consider uploading reports for the commit ff1cb27 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #944      +/-   ##
==========================================
- Coverage   86.76%   86.66%   -0.10%     
==========================================
  Files         107      107              
  Lines        1685     1695      +10     
  Branches      350      352       +2     
==========================================
+ Hits         1462     1469       +7     
- Misses        223      226       +3     
Impacted Files Coverage Δ
src/utils/getHttpRequestName.ts 33.33% <0.00%> (-6.67%) ⬇️
src/index.ts 80.00% <50.00%> (-2.15%) ⬇️
src/utils/registerHandlebarTemplates.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8a3729...ff1cb27. Read the comment docs.

@ferdikoomen
Copy link
Owner Author

ferdikoomen commented Jan 27, 2022

@kutlugsahin

The request method now looks like this (i added comments to make it clear, but those are not in the output):

/**
 * Request method
 * @param config The OpenAPI configuration object
 * @param http The Angular HTTP client
 * @param options The request options from the service
 * @returns Observable<T>
 * @throws ApiError
 */
export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: ApiRequestOptions): Observable<T> => {
    const url = getUrl(config, options);
    const formData = getFormData(options);
    const body = getRequestBody(options);

    // getHeaders has been converted to observable
    return getHeaders(config, options).pipe(
        mergeMap( headers => {
            // When have received the headers, then we can send the request
            return sendRequest<T>(config, options, http, url, formData, body, headers);
        }),
        // We map the response (if successful)
        map(response => {
            const responseBody = getResponseBody(response);
            const responseHeader = getResponseHeader(response, options.responseHeader);
            return {
                url,
                ok: response.ok,
                status: response.status,
                statusText: response.statusText,
                body: responseHeader ?? responseBody,
            } as ApiResult;
        }),
        // If there is a HTTP error, then we capture it
        // If this is a error without a status (network error) we just throw it
        // Otherwise we convert it to a result object for further processing (error code could be associated with a custom error message)
        catchError((error: HttpErrorResponse) => {
            if (!error.status) {
                return throwError(() => error);
            }
            return of({
                url,
                ok: error.ok,
                status: error.status,
                statusText: error.statusText,
                body: error.statusText,
            } as ApiResult);
        }),
        // We catch the ApiResult object and check it for errors
        // If catchErrorCodes does not find anything then we return the result
        map(result => {
            catchErrorCodes(options, result);
            return result.body as T;
        }),
        // If catchErrorCodes did throw an error, then we just throw it to the subscriber
        catchError((error: ApiError) => {
            return throwError(() => error);
        }),
    );
};

@ferdikoomen
Copy link
Owner Author

The client passes the test cases and you can still do things like catchError() and retry() on the observer

@ferdikoomen
Copy link
Owner Author

note to self: need to switch to switchMap instead of mergeMap

@ferdikoomen ferdikoomen merged commit 8d5e458 into master Jan 28, 2022
@ferdikoomen ferdikoomen deleted the feature/angular branch January 28, 2022 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants