Skip to content

Commit fa5b2c4

Browse files
Merge pull request #434 from laravel/type-fixing
Type fixes in core library
2 parents 3c0c6d1 + 37a6cd1 commit fa5b2c4

File tree

10 files changed

+184
-62
lines changed

10 files changed

+184
-62
lines changed

packages/laravel-echo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
"@babel/plugin-transform-numeric-separator": "^7.25.9",
3838
"@babel/plugin-transform-object-assign": "^7.25.9",
3939
"@babel/preset-env": "^7.26.7",
40+
"@types/jquery": "^3.5.32",
4041
"@types/node": "^20.0.0",
4142
"@typescript-eslint/eslint-plugin": "^8.21.0",
4243
"@typescript-eslint/parser": "^8.21.0",
44+
"axios": "^1.9.0",
4345
"eslint": "^9.0.0",
4446
"prettier": "^3.5.3",
4547
"pusher-js": "^8.0",

packages/laravel-echo/src/connector/connector.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="window" />
2+
13
import type { Channel, PresenceChannel } from "../channel";
24
import type { BroadcastDriver, EchoOptions } from "../echo";
35

@@ -88,29 +90,14 @@ export abstract class Connector<
8890
* Extract the CSRF token from the page.
8991
*/
9092
protected csrfToken(): null | string {
91-
let selector;
92-
93-
if (
94-
typeof window !== "undefined" &&
95-
typeof window.Laravel !== "undefined" &&
96-
window.Laravel.csrfToken
97-
) {
98-
return window.Laravel.csrfToken;
99-
}
100-
101-
if (this.options.csrfToken) {
102-
return this.options.csrfToken;
103-
}
104-
105-
if (
106-
typeof document !== "undefined" &&
107-
typeof document.querySelector === "function" &&
108-
(selector = document.querySelector('meta[name="csrf-token"]'))
109-
) {
110-
return selector.getAttribute("content");
111-
}
112-
113-
return null;
93+
return (
94+
window?.Laravel?.csrfToken ??
95+
this.options.csrfToken ??
96+
document
97+
?.querySelector('meta[name="csrf-token"]')
98+
?.getAttribute("content") ??
99+
null
100+
);
114101
}
115102

116103
/**

packages/laravel-echo/src/connector/pusher-connector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class PusherConnector<
4242
*/
4343
channels: Record<string, AnyPusherChannel> = {};
4444

45-
options: PusherOptions<TBroadcastDriver>;
45+
declare options: PusherOptions<TBroadcastDriver>;
4646

4747
/**
4848
* Create a fresh Pusher connection.

packages/laravel-echo/src/echo.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { InternalAxiosRequestConfig } from "axios";
12
import {
23
Channel,
34
NullChannel,
@@ -179,7 +180,9 @@ export default class Echo<T extends keyof Broadcaster> {
179180
* send a connections socket id to a Laravel app with a X-Socket-Id header.
180181
*/
181182
registerInterceptors(): void {
182-
if (typeof Vue === "function" && Vue.http) {
183+
// TODO: This package is deprecated and we should remove it in a future version.
184+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
185+
if (typeof Vue !== "undefined" && Vue?.http) {
183186
this.registerVueRequestInterceptor();
184187
}
185188

@@ -200,12 +203,15 @@ export default class Echo<T extends keyof Broadcaster> {
200203
* Register a Vue HTTP interceptor to add the X-Socket-ID header.
201204
*/
202205
registerVueRequestInterceptor(): void {
206+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
203207
Vue.http.interceptors.push(
204208
(request: Record<any, any>, next: CallableFunction) => {
205209
if (this.socketId()) {
210+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
206211
request.headers.set("X-Socket-ID", this.socketId());
207212
}
208213

214+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
209215
next();
210216
},
211217
);
@@ -215,13 +221,15 @@ export default class Echo<T extends keyof Broadcaster> {
215221
* Register an Axios HTTP interceptor to add the X-Socket-ID header.
216222
*/
217223
registerAxiosRequestInterceptor(): void {
218-
axios.interceptors.request.use((config: Record<any, any>) => {
219-
if (this.socketId()) {
220-
config.headers["X-Socket-Id"] = this.socketId();
221-
}
224+
axios!.interceptors.request.use(
225+
(config: InternalAxiosRequestConfig<any>) => {
226+
if (this.socketId()) {
227+
config.headers["X-Socket-Id"] = this.socketId();
228+
}
222229

223-
return config;
224-
});
230+
return config;
231+
},
232+
);
225233
}
226234

227235
/**
@@ -348,6 +356,7 @@ type GenericOptions<TBroadcaster extends keyof Broadcaster> = {
348356
host?: string | null;
349357
key?: string | null;
350358
namespace?: string | false;
359+
withoutInterceptors?: boolean;
351360

352361
[key: string]: any;
353362
};

packages/laravel-echo/tests/echo.test.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,47 @@ import Echo from "../src/echo";
44

55
describe("Echo", () => {
66
test("it will not throw error for supported driver", () => {
7-
expect(() => new Echo({ broadcaster: "reverb" })).not.toThrow(
8-
"Broadcaster string reverb is not supported.",
9-
);
7+
expect(
8+
() =>
9+
new Echo({ broadcaster: "reverb", withoutInterceptors: true }),
10+
).not.toThrow("Broadcaster string reverb is not supported.");
1011

11-
expect(() => new Echo({ broadcaster: "pusher" })).not.toThrow(
12-
"Broadcaster string pusher is not supported.",
13-
);
12+
expect(
13+
() =>
14+
new Echo({ broadcaster: "pusher", withoutInterceptors: true }),
15+
).not.toThrow("Broadcaster string pusher is not supported.");
1416

15-
expect(() => new Echo({ broadcaster: "socket.io" })).not.toThrow(
16-
"Broadcaster string socket.io is not supported.",
17-
);
17+
expect(
18+
() =>
19+
new Echo({
20+
broadcaster: "socket.io",
21+
withoutInterceptors: true,
22+
}),
23+
).not.toThrow("Broadcaster string socket.io is not supported.");
1824

19-
expect(() => new Echo({ broadcaster: "null" })).not.toThrow(
20-
"Broadcaster string null is not supported.",
21-
);
22-
expect(() => new Echo({ broadcaster: NullConnector })).not.toThrow();
23-
24-
// eslint-disable-next-line
25-
// @ts-ignore
26-
// eslint-disable-next-line @typescript-eslint/no-empty-function
27-
expect(() => new Echo({ broadcaster: () => {} })).not.toThrow(
28-
"Broadcaster function is not supported.",
29-
);
25+
expect(
26+
() => new Echo({ broadcaster: "null", withoutInterceptors: true }),
27+
).not.toThrow("Broadcaster string null is not supported.");
28+
expect(
29+
() =>
30+
new Echo({
31+
broadcaster: NullConnector,
32+
withoutInterceptors: true,
33+
}),
34+
).not.toThrow();
35+
expect(
36+
() =>
37+
// @ts-ignore
38+
// eslint-disable-next-line @typescript-eslint/no-empty-function
39+
new Echo({ broadcaster: () => {}, withoutInterceptors: true }),
40+
).not.toThrow("Broadcaster function is not supported.");
3041
});
3142

3243
test("it will throw error for unsupported driver", () => {
33-
// eslint-disable-next-line
34-
// @ts-ignore
35-
expect(() => new Echo({ broadcaster: "foo" })).toThrow(
36-
"Broadcaster string foo is not supported.",
37-
);
44+
expect(
45+
// @ts-ignore
46+
// eslint-disable-next-line
47+
() => new Echo({ broadcaster: "foo", withoutInterceptors: true }),
48+
).toThrow("Broadcaster string foo is not supported.");
3849
});
3950
});

packages/laravel-echo/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"emitDecoratorMetadata": true,
1919
"esModuleInterop": true,
2020
"strict": true,
21-
"typeRoots": ["node_modules/@types"],
21+
"typeRoots": ["node_modules/@types", "./typings"],
2222
"lib": ["dom", "es2020"]
2323
},
2424
"include": ["./typings/**/*.ts", "./src/**/*.ts"],
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { io } from "socket.io-client";
1+
import type { AxiosStatic } from "axios";
2+
import type { JQueryStatic } from "jquery";
23
import type Pusher from "pusher-js";
3-
4-
export {};
4+
import type { io } from "socket.io-client";
55

66
declare global {
77
interface Window {
@@ -10,7 +10,16 @@ declare global {
1010
};
1111

1212
io?: typeof io;
13-
1413
Pusher?: typeof Pusher;
14+
15+
Vue?: any;
16+
axios?: AxiosStatic;
17+
jQuery?: JQueryStatic;
18+
Turbo?: object;
1519
}
20+
21+
const Vue: any | undefined;
22+
const axios: AxiosStatic | undefined;
23+
const jQuery: JQueryStatic | undefined;
24+
const Turbo: object | undefined;
1625
}

packages/laravel-echo/vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const config: UserConfig = (() => {
5252
emptyOutDir: true,
5353
...common,
5454
},
55+
test: {
56+
globals: true,
57+
environment: "jsdom",
58+
},
5559
};
5660
})();
5761

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"ably",
99
"react"
1010
],
11-
"homepage": "https://github.com/laravel/echotree/2.x/packages/react",
11+
"homepage": "https://github.com/laravel/echo/tree/2.x/packages/react",
1212
"repository": {
1313
"type": "git",
1414
"url": "https://github.com/laravel/echo"

0 commit comments

Comments
 (0)