Skip to content
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
49 changes: 39 additions & 10 deletions types/rails__request.js/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
export class FetchRequest {
constructor(method: string, url: string, options?: Options);
addHeader(key: string, value: string): void;
constructor(method: string, url: string | URL, options?: Options);
perform(): Promise<FetchResponse>;
addHeader(key: string, value: string): void;
sameHostname(): boolean;
get fetchOptions(): RequestInit & { method: string; headers: HeadersInit };
get headers(): HeadersInit;
get csrfToken(): string | undefined;
get contentType(): string | undefined;
get accept(): string;
get body(): BodyInit | Record<string, unknown> | null | undefined;
get query(): string;
get url(): string;
get responseKind(): ResponseKind;
get signal(): AbortSignal | null | undefined;
get redirect(): RequestRedirect;
get credentials(): RequestCredentials;
get keepalive(): boolean;
get additionalHeaders(): HeadersInit;
get formattedBody(): string;
}

export type ResponseKind = "html" | "turbo-stream" | "json" | "script";

export interface Options {
body?: BodyInit | Record<any, any>;
body?: BodyInit | Record<string, unknown> | null;
contentType?: string;
headers?: HeadersInit;
credentials?: RequestCredentials;
query?: Record<any, any> | FormData | URLSearchParams;
responseKind?: "html" | "turbo-stream" | "json";
query?: Record<string, unknown> | FormData | URLSearchParams;
responseKind?: ResponseKind;
signal?: AbortSignal | null;
redirect?: RequestRedirect;
keepalive?: boolean;
}

export class FetchResponse {
get statusCode(): number;
get redirected(): boolean;
get ok(): boolean;
get unauthenticated(): boolean;
get unprocessableEntity(): boolean;
get authenticationURL(): string | null;
get contentType(): string;
get headers(): Headers;
get html(): Promise<string>;
get json(): Promise<any>;
get text(): Promise<string>;
get isTurboStream(): boolean;
get isScript(): boolean;
renderTurboStream(): Promise<void>;
activeScript(): Promise<void>;
}

export class RequestInterceptor {
static register(interceptor: (request: FetchRequest) => void | Promise<void>): void;
static get(): (request: FetchRequest) => void | Promise<void>;
static reset(): void;
}

export function get(url: string, options?: Options): Promise<FetchResponse>;
export function post(url: string, options?: Options): Promise<FetchResponse>;
export function put(url: string, options?: Options): Promise<FetchResponse>;
export function patch(url: string, options?: Options): Promise<FetchResponse>;
export function destroy(url: string, options?: Options): Promise<FetchResponse>;
export function get(url: string | URL, options?: Options): Promise<FetchResponse>;
export function post(url: string | URL, options?: Options): Promise<FetchResponse>;
export function put(url: string | URL, options?: Options): Promise<FetchResponse>;
export function patch(url: string | URL, options?: Options): Promise<FetchResponse>;
export function destroy(url: string | URL, options?: Options): Promise<FetchResponse>;
57 changes: 54 additions & 3 deletions types/rails__request.js/rails__request.js-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,39 @@ interface TestJsonResponse {
key2: number;
}

const { signal } = new AbortController();

const request = new FetchRequest("post", "https://example.com", {
body: { name: "Request.JS" },
contentType: "application/json",
headers: { "X-Test": "Test" },
credentials: "include",
query: { "test": "test" },
responseKind: "json",
signal,
});

// $ExpectType HeadersInit
request.headers;
// $ExpectType string | undefined
request.csrfToken;
// $ExpectType string | undefined
request.contentType;
// $ExpectType string
request.accept;
// $ExpectType BodyInit | Record<string, unknown> | null | undefined
request.body;
// $ExpectType string
request.query;
// $ExpectType AbortSignal | null | undefined
request.signal;

// $ExpectType string
request.fetchOptions.method;

// $ExpectType (HeadersInit | undefined) & HeadersInit
request.fetchOptions.headers;

let testJsonResponseVar: TestJsonResponse;
let headersVar: Headers;
let stringVar: string;
Expand All @@ -36,20 +60,47 @@ async function main() {
numberVar = response.statusCode;
stringVar = await response.text;

response = await get("https://example.com");
response = await get("https://example.com", { responseKind: "json" });
testJsonResponseVar = await response.json as TestJsonResponse;

response = await get("https://example.com/ex/turbo-stream", { responseKind: "turbo-stream" });
await response.renderTurboStream();

response = await get("https://example.com/ex/script", { responseKind: "script" });
await response.activeScript();

response = await post("https://example.com", {
body: new FormData(),
});

response = await post("https://example.com", {
body: new FormData(),
keepalive: true,
});

const emptyFile = new File([], "empty.txt", {
type: "text/plain",
lastModified: Date.now(),
});

response = await post("https://example.com", {
body: emptyFile,
keepalive: true,
});

response = await put("https://example.com", {
query: new URLSearchParams(),
});

response = await patch("https://example.com");
const myURL = new URL("https://example.com");

response = await patch(myURL);

response = await patch("https://example.com/this-will-redirect", {
redirect: "follow",
});

response = await destroy("https://example.com");
response = await destroy("https://example.com", { body: null });

RequestInterceptor.register(async (request) => {
request.addHeader("Authorization", "Bearer 12345");
Expand Down
1 change: 1 addition & 0 deletions types/webpack-hot-middleware/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare namespace WebpackHotMiddleware {
log?: false | Logger | undefined;
path?: string | undefined;
heartbeat?: number | undefined;
statsOptions?: webpack.StatsOptions;
}

type Logger = (message?: any, ...optionalParams: any[]) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const compiler = webpack({})!;
let webpackHotMiddlewareInstance = webpackHotMiddleware(compiler);

webpackHotMiddlewareInstance = webpackHotMiddleware(compiler, {
statsOptions: { cached: false },
log: console.log.bind(console),
path: "/__what",
heartbeat: 2000,
Expand Down