Skip to content

Commit

Permalink
fix(typings): properly type the adapter attribute
Browse files Browse the repository at this point in the history
Related: #3796
  • Loading branch information
darrachequesne committed May 11, 2021
1 parent b84ed1e commit 891b187
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
15 changes: 9 additions & 6 deletions lib/index.ts
Expand Up @@ -37,6 +37,8 @@ type ParentNspNameMatchFn = (
fn: (err: Error | null, success: boolean) => void
) => void;

type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);

interface EngineOptions {
/**
* how many ms without a pong packet to consider the connection closed
Expand Down Expand Up @@ -152,7 +154,7 @@ interface ServerOptions extends EngineAttachOptions {
* the adapter to use
* @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
*/
adapter: any;
adapter: AdapterConstructor;
/**
* the parser to use
* @default the default parser (https://github.com/socketio/socket.io-parser)
Expand Down Expand Up @@ -207,7 +209,7 @@ export class Server<
ParentNspNameMatchFn,
ParentNamespace<ListenEvents, EmitEvents, ServerSideEvents>
> = new Map();
private _adapter?: typeof Adapter;
private _adapter?: AdapterConstructor;
private _serveClient: boolean;
private opts: Partial<EngineOptions>;
private eio;
Expand Down Expand Up @@ -360,10 +362,11 @@ export class Server<
* @return self when setting or value when getting
* @public
*/
public adapter(): typeof Adapter | undefined;
public adapter(v: typeof Adapter): this;
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this {
public adapter(): AdapterConstructor | undefined;
public adapter(v: AdapterConstructor): this;
public adapter(
v?: AdapterConstructor
): AdapterConstructor | undefined | this {
if (!arguments.length) return this._adapter;
this._adapter = v;
for (const nsp of this._nsps.values()) {
Expand Down
1 change: 1 addition & 0 deletions lib/namespace.ts
Expand Up @@ -102,6 +102,7 @@ export class Namespace<
* @private
*/
_initAdapter(): void {
// @ts-ignore
this.adapter = new (this.server.adapter()!)(this);
}

Expand Down
24 changes: 23 additions & 1 deletion test/socket.io.test-d.ts
@@ -1,8 +1,9 @@
"use strict";
import { Server, Socket } from "..";
import { Namespace, Server, Socket } from "..";
import type { DefaultEventsMap } from "../lib/typed-events";
import { createServer } from "http";
import { expectError, expectType } from "tsd";
import { Adapter } from "socket.io-adapter";

// This file is run by tsd, not mocha.

Expand Down Expand Up @@ -275,4 +276,25 @@ describe("server", () => {
});
});
});

describe("adapter", () => {
it("accepts arguments of the correct types", () => {
const io = new Server({
adapter: (nsp) => new Adapter(nsp),
});
io.adapter(Adapter);

class MyCustomAdapter extends Adapter {
constructor(nsp, readonly opts) {
super(nsp);
}
}
io.adapter((nsp) => new MyCustomAdapter(nsp, { test: "123" }));
});

it("does not accept arguments of wrong types", () => {
const io = new Server();
expectError(io.adapter((nsp) => "nope"));
});
});
});

0 comments on commit 891b187

Please sign in to comment.