From 676e9074689c11260e9f04818531b79ab495de89 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Fri, 25 Aug 2023 21:14:50 +0100 Subject: [PATCH] chore(internal): add helper method (#137) --- src/core.ts | 24 ++++++++++++++++++------ src/index.ts | 6 +++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/core.ts b/src/core.ts index db0b08e..fcee8e8 100644 --- a/src/core.ts +++ b/src/core.ts @@ -8,6 +8,7 @@ import { type RequestInfo, type RequestInit, type Response, + type HeadersInit, } from '@tryfinch/finch-api/_shims/fetch'; export { type Response }; import { isMultipartBody } from './uploads'; @@ -146,7 +147,7 @@ export abstract class APIClient { this.fetch = overridenFetch ?? fetch; } - protected authHeaders(): Headers { + protected authHeaders(opts: FinalRequestOptions): Headers { return {}; } @@ -158,13 +159,13 @@ export abstract class APIClient { * Authorization: 'Bearer 123', * } */ - protected defaultHeaders(): Headers { + protected defaultHeaders(opts: FinalRequestOptions): Headers { return { Accept: 'application/json', 'Content-Type': 'application/json', 'User-Agent': this.getUserAgent(), ...getPlatformHeaders(), - ...this.authHeaders(), + ...this.authHeaders(opts), }; } @@ -265,7 +266,7 @@ export abstract class APIClient { const reqHeaders: Record = { ...(contentLength && { 'Content-Length': contentLength }), - ...this.defaultHeaders(), + ...this.defaultHeaders(options), ...headers, }; // let builtin fetch set the Content-Type for multipart bodies @@ -297,7 +298,18 @@ export abstract class APIClient { * This is useful for cases where you want to add certain headers based off of * the request properties, e.g. `method` or `url`. */ - protected async prepareRequest(request: RequestInit, { url }: { url: string }): Promise {} + protected async prepareRequest( + request: RequestInit, + { url, options }: { url: string; options: FinalRequestOptions }, + ): Promise {} + + protected parseHeaders(headers: HeadersInit | null | undefined): Record { + return ( + !headers ? {} + : Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header])) + : { ...headers } + ); + } protected makeStatusError( status: number | undefined, @@ -326,7 +338,7 @@ export abstract class APIClient { const { req, url, timeout } = this.buildRequest(options); - await this.prepareRequest(req, { url }); + await this.prepareRequest(req, { url, options }); debug('request', url, options, req.headers); diff --git a/src/index.ts b/src/index.ts index 8749047..ab52c3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -183,9 +183,9 @@ export class Finch extends Core.APIClient { return this._options.defaultQuery; } - protected override defaultHeaders(): Core.Headers { + protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers { return { - ...super.defaultHeaders(), + ...super.defaultHeaders(opts), 'Finch-API-Version': '2020-09-17', ...this._options.defaultHeaders, }; @@ -204,7 +204,7 @@ export class Finch extends Core.APIClient { ); } - protected override authHeaders(): Core.Headers { + protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { if (this.accessToken == null) { return {}; }