Skip to content

Commit

Permalink
Merge pull request #5 from TuentyFaiv/feat/global
Browse files Browse the repository at this point in the history
Feat/global
  • Loading branch information
TuentyFaiv authored Oct 10, 2023
2 parents 898616b + 89df072 commit 17ffdba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tuentyfaiv/http",
"description": "Library for http requests based on fetch",
"version": "0.1.11",
"version": "0.1.12",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/logic/classes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ContentType } from "../typing/enums/content";
import { HttpMethod, HttpMethodLower } from "../typing/enums/methods";

import type {
HttpContract,
HttpConfigInitial,
HttpConfigConnection,
HttpConfigRequest,
Expand All @@ -15,12 +16,13 @@ import type {
HttpConnectionReturn,
HttpGlobalConfig,
HttpMethods,
HttpGlobalAction,
} from "../typing/classes/http.typing";
import type { HttpLog } from "../typing/functions/log.typing";

type Http = HttpMethods & HttpInstance;

export class HttpInstance {
export class HttpInstance implements HttpContract {
#api: string;
#headers: Headers;
#params: Required<HttpConfigInitial>["params"];
Expand Down Expand Up @@ -286,4 +288,14 @@ export class HttpInstance {
#show<T, P>(config: HttpConfigConnection<T, P>, request: HttpLog) {
if (this.#getConfig("log", config)) logger(request);
}

public global = async <T = string>(config: HttpGlobalAction<T>): Promise<void> => {
const { headers, params } = await config({
headers: this.#headers,
params: this.#params as Record<string, T>,
});

this.#headers = headers;
this.#params = params;
}
}
11 changes: 11 additions & 0 deletions src/logic/typing/classes/http.typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export type HttpMethods = {
get: <R, P = undefined>(endpoint: string, config?: HttpConfigGet<P>) => Promise<HttpConnectionReturn<R>>;
};

export interface HttpContract {
global<T>(config: HttpGlobalAction<T>): Promise<void>;
}

export type HttpGlobalConfig = Partial<Pick<HttpConfigConnection<unknown>, GlobalOptions>>;

export interface HttpConfigInitial extends HttpGlobalConfig {
Expand Down Expand Up @@ -87,3 +91,10 @@ export interface HttpStorageAsync {
setItem(key: string, value: string): Promise<void>;
removeItem(key: string): Promise<void>;
}

export interface HttpGlobalActionConfig<T> {
headers: Headers;
params: Record<string, T>;
}

export type HttpGlobalAction<T> = (config: HttpGlobalActionConfig<T>) => Promise<HttpGlobalActionConfig<T>>;
20 changes: 19 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,25 @@ StarWarsAPI.get("/people", { log: true }).then((response) => {
console.log(response);
});

http.get("https://rickandmortyapi.com/api/a", { log: true }).then((response) => {

http.global(async ({ headers, params }) => {
console.log({ headers, params });

headers.set("X-Test-Global", "global header");

return {
headers,
params
};
})


http.get("https://rickandmortyapi.com/api/a", {
log: true,
// headers: new Headers({
// "X-Test": "test",
// })
}).then((response) => {
console.log(response);
}).catch((error) => {
if (error instanceof ServiceError) {
Expand Down

0 comments on commit 17ffdba

Please sign in to comment.