-
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 #14 from WillianAgostini/interceptor-manager
feat: 🚀 Created interceptor manager
- Loading branch information
Showing
7 changed files
with
483 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
InterceptorRequest, | ||
InterceptorResponse, | ||
NordusConfig, | ||
NordusConfigApi, | ||
} from "./request"; | ||
import { asArray, randomUUID, getValueByKey } from "./utils"; | ||
|
||
export interface NordusInterceptorManager<V> { | ||
use(interceptor: V): string; | ||
eject(id: string): void; | ||
clear(): void; | ||
} | ||
|
||
export function requestInterptorsManager( | ||
nordusConfigApi: NordusConfigApi, | ||
): NordusInterceptorManager<InterceptorRequest> { | ||
return { | ||
use: (interceptor: InterceptorRequest) => { | ||
return addInterceptor(nordusConfigApi.interceptors.request, interceptor); | ||
}, | ||
eject: (id: string) => { | ||
nordusConfigApi.interceptors.request = removeInterceptor( | ||
nordusConfigApi.interceptors.request, | ||
id, | ||
); | ||
}, | ||
clear: () => { | ||
nordusConfigApi.interceptors.request = []; | ||
}, | ||
}; | ||
} | ||
|
||
export function responseInterceptorsManager( | ||
nordusConfigApi: NordusConfigApi, | ||
): NordusInterceptorManager<InterceptorResponse> { | ||
return { | ||
use: (interceptor: InterceptorResponse) => { | ||
return addInterceptor(nordusConfigApi.interceptors.response, interceptor); | ||
}, | ||
eject: (id: string) => { | ||
nordusConfigApi.interceptors.response = removeInterceptor( | ||
nordusConfigApi.interceptors.response, | ||
id, | ||
); | ||
}, | ||
clear: () => { | ||
nordusConfigApi.interceptors.response = []; | ||
}, | ||
}; | ||
} | ||
|
||
export function createInterceptors(nordusConfig: NordusConfig) { | ||
const interceptorsRequest = asArray(nordusConfig.interceptors?.request); | ||
const interceptorsResponse = asArray(nordusConfig.interceptors?.response); | ||
if (!nordusConfig.interceptors) | ||
nordusConfig.interceptors = { | ||
request: interceptorsRequest, | ||
response: interceptorsResponse, | ||
}; | ||
} | ||
|
||
export function addInterceptor<T>(list: T[], interceptor: T) { | ||
list.push(interceptor); | ||
const uuid = randomUUID(); | ||
Object.defineProperty(interceptor, "__interceptorId", { | ||
value: uuid, | ||
}); | ||
return uuid; | ||
} | ||
|
||
export function removeInterceptor<T>(interceptor: T[], id: string) { | ||
return interceptor.filter((element) => { | ||
return getValueByKey("__interceptorId", element) != id; | ||
}); | ||
} |
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,7 +1,30 @@ | ||
import crypto from "crypto"; | ||
|
||
export function append<T>(arr: T[], element: T | T[]) { | ||
if (Array.isArray(element)) { | ||
arr.push(...element); | ||
} else { | ||
arr.push(element); | ||
} | ||
return arr; | ||
} | ||
|
||
export function randomUUID() { | ||
return crypto.randomUUID(); | ||
} | ||
|
||
export function propertyOf<TObj>(name: keyof TObj) { | ||
return name; | ||
} | ||
|
||
export function getValueByKey(key: string, obj: any) { | ||
const attr = key as keyof typeof obj; | ||
const __interceptorId = obj[attr]; | ||
return __interceptorId; | ||
} | ||
|
||
export function asArray(obj: any) { | ||
if (!obj) return []; | ||
if (Array.isArray(obj)) return obj; | ||
return [obj]; | ||
} |
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,3 @@ | ||
export function delay(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |
Oops, something went wrong.