-
-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40ef68f
commit 975d44e
Showing
23 changed files
with
363 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ export enum HttpClient { | |
XHR = 'xhr', | ||
NODE = 'node', | ||
AXIOS = 'axios', | ||
ANGULAR = 'angular', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
async function getHeaders(options: ApiRequestOptions): Promise<HttpHeaders> { | ||
const token = await resolve(options, OpenAPI.TOKEN); | ||
const username = await resolve(options, OpenAPI.USERNAME); | ||
const password = await resolve(options, OpenAPI.PASSWORD); | ||
const additionalHeaders = await resolve(options, OpenAPI.HEADERS); | ||
|
||
const defaultHeaders = Object.entries({ | ||
Accept: 'application/json', | ||
...additionalHeaders, | ||
...options.headers, | ||
}) | ||
.filter(([_, value]) => isDefined(value)) | ||
.reduce((headers, [key, value]) => ({ | ||
...headers, | ||
[key]: String(value), | ||
}), {} as Record<string, string>); | ||
|
||
const headers = new HttpHeaders(defaultHeaders); | ||
|
||
if (isStringWithValue(token)) { | ||
headers.append('Authorization', `Bearer ${token}`); | ||
} | ||
|
||
if (isStringWithValue(username) && isStringWithValue(password)) { | ||
const credentials = base64(`${username}:${password}`); | ||
headers.append('Authorization', `Basic ${credentials}`); | ||
} | ||
|
||
if (options.body) { | ||
if (options.mediaType) { | ||
headers.append('Content-Type', options.mediaType); | ||
} else if (isBlob(options.body)) { | ||
headers.append('Content-Type', options.body.type || 'application/octet-stream'); | ||
} else if (isString(options.body)) { | ||
headers.append('Content-Type', 'text/plain'); | ||
} else if (!isFormData(options.body)) { | ||
headers.append('Content-Type', 'application/json'); | ||
} | ||
} | ||
|
||
return headers; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function getRequestBody(options: ApiRequestOptions): BodyInit | undefined { | ||
if (options.body) { | ||
if (options.mediaType?.includes('/json')) { | ||
return JSON.stringify(options.body) | ||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) { | ||
return options.body; | ||
} else { | ||
return JSON.stringify(options.body); | ||
} | ||
} | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
async function getResponseBody(response: Response): Promise<any> { | ||
if (response.status !== 204) { | ||
try { | ||
const contentType = response.headers.get('Content-Type'); | ||
if (contentType) { | ||
const isJSON = contentType.toLowerCase().startsWith('application/json'); | ||
if (isJSON) { | ||
return await response.json(); | ||
} else { | ||
return await response.text(); | ||
} | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function getResponseHeader(response: Response, responseHeader?: string): string | undefined { | ||
if (responseHeader) { | ||
const content = response.headers.get(responseHeader); | ||
if (isString(content)) { | ||
return content; | ||
} | ||
} | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{{>header}} | ||
|
||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { ApiError } from './ApiError'; | ||
import type { ApiRequestOptions } from './ApiRequestOptions'; | ||
import type { ApiResult } from './ApiResult'; | ||
import { CancelablePromise } from './CancelablePromise'; | ||
import type { OnCancel } from './CancelablePromise'; | ||
import { OpenAPI } from './OpenAPI'; | ||
|
||
{{>functions/isDefined}} | ||
|
||
|
||
{{>functions/isString}} | ||
|
||
|
||
{{>functions/isStringWithValue}} | ||
|
||
|
||
{{>functions/isBlob}} | ||
|
||
|
||
{{>functions/isFormData}} | ||
|
||
|
||
{{>functions/base64}} | ||
|
||
|
||
{{>functions/getQueryString}} | ||
|
||
|
||
{{>functions/getUrl}} | ||
|
||
|
||
{{>functions/getFormData}} | ||
|
||
|
||
{{>functions/resolve}} | ||
|
||
|
||
{{>angular/getHeaders}} | ||
|
||
|
||
{{>angular/getRequestBody}} | ||
|
||
|
||
{{>angular/sendRequest}} | ||
|
||
|
||
{{>angular/getResponseHeader}} | ||
|
||
|
||
{{>angular/getResponseBody}} | ||
|
||
|
||
{{>functions/catchErrors}} | ||
|
||
|
||
/** | ||
* Request using fetch client | ||
* @param http The Angular HTTP client | ||
* @param options The request options from the service | ||
* @returns CancelablePromise<T> | ||
* @throws ApiError | ||
*/ | ||
export function request<T>(http: HttpClient, options: ApiRequestOptions): Observable<T> { | ||
return new CancelablePromise<T>(async (resolve, reject, onCancel) => { | ||
try { | ||
const url = getUrl(options); | ||
const formData = getFormData(options); | ||
const body = getRequestBody(options); | ||
const headers = await getHeaders(options); | ||
|
||
if (!onCancel.isCancelled) { | ||
const response = await sendRequest(options, url, formData, body, headers, onCancel); | ||
const responseBody = await getResponseBody(response); | ||
const responseHeader = getResponseHeader(response, options.responseHeader); | ||
|
||
const result: ApiResult = { | ||
url, | ||
ok: response.ok, | ||
status: response.status, | ||
statusText: response.statusText, | ||
body: responseHeader || responseBody, | ||
}; | ||
|
||
catchErrors(options, result); | ||
|
||
resolve(result.body); | ||
} | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
async function sendRequest( | ||
options: ApiRequestOptions, | ||
url: string, | ||
formData: FormData | undefined, | ||
body: BodyInit | undefined, | ||
headers: Headers, | ||
onCancel: OnCancel | ||
): Promise<Response> { | ||
const controller = new AbortController(); | ||
|
||
const request: RequestInit = { | ||
headers, | ||
body: body || formData, | ||
method: options.method, | ||
signal: controller.signal, | ||
}; | ||
|
||
if (OpenAPI.WITH_CREDENTIALS) { | ||
request.credentials = OpenAPI.CREDENTIALS; | ||
} | ||
|
||
onCancel(() => controller.abort()); | ||
|
||
return await fetch(url, request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{{~#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals~}} | ||
{{~#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals~}} | ||
{{~#equals @root.httpClient 'axios'}}{{>axios/request}}{{/equals~}} | ||
{{~#equals @root.httpClient 'angular'}}{{>angular/request}}{{/equals~}} | ||
{{~#equals @root.httpClient 'node'}}{{>node/request}}{{/equals~}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.