From 2dd0288b5d8b03a4609481bc991b946d172f228a Mon Sep 17 00:00:00 2001 From: Thomas Cranny Date: Fri, 18 Oct 2024 17:30:30 +1000 Subject: [PATCH 1/2] Add typed response data for z.request --- packages/core/types/index.test-d.ts | 33 ++++++++++++++++++++++++++ packages/core/types/zapier.custom.d.ts | 28 ++++++++++++---------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/packages/core/types/index.test-d.ts b/packages/core/types/index.test-d.ts index a8952bfe9..468ceb232 100644 --- a/packages/core/types/index.test-d.ts +++ b/packages/core/types/index.test-d.ts @@ -166,3 +166,36 @@ const app: App = { searches: { [search.key]: search }, }; expectType(app); + +// Return types from z.request +async (z: ZObject) => { + const resp = await z.request<{ id: number; name: string }>( + 'https://example.com' + ); + expectType<{ id: number; name: string }>(resp.data); +}; + +async (z: ZObject) => { + const resp = await z.request<{ id: number; name: string }>({ + url: 'https://example.com', + }); + expectType<{ id: number; name: string }>(resp.data); +}; + +// Return types from z.request (raw) +async (z: ZObject) => { + const resp = await z.request<{ id: number; name: string }>( + 'https://example.com', + { raw: true } + ); + const result = await resp.json(); + expectType<{ id: number; name: string }>(result); +}; +async (z: ZObject) => { + const resp = await z.request<{ id: number; name: string }>({ + raw: true, + url: 'https://example.com', + }); + const result = await resp.json(); + expectType<{ id: number; name: string }>(result); +}; diff --git a/packages/core/types/zapier.custom.d.ts b/packages/core/types/zapier.custom.d.ts index dd2fd7b12..e2cf89b5d 100644 --- a/packages/core/types/zapier.custom.d.ts +++ b/packages/core/types/zapier.custom.d.ts @@ -117,16 +117,16 @@ interface BaseHttpResponse { request: HttpRequestOptions; } -export interface HttpResponse extends BaseHttpResponse { +export interface HttpResponse extends BaseHttpResponse { content: string; - data?: any; - json?: any; + data: T; + json?: T; } -export interface RawHttpResponse extends BaseHttpResponse { +export interface RawHttpResponse extends BaseHttpResponse { body: NodeJS.ReadableStream; buffer(): Promise; - json(): Promise; + json(): Promise; text(): Promise; } @@ -138,16 +138,20 @@ type DehydrateFunc = ( export interface ZObject { request: { // most specific overloads go first - ( + ( url: string, options: HttpRequestOptions & { raw: true } - ): Promise; - ( + ): Promise>; + ( options: HttpRequestOptions & { raw: true; url: string } - ): Promise; - - (url: string, options?: HttpRequestOptions): Promise; - (options: HttpRequestOptions & { url: string }): Promise; + ): Promise>; + + (url: string, options?: HttpRequestOptions): Promise< + HttpResponse + >; + (options: HttpRequestOptions & { url: string }): Promise< + HttpResponse + >; }; console: Console; From d91ca5fc8ad34e84542c1579485724a16ef9a478 Mon Sep 17 00:00:00 2001 From: Thomas Cranny Date: Fri, 18 Oct 2024 17:31:09 +1000 Subject: [PATCH 2/2] Deprecate .json in HttpResponse --- packages/core/types/index.test-d.ts | 3 ++- packages/core/types/zapier.custom.d.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/types/index.test-d.ts b/packages/core/types/index.test-d.ts index 468ceb232..4b65ae2de 100644 --- a/packages/core/types/index.test-d.ts +++ b/packages/core/types/index.test-d.ts @@ -18,7 +18,7 @@ import type { Trigger, } from './zapier.generated'; -import { expectType } from 'tsd'; +import { expectType, expectDeprecated } from 'tsd'; const basicDisplay: BasicDisplay = { label: 'some-label', @@ -173,6 +173,7 @@ async (z: ZObject) => { 'https://example.com' ); expectType<{ id: number; name: string }>(resp.data); + expectDeprecated(resp.json); }; async (z: ZObject) => { diff --git a/packages/core/types/zapier.custom.d.ts b/packages/core/types/zapier.custom.d.ts index e2cf89b5d..d0b6aaa71 100644 --- a/packages/core/types/zapier.custom.d.ts +++ b/packages/core/types/zapier.custom.d.ts @@ -120,6 +120,7 @@ interface BaseHttpResponse { export interface HttpResponse extends BaseHttpResponse { content: string; data: T; + /** @deprecated Since v10.0.0. Use `data` instead. */ json?: T; }