* Resolve the request by returning a body plus additional HTTP information (such as response * headers) if provided. * If the request specifies an expected body type, the body is converted into the requested type. * Otherwise, the body is converted to `JSON` by default. * * Both succe
(
body:
| ArrayBuffer
| Blob
| boolean
| string
| number
| Object
| (boolean | string | number | Object | null)[]
| null,
opts: {
headers?: HttpHeaders | {[name: string]: string | string[]};
status?: number;
statusText?: string;
} = {},
)
| 61 | * Both successful and unsuccessful responses can be delivered via `flush()`. |
| 62 | */ |
| 63 | flush( |
| 64 | body: |
| 65 | | ArrayBuffer |
| 66 | | Blob |
| 67 | | boolean |
| 68 | | string |
| 69 | | number |
| 70 | | Object |
| 71 | | (boolean | string | number | Object | null)[] |
| 72 | | null, |
| 73 | opts: { |
| 74 | headers?: HttpHeaders | {[name: string]: string | string[]}; |
| 75 | status?: number; |
| 76 | statusText?: string; |
| 77 | } = {}, |
| 78 | ): void { |
| 79 | if (this.cancelled) { |
| 80 | throw new Error(`Cannot flush a cancelled request.`); |
| 81 | } |
| 82 | const url = this.request.urlWithParams; |
| 83 | const headers = |
| 84 | opts.headers instanceof HttpHeaders ? opts.headers : new HttpHeaders(opts.headers); |
| 85 | body = _maybeConvertBody(this.request.responseType, body); |
| 86 | let statusText: string | undefined = opts.statusText; |
| 87 | let status: number = opts.status !== undefined ? opts.status : HttpStatusCode.Ok; |
| 88 | if (opts.status === undefined) { |
| 89 | if (body === null) { |
| 90 | status = HttpStatusCode.NoContent; |
| 91 | statusText ||= 'No Content'; |
| 92 | } else { |
| 93 | statusText ||= 'OK'; |
| 94 | } |
| 95 | } |
| 96 | if (statusText === undefined) { |
| 97 | throw new Error('statusText is required when setting a custom status.'); |
| 98 | } |
| 99 | if (status >= 200 && status < 300) { |
| 100 | this.observer.next(new HttpResponse<any>({body, headers, status, statusText, url})); |
| 101 | this.observer.complete(); |
| 102 | } else { |
| 103 | this.observer.error(new HttpErrorResponse({error: body, headers, status, statusText, url})); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Resolve the request by returning an `ErrorEvent` (e.g. simulating a network failure). |
no test coverage detected