Skip to content

Commit a3e1203

Browse files
authored
🤖 Merge PR DefinitelyTyped#73437 Flesh out typings for Rails Request.JS, targeting latest version v0.0.12 by @myabc
1 parent aa099be commit a3e1203

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed
Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,65 @@
11
export class FetchRequest {
2-
constructor(method: string, url: string, options?: Options);
3-
addHeader(key: string, value: string): void;
2+
constructor(method: string, url: string | URL, options?: Options);
43
perform(): Promise<FetchResponse>;
4+
addHeader(key: string, value: string): void;
5+
sameHostname(): boolean;
6+
get fetchOptions(): RequestInit & { method: string; headers: HeadersInit };
7+
get headers(): HeadersInit;
8+
get csrfToken(): string | undefined;
9+
get contentType(): string | undefined;
10+
get accept(): string;
11+
get body(): BodyInit | Record<string, unknown> | null | undefined;
12+
get query(): string;
13+
get url(): string;
14+
get responseKind(): ResponseKind;
15+
get signal(): AbortSignal | null | undefined;
16+
get redirect(): RequestRedirect;
17+
get credentials(): RequestCredentials;
18+
get keepalive(): boolean;
19+
get additionalHeaders(): HeadersInit;
20+
get formattedBody(): string;
521
}
622

23+
export type ResponseKind = "html" | "turbo-stream" | "json" | "script";
24+
725
export interface Options {
8-
body?: BodyInit | Record<any, any>;
26+
body?: BodyInit | Record<string, unknown> | null;
927
contentType?: string;
1028
headers?: HeadersInit;
1129
credentials?: RequestCredentials;
12-
query?: Record<any, any> | FormData | URLSearchParams;
13-
responseKind?: "html" | "turbo-stream" | "json";
30+
query?: Record<string, unknown> | FormData | URLSearchParams;
31+
responseKind?: ResponseKind;
32+
signal?: AbortSignal | null;
33+
redirect?: RequestRedirect;
34+
keepalive?: boolean;
1435
}
1536

1637
export class FetchResponse {
1738
get statusCode(): number;
1839
get redirected(): boolean;
1940
get ok(): boolean;
41+
get unauthenticated(): boolean;
42+
get unprocessableEntity(): boolean;
43+
get authenticationURL(): string | null;
2044
get contentType(): string;
2145
get headers(): Headers;
2246
get html(): Promise<string>;
2347
get json(): Promise<any>;
2448
get text(): Promise<string>;
49+
get isTurboStream(): boolean;
50+
get isScript(): boolean;
51+
renderTurboStream(): Promise<void>;
52+
activeScript(): Promise<void>;
2553
}
2654

2755
export class RequestInterceptor {
2856
static register(interceptor: (request: FetchRequest) => void | Promise<void>): void;
57+
static get(): (request: FetchRequest) => void | Promise<void>;
2958
static reset(): void;
3059
}
3160

32-
export function get(url: string, options?: Options): Promise<FetchResponse>;
33-
export function post(url: string, options?: Options): Promise<FetchResponse>;
34-
export function put(url: string, options?: Options): Promise<FetchResponse>;
35-
export function patch(url: string, options?: Options): Promise<FetchResponse>;
36-
export function destroy(url: string, options?: Options): Promise<FetchResponse>;
61+
export function get(url: string | URL, options?: Options): Promise<FetchResponse>;
62+
export function post(url: string | URL, options?: Options): Promise<FetchResponse>;
63+
export function put(url: string | URL, options?: Options): Promise<FetchResponse>;
64+
export function patch(url: string | URL, options?: Options): Promise<FetchResponse>;
65+
export function destroy(url: string | URL, options?: Options): Promise<FetchResponse>;

‎types/rails__request.js/rails__request.js-tests.ts‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,39 @@ interface TestJsonResponse {
55
key2: number;
66
}
77

8+
const { signal } = new AbortController();
9+
810
const request = new FetchRequest("post", "https://example.com", {
911
body: { name: "Request.JS" },
1012
contentType: "application/json",
1113
headers: { "X-Test": "Test" },
1214
credentials: "include",
1315
query: { "test": "test" },
1416
responseKind: "json",
17+
signal,
1518
});
1619

20+
// $ExpectType HeadersInit
21+
request.headers;
22+
// $ExpectType string | undefined
23+
request.csrfToken;
24+
// $ExpectType string | undefined
25+
request.contentType;
26+
// $ExpectType string
27+
request.accept;
28+
// $ExpectType BodyInit | Record<string, unknown> | null | undefined
29+
request.body;
30+
// $ExpectType string
31+
request.query;
32+
// $ExpectType AbortSignal | null | undefined
33+
request.signal;
34+
35+
// $ExpectType string
36+
request.fetchOptions.method;
37+
38+
// $ExpectType (HeadersInit | undefined) & HeadersInit
39+
request.fetchOptions.headers;
40+
1741
let testJsonResponseVar: TestJsonResponse;
1842
let headersVar: Headers;
1943
let stringVar: string;
@@ -36,20 +60,47 @@ async function main() {
3660
numberVar = response.statusCode;
3761
stringVar = await response.text;
3862

39-
response = await get("https://example.com");
63+
response = await get("https://example.com", { responseKind: "json" });
4064
testJsonResponseVar = await response.json as TestJsonResponse;
4165

66+
response = await get("https://example.com/ex/turbo-stream", { responseKind: "turbo-stream" });
67+
await response.renderTurboStream();
68+
69+
response = await get("https://example.com/ex/script", { responseKind: "script" });
70+
await response.activeScript();
71+
4272
response = await post("https://example.com", {
4373
body: new FormData(),
4474
});
4575

76+
response = await post("https://example.com", {
77+
body: new FormData(),
78+
keepalive: true,
79+
});
80+
81+
const emptyFile = new File([], "empty.txt", {
82+
type: "text/plain",
83+
lastModified: Date.now(),
84+
});
85+
86+
response = await post("https://example.com", {
87+
body: emptyFile,
88+
keepalive: true,
89+
});
90+
4691
response = await put("https://example.com", {
4792
query: new URLSearchParams(),
4893
});
4994

50-
response = await patch("https://example.com");
95+
const myURL = new URL("https://example.com");
96+
97+
response = await patch(myURL);
98+
99+
response = await patch("https://example.com/this-will-redirect", {
100+
redirect: "follow",
101+
});
51102

52-
response = await destroy("https://example.com");
103+
response = await destroy("https://example.com", { body: null });
53104

54105
RequestInterceptor.register(async (request) => {
55106
request.addHeader("Authorization", "Bearer 12345");

0 commit comments

Comments
 (0)