generated from GamingAPI/template-api-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[create-pull-request] automated change Co-authored-by: GamingEventapiBot <[email protected]>
- Loading branch information
1 parent
50bb8a3
commit e6d16ef
Showing
7 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"template_last_version": "0.5.21", | ||
"document_last_version": "0.5.0" | ||
"document_last_version": "0.6.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import ServerStopped from '../models/ServerStopped'; | ||
import * as Nats from 'nats'; | ||
import { | ||
ErrorCode, | ||
NatsTypescriptTemplateError | ||
} from '../NatsTypescriptTemplateError'; | ||
/** | ||
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel | ||
* @module v0RustServersServerIdEventsStopped | ||
*/ | ||
/** | ||
* Internal functionality to setup subscription on the `v0/rust/servers/{server_id}/events/stopped` channel | ||
* | ||
* @param onDataCallback to call when messages are received | ||
* @param nc to subscribe with | ||
* @param codec used to convert messages | ||
* @param server_id parameter to use in topic | ||
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified | ||
*/ | ||
export function subscribe( | ||
onDataCallback: ( | ||
err ? : NatsTypescriptTemplateError, | ||
msg ? : ServerStopped, server_id ? : string) => void, | ||
nc: Nats.NatsConnection, | ||
codec: Nats.Codec < any > , server_id: string, | ||
options ? : Nats.SubscriptionOptions | ||
): Promise < Nats.Subscription > { | ||
return new Promise(async (resolve, reject) => { | ||
let subscribeOptions: Nats.SubscriptionOptions = { | ||
...options | ||
}; | ||
try { | ||
let subscription = nc.subscribe(`v0.rust.servers.${server_id}.events.stopped`, subscribeOptions); | ||
(async () => { | ||
for await (const msg of subscription) { | ||
const unmodifiedChannel = `v0.rust.servers.{server_id}.events.stopped`; | ||
let channel = msg.subject; | ||
const serverIdSplit = unmodifiedChannel.split("{server_id}"); | ||
const splits = [ | ||
serverIdSplit[0], | ||
serverIdSplit[1] | ||
]; | ||
channel = channel.substring(splits[0].length); | ||
const serverIdEnd = channel.indexOf(splits[1]); | ||
const serverIdParam = "" + channel.substring(0, serverIdEnd); | ||
let receivedData: any = codec.decode(msg.data); | ||
onDataCallback(undefined, ServerStopped.unmarshal(receivedData), serverIdParam); | ||
} | ||
console.log("subscription closed"); | ||
})(); | ||
resolve(subscription); | ||
} catch (e: any) { | ||
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e)); | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
|
||
class ServerStopped { | ||
private _timestamp: string; | ||
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>; | ||
|
||
constructor(input: { | ||
timestamp: string, | ||
}) { | ||
this._timestamp = input.timestamp; | ||
} | ||
|
||
get timestamp(): string { return this._timestamp; } | ||
set timestamp(timestamp: string) { this._timestamp = timestamp; } | ||
|
||
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; } | ||
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; } | ||
|
||
public marshal() : string { | ||
let json = '{' | ||
if(this.timestamp !== undefined) { | ||
json += `"timestamp": ${typeof this.timestamp === 'number' || typeof this.timestamp === 'boolean' ? this.timestamp : JSON.stringify(this.timestamp)},`; | ||
} | ||
|
||
if(this.additionalProperties !== undefined) { | ||
for (const [key, value] of this.additionalProperties.entries()) { | ||
//Only render additionalProperties which are not already a property | ||
if(Object.keys(this).includes(String(key))) continue; | ||
json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`; | ||
} | ||
} | ||
|
||
//Remove potential last comma | ||
return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`; | ||
} | ||
|
||
public static unmarshal(json: string | object): ServerStopped { | ||
const obj = typeof json === "object" ? json : JSON.parse(json); | ||
const instance = new ServerStopped({} as any); | ||
|
||
if (obj["timestamp"] !== undefined) { | ||
instance.timestamp = obj["timestamp"]; | ||
} | ||
|
||
//Not part of core properties | ||
|
||
if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();} | ||
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return !["timestamp"].includes(key);}))) { | ||
|
||
instance.additionalProperties.set(key, value as any); | ||
} | ||
return instance; | ||
} | ||
} | ||
export default ServerStopped; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/testclient/testchannels/V0RustServersServerIdEventsStopped.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import ServerStopped from '../../models/ServerStopped'; | ||
import * as Nats from 'nats'; | ||
import { | ||
ErrorCode, | ||
NatsTypescriptTemplateError | ||
} from '../../NatsTypescriptTemplateError'; | ||
/** | ||
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel | ||
* @module v0RustServersServerIdEventsStopped | ||
*/ | ||
/** | ||
* Internal functionality to publish message to channel | ||
* v0/rust/servers/{server_id}/events/stopped | ||
* | ||
* @param message to publish | ||
* @param nc to publish with | ||
* @param codec used to convert messages | ||
* @param server_id parameter to use in topic | ||
* @param options to publish with | ||
*/ | ||
export function publish( | ||
message: ServerStopped, | ||
nc: Nats.NatsConnection, | ||
codec: Nats.Codec < any > , server_id: string, | ||
options ? : Nats.PublishOptions | ||
): Promise < void > { | ||
return new Promise < void > (async (resolve, reject) => { | ||
try { | ||
let dataToSend: any = message.marshal(); | ||
dataToSend = codec.encode(dataToSend); | ||
nc.publish(`v0.rust.servers.${server_id}.events.stopped`, dataToSend, options); | ||
resolve(); | ||
} catch (e: any) { | ||
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e)); | ||
} | ||
}); | ||
}; |
66 changes: 66 additions & 0 deletions
66
tests/integration/V0RustServersServerIdEventsStopped.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
describe, | ||
it, | ||
before | ||
} from 'mocha'; | ||
import { | ||
expect | ||
} from 'chai'; | ||
import * as Client from '../../src' | ||
import * as TestClient from '../../src/testclient' | ||
import { | ||
NatsTypescriptTemplateError | ||
} from '../../src/NatsTypescriptTemplateError'; | ||
describe('v0/rust/servers/{server_id}/events/stopped can talk to itself', () => { | ||
var client: Client.NatsAsyncApiClient; | ||
var testClient: TestClient.NatsAsyncApiTestClient; | ||
before(async () => { | ||
client = new Client.NatsAsyncApiClient(); | ||
testClient = new TestClient.NatsAsyncApiTestClient(); | ||
const natsHost = process.env.NATS_HOST || "0.0.0.0" | ||
const natsPort = process.env.NATS_PORT || "4222" | ||
const natsUrl = `${natsHost}:${natsPort}` | ||
await client.connectToHost(natsUrl); | ||
await testClient.connectToHost(natsUrl); | ||
}); | ||
it('can send message', async () => { | ||
var receivedError: NatsTypescriptTemplateError | undefined = undefined; | ||
var receivedMsg: Client.ServerStopped | undefined = undefined; | ||
var receivedServerId: string | undefined = undefined | ||
var publishMessage: TestClient.ServerStopped = TestClient.ServerStopped.unmarshal({ | ||
"timestamp": "2016-08-29T09:12:33.001Z" | ||
}); | ||
var ServerIdToSend: string = "string" | ||
const subscription = await client.subscribeToV0RustServersServerIdEventsStopped((err, msg, server_id) => { | ||
receivedError = err; | ||
receivedMsg = msg; | ||
receivedServerId = server_id | ||
}, ServerIdToSend, | ||
true | ||
); | ||
const tryAndWaitForResponse = new Promise((resolve, reject) => { | ||
let isReturned = false; | ||
setTimeout(() => { | ||
if (!isReturned) { | ||
reject(new Error("Timeout")); | ||
} | ||
}, 3000) | ||
setInterval(async () => { | ||
if (subscription.getReceived() === 1) { | ||
resolve(undefined); | ||
isReturned = true | ||
} | ||
}, 100); | ||
}); | ||
await testClient.publishToV0RustServersServerIdEventsStopped(publishMessage, ServerIdToSend); | ||
await tryAndWaitForResponse; | ||
expect(receivedError).to.be.undefined; | ||
expect(receivedMsg).to.not.be.undefined; | ||
expect(receivedMsg!.marshal()).to.equal(publishMessage.marshal()); | ||
expect(receivedServerId).to.be.equal(ServerIdToSend); | ||
}); | ||
after(async () => { | ||
await client.disconnect(); | ||
await testClient.disconnect(); | ||
}); | ||
}); |