DefinitelyTyped/types/socketcluster-client/socketcluster-client-tests.ts
Daniel Rose 3b611ed646
Update various socketcluster types to newest versions (#42068)
* Add type definitions for async-stream-emitter, consumable-stream, writable-consumable-stream, stream-demux, ag-channel, ag-simple-broker, ncom, async-iterable-stream

* Upgrade sc-broker to 8.0

* Upgrade socketcluster-client to 15.1

* Rename definition files to match module file names

The files in the module were renamed.

* Move socketcluster-server to v14 folder

In preparation for socketcluster-server v15, since the old version is still used by other type packages.

* Update scc-broker-client to 7.0

* Add current socketcluster-server type definitions

Current version is v15.0

* Move sc-broker-cluster to v6 folder

In preparation for sc-broker-cluster v9, since the old version is still used by other type packages.

* Add current sc-broker-cluster type definitions

Current version is v9.0

* Move sc-channel to v1 folder

In preparation for sc-channel v2, since the old version is still used by other type packages.

* Add current sc-channel type definitions

Current version is v2.0

* Include the relevant sc-broker-cluster type-definitions directly in sc-channel

It can be run using older and newer version of sc-broker-cluster, which have differently versioned dependencies.

* Move sc-channel tests to sc-broker-cluster

In the tests we use sc-broker-cluster. If the tests are in sc-channel, they drag in all dependencies for sc-broker-cluster, including esnext.asynciterable, which we don't want.

* Simplify sc-errors tests

In the tests we used socketcluster-server. That dragged in all of its dependencies, including esnext.asynciterable, which we don't want.

* Move sc-channel to v1 folder

In preparation for sc-channel v2, since the old version is still used by other type packages.
2020-02-05 08:59:09 -08:00

121 lines
2.6 KiB
TypeScript

// Adapted from README
import { create } from 'socketcluster-client';
const socket = create({
hostname: 'localhost',
port: 8000,
});
socket.transmit('foo', 123);
(async () => {
// $ExpectType number
await socket.invoke('myProc', 123);
})();
(async () => {
// Subscribe to a channel.
const myChannel = socket.subscribe('myChannel');
await myChannel.listener('subscribe').once();
// $ExpectType ChannelState
myChannel.state;
})();
(async () => {
const myChannel = socket.channel('myChannel');
myChannel.subscribe();
await myChannel.listener('subscribe').once();
// $ExpectType ChannelState
myChannel.state;
myChannel.transmitPublish('This is a message');
})();
socket.transmitPublish('myChannel', 'This is a message');
(async () => {
const myChannel = socket.channel('myChannel');
try {
await myChannel.invokePublish('This is a message');
} catch (error) {}
try {
const response = await socket.invokePublish('myChannel', 'This is a message');
// $ExpectType string
response.channel;
// $ExpectType string
response.data;
} catch (error) {}
})();
(async () => {
const myChannel = socket.channel('myChannel');
// tslint:disable-next-line: await-promise Bug in tslint: https://github.com/palantir/tslint/issues/3997
for await (const data of myChannel) {
// $ExpectType any
data;
}
})();
(async () => {
const secureOptions = {
hostname: 'securedomain.com',
secure: true,
port: 443,
rejectUnauthorized: false, // Only necessary during debug if using a self-signed certificate
};
const secureSocket = create(secureOptions);
const authStatus = await secureSocket.authenticate('abc');
// $ExpectType Error
authStatus.authError;
// $ExpectType AuthStates
authStatus.isAuthenticated;
})();
const mostOptions = {
path: '/socketcluster/',
port: 8000,
hostname: '127.0.0.1',
autoConnect: true,
secure: false,
rejectUnauthorized: false,
connectTimeout: 10000,
ackTimeout: 10000,
channelPrefix: null,
disconnectOnUnload: true,
autoReconnectOptions: {
initialDelay: 10000,
randomness: 10000,
multiplier: 1.5,
maxDelay: 60000,
},
authEngine: null,
codecEngine: null,
subscriptionRetryOptions: {},
query: {
yourparam: 'hello',
},
};
create(mostOptions);
const oldVersionSocket = create({
protocolVersion: 1,
path: '/socketcluster/',
});
// $ExpectType ProtocolVersions
oldVersionSocket.protocolVersion;