Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replacing protocol lookup with ternary #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/access-token-jwt/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { URL } from 'url';
import { Agent as HttpAgent, get as getHttp } from 'http';
import { Agent as HttpsAgent, get as getHttps } from 'https';
import { once } from 'events';
import type { ClientRequest, IncomingMessage } from 'http';
import type { IncomingMessage } from 'http';
import { TextDecoder } from 'util';

const decoder = new TextDecoder();
Expand All @@ -18,13 +18,6 @@ const concat = (...buffers: Uint8Array[]): Uint8Array => {
return buf;
};

const protocols: {
[protocol: string]: (...args: Parameters<typeof getHttps>) => ClientRequest;
} = {
'https:': getHttps,
'http:': getHttp,
};

export interface FetchOptions {
agent?: HttpAgent | HttpsAgent;
timeoutDuration?: number;
Expand All @@ -34,7 +27,9 @@ const fetch = async <TResponse>(
url: URL,
{ agent, timeoutDuration: timeout }: FetchOptions
): Promise<TResponse> => {
const req = protocols[url.protocol](url.href, {
const get = url.protocol === 'https:' ? getHttps : getHttp;

const req = get(url.href, {
agent,
timeout,
});
Expand Down
Loading