Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Typescript Implementation #180

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ yarn-error.log
tests/integration/config/production.js
tests/integration/config/staging.js
tests/integration/config/development.js
.vscode/
2 changes: 2 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
singleQuote: true
trailingComma: all
34 changes: 8 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@pusher/chatkit-client",
"description": "Pusher Chatkit client SDK for browsers and react native",
"main": "dist/web/chatkit.js",
"types": "dist/web/declarations/index.d.ts",
"version": "1.5.0",
"author": "Pusher",
"license": "MIT",
Expand All @@ -15,6 +16,7 @@
},
"dependencies": {
"@pusher/platform": "^0.16.1",
"@types/ramda": "^0.26.9",
"ramda": "^0.25.0"
},
"devDependencies": {
Expand All @@ -26,9 +28,6 @@
"babel-preset-env": "^1.6.1",
"babelify": "^8.0.0",
"browserify": "^15.2.0",
"eslint": "^5.8.0",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-prettier": "^3.0.0",
"prettier": "1.14.3",
"publish-please": "^5.2.0",
"rollup": "^0.55.3",
Expand All @@ -37,15 +36,18 @@
"rollup-plugin-commonjs": "^8.3.0",
"rollup-plugin-json": "^2.3.0",
"rollup-plugin-node-resolve": "^3.0.2",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-uglify": "^3.0.0",
"snazzy": "^7.0.0",
"tap-colorize": "^1.2.0",
"tape": "^4.8.0",
"tape-run": "^4.0.0"
"tape-run": "^4.0.0",
"tslint": "^5.8.0",
"typescript": "^3.4.0"
},
"scripts": {
"lint": "eslint src tests rollup",
"format": "prettier --write src/**/*.js tests/**/*.js rollup/**/*.js example/**/*.js",
"lint": "tslint -c tslint.json 'src/**/*.ts'",
"format": "prettier --write src/**/*.ts tests/**/*.js rollup/**/*.js example/**/*.js",
"build": "yarn build:web && yarn build:react-native",
"build:web": "rollup -c rollup/web.js",
"build:react-native": "rollup -c rollup/react-native.js",
Expand All @@ -61,25 +63,5 @@
"prettier": {
"semi": false,
"trailingComma": "all"
},
"eslintConfig": {
"extends": [
"prettier",
"eslint:recommended"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": "error"
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018
},
"env": {
"browser": true,
"es6": true
}
}
}
17 changes: 4 additions & 13 deletions rollup/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import commonjs from "rollup-plugin-commonjs"
import resolve from "rollup-plugin-node-resolve"
import uglify from "rollup-plugin-uglify"
import json from "rollup-plugin-json"
import typescript from "rollup-plugin-typescript"

const pusherPlatformExports = [
"BaseClient",
Expand All @@ -12,27 +13,17 @@ const pusherPlatformExports = [
]

export default {
input: "src/main.js",
input: "src/main.ts",
plugins: [
json(),
babel({
presets: [
[
"env",
{
modules: false,
},
],
],
plugins: ["external-helpers", "transform-object-rest-spread"],
exclude: ["node_modules/**"],
}),
resolve(),
typescript({tsconfig: "tsconfig.json"}),
commonjs({
namedExports: {
"node_modules/@pusher/platform/dist/web/pusher-platform.js": pusherPlatformExports,
"node_modules/@pusher/platform/react-native.js": pusherPlatformExports,
},
extensions: ['.js', '.ts'],
}),
uglify(),
],
Expand Down
33 changes: 28 additions & 5 deletions src/attachment.js → src/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Instance } from "@pusher/platform";
import { BasicMessagePartPayload } from "./message";

export interface AttachmentMessagePartPayload extends BasicMessagePartPayload {
name: string;
size: number;
_id: string;
_downloadURL: string;
_expiration: Date;
}

export class Attachment {
constructor(basicAttachment, roomId, instance) {

public type: string;
public name: string;
public size: number;
public customData?: any;

private _id: string;
private _downloadURL: string;
private _expiration: Date;
private _roomId: string;
private _instance: Instance;

public constructor(basicAttachment: AttachmentMessagePartPayload, roomId: string, instance: Instance) {
this.type = basicAttachment.type
this.name = basicAttachment.name
this.size = basicAttachment.size
Expand All @@ -20,25 +43,25 @@ export class Attachment {
this._fetchNewDownloadURL = this._fetchNewDownloadURL.bind(this)
}

url() {
public url() {
return this.urlExpiry().getTime() - Date.now() < 1000 * 60 * 30
? this._fetchNewDownloadURL()
: Promise.resolve(this._downloadURL)
}

urlExpiry() {
public urlExpiry() {
return this._expiration
}

_fetchNewDownloadURL() {
private _fetchNewDownloadURL() {
return this._instance
.request({
method: "GET",
path: `rooms/${encodeURIComponent(this._roomId)}/attachments/${
this._id
}`,
})
.then(res => {
.then((res: any) => {
const { download_url, expiration } = JSON.parse(res)
this._downloadURL = download_url
this._expiration = new Date(expiration)
Expand Down
49 changes: 31 additions & 18 deletions src/chat-manager.js → src/chat-manager.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
import { BaseClient, HOST_BASE, Instance } from "@pusher/platform"
import { BaseClient, HOST_BASE, Instance, Logger } from "@pusher/platform"
import { split } from "ramda"

import { CurrentUser } from "./current-user"
import { typeCheck, typeCheckObj } from "./utils"
import { TokenProvider } from './token-provider'
import { DEFAULT_CONNECTION_TIMEOUT } from "./constants"

import { version } from "../package.json"

export class ChatManager {
constructor({ instanceLocator, tokenProvider, userId, ...options } = {}) {
typeCheck("instanceLocator", "string", instanceLocator)
typeCheck("tokenProvider", "object", tokenProvider)
typeCheck("tokenProvider.fetchToken", "function", tokenProvider.fetchToken)
typeCheck("userId", "string", userId)

public userId: string;
public connectionTimeout: number;
public currentUser?: CurrentUser;

public serverInstanceV2: Instance;
public serverInstanceV4: Instance;
public filesInstance: Instance;
public cursorsInstance: Instance;
public presenceInstance: Instance;

public constructor({instanceLocator, tokenProvider, userId, baseClient, logger, connectionTimeout}: {
instanceLocator: string,
tokenProvider: TokenProvider,
userId: string,
baseClient?: BaseClient,
logger?: Logger,
connectionTimeout?: number,
}) {
const cluster = split(":", instanceLocator)[1]
if (cluster === undefined) {
throw new TypeError(
`expected instanceLocator to be of the format x:y:z, but was ${instanceLocator}`,
)
}
const baseClient =
options.baseClient ||
baseClient =
baseClient ||
new BaseClient({
host: `${cluster}.${HOST_BASE}`,
logger: options.logger,
logger: logger,
sdkProduct: "chatkit",
sdkVersion: version,
})
if (typeof tokenProvider.setUserId === "function") {
tokenProvider.setUserId(userId)
if (tokenProvider.setUserId) {
tokenProvider.setUserId(userId);
}
const instanceOptions = {
client: baseClient,
locator: instanceLocator,
logger: options.logger,
tokenProvider,
logger: logger,
tokenProvider: tokenProvider,
}
this.serverInstanceV2 = new Instance({
serviceName: "chatkit",
Expand Down Expand Up @@ -63,14 +77,13 @@ export class ChatManager {
})
this.userId = userId
this.connectionTimeout =
options.connectionTimeout || DEFAULT_CONNECTION_TIMEOUT
connectionTimeout || DEFAULT_CONNECTION_TIMEOUT

this.connect = this.connect.bind(this)
this.disconnect = this.disconnect.bind(this)
}

connect(hooks = {}) {
typeCheckObj("hooks", "function", hooks)
public connect(hooks: CurrentUser['hooks']['global'] = {}) {
const currentUser = new CurrentUser({
hooks,
id: this.userId,
Expand All @@ -90,7 +103,7 @@ export class ChatManager {
})
}

disconnect() {
public disconnect() {
if (this.currentUser) this.currentUser.disconnect()
}
}
File renamed without changes.
Loading