Skip to content

Commit

Permalink
Merge pull request #895 from zapier/IQQ-1861-type-zrequest-data
Browse files Browse the repository at this point in the history
feat(types): Type z.request responses; deprecate `.json`
  • Loading branch information
kola-er authored Oct 21, 2024
2 parents 6bdc130 + d91ca5f commit afa1140
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
36 changes: 35 additions & 1 deletion packages/core/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -166,3 +166,37 @@ const app: App = {
searches: { [search.key]: search },
};
expectType<App>(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);
expectDeprecated(resp.json);
};

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);
};
29 changes: 17 additions & 12 deletions packages/core/types/zapier.custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,17 @@ interface BaseHttpResponse {
request: HttpRequestOptions;
}

export interface HttpResponse extends BaseHttpResponse {
export interface HttpResponse<T = any> extends BaseHttpResponse {
content: string;
data?: any;
json?: any;
data: T;
/** @deprecated Since v10.0.0. Use `data` instead. */
json?: T;
}

export interface RawHttpResponse extends BaseHttpResponse {
export interface RawHttpResponse<T = any> extends BaseHttpResponse {
body: NodeJS.ReadableStream;
buffer(): Promise<Buffer>;
json(): Promise<object>;
json(): Promise<T>;
text(): Promise<string>;
}

Expand All @@ -138,16 +139,20 @@ type DehydrateFunc = <T>(
export interface ZObject {
request: {
// most specific overloads go first
(
<T = any>(
url: string,
options: HttpRequestOptions & { raw: true }
): Promise<RawHttpResponse>;
(
): Promise<RawHttpResponse<T>>;
<T = any>(
options: HttpRequestOptions & { raw: true; url: string }
): Promise<RawHttpResponse>;

(url: string, options?: HttpRequestOptions): Promise<HttpResponse>;
(options: HttpRequestOptions & { url: string }): Promise<HttpResponse>;
): Promise<RawHttpResponse<T>>;

<T = any>(url: string, options?: HttpRequestOptions): Promise<
HttpResponse<T>
>;
<T = any>(options: HttpRequestOptions & { url: string }): Promise<
HttpResponse<T>
>;
};

console: Console;
Expand Down

0 comments on commit afa1140

Please sign in to comment.