Skip to content

Commit

Permalink
feat: new http-primer package (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD authored Oct 28, 2024
2 parents a1e430a + c3862fc commit aa3ba90
Show file tree
Hide file tree
Showing 15 changed files with 2,260 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"dependencies": {
"@alwatr/delay": "workspace:^",
"@alwatr/global-this": "workspace:^",
"@alwatr/http-primer": "workspace:^",
"@alwatr/logger": "workspace:^",
"@alwatr/package-tracer": "workspace:^",
"@alwatr/parse-duration": "workspace:^"
Expand Down
9 changes: 5 additions & 4 deletions packages/fetch/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {delay} from '@alwatr/delay';
import {getGlobalThis} from '@alwatr/global-this';
import {HttpStatusCodes, MimeTypes} from '@alwatr/http-primer';
import {createLogger} from '@alwatr/logger';
import {packageTracer} from '@alwatr/package-tracer';
import {parseDuration} from '@alwatr/parse-duration';
Expand Down Expand Up @@ -60,14 +61,14 @@ export function processOptions_(options: FetchOptions): Required<FetchOptions> {

if (options.bodyJson !== undefined) {
options.body = JSON.stringify(options.bodyJson);
options.headers['Content-Type'] = 'application/json';
options.headers['content-type'] = MimeTypes.JSON;
}

if (options.bearerToken !== undefined) {
options.headers.Authorization = `Bearer ${options.bearerToken}`;
options.headers.authorization = `Bearer ${options.bearerToken}`;
}
else if (options.alwatrAuth !== undefined) {
options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;
options.headers.authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;
}

return options as Required<FetchOptions>;
Expand Down Expand Up @@ -208,7 +209,7 @@ export async function handleRetryPattern_(options: Required<FetchOptions>): Prom
try {
const response = await handleTimeout_(options);

if (response.status < 500) {
if (response.status < HttpStatusCodes.Error_Server_500_Internal_Server_Error) {
return response;
}
// else
Expand Down
6 changes: 4 additions & 2 deletions packages/fetch/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {HttpStatusCodes, type HttpStatusCode} from '@alwatr/http-primer';

import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';

import type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';
Expand Down Expand Up @@ -39,13 +41,13 @@ export async function fetchJson<T extends JsonObject>(options: FetchOptions): Pr
responseText = await response.text();
responseJson = JSON.parse(responseText) as ResponseSuccess<T>;
responseJson.ok = true;
responseJson.statusCode = response.status;
responseJson.statusCode = response.status as HttpStatusCode;
return responseJson;
}
catch (error) {
const responseError: ResponseError = {
ok: false,
statusCode: response?.status ?? 500,
statusCode: (response?.status as HttpStatusCode) ?? HttpStatusCodes.Error_Server_500_Internal_Server_Error,
errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,
errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,
responseText,
Expand Down
12 changes: 4 additions & 8 deletions packages/fetch/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type {HttpMethod, HttpRequestHeaders, HttpStatusCode} from '@alwatr/http-primer';
import type {Duration} from '@alwatr/parse-duration';

/**
* Represents the available HTTP methods.
*/
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE';

/**
* Represents a dictionary of query parameters.
* The keys are strings and the values can be strings, numbers, or booleans.
Expand Down Expand Up @@ -51,7 +47,7 @@ export interface FetchOptions extends RequestInit {
/**
* A Headers object to set the request's headers.
*/
headers?: DictionaryReq<string>;
headers?: Record<string, string> & HttpRequestHeaders;

/**
* A timeout for the fetch request.
Expand Down Expand Up @@ -139,12 +135,12 @@ export interface FetchOptions extends RequestInit {

export type ResponseSuccess<T extends JsonObject> = T & {
ok: true;
statusCode: number;
statusCode: HttpStatusCode;
};

export type ResponseError = {
ok: false;
statusCode: number;
statusCode: HttpStatusCode;
errorCode: string;
errorMessage: string;
responseText?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{"path": "../logger"},
{"path": "../global-this"},
{"path": "../delay"},
{"path": "../parse-duration"}
{"path": "../parse-duration"},
{"path": "../http-primer"},
]
}
Loading

0 comments on commit aa3ba90

Please sign in to comment.