From 28890a37e710174dfd68c5576b61b3549f44d1cd Mon Sep 17 00:00:00 2001 From: Andrei Mihu Date: Mon, 14 May 2018 14:53:17 +0100 Subject: [PATCH] Improve group and friends test suite. --- dist/api.gen.d.ts | 41 +-- dist/client.d.ts | 3 + dist/nakama-js.cjs.js | 334 ++++++++++++++++-- dist/nakama-js.esm.js | 334 ++++++++++++++++-- dist/nakama-js.umd.js | 334 ++++++++++++++++-- src/api.gen.ts | 91 +---- src/client.ts | 321 +++++++++++++++-- test/client-friend.test.js | 150 ++++++++ ...ns.test.js => socket-notification.test.js} | 2 +- 9 files changed, 1366 insertions(+), 244 deletions(-) create mode 100644 test/client-friend.test.js rename test/{socket-notifications.test.js => socket-notification.test.js} (98%) diff --git a/dist/api.gen.d.ts b/dist/api.gen.d.ts index 8572230..91e79f3 100644 --- a/dist/api.gen.d.ts +++ b/dist/api.gen.d.ts @@ -53,10 +53,6 @@ export interface ApiAccountGoogle { export interface ApiAccountSteam { token?: string; } -export interface ApiAddGroupUsersRequest { - group_id?: string; - user_ids?: Array; -} export interface ApiChannelMessage { channel_id?: string; code?: number; @@ -116,13 +112,6 @@ export interface ApiGroupList { export interface ApiGroupUserList { group_users?: Array; } -export interface ApiJoinGroupRequest { - group_id?: string; -} -export interface ApiKickGroupUsersRequest { - group_id?: string; - user_ids?: Array; -} export interface ApiLeaderboardRecord { create_time?: string; expiry_time?: string; @@ -142,9 +131,6 @@ export interface ApiLeaderboardRecordList { prev_cursor?: string; records?: Array; } -export interface ApiLeaveGroupRequest { - group_id?: string; -} export interface ApiMatch { authoritative?: boolean; label?: string; @@ -167,10 +153,6 @@ export interface ApiNotificationList { cacheable_cursor?: string; notifications?: Array; } -export interface ApiPromoteGroupUsersRequest { - group_id?: string; - user_ids?: Array; -} export interface ApiReadStorageObjectId { collection?: string; key?: string; @@ -267,17 +249,8 @@ export interface ApiWriteStorageObject { export interface ApiWriteStorageObjectsRequest { objects?: Array; } -export interface ProtobufBoolValue { - value?: boolean; -} export interface ProtobufEmpty { } -export interface ProtobufInt32Value { - value?: number; -} -export interface ProtobufStringValue { - value?: string; -} export declare const NakamaApi: (configuration?: ConfigurationParameters) => { healthcheck(options?: any): Promise; getAccount(options?: any): Promise; @@ -304,7 +277,7 @@ export declare const NakamaApi: (configuration?: ConfigurationParameters) => { unlinkGoogle(body: ApiAccountGoogle, options?: any): Promise; unlinkSteam(body: ApiAccountSteam, options?: any): Promise; listChannelMessages(channelId: string, limit?: number | undefined, forward?: boolean | undefined, cursor?: string | undefined, options?: any): Promise; - deleteFriends(options?: any): Promise; + deleteFriends(ids?: string[] | undefined, usernames?: string[] | undefined, options?: any): Promise; listFriends(options?: any): Promise; addFriends(options?: any): Promise; blockFriends(options?: any): Promise; @@ -313,17 +286,17 @@ export declare const NakamaApi: (configuration?: ConfigurationParameters) => { createGroup(body: ApiCreateGroupRequest, options?: any): Promise; deleteGroup(groupId: string, options?: any): Promise; updateGroup(groupId: string, body: ApiUpdateGroupRequest, options?: any): Promise; - addGroupUsers(groupId: string, body: ApiAddGroupUsersRequest, options?: any): Promise; - joinGroup(groupId: string, body: ApiJoinGroupRequest, options?: any): Promise; - kickGroupUsers(groupId: string, body: ApiKickGroupUsersRequest, options?: any): Promise; - leaveGroup(groupId: string, body: ApiLeaveGroupRequest, options?: any): Promise; - promoteGroupUsers(groupId: string, body: ApiPromoteGroupUsersRequest, options?: any): Promise; + addGroupUsers(groupId: string, options?: any): Promise; + joinGroup(groupId: string, options?: any): Promise; + kickGroupUsers(groupId: string, options?: any): Promise; + leaveGroup(groupId: string, options?: any): Promise; + promoteGroupUsers(groupId: string, options?: any): Promise; listGroupUsers(groupId: string, options?: any): Promise; deleteLeaderboardRecord(leaderboardId: string, options?: any): Promise; listLeaderboardRecords(leaderboardId: string, ownerIds?: string[] | undefined, limit?: number | undefined, cursor?: string | undefined, options?: any): Promise; writeLeaderboardRecord(leaderboardId: string, body: WriteLeaderboardRecordRequestLeaderboardRecordWrite, options?: any): Promise; listMatches(limit?: number | undefined, authoritative?: boolean | undefined, label?: string | undefined, minSize?: number | undefined, maxSize?: number | undefined, options?: any): Promise; - deleteNotifications(options?: any): Promise; + deleteNotifications(ids?: string[] | undefined, options?: any): Promise; listNotifications(limit?: number | undefined, cacheableCursor?: string | undefined, options?: any): Promise; rpcFunc2(id: string, payload?: string | undefined, httpKey?: string | undefined, options?: any): Promise; rpcFunc(id: string, body: string, options?: any): Promise; diff --git a/dist/client.d.ts b/dist/client.d.ts index c0971df..5cb550b 100644 --- a/dist/client.d.ts +++ b/dist/client.d.ts @@ -156,13 +156,16 @@ export declare class Client { private readonly configuration; constructor(serverkey?: string, host?: string, port?: string, useSSL?: boolean, timeout?: number, verbose?: boolean); addGroupUsers(session: Session, groupId: string, ids?: Array): Promise; + addFriends(session: Session, ids?: Array, usernames?: Array): Promise; authenticateCustom(request: ApiAccountCustom): Promise; authenticateDevice(request: ApiAccountDevice): Promise; authenticateEmail(request: ApiAccountEmail): Promise; authenticateFacebook(request: ApiAccountFacebook): Promise; authenticateGoogle(request: ApiAccountGoogle): Promise; + blockFriends(session: Session, ids?: Array, usernames?: Array): Promise; createGroup(session: Session, request: ApiCreateGroupRequest): Promise; createSocket(useSSL?: boolean, verbose?: boolean): Socket; + deleteFriends(session: Session, ids?: Array, usernames?: Array): Promise; deleteGroup(session: Session, groupId: string): Promise; deleteNotifications(session: Session, ids?: Array): Promise; deleteStorageObjects(session: Session, request: ApiDeleteStorageObjectsRequest): Promise; diff --git a/dist/nakama-js.cjs.js b/dist/nakama-js.cjs.js index 4758696..ef58a51 100644 --- a/dist/nakama-js.cjs.js +++ b/dist/nakama-js.cjs.js @@ -1796,10 +1796,13 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteFriends: function (options) { + deleteFriends: function (ids, usernames, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/friend"; - var queryParams = {}; + var queryParams = { + ids: ids, + usernames: usernames, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -2216,14 +2219,11 @@ var NakamaApi = function (configuration) { }), ]); }, - addGroupUsers: function (groupId, body, options) { + addGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/add" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2253,7 +2253,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2268,14 +2267,11 @@ var NakamaApi = function (configuration) { }), ]); }, - joinGroup: function (groupId, body, options) { + joinGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/join" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2305,7 +2301,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2320,14 +2315,11 @@ var NakamaApi = function (configuration) { }), ]); }, - kickGroupUsers: function (groupId, body, options) { + kickGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/kick" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2357,7 +2349,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2372,14 +2363,11 @@ var NakamaApi = function (configuration) { }), ]); }, - leaveGroup: function (groupId, body, options) { + leaveGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/leave" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2409,7 +2397,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2424,14 +2411,11 @@ var NakamaApi = function (configuration) { }), ]); }, - promoteGroupUsers: function (groupId, body, options) { + promoteGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/promote" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2461,7 +2445,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2726,10 +2709,12 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteNotifications: function (options) { + deleteNotifications: function (ids, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/notification"; - var queryParams = {}; + var queryParams = { + ids: ids, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -3539,9 +3524,102 @@ var Client = (function () { this.apiClient = NakamaApi(this.configuration); } Client.prototype.addGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.addGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/add"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; + Client.prototype.addFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.authenticateCustom = function (request) { @@ -3569,6 +3647,56 @@ var Client = (function () { return Session.restore(apiSession.token || ""); }); }; + Client.prototype.blockFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend/block"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.createGroup = function (session, request) { this.configuration.bearerToken = (session && session.token); return this.apiClient.createGroup(request).then(function (response) { @@ -3593,6 +3721,56 @@ var Client = (function () { if (verbose === void 0) { verbose = false; } return new DefaultSocket(this.host, this.port, useSSL, verbose); }; + Client.prototype.deleteFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "DELETE" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.deleteGroup = function (session, groupId) { this.configuration.bearerToken = (session && session.token); return this.apiClient.deleteGroup(groupId).then(function (response) { @@ -3703,9 +3881,52 @@ var Client = (function () { }); }; Client.prototype.kickGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.kickGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/kick"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.leaveGroup = function (session, groupId) { @@ -4000,9 +4221,52 @@ var Client = (function () { }); }; Client.prototype.promoteGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.promoteGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/promote"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.readStorageObjects = function (session, request) { diff --git a/dist/nakama-js.esm.js b/dist/nakama-js.esm.js index e93eef5..d6e513f 100644 --- a/dist/nakama-js.esm.js +++ b/dist/nakama-js.esm.js @@ -1792,10 +1792,13 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteFriends: function (options) { + deleteFriends: function (ids, usernames, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/friend"; - var queryParams = {}; + var queryParams = { + ids: ids, + usernames: usernames, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -2212,14 +2215,11 @@ var NakamaApi = function (configuration) { }), ]); }, - addGroupUsers: function (groupId, body, options) { + addGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/add" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2249,7 +2249,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2264,14 +2263,11 @@ var NakamaApi = function (configuration) { }), ]); }, - joinGroup: function (groupId, body, options) { + joinGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/join" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2301,7 +2297,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2316,14 +2311,11 @@ var NakamaApi = function (configuration) { }), ]); }, - kickGroupUsers: function (groupId, body, options) { + kickGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/kick" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2353,7 +2345,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2368,14 +2359,11 @@ var NakamaApi = function (configuration) { }), ]); }, - leaveGroup: function (groupId, body, options) { + leaveGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/leave" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2405,7 +2393,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2420,14 +2407,11 @@ var NakamaApi = function (configuration) { }), ]); }, - promoteGroupUsers: function (groupId, body, options) { + promoteGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/promote" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2457,7 +2441,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2722,10 +2705,12 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteNotifications: function (options) { + deleteNotifications: function (ids, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/notification"; - var queryParams = {}; + var queryParams = { + ids: ids, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -3535,9 +3520,102 @@ var Client = (function () { this.apiClient = NakamaApi(this.configuration); } Client.prototype.addGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.addGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/add"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; + Client.prototype.addFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.authenticateCustom = function (request) { @@ -3565,6 +3643,56 @@ var Client = (function () { return Session.restore(apiSession.token || ""); }); }; + Client.prototype.blockFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend/block"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.createGroup = function (session, request) { this.configuration.bearerToken = (session && session.token); return this.apiClient.createGroup(request).then(function (response) { @@ -3589,6 +3717,56 @@ var Client = (function () { if (verbose === void 0) { verbose = false; } return new DefaultSocket(this.host, this.port, useSSL, verbose); }; + Client.prototype.deleteFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "DELETE" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.deleteGroup = function (session, groupId) { this.configuration.bearerToken = (session && session.token); return this.apiClient.deleteGroup(groupId).then(function (response) { @@ -3699,9 +3877,52 @@ var Client = (function () { }); }; Client.prototype.kickGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.kickGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/kick"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.leaveGroup = function (session, groupId) { @@ -3996,9 +4217,52 @@ var Client = (function () { }); }; Client.prototype.promoteGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.promoteGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/promote"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.readStorageObjects = function (session, request) { diff --git a/dist/nakama-js.umd.js b/dist/nakama-js.umd.js index 5d2f568..9e81b00 100644 --- a/dist/nakama-js.umd.js +++ b/dist/nakama-js.umd.js @@ -1798,10 +1798,13 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteFriends: function (options) { + deleteFriends: function (ids, usernames, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/friend"; - var queryParams = {}; + var queryParams = { + ids: ids, + usernames: usernames, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -2218,14 +2221,11 @@ var NakamaApi = function (configuration) { }), ]); }, - addGroupUsers: function (groupId, body, options) { + addGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/add" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2255,7 +2255,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2270,14 +2269,11 @@ var NakamaApi = function (configuration) { }), ]); }, - joinGroup: function (groupId, body, options) { + joinGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/join" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2307,7 +2303,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2322,14 +2317,11 @@ var NakamaApi = function (configuration) { }), ]); }, - kickGroupUsers: function (groupId, body, options) { + kickGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/kick" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2359,7 +2351,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2374,14 +2365,11 @@ var NakamaApi = function (configuration) { }), ]); }, - leaveGroup: function (groupId, body, options) { + leaveGroup: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/leave" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2411,7 +2399,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2426,14 +2413,11 @@ var NakamaApi = function (configuration) { }), ]); }, - promoteGroupUsers: function (groupId, body, options) { + promoteGroupUsers: function (groupId, options) { if (options === void 0) { options = {}; } if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } var urlPath = "/v2/group/{group_id}/promote" .replace("{group_id}", encodeURIComponent(String(groupId))); var queryParams = {}; @@ -2463,7 +2447,6 @@ var NakamaApi = function (configuration) { headers["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password); } fetchOptions.headers = __assign({}, headers, options.headers); - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { if (response.status >= 200 && response.status < 300) { @@ -2728,10 +2711,12 @@ var NakamaApi = function (configuration) { }), ]); }, - deleteNotifications: function (options) { + deleteNotifications: function (ids, options) { if (options === void 0) { options = {}; } var urlPath = "/v2/notification"; - var queryParams = {}; + var queryParams = { + ids: ids, + }; var urlQuery = "?" + Object.keys(queryParams) .map(function (k) { if (queryParams[k] instanceof Array) { @@ -3541,9 +3526,102 @@ var Client = (function () { this.apiClient = NakamaApi(this.configuration); } Client.prototype.addGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.addGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/add"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; + Client.prototype.addFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.authenticateCustom = function (request) { @@ -3571,6 +3649,56 @@ var Client = (function () { return Session.restore(apiSession.token || ""); }); }; + Client.prototype.blockFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend/block"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.createGroup = function (session, request) { this.configuration.bearerToken = (session && session.token); return this.apiClient.createGroup(request).then(function (response) { @@ -3595,6 +3723,56 @@ var Client = (function () { if (verbose === void 0) { verbose = false; } return new DefaultSocket(this.host, this.port, useSSL, verbose); }; + Client.prototype.deleteFriends = function (session, ids, usernames) { + var _this = this; + this.configuration.bearerToken = (session && session.token); + var urlPath = "/v2/friend"; + var queryParams = { + ids: ids, + usernames: usernames + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "DELETE" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); + }); + }; Client.prototype.deleteGroup = function (session, groupId) { this.configuration.bearerToken = (session && session.token); return this.apiClient.deleteGroup(groupId).then(function (response) { @@ -3705,9 +3883,52 @@ var Client = (function () { }); }; Client.prototype.kickGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.kickGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/kick"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.leaveGroup = function (session, groupId) { @@ -4002,9 +4223,52 @@ var Client = (function () { }); }; Client.prototype.promoteGroupUsers = function (session, groupId, ids) { + var _this = this; this.configuration.bearerToken = (session && session.token); - return this.apiClient.promoteGroupUsers(groupId, { user_ids: ids }).then(function (response) { - return response !== undefined; + var urlPath = "/v2/group/" + groupId + "/promote"; + var queryParams = { + user_ids: ids + }; + var urlQuery = "?" + Object.keys(queryParams) + .map(function (k) { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce(function (prev, curr) { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } + else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + var fetchOptions = __assign({ method: "POST" }); + var headers = { + "Accept": "application/json", + "Content-Type": "application/json", + }; + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } + else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + fetchOptions.headers = __assign({}, headers); + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then(function (response) { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } + else { + throw response; + } + }), + new Promise(function (_, reject) { + return setTimeout(reject, _this.configuration.timeoutMs, "Request timed out."); + }), + ]).then(function (response) { + return Promise.resolve(response != undefined); }); }; Client.prototype.readStorageObjects = function (session, request) { diff --git a/src/api.gen.ts b/src/api.gen.ts index 4389b75..b50aaae 100644 --- a/src/api.gen.ts +++ b/src/api.gen.ts @@ -95,13 +95,6 @@ export interface ApiAccountSteam { // The account token received from Steam to access their profile API. token?: string; } -/** Add users to a group. */ -export interface ApiAddGroupUsersRequest { - // The group to add users to. - group_id?: string; - // The users to add. - user_ids?: Array; -} /** A message sent on a channel. */ export interface ApiChannelMessage { // The channel this message belongs to. @@ -210,18 +203,6 @@ export interface ApiGroupUserList { // User-role pairs for a group. group_users?: Array; } -/** Immediately join an open group, or request to join a closed one. */ -export interface ApiJoinGroupRequest { - // The group ID to join. - group_id?: string; -} -/** Kick a set of users from a group. */ -export interface ApiKickGroupUsersRequest { - // The group ID to kick from. - group_id?: string; - // The users to kick. - user_ids?: Array; -} /** Represents a complete leaderboard record with all scores and associated metadata. */ export interface ApiLeaderboardRecord { // The UNIX time when the leaderboard record was created. @@ -258,11 +239,6 @@ export interface ApiLeaderboardRecordList { // A list of leaderboard records. records?: Array; } -/** Leave a group. */ -export interface ApiLeaveGroupRequest { - // The group ID to leave. - group_id?: string; -} /** Represents a realtime match. */ export interface ApiMatch { // True if it's an server-managed authoritative match, false otherwise. @@ -303,13 +279,6 @@ export interface ApiNotificationList { // Collection of notifications. notifications?: Array; } -/** Promote a set of users in a group to the next role up. */ -export interface ApiPromoteGroupUsersRequest { - // The group ID to promote in. - group_id?: string; - // The users to promote. - user_ids?: Array; -} /** Storage objects to get. */ export interface ApiReadStorageObjectId { // The collection which stores the object. @@ -486,13 +455,6 @@ export interface ApiWriteStorageObjectsRequest { // The objects to store on the server. objects?: Array; } -/** Wrapper message for `bool`. - -The JSON representation for `BoolValue` is JSON `true` and `false`. */ -export interface ProtobufBoolValue { - // The bool value. - value?: boolean; -} /** service Foo { rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); } @@ -500,20 +462,6 @@ export interface ProtobufBoolValue { The JSON representation for `Empty` is empty JSON object `{}`. */ export interface ProtobufEmpty { } -/** Wrapper message for `int32`. - -The JSON representation for `Int32Value` is JSON number. */ -export interface ProtobufInt32Value { - // The int32 value. - value?: number; -} -/** Wrapper message for `string`. - -The JSON representation for `StringValue` is JSON string. */ -export interface ProtobufStringValue { - // The string value. - value?: string; -} export const NakamaApi = (configuration: ConfigurationParameters = { basePath: BASE_PATH, @@ -1794,10 +1742,12 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Delete one or more users by ID or username. */ - deleteFriends(options: any = {}): Promise { + deleteFriends(ids?: Array, usernames?: Array, options: any = {}): Promise { const urlPath = "/v2/friend"; const queryParams = { + ids: ids, + usernames: usernames, } as any; const urlQuery = "?" + Object.keys(queryParams) .map(k => { @@ -2240,13 +2190,10 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Add users to a group. */ - addGroupUsers(groupId: string, body: ApiAddGroupUsersRequest, options: any = {}): Promise { + addGroupUsers(groupId: string, options: any = {}): Promise { if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } const urlPath = "/v2/group/{group_id}/add" .replace("{group_id}", encodeURIComponent(String(groupId))); @@ -2279,7 +2226,6 @@ export const NakamaApi = (configuration: ConfigurationParameters = { } fetchOptions.headers = {...headers, ...options.headers}; - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { @@ -2295,13 +2241,10 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Immediately join an open group, or request to join a closed one. */ - joinGroup(groupId: string, body: ApiJoinGroupRequest, options: any = {}): Promise { + joinGroup(groupId: string, options: any = {}): Promise { if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } const urlPath = "/v2/group/{group_id}/join" .replace("{group_id}", encodeURIComponent(String(groupId))); @@ -2334,7 +2277,6 @@ export const NakamaApi = (configuration: ConfigurationParameters = { } fetchOptions.headers = {...headers, ...options.headers}; - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { @@ -2350,13 +2292,10 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Kick a set of users from a group. */ - kickGroupUsers(groupId: string, body: ApiKickGroupUsersRequest, options: any = {}): Promise { + kickGroupUsers(groupId: string, options: any = {}): Promise { if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } const urlPath = "/v2/group/{group_id}/kick" .replace("{group_id}", encodeURIComponent(String(groupId))); @@ -2389,7 +2328,6 @@ export const NakamaApi = (configuration: ConfigurationParameters = { } fetchOptions.headers = {...headers, ...options.headers}; - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { @@ -2405,13 +2343,10 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Leave a group the user is a member of. */ - leaveGroup(groupId: string, body: ApiLeaveGroupRequest, options: any = {}): Promise { + leaveGroup(groupId: string, options: any = {}): Promise { if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } const urlPath = "/v2/group/{group_id}/leave" .replace("{group_id}", encodeURIComponent(String(groupId))); @@ -2444,7 +2379,6 @@ export const NakamaApi = (configuration: ConfigurationParameters = { } fetchOptions.headers = {...headers, ...options.headers}; - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { @@ -2460,13 +2394,10 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ]); }, /** Promote a set of users in a group to the next role up. */ - promoteGroupUsers(groupId: string, body: ApiPromoteGroupUsersRequest, options: any = {}): Promise { + promoteGroupUsers(groupId: string, options: any = {}): Promise { if (groupId === null || groupId === undefined) { throw new Error("'groupId' is a required parameter but is null or undefined."); } - if (body === null || body === undefined) { - throw new Error("'body' is a required parameter but is null or undefined."); - } const urlPath = "/v2/group/{group_id}/promote" .replace("{group_id}", encodeURIComponent(String(groupId))); @@ -2499,7 +2430,6 @@ export const NakamaApi = (configuration: ConfigurationParameters = { } fetchOptions.headers = {...headers, ...options.headers}; - fetchOptions.body = JSON.stringify(body || {}); return Promise.race([ fetch(configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { @@ -2777,11 +2707,12 @@ export const NakamaApi = (configuration: ConfigurationParameters = { ), ]); }, - /** Delete one or more users by ID or username. */ - deleteNotifications(options: any = {}): Promise { + /** Delete one or more notifications for the current user. */ + deleteNotifications(ids?: Array, options: any = {}): Promise { const urlPath = "/v2/notification"; const queryParams = { + ids: ids, } as any; const urlQuery = "?" + Object.keys(queryParams) .map(k => { diff --git a/src/client.ts b/src/client.ts index 7dec50b..7ed6c04 100644 --- a/src/client.ts +++ b/src/client.ts @@ -372,18 +372,109 @@ export class Client { /** Add users to a group, or accept their join requests. */ addGroupUsers(session: Session, groupId: string, ids?: Array): Promise { this.configuration.bearerToken = (session && session.token); - return this.apiClient.addGroupUsers(groupId, { user_ids: ids }).then((response: ProtobufEmpty) => { - return response !== undefined; + + const urlPath = "/v2/group/" + groupId + "/add"; + + const queryParams = { + user_ids: ids + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "POST" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); }); } /** Add friends by ID or username to a user's account. */ - // addFriends(session: Session, ids?: Array, usernames?: Array): Promise { - // this.configuration.bearerToken = (session && session.token); - // return this.apiClient.addFriends(ids, usernames).then((response: ProtobufEmpty) => { - // return Promise.resolve(response != undefined); - // }); - // } + addFriends(session: Session, ids?: Array, usernames?: Array): Promise { + this.configuration.bearerToken = (session && session.token); + + const urlPath = "/v2/friend"; + + const queryParams = { + ids: ids, + usernames: usernames + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "POST" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); + }); + } /** Authenticate a user with a custom id against the server. */ authenticateCustom(request: ApiAccountCustom): Promise { @@ -421,12 +512,58 @@ export class Client { } /** Block one or more users by ID or username. */ - // blockFriends(session: Session, ids?: Array, usernames?: Array): Promise { - // this.configuration.bearerToken = (session && session.token); - // return this.apiClient.blockFriends(ids, usernames).then((response: ProtobufEmpty) => { - // return Promise.resolve(response != undefined); - // }); - // } + blockFriends(session: Session, ids?: Array, usernames?: Array): Promise { + this.configuration.bearerToken = (session && session.token); + + const urlPath = "/v2/friend/block"; + + const queryParams = { + ids: ids, + usernames: usernames + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "POST" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); + }); + } /** Create a new group with the current user as the creator and superadmin. */ createGroup(session: Session, request: ApiCreateGroupRequest): Promise { @@ -455,12 +592,58 @@ export class Client { } /** Delete one or more users by ID or username. */ - // deleteFriends(session: Session, ids?: Array, usernames?: Array): Promise { - // this.configuration.bearerToken = (session && session.token); - // return this.apiClient.deleteFriends(ids, usernames).then((response: ProtobufEmpty) => { - // return Promise.resolve(response != undefined); - // }); - // } + deleteFriends(session: Session, ids?: Array, usernames?: Array): Promise { + this.configuration.bearerToken = (session && session.token); + + const urlPath = "/v2/friend"; + + const queryParams = { + ids: ids, + usernames: usernames + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "DELETE" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); + }); + } /** Delete a group the user is part of and has permissions to delete. */ deleteGroup(session: Session, groupId: string): Promise { @@ -592,8 +775,53 @@ export class Client { /** Kick users from a group, or decline their join requests. */ kickGroupUsers(session: Session, groupId: string, ids?: Array): Promise { this.configuration.bearerToken = (session && session.token); - return this.apiClient.kickGroupUsers(groupId, { user_ids: ids }).then((response: ProtobufEmpty) => { - return response !== undefined; + + const urlPath = "/v2/group/" + groupId + "/kick"; + + const queryParams = { + user_ids: ids + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "POST" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); }); } @@ -938,8 +1166,53 @@ export class Client { /** Promote users in a group to the next role up. */ promoteGroupUsers(session: Session, groupId: string, ids?: Array): Promise { this.configuration.bearerToken = (session && session.token); - return this.apiClient.promoteGroupUsers(groupId, { user_ids: ids }).then((response: ProtobufEmpty) => { - return response !== undefined; + + const urlPath = "/v2/group/" + groupId + "/promote"; + + const queryParams = { + user_ids: ids + } as any; + const urlQuery = "?" + Object.keys(queryParams) + .map(k => { + if (queryParams[k] instanceof Array) { + return queryParams[k].reduce((prev: any, curr: any) => { + return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&"; + }, ""); + } else { + if (queryParams[k] != null) { + return encodeURIComponent(k) + "=" + encodeURIComponent(queryParams[k]) + "&"; + } + } + }) + .join(""); + + const fetchOptions = {...{ method: "POST" /*, keepalive: true */ }} as any; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + } as any; + + if (this.configuration.bearerToken) { + headers["Authorization"] = "Bearer " + this.configuration.bearerToken; + } else if (this.configuration.username) { + headers["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password); + } + + fetchOptions.headers = {...headers}; + + return Promise.race([ + fetch(this.configuration.basePath + urlPath + urlQuery, fetchOptions).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + throw response; + } + }), + new Promise((_, reject) => + setTimeout(reject, this.configuration.timeoutMs, "Request timed out.") + ), + ]).then((response: ProtobufEmpty) => { + return Promise.resolve(response != undefined); }); } diff --git a/test/client-friend.test.js b/test/client-friend.test.js new file mode 100644 index 0000000..ae6728d --- /dev/null +++ b/test/client-friend.test.js @@ -0,0 +1,150 @@ +/** + * Copyright 2018 The Nakama Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const fs = require("fs"); +const TIMEOUT = 5000; + +// util to generate a random id. +const generateid = () => { + return [...Array(30)].map(() => Math.random().toString(36)[3]).join(''); +}; + +describe('Friend Tests', () => { + let page; + + beforeAll(async () => { + page = await global.__BROWSER__.newPage(); + + page.on("console", msg => console.log("PAGE LOG:", msg.text())); + page.on("error", err => console.log("PAGE LOG ERROR:", err)); + page.on("pageerror", err => console.log("PAGE LOG ERROR:", err)); + + const nakamaJsLib = fs.readFileSync(__dirname + "/../dist/nakama-js.umd.js", "utf8"); + await page.evaluateOnNewDocument(nakamaJsLib); + await page.goto("about:blank"); + }, TIMEOUT); + + it('should add friend, then list', async () => { + const customid1 = generateid(); + const customid2 = generateid(); + + const result = await page.evaluate(async (customid1, customid2) => { + const client1 = new nakamajs.Client(); + const session1 = await client1.authenticateCustom({ id: customid1 }); + const client2 = new nakamajs.Client(); + const session2 = await client2.authenticateCustom({ id: customid2 }); + + return client1.addFriends(session1, [session2.user_id]) + .then(result => { + return client1.listFriends(session1); + }); + }, customid1, customid2); + + expect(result).not.toBeNull(); + expect(result.friends.length).toBe(1); + expect(result.friends[0].state).toBe(3); + }); + + it('should receive friend invite, then list', async () => { + const customid1 = generateid(); + const customid2 = generateid(); + + const result = await page.evaluate(async (customid1, customid2) => { + const client1 = new nakamajs.Client(); + const session1 = await client1.authenticateCustom({ id: customid1 }); + const client2 = new nakamajs.Client(); + const session2 = await client2.authenticateCustom({ id: customid2 }); + + return client1.addFriends(session1, [session2.user_id]) + .then(result => { + return client2.listFriends(session2); + }); + }, customid1, customid2); + + expect(result).not.toBeNull(); + expect(result.friends.length).toBe(1); + expect(result.friends[0].state).toBe(2); + }); + + it('should block friend, then list', async () => { + const customid1 = generateid(); + const customid2 = generateid(); + + const result = await page.evaluate(async (customid1, customid2) => { + const client1 = new nakamajs.Client(); + const session1 = await client1.authenticateCustom({ id: customid1 }); + const client2 = new nakamajs.Client(); + const session2 = await client2.authenticateCustom({ id: customid2 }); + + return client1.blockFriends(session1, [session2.user_id]) + .then(result => { + return client1.listFriends(session1); + }); + }, customid1, customid2); + + expect(result).not.toBeNull(); + expect(result.friends.length).toBe(1); + expect(result.friends[0].state).toBe(4); + }); + + it('should add friend, accept, then list', async () => { + const customid1 = generateid(); + const customid2 = generateid(); + + const result = await page.evaluate(async (customid1, customid2) => { + const client1 = new nakamajs.Client(); + const session1 = await client1.authenticateCustom({ id: customid1 }); + const client2 = new nakamajs.Client(); + const session2 = await client2.authenticateCustom({ id: customid2 }); + + return client1.addFriends(session1, [session2.user_id]) + .then(result => { + return client2.addFriends(session2, [session1.user_id]); + }) + .then(result => { + return client1.listFriends(session1); + }); + }, customid1, customid2); + + expect(result).not.toBeNull(); + expect(result.friends.length).toBe(1); + expect(result.friends[0].state).toBe(1); + }); + + it('should add friend, reject, then list', async () => { + const customid1 = generateid(); + const customid2 = generateid(); + + const result = await page.evaluate(async (customid1, customid2) => { + const client1 = new nakamajs.Client(); + const session1 = await client1.authenticateCustom({ id: customid1 }); + const client2 = new nakamajs.Client(); + const session2 = await client2.authenticateCustom({ id: customid2 }); + + return client1.addFriends(session1, [session2.user_id]) + .then(result => { + return client2.deleteFriends(session2, [session1.user_id]); + }) + .then(result => { + return client1.listFriends(session1); + }); + }, customid1, customid2); + + expect(result).not.toBeNull(); + expect(result.friends.length).toBe(0); + }); + +}, TIMEOUT); diff --git a/test/socket-notifications.test.js b/test/socket-notification.test.js similarity index 98% rename from test/socket-notifications.test.js rename to test/socket-notification.test.js index 483b038..42e3ebc 100644 --- a/test/socket-notifications.test.js +++ b/test/socket-notification.test.js @@ -22,7 +22,7 @@ const generateid = () => { return [...Array(30)].map(() => Math.random().toString(36)[3]).join(''); }; -describe('Notifications Tests', () => { +describe('Notification Tests', () => { let page; beforeAll(async () => {