From 1e9cb5ea4bde338f35f30e59722d56e32eccc3a1 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Wed, 22 Mar 2023 22:52:23 +0100 Subject: [PATCH] fix: declare `File` for envs without `lib.dom` (#175) --- src/assets/AssetsClient.ts | 19 ++++++++++--------- src/types.ts | 9 +++++++++ test/warnings.test.ts | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/assets/AssetsClient.ts b/src/assets/AssetsClient.ts index 3dad6502..c9eed542 100644 --- a/src/assets/AssetsClient.ts +++ b/src/assets/AssetsClient.ts @@ -10,6 +10,7 @@ import type { ResponseEvent, SanityAssetDocument, SanityImageAssetDocument, + UploadBody, UploadClientConfig, } from '../types' import * as validators from '../validators' @@ -32,7 +33,7 @@ export class ObservableAssetsClient { */ upload( assetType: 'file', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Observable> @@ -45,7 +46,7 @@ export class ObservableAssetsClient { */ upload( assetType: 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Observable> /** @@ -57,12 +58,12 @@ export class ObservableAssetsClient { */ upload( assetType: 'file' | 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Observable> upload( assetType: 'file' | 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Observable> { return _upload(this.#client, this.#httpRequest, assetType, body, options) @@ -87,7 +88,7 @@ export class AssetsClient { */ upload( assetType: 'file', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Promise /** @@ -99,7 +100,7 @@ export class AssetsClient { */ upload( assetType: 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Promise /** @@ -111,12 +112,12 @@ export class AssetsClient { */ upload( assetType: 'file' | 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Promise upload( assetType: 'file' | 'image', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, options?: UploadClientConfig ): Promise { const observable = _upload(this.#client, this.#httpRequest, assetType, body, options) @@ -137,7 +138,7 @@ function _upload( client: SanityClient | ObservableSanityClient, httpRequest: HttpRequest, assetType: 'image' | 'file', - body: File | Blob | Buffer | NodeJS.ReadableStream, + body: UploadBody, opts: UploadClientConfig = {} ): Observable> { validators.validateAssetType(assetType) diff --git a/src/types.ts b/src/types.ts index d64a9cb2..31cafd1d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,4 @@ +// deno-lint-ignore-file no-empty-interface import type {Requester} from 'get-it' /** @@ -6,6 +7,14 @@ import type {Requester} from 'get-it' */ export type Any = any // eslint-disable-line @typescript-eslint/no-explicit-any +declare global { + // Declare empty stub interfaces for environments where "dom" lib is not included + interface File {} +} + +/** @public */ +export type UploadBody = File | Blob | Buffer | NodeJS.ReadableStream + /** @public */ export interface RequestOptions { timeout?: number diff --git a/test/warnings.test.ts b/test/warnings.test.ts index d68d242b..71ee6c8f 100644 --- a/test/warnings.test.ts +++ b/test/warnings.test.ts @@ -19,7 +19,7 @@ describe('Client config warnings', async () => { ) }) - test.skipIf(isEdge)('warns if in browser on localhost and a token is provided', () => { + test('warns if in browser on localhost and a token is provided', () => { const restoreWindow = global.window global.window = {location: {hostname: 'localhost'}} as any createClient({projectId: 'abc123', useCdn: false, token: 'foo', apiVersion: '1'})