Skip to content

Commit

Permalink
Merge pull request #356 from GetStream/feature/typescript
Browse files Browse the repository at this point in the history
Typescript
  • Loading branch information
Amin Mahboubi authored Aug 11, 2020
2 parents c4b49d2 + 85f26d2 commit 617554d
Show file tree
Hide file tree
Showing 65 changed files with 8,299 additions and 7,551 deletions.
7 changes: 3 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"presets": [
[
"@babel/preset-env",
{ "targets": { "browsers": ["last 2 versions", "ie >= 10"] } }
]
["@babel/preset-env", { "targets": { "browsers": ["last 2 versions", "ie >= 10"] } }],
"@babel/preset-typescript"
],
"sourceType": "unambiguous",
"plugins": [
"@babel/plugin-transform-object-assign",
"@babel/plugin-transform-runtime",
"@babel/proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
],
"ignore": ["node_modules", "dist", "lib"]
Expand Down
70 changes: 56 additions & 14 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
const sharedRules = {
'consistent-return': 0,
'no-underscore-dangle': 0,
'import/prefer-default-export': 0,
'import/extensions': ['error', 'never', { json: 'always' }],
'func-names': 0,
'no-param-reassign': 0,
'prefer-destructuring': ['error', { object: true, array: false }],
'max-classes-per-file': 0,
'no-plusplus': 0,
'sonarjs/cognitive-complexity': 0,
'sonarjs/no-collapsible-if': 0,
'sonarjs/no-duplicate-string': 0,
'sonarjs/no-identical-functions': 0,
};

const settings = {
'import/resolver': {
node: { extensions: ['.js', '.ts'] },
},
};

module.exports = {
root: true,
plugins: ['prettier', 'chai-friendly'],
extends: [
'airbnb-base',
Expand All @@ -17,18 +40,37 @@ module.exports = {
globals: {
process: true,
},
rules: {
'consistent-return': 0,
'no-underscore-dangle': 0,
'import/prefer-default-export': 0,
'func-names': 0,
'no-param-reassign': 0,
'prefer-destructuring': ['error', { object: true, array: false }],
'max-classes-per-file': 0,
'no-plusplus': 0,
'sonarjs/cognitive-complexity': 0,
'sonarjs/no-collapsible-if': 0,
'sonarjs/no-duplicate-string': 0,
'sonarjs/no-identical-functions': 0,
},
settings,
rules: sharedRules,
overrides: [
{
files: ['**/*.ts'],
plugins: ['prettier', '@typescript-eslint', 'typescript-sort-keys'],
extends: [
'airbnb-base',
'eslint:recommended',
'plugin:prettier/recommended',
'prettier/@typescript-eslint',
'plugin:@typescript-eslint/recommended',
'plugin:sonarjs/recommended',
],
parser: '@typescript-eslint/parser',
settings,
rules: {
...sharedRules,
camelcase: 0,
'lines-between-class-members': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0,
'no-useless-constructor': 0,
'@typescript-eslint/triple-slash-reference': 0,
'typescript-sort-keys/interface': [
'error',
'asc',
{ caseSensitive: false, natural: true, requiredFirst: true },
],
'typescript-sort-keys/string-enum': ['error', 'asc', { caseSensitive: false, natural: true }],
},
},
],
};
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: build
on:
push:
branches:
- 'master'
pull_request:

on: [push]

jobs:
build:
Expand Down Expand Up @@ -46,5 +43,6 @@ jobs:
if [ "$CLOUD_TESTS" == "yes" ]; then yarn run test-cloud; fi
if [ "$CLOUD_TESTS" == "yes" ]; then yarn run test-integration-node; fi
if [ "$RUN_LINTERS" == "yes" ]; then yarn run lint; fi
if [ "$RUN_LINTERS" == "yes" ]; then yarn run dtslint; fi
if [ "$RUN_LINTERS" == "yes" ]; then yarn run types; fi
if [ "$RUN_LINTERS" == "yes" ]; then yarn run test-types; fi
if [ "$BROWSER_TESTS" == "yes" ]; then yarn run test-browser; fi
14 changes: 0 additions & 14 deletions .jsdoc

This file was deleted.

66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,72 @@ client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp,
client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, null, ['feed:1234']);
```

### Typescript

```typescript
import { connect, EnrichedActivity, NotificationActivity } from getstream;

type User1Type = { name: string; username: string; image?: string };
type User2Type = { name: string; avatar?: string };
type ActivityType = { attachments: string[]; text: string };
type Collection1Type = { cid: string; rating?: number };
type Collection2Type = { branch: number; location: string };

type ReactionType = { text: string };
type ChildReactionType = { text?: string };

const client = connect<
User1Type | User2Type,
ActivityType,
Collection1Type | Collection2Type,
ReactionType,
ChildReactionType
>('api_key', 'secret!', 'app_id');

// if you have different union types like "User1Type | User2Type" you can use type guards as follow:
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
function isUser1Type(user: User1Type | User2Type): user is User1Type {
return (user as User1Type).username !== undefined;
}

client
.user('user_id')
.get()
.then((user) => {
const { data, id } = user;
if (isUser1Type(data)) return data.username;
return id;
});

// notification: StreamFeed<User1Type | User2Type, ActivityType, Collection1Type | Collection2Type, ReactionType, ChildReactionType>
const timeline = client.feed('timeline', 'feed_id');
timeline.get({ withOwnChildren: true, withOwnReactions: true }).then((response) => {
// response: FeedAPIResponse<User1Type | User2Type, ActivityType, Collection1Type | Collection2Type, ReactionType, ChildReactionType>
if (response.next !== '') return response.next;

return (response.results as EnrichedActivity<User2Type, ActivityType>[]).map((activity) => {
return activity.id + activity.text + (activity.actor as User2Type).name;
});
});

// notification: StreamFeed<User1Type | User2Type, ActivityType, Collection1Type | Collection2Type, ReactionType, ChildReactionType>
const notification = client.feed('notification', 'feed_id');
notification.get({ mark_read: true, mark_seen: true }).then((response) => {
// response: FeedAPIResponse<User1Type | User2Type, ActivityType, Collection1Type | Collection2Type, ReactionType, ChildReactionType>
if (response.unread || response.unseen) return response.next;

return (response.results as NotificationActivity<ActivityType>[]).map((activityGroup) => {
const { activities, id, verb, activity_count, actor_count } = activityGroup;
return activities[0].text + id + actor_count + activity_count + verb;
});
});

client.collections.get('collection_1', 'taco').then((item: CollectionEntry<Collection1Type>) => {
if (item.data.rating) return { [item.data.cid]: item.data.rating };
return item.id;
});
```

### Realtime (Faye)

Stream uses [Faye](http://faye.jcoglan.com/browser.html) for realtime notifications. Below is quick guide to subscribing to feed changes
Expand Down
2 changes: 2 additions & 0 deletions babel-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable import/no-extraneous-dependencies */
require('@babel/register')({ extensions: ['.js', '.ts'] });
Loading

0 comments on commit 617554d

Please sign in to comment.