Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Type z.request responses; deprecate .json #895

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkcranny I wonder if the default better be unknown, so that we don't effectively disable type checking for all downstream code.

<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
Loading