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

Enable TypeScript strict for packages/auth-fetch #1493

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions packages/auth-fetch/src/auth-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function getAuth(options: AuthFetchOptions, url: string | URL, method: str

export function createAuthFetch<B, M>(
h: fetcher<B, M>,
parser: (body: M, responseType: HttpFetchResponseType) => Promise<any>
parser: (body: M, responseType: HttpFetchResponseType | undefined) => Promise<any>
) {
const authHttpFetch = async <T extends HttpFetchOptions<B>>(options: T & AuthFetchOptions): ReturnType<typeof h<T>> => {
const method = getFetchMethod(options);
Expand Down Expand Up @@ -99,7 +99,7 @@ export function createAuthFetch<B, M>(
};
}

let authenticateHeaders: string | string[] = initialResponse.headers.get('www-authenticate');
let authenticateHeaders: string | string[] | null = initialResponse.headers.get('www-authenticate');
if (!authenticateHeaders)
throw new Error('Did not find WWW-Authenticate header.');

Expand Down
1 change: 1 addition & 0 deletions packages/auth-fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"inlineSources": true,
"declaration": true,
"resolveJsonModule": true,
"strict": true
},
"include": [
"src/**/*"
Expand Down
8 changes: 5 additions & 3 deletions server/src/fetch/http-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ const StreamParser: FetchParser<IncomingMessage> = {
}
}

export function getHttpFetchParser(responseType: HttpFetchResponseType) {
export function getHttpFetchParser(responseType: HttpFetchResponseType | undefined) {
switch (responseType) {
case 'json':
return JSONParser;
case 'text':
return TextParser;
case 'readable':
return StreamParser;
case 'buffer':
case undefined:
return BufferParser;
longzheng marked this conversation as resolved.
Show resolved Hide resolved
}
return BufferParser;
}

export function httpFetchParseIncomingMessage(readable: IncomingMessage, responseType: HttpFetchResponseType) {
export function httpFetchParseIncomingMessage(readable: IncomingMessage, responseType: HttpFetchResponseType | undefined) {
return getHttpFetchParser(responseType).parse(readable);
}

Expand Down
12 changes: 7 additions & 5 deletions server/src/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type fetcher<B, M> = <T extends HttpFetchOptions<B>>(options: T) => Promi
>>;


export function getHttpFetchAccept(responseType: HttpFetchResponseType) {
export function getHttpFetchAccept(responseType: HttpFetchResponseType | undefined) {
switch (responseType) {
case 'json':
return 'application/json';
Expand All @@ -89,15 +89,15 @@ export function setHeader(headers: [string, string][], key: string, value: strin
headers.push([key, value]);
}

export function setDefaultHttpFetchAccept(headers: [string, string][], responseType: HttpFetchResponseType) {
export function setDefaultHttpFetchAccept(headers: [string, string][], responseType: HttpFetchResponseType | undefined) {
if (hasHeader(headers, 'Accept'))
return;
const accept = getHttpFetchAccept(responseType);
if (accept)
setHeader(headers, 'Accept', accept);
}

export function createHeadersArray(headers: HeadersInit): [string, string][] {
export function createHeadersArray(headers: HeadersInit | undefined): [string, string][] {
const headersArray: [string, string][] = [];
if (!headers)
return headersArray;
Expand Down Expand Up @@ -144,16 +144,18 @@ export function createStringOrBufferBody(headers: [string, string][], body: any)
return body;
}

export async function domFetchParseIncomingMessage(response: Response, responseType: HttpFetchResponseType) {
export async function domFetchParseIncomingMessage(response: Response, responseType: HttpFetchResponseType | undefined) {
switch (responseType) {
case 'json':
return response.json();
case 'text':
return response.text();
case 'readable':
return response;
case 'buffer':
case undefined:
return new Uint8Array(await response.arrayBuffer());
}
return new Uint8Array(await response.arrayBuffer());
}

export async function domFetch<T extends HttpFetchOptions<BodyInit>>(options: T): Promise<HttpFetchResponse<
Expand Down