-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from lemoncloud-io/feature/louis-update-azure-…
…http Feature/louis update azure http
- Loading branch information
Showing
8 changed files
with
198 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { Body, Headers, Params } from '../types'; | ||
import { AzureStorageService, USE_X_LEMON_IDENTITY_KEY } from '../token-storage'; | ||
|
||
/** | ||
* Class to build and execute HTTP requests with AWS signing | ||
* @example | ||
* ```ts | ||
* const response: AxiosResponse<OAuthResponse> = await new AzureHttpRequestBuilder({ | ||
* method: 'GET', | ||
* baseURL: `https://api.lemoncloud.io/v1/oauth`, | ||
* }) | ||
* .addHeaders({ Cookie: this.cookie }) | ||
* .setParams({ page: 0 }) | ||
* .execute(); | ||
* ``` | ||
*/ | ||
export class AzureHttpRequestBuilder { | ||
private axiosInstance: AxiosInstance; | ||
private config: AxiosRequestConfig = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'get', | ||
}; | ||
|
||
/** | ||
* Creates an instance of AWSHttpRequestBuilder. | ||
* @param {AzureStorageService} tokenStorage - The AWS storage service for token management. | ||
* @param {AxiosRequestConfig} config - The Axios request configuration. | ||
* @throws {Error} If tokenStorage, method, or baseURL are not defined. | ||
*/ | ||
constructor( | ||
private readonly tokenStorage: AzureStorageService, | ||
config: AxiosRequestConfig | ||
) { | ||
if (!tokenStorage) { | ||
throw new Error('tokenStorage should be defined!'); | ||
} | ||
if (!config.method) { | ||
throw new Error('method should be defined!'); | ||
} | ||
if (!config.baseURL) { | ||
throw new Error('baseURL should be defined!'); | ||
} | ||
this.config = { ...this.config, ...config }; | ||
this.axiosInstance = axios.create(this.config); | ||
} | ||
|
||
/** | ||
* Sets the request headers. | ||
* @param {Headers} headers - Headers to set. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
setHeaders(headers: Headers): AzureHttpRequestBuilder { | ||
this.config.headers = headers; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the request parameters. | ||
* @param {Params} params - Parameters to set. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
setParams(params: Params): AzureHttpRequestBuilder { | ||
this.config.params = params; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the request body. | ||
* @param {Body} data - Body data to set. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
setBody(data: Body): AzureHttpRequestBuilder { | ||
this.config.data = data; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the request method. | ||
* @param {string} method - HTTP method to set. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
setMethod(method: string): AzureHttpRequestBuilder { | ||
this.config.method = method; | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds additional headers to the request. | ||
* @param {Headers} headers - Headers to add. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
addHeaders(headers: Headers = {}): AzureHttpRequestBuilder { | ||
this.config.headers = { ...this.config.headers, ...headers }; | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds additional Axios request configuration. | ||
* @param {AxiosRequestConfig} config - The configuration to add. | ||
* @returns {AWSHttpRequestBuilder} - Returns the current instance to allow method chaining. | ||
*/ | ||
addAxiosRequestConfig(config: AxiosRequestConfig): AzureHttpRequestBuilder { | ||
this.config = { ...this.config, ...config }; | ||
return this; | ||
} | ||
|
||
/** | ||
* Executes the HTTP request. | ||
* @template T | ||
* @returns {Promise<AxiosResponse<T>>} - Promise containing the response. | ||
* @throws {Error} If an error occurs during the request. | ||
*/ | ||
async execute<T>(): Promise<AxiosResponse<T>> { | ||
try { | ||
await this.addCodeParams(); | ||
await this.addBearerTokenToHeader(); | ||
await this.addXLemonIdentityToHeader(); | ||
return await this.axiosInstance.request<T>(this.config); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
/** | ||
* Adds code parameters to the request configuration. | ||
* Retrieves the `hostKey` and `clientId` from the token storage and sets them as request parameters. | ||
* @private | ||
* @async | ||
* @returns {Promise<void>} - A promise that resolves when the parameters are added. | ||
*/ | ||
private async addCodeParams(): Promise<void> { | ||
const code = (await this.tokenStorage.getItem('hostKey')) || ''; | ||
if (!code) { | ||
return; | ||
} | ||
const clientId = (await this.tokenStorage.getItem('clientId')) || 'default'; | ||
const originParams = this.config.params || {}; | ||
this.setParams({ ...originParams, code, clientId }); | ||
} | ||
|
||
/** | ||
* Adds a Bearer token to the request headers. | ||
* Retrieves the `identityToken` from the token storage and sets it as the `Authorization` header. | ||
* @private | ||
* @async | ||
* @returns {Promise<void>} - A promise that resolves when the token is added. | ||
*/ | ||
private async addBearerTokenToHeader(): Promise<void> { | ||
const identityToken = (await this.tokenStorage.getItem('identityToken')) || ''; | ||
if (!identityToken) { | ||
return; | ||
} | ||
this.addHeaders({ Authorization: `Bearer ${identityToken}` }); | ||
} | ||
|
||
/** | ||
* Adds the x-lemon-identity token to the request headers if required. | ||
* Checks if the `USE_X_LEMON_IDENTITY_KEY` is set in the token storage and, if true, | ||
* retrieves the `identityToken` and sets it as the `x-lemon-identity` header. | ||
* @private | ||
* @async | ||
* @returns {Promise<void>} - A promise that resolves when the token is added. | ||
*/ | ||
private async addXLemonIdentityToHeader(): Promise<void> { | ||
const useXLemonIdentity = await this.tokenStorage.getItem(USE_X_LEMON_IDENTITY_KEY); | ||
if (!useXLemonIdentity || useXLemonIdentity === 'false') { | ||
return; | ||
} | ||
const identityToken = await this.tokenStorage.getItem('identityToken'); | ||
this.addHeaders({ 'x-lemon-identity': identityToken }); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from './http-request.builder'; | ||
export * from './aws-http-request.builder'; | ||
export * from './azure-http-request.builder'; |
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,5 +1,6 @@ | ||
export * from './utils'; | ||
export * from './types'; | ||
export * from './core'; | ||
export * from './http'; | ||
export * from './token-storage'; | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './vendor'; |
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