Skip to content

Commit e6d16ef

Browse files
feat: minor version change (#11)
[create-pull-request] automated change Co-authored-by: GamingEventapiBot <GamingEventapiBot@users.noreply.github.com>
1 parent 50bb8a3 commit e6d16ef

File tree

7 files changed

+292
-1
lines changed

7 files changed

+292
-1
lines changed

configs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"template_last_version": "0.5.21",
3-
"document_last_version": "0.5.0"
3+
"document_last_version": "0.6.0"
44
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import ServerStopped from '../models/ServerStopped';
2+
import * as Nats from 'nats';
3+
import {
4+
ErrorCode,
5+
NatsTypescriptTemplateError
6+
} from '../NatsTypescriptTemplateError';
7+
/**
8+
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel
9+
* @module v0RustServersServerIdEventsStopped
10+
*/
11+
/**
12+
* Internal functionality to setup subscription on the `v0/rust/servers/{server_id}/events/stopped` channel
13+
*
14+
* @param onDataCallback to call when messages are received
15+
* @param nc to subscribe with
16+
* @param codec used to convert messages
17+
* @param server_id parameter to use in topic
18+
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
19+
*/
20+
export function subscribe(
21+
onDataCallback: (
22+
err ? : NatsTypescriptTemplateError,
23+
msg ? : ServerStopped, server_id ? : string) => void,
24+
nc: Nats.NatsConnection,
25+
codec: Nats.Codec < any > , server_id: string,
26+
options ? : Nats.SubscriptionOptions
27+
): Promise < Nats.Subscription > {
28+
return new Promise(async (resolve, reject) => {
29+
let subscribeOptions: Nats.SubscriptionOptions = {
30+
...options
31+
};
32+
try {
33+
let subscription = nc.subscribe(`v0.rust.servers.${server_id}.events.stopped`, subscribeOptions);
34+
(async () => {
35+
for await (const msg of subscription) {
36+
const unmodifiedChannel = `v0.rust.servers.{server_id}.events.stopped`;
37+
let channel = msg.subject;
38+
const serverIdSplit = unmodifiedChannel.split("{server_id}");
39+
const splits = [
40+
serverIdSplit[0],
41+
serverIdSplit[1]
42+
];
43+
channel = channel.substring(splits[0].length);
44+
const serverIdEnd = channel.indexOf(splits[1]);
45+
const serverIdParam = "" + channel.substring(0, serverIdEnd);
46+
let receivedData: any = codec.decode(msg.data);
47+
onDataCallback(undefined, ServerStopped.unmarshal(receivedData), serverIdParam);
48+
}
49+
console.log("subscription closed");
50+
})();
51+
resolve(subscription);
52+
} catch (e: any) {
53+
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e));
54+
}
55+
})
56+
}

src/index.ts

+46
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ import {
55
} from './NatsTypescriptTemplateError';
66
import * as Nats from 'nats';
77
import * as v0RustServersServerIdEventsStartedChannel from "./channels/V0RustServersServerIdEventsStarted";
8+
import * as v0RustServersServerIdEventsStoppedChannel from "./channels/V0RustServersServerIdEventsStopped";
89
import * as v0RustServersServerIdEventsPlayerSteamIdChattedChannel from "./channels/V0RustServersServerIdEventsPlayerSteamIdChatted";
910
import ServerStarted from "./models/ServerStarted";
11+
import ServerStopped from "./models/ServerStopped";
1012
import ChatMessage from "./models/ChatMessage";
1113
export {
1214
v0RustServersServerIdEventsStartedChannel
1315
};
16+
export {
17+
v0RustServersServerIdEventsStoppedChannel
18+
};
1419
export {
1520
v0RustServersServerIdEventsPlayerSteamIdChattedChannel
1621
};
1722
export {
1823
ServerStarted
1924
};
25+
export {
26+
ServerStopped
27+
};
2028
export {
2129
ChatMessage
2230
};
@@ -161,6 +169,44 @@ export class NatsAsyncApiClient {
161169
}
162170
});
163171
}
172+
/**
173+
* Subscribe to the `v0/rust/servers/{server_id}/events/stopped`
174+
*
175+
* Channel for the API to process for when a server has stopped
176+
*
177+
* @param onDataCallback to call when messages are received
178+
* @param server_id parameter to use in topic
179+
* @param flush ensure client is force flushed after subscribing
180+
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
181+
*/
182+
public subscribeToV0RustServersServerIdEventsStopped(
183+
onDataCallback: (
184+
err ? : NatsTypescriptTemplateError,
185+
msg ? : ServerStopped, server_id ? : string) => void, server_id: string,
186+
flush ? : boolean,
187+
options ? : Nats.SubscriptionOptions
188+
): Promise < Nats.Subscription > {
189+
return new Promise(async (resolve, reject) => {
190+
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined) {
191+
try {
192+
const sub = await v0RustServersServerIdEventsStoppedChannel.subscribe(
193+
onDataCallback,
194+
this.nc,
195+
this.codec, server_id,
196+
options
197+
);
198+
if (flush) {
199+
await this.nc.flush();
200+
}
201+
resolve(sub);
202+
} catch (e: any) {
203+
reject(e);
204+
}
205+
} else {
206+
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
207+
}
208+
});
209+
}
164210
/**
165211
* Subscribe to the `v0/rust/servers/{server_id}/events/player/{steam_id}/chatted`
166212
*

src/models/ServerStopped.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
class ServerStopped {
4+
private _timestamp: string;
5+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
6+
7+
constructor(input: {
8+
timestamp: string,
9+
}) {
10+
this._timestamp = input.timestamp;
11+
}
12+
13+
get timestamp(): string { return this._timestamp; }
14+
set timestamp(timestamp: string) { this._timestamp = timestamp; }
15+
16+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
17+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
18+
19+
public marshal() : string {
20+
let json = '{'
21+
if(this.timestamp !== undefined) {
22+
json += `"timestamp": ${typeof this.timestamp === 'number' || typeof this.timestamp === 'boolean' ? this.timestamp : JSON.stringify(this.timestamp)},`;
23+
}
24+
25+
if(this.additionalProperties !== undefined) {
26+
for (const [key, value] of this.additionalProperties.entries()) {
27+
//Only render additionalProperties which are not already a property
28+
if(Object.keys(this).includes(String(key))) continue;
29+
json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`;
30+
}
31+
}
32+
33+
//Remove potential last comma
34+
return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`;
35+
}
36+
37+
public static unmarshal(json: string | object): ServerStopped {
38+
const obj = typeof json === "object" ? json : JSON.parse(json);
39+
const instance = new ServerStopped({} as any);
40+
41+
if (obj["timestamp"] !== undefined) {
42+
instance.timestamp = obj["timestamp"];
43+
}
44+
45+
//Not part of core properties
46+
47+
if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();}
48+
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return !["timestamp"].includes(key);}))) {
49+
50+
instance.additionalProperties.set(key, value as any);
51+
}
52+
return instance;
53+
}
54+
}
55+
export default ServerStopped;

src/testclient/index.ts

+31
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ import {
44
} from '../NatsTypescriptTemplateError';
55
import * as Nats from 'nats';
66
import * as v0RustServersServerIdEventsStartedChannel from "./testchannels/V0RustServersServerIdEventsStarted";
7+
import * as v0RustServersServerIdEventsStoppedChannel from "./testchannels/V0RustServersServerIdEventsStopped";
78
import * as v0RustServersServerIdEventsPlayerSteamIdChattedChannel from "./testchannels/V0RustServersServerIdEventsPlayerSteamIdChatted";
89
import ServerStarted from "../models/ServerStarted";
10+
import ServerStopped from "../models/ServerStopped";
911
import ChatMessage from "../models/ChatMessage";
1012
export {
1113
v0RustServersServerIdEventsStartedChannel
1214
};
15+
export {
16+
v0RustServersServerIdEventsStoppedChannel
17+
};
1318
export {
1419
v0RustServersServerIdEventsPlayerSteamIdChattedChannel
1520
};
1621
export {
1722
ServerStarted
1823
};
24+
export {
25+
ServerStopped
26+
};
1927
export {
2028
ChatMessage
2129
};
@@ -138,6 +146,29 @@ export class NatsAsyncApiTestClient {
138146
return Promise.reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
139147
}
140148
}
149+
/**
150+
* Publish to the `v0/rust/servers/{server_id}/events/stopped` channel
151+
*
152+
* Channel for the API to process for when a server has stopped
153+
*
154+
* @param message to publish
155+
* @param server_id parameter to use in topic
156+
*/
157+
public publishToV0RustServersServerIdEventsStopped(
158+
message: ServerStopped, server_id: string,
159+
options ? : Nats.PublishOptions
160+
): Promise < void > {
161+
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined) {
162+
return v0RustServersServerIdEventsStoppedChannel.publish(
163+
message,
164+
this.nc,
165+
this.codec, server_id,
166+
options
167+
);
168+
} else {
169+
return Promise.reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
170+
}
171+
}
141172
/**
142173
* Publish to the `v0/rust/servers/{server_id}/events/player/{steam_id}/chatted` channel
143174
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ServerStopped from '../../models/ServerStopped';
2+
import * as Nats from 'nats';
3+
import {
4+
ErrorCode,
5+
NatsTypescriptTemplateError
6+
} from '../../NatsTypescriptTemplateError';
7+
/**
8+
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel
9+
* @module v0RustServersServerIdEventsStopped
10+
*/
11+
/**
12+
* Internal functionality to publish message to channel
13+
* v0/rust/servers/{server_id}/events/stopped
14+
*
15+
* @param message to publish
16+
* @param nc to publish with
17+
* @param codec used to convert messages
18+
* @param server_id parameter to use in topic
19+
* @param options to publish with
20+
*/
21+
export function publish(
22+
message: ServerStopped,
23+
nc: Nats.NatsConnection,
24+
codec: Nats.Codec < any > , server_id: string,
25+
options ? : Nats.PublishOptions
26+
): Promise < void > {
27+
return new Promise < void > (async (resolve, reject) => {
28+
try {
29+
let dataToSend: any = message.marshal();
30+
dataToSend = codec.encode(dataToSend);
31+
nc.publish(`v0.rust.servers.${server_id}.events.stopped`, dataToSend, options);
32+
resolve();
33+
} catch (e: any) {
34+
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e));
35+
}
36+
});
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
describe,
3+
it,
4+
before
5+
} from 'mocha';
6+
import {
7+
expect
8+
} from 'chai';
9+
import * as Client from '../../src'
10+
import * as TestClient from '../../src/testclient'
11+
import {
12+
NatsTypescriptTemplateError
13+
} from '../../src/NatsTypescriptTemplateError';
14+
describe('v0/rust/servers/{server_id}/events/stopped can talk to itself', () => {
15+
var client: Client.NatsAsyncApiClient;
16+
var testClient: TestClient.NatsAsyncApiTestClient;
17+
before(async () => {
18+
client = new Client.NatsAsyncApiClient();
19+
testClient = new TestClient.NatsAsyncApiTestClient();
20+
const natsHost = process.env.NATS_HOST || "0.0.0.0"
21+
const natsPort = process.env.NATS_PORT || "4222"
22+
const natsUrl = `${natsHost}:${natsPort}`
23+
await client.connectToHost(natsUrl);
24+
await testClient.connectToHost(natsUrl);
25+
});
26+
it('can send message', async () => {
27+
var receivedError: NatsTypescriptTemplateError | undefined = undefined;
28+
var receivedMsg: Client.ServerStopped | undefined = undefined;
29+
var receivedServerId: string | undefined = undefined
30+
var publishMessage: TestClient.ServerStopped = TestClient.ServerStopped.unmarshal({
31+
"timestamp": "2016-08-29T09:12:33.001Z"
32+
});
33+
var ServerIdToSend: string = "string"
34+
const subscription = await client.subscribeToV0RustServersServerIdEventsStopped((err, msg, server_id) => {
35+
receivedError = err;
36+
receivedMsg = msg;
37+
receivedServerId = server_id
38+
}, ServerIdToSend,
39+
true
40+
);
41+
const tryAndWaitForResponse = new Promise((resolve, reject) => {
42+
let isReturned = false;
43+
setTimeout(() => {
44+
if (!isReturned) {
45+
reject(new Error("Timeout"));
46+
}
47+
}, 3000)
48+
setInterval(async () => {
49+
if (subscription.getReceived() === 1) {
50+
resolve(undefined);
51+
isReturned = true
52+
}
53+
}, 100);
54+
});
55+
await testClient.publishToV0RustServersServerIdEventsStopped(publishMessage, ServerIdToSend);
56+
await tryAndWaitForResponse;
57+
expect(receivedError).to.be.undefined;
58+
expect(receivedMsg).to.not.be.undefined;
59+
expect(receivedMsg!.marshal()).to.equal(publishMessage.marshal());
60+
expect(receivedServerId).to.be.equal(ServerIdToSend);
61+
});
62+
after(async () => {
63+
await client.disconnect();
64+
await testClient.disconnect();
65+
});
66+
});

0 commit comments

Comments
 (0)