Skip to content

Commit

Permalink
Merge pull request #23 from lemoncloud-io/feature/louis-update-type
Browse files Browse the repository at this point in the history
feat: add HttpResponse type
  • Loading branch information
louis-lemon authored Jun 10, 2024
2 parents 09e2167 + 4829273 commit d0b91d5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.10.0
v20.11.1
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Creates an instance of `AWSWebCore` with the specified configuration.

Initializes the AWSWebCore instance.

- `signedRequest<T>(method: string, url: string, params?: Params, body?: Body, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>`
- `signedRequest<T>(method: string, url: string, params?: Params, body?: Body, config?: AxiosRequestConfig): Promise<HttpResponse<T>>`

Makes a signed HTTP request.

Expand Down Expand Up @@ -196,7 +196,7 @@ Creates an instance of `AzureWebCore` with the specified configuration.

Initializes the AzureWebCore instance.

- `signedRequest<T>(method: string, url: string, params?: Params, body?: Body, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>`
- `signedRequest<T>(method: string, url: string, params?: Params, body?: Body, config?: AxiosRequestConfig): Promise<HttpResponse<T>>`

Makes a signed HTTP request.

Expand Down
19 changes: 7 additions & 12 deletions src/core/aws-web.core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AWSWebCoreState,
Body,
HttpResponse,
LemonCredentials,
LemonKMS,
LemonOAuthToken,
Expand All @@ -12,7 +13,7 @@ import {
} from '../types';
import { AWSStorageService, USE_X_LEMON_IDENTITY_KEY } from '../token-storage';
import { calcSignature, LoggerService } from '../utils';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosRequestConfig } from 'axios';
import { AWSHttpRequestBuilder, HttpRequestBuilder } from '../http';
import AWS from 'aws-sdk/global.js';

Expand Down Expand Up @@ -87,15 +88,9 @@ export class AWSWebCore implements WebCoreService {
* @param {Params} [params={}] - The request parameters.
* @param {Body} body - The request body.
* @param {AxiosRequestConfig} [config] - Additional Axios request configuration.
* @returns {Promise<AxiosResponse<T>>} - The Axios response.
* @returns {Promise<Response<T>>} - The Axios response.
*/
async request<T>(
method: string,
url: string,
params: Params = {},
body?: Body,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
async request<T>(method: string, url: string, params: Params = {}, body?: Body, config?: AxiosRequestConfig): Promise<HttpResponse<T>> {
const builder = new HttpRequestBuilder({
method,
baseURL: url,
Expand Down Expand Up @@ -127,15 +122,15 @@ export class AWSWebCore implements WebCoreService {
* @param {Params} [params={}] - The request parameters.
* @param {Body} body - The request body.
* @param {AxiosRequestConfig} [config] - Additional Axios request configuration.
* @returns {Promise<AxiosResponse<T>>} - The Axios response.
* @returns {Promise<HttpResponse<T>>} - The Axios response.
*/
async signedRequest<T>(
method: string,
url: string,
params: Params = {},
body?: Body,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
): Promise<HttpResponse<T>> {
const builder = new AWSHttpRequestBuilder(this.tokenStorage, {
method,
baseURL: url,
Expand Down Expand Up @@ -250,7 +245,7 @@ export class AWSWebCore implements WebCoreService {
body = { ...body, domain };
}

const response: AxiosResponse<LemonOAuthToken> = await this.signedRequest(
const response: HttpResponse<LemonOAuthToken> = await this.signedRequest(
'POST',
url ? url : `${this.config.oAuthEndpoint}/oauth/${cached.authId}/refresh`,
{},
Expand Down
18 changes: 6 additions & 12 deletions src/core/azure-web.core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AzureWebCoreState, Body, LemonOAuthToken, Params, WebCoreConfig, WebCoreService } from '../types';
import { AzureWebCoreState, Body, HttpResponse, LemonOAuthToken, Params, WebCoreConfig, WebCoreService } from '../types';
import { AzureStorageService, USE_X_LEMON_IDENTITY_KEY } from '../token-storage';
import { LoggerService } from '../utils';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosRequestConfig } from 'axios';
import { AzureHttpRequestBuilder, HttpRequestBuilder } from '../http';

/**
Expand Down Expand Up @@ -58,15 +58,9 @@ export class AzureWebCore implements WebCoreService {
* @param {Params} [params={}] - The request parameters.
* @param {Body} body - The request body.
* @param {AxiosRequestConfig} [config] - Additional Axios request configuration.
* @returns {Promise<AxiosResponse<T>>} - The Axios response.
* @returns {Promise<HttpResponse<T>>} - The Axios response.
*/
async request<T>(
method: string,
url: string,
params: Params = {},
body?: Body,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
async request<T>(method: string, url: string, params: Params = {}, body?: Body, config?: AxiosRequestConfig): Promise<HttpResponse<T>> {
const builder = new HttpRequestBuilder({
method,
baseURL: url,
Expand Down Expand Up @@ -98,15 +92,15 @@ export class AzureWebCore implements WebCoreService {
* @param {Params} [params={}] - The URL parameters for the request.
* @param {Body} [body] - The request body.
* @param {AxiosRequestConfig} [config] - Additional Axios request configuration.
* @returns {Promise<AxiosResponse<T>>} - The Axios response.
* @returns {Promise<HttpResponse<T>>} - The Axios response.
*/
async signedRequest<T>(
method: string,
url: string,
params: Params = {},
body?: Body,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
): Promise<HttpResponse<T>> {
const builder = new AzureHttpRequestBuilder(this.tokenStorage, {
method,
baseURL: url,
Expand Down
10 changes: 5 additions & 5 deletions src/http/aws-http-request.builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosHeaders, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Body, Headers, HttpRequestData, Params } from '../types';
import axios, { AxiosHeaders, AxiosInstance, AxiosRequestConfig } from 'axios';
import { Body, Headers, HttpRequestData, HttpResponse, Params } from '../types';
import { AWSStorageService, REGION_KEY, USE_X_LEMON_IDENTITY_KEY } from '../token-storage';
import AWS from 'aws-sdk/global.js';
import { sigV4Client } from '../vendor';
Expand All @@ -9,7 +9,7 @@ import { isEmptyObject, LoggerService } from '../utils';
* Class to build and execute HTTP requests with AWS signing
* @example
* ```ts
* const response: AxiosResponse<OAuthResponse> = await new AWSHttpRequestBuilder({
* const response: HttpResponse<OAuthResponse> = await new AWSHttpRequestBuilder({
* method: 'GET',
* baseURL: `https://api.lemoncloud.io/v1/oauth`,
* })
Expand Down Expand Up @@ -115,10 +115,10 @@ export class AWSHttpRequestBuilder {
/**
* Executes the HTTP request.
* @template T
* @returns {Promise<AxiosResponse<T>>} - Promise containing the response.
* @returns {Promise<HttpResponse<T>>} - Promise containing the response.
* @throws {Error} If an error occurs during the request.
*/
async execute<T>(): Promise<AxiosResponse<T>> {
async execute<T>(): Promise<HttpResponse<T>> {
try {
const signedClient = await this.getSignedClient(this.config.baseURL);
const data: HttpRequestData = {
Expand Down
10 changes: 5 additions & 5 deletions src/http/azure-http-request.builder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Body, Headers, Params } from '../types';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { Body, Headers, HttpResponse, 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({
* const response: HttpResponse<OAuthResponse> = await new AzureHttpRequestBuilder({
* method: 'GET',
* baseURL: `https://api.lemoncloud.io/v1/oauth`,
* })
Expand Down Expand Up @@ -110,10 +110,10 @@ export class AzureHttpRequestBuilder {
/**
* Executes the HTTP request.
* @template T
* @returns {Promise<AxiosResponse<T>>} - Promise containing the response.
* @returns {Promise<HttpResponse<T>>} - Promise containing the response.
* @throws {Error} If an error occurs during the request.
*/
async execute<T>(): Promise<AxiosResponse<T>> {
async execute<T>(): Promise<HttpResponse<T>> {
try {
await this.addCodeParams();
await this.addBearerTokenToHeader();
Expand Down
10 changes: 5 additions & 5 deletions src/http/http-request.builder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Body, Headers, Params } from '../types';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { Body, Headers, HttpResponse, Params } from '../types';

/**
* Class to build and execute HTTP requests
* @example
* ```ts
* const response: AxiosResponse<OAuthResponse> = await new HttpRequestBuilder({
* const response: HttpResponse<OAuthResponse> = await new HttpRequestBuilder({
* method: 'GET',
* baseURL: `https://api.lemoncloud.io/v1/oauth`,
* })
Expand Down Expand Up @@ -103,10 +103,10 @@ export class HttpRequestBuilder {
/**
* Executes the HTTP request
* @template T
* @returns {Promise<AxiosResponse<T>>} - Promise containing the response
* @returns {Promise<HttpResponse<T>>} - Promise containing the response
* @throws {Error} If an error occurs during the request
*/
async execute<T>(): Promise<AxiosResponse<T>> {
async execute<T>(): Promise<HttpResponse<T>> {
try {
return await this.axiosInstance.request<T>(this.config);
} catch (error) {
Expand Down
9 changes: 9 additions & 0 deletions src/types/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ export interface HttpRequestData {
*/
body?: Body;
}

export interface HttpResponse<T = any> {
data: T;
status: number;
statusText: string;
headers?: any;
config?: any;
request?: any;
}

0 comments on commit d0b91d5

Please sign in to comment.