From 34e716410701736c7bf7ba9e69dcad8247fe623b Mon Sep 17 00:00:00 2001 From: darthmaim Date: Wed, 24 Jul 2024 19:19:33 +0200 Subject: [PATCH] Improve options --- .changeset/rotten-bats-smell.md | 5 +++++ .changeset/two-needles-sip.md | 6 ++++++ packages/fetch/src/index.ts | 10 +++++++--- packages/types/endpoints.ts | 3 ++- 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 .changeset/rotten-bats-smell.md create mode 100644 .changeset/two-needles-sip.md diff --git a/.changeset/rotten-bats-smell.md b/.changeset/rotten-bats-smell.md new file mode 100644 index 0000000..2d8b1b0 --- /dev/null +++ b/.changeset/rotten-bats-smell.md @@ -0,0 +1,5 @@ +--- +"@gw2api/fetch": minor +--- + +Make `options` parameter optional if it does not contain required properties diff --git a/.changeset/two-needles-sip.md b/.changeset/two-needles-sip.md new file mode 100644 index 0000000..e9f6d06 --- /dev/null +++ b/.changeset/two-needles-sip.md @@ -0,0 +1,6 @@ +--- +"@gw2api/fetch": patch +"@gw2api/types": patch +--- + +Allow passing partial options for unknown endpoints diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index cc69a47..ffbc133 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -1,14 +1,18 @@ import type { AuthenticatedOptions, EndpointType, KnownEndpoint, LocalizedOptions, OptionsByEndpoint } from '@gw2api/types/endpoints'; import { SchemaVersion } from '@gw2api/types/schema'; -// TODO: make `options` optional if `OptionsByEndpoint` only contains optional props +type RequiredKeys = { [K in keyof T]-?: {} extends Pick ? never : K }[keyof T]; + +// if OptionsByEndpoint has no required keys, make the options parameter optional +type Args = RequiredKeys> extends never + ? [endpoint: Url, options?: FetchGw2ApiOptions & OptionsByEndpoint & FetchOptions] + : [endpoint: Url, options: FetchGw2ApiOptions & OptionsByEndpoint & FetchOptions] export function fetchGw2Api< Url extends KnownEndpoint | (string & {}), Schema extends SchemaVersion = undefined >( - endpoint: Url, - options: FetchGw2ApiOptions & OptionsByEndpoint & FetchOptions + ...[endpoint, options]: Args ): Promise> { const url = new URL(endpoint, 'https://api.guildwars2.com/'); diff --git a/packages/types/endpoints.ts b/packages/types/endpoints.ts index 88184c3..2305ad3 100644 --- a/packages/types/endpoints.ts +++ b/packages/types/endpoints.ts @@ -362,7 +362,8 @@ export type OptionsByEndpoint = Endpoint extends KnownAuthenticatedEndpoint ? Options & AuthenticatedOptions : Endpoint extends KnownLocalizedEndpoint ? Options & LocalizedOptions : Endpoint extends CreateSubtokenUrl<'/v2/createsubtoken'> ? Options & AuthenticatedOptions : - Options + Endpoint extends KnownEndpoint | BulkExpandedEndpointUrl ? Options : + Partial // result type for endpoint export type EndpointType =