summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-09-08 04:13:01 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-09-08 04:13:01 +0900
commit3feeaccf5955ebf17c26ccc06f32bb39624b89f6 (patch)
treefa5d7ee54ba77dcc76ff3cae1d6791dcae499f92
parentwip dark (diff)
downloadsharkey-3feeaccf5955ebf17c26ccc06f32bb39624b89f6.tar.gz
sharkey-3feeaccf5955ebf17c26ccc06f32bb39624b89f6.tar.bz2
sharkey-3feeaccf5955ebf17c26ccc06f32bb39624b89f6.zip
Add type definition
-rw-r--r--src/api/endpoints/posts/create.ts4
-rw-r--r--src/api/models/post.ts14
-rw-r--r--src/api/models/user.ts40
-rw-r--r--src/utils/type.ts3
4 files changed, 57 insertions, 4 deletions
diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts
index eb979402c4..805dba7f83 100644
--- a/src/api/endpoints/posts/create.ts
+++ b/src/api/endpoints/posts/create.ts
@@ -6,7 +6,7 @@ import deepEqual = require('deep-equal');
import parse from '../../common/text';
import Post from '../../models/post';
import { isValidText } from '../../models/post';
-import User from '../../models/user';
+import { default as User, IUser } from '../../models/user';
import Following from '../../models/following';
import DriveFile from '../../models/drive-file';
import Watching from '../../models/post-watching';
@@ -24,7 +24,7 @@ import config from '../../../conf';
* @param {any} app
* @return {Promise<any>}
*/
-module.exports = (params, user, app) => new Promise(async (res, rej) => {
+module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// Get 'text' parameter
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
if (textErr) return rej('invalid text');
diff --git a/src/api/models/post.ts b/src/api/models/post.ts
index baab63f991..8b9f7f5ef6 100644
--- a/src/api/models/post.ts
+++ b/src/api/models/post.ts
@@ -1,3 +1,5 @@
+import * as mongo from 'mongodb';
+
import db from '../../db/mongodb';
export default db.get('posts') as any; // fuck type definition
@@ -5,3 +7,15 @@ export default db.get('posts') as any; // fuck type definition
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}
+
+export type IPost = {
+ _id: mongo.ObjectID;
+ created_at: Date;
+ media_ids: mongo.ObjectID[];
+ reply_to_id: mongo.ObjectID;
+ repost_id: mongo.ObjectID;
+ poll: {}; // todo
+ text: string;
+ user_id: mongo.ObjectID;
+ app_id: mongo.ObjectID;
+};
diff --git a/src/api/models/user.ts b/src/api/models/user.ts
index 9f8cf0161d..1591b339bc 100644
--- a/src/api/models/user.ts
+++ b/src/api/models/user.ts
@@ -1,4 +1,7 @@
+import * as mongo from 'mongodb';
+
import db from '../../db/mongodb';
+import { IPost } from './post';
const collection = db.get('users');
@@ -31,6 +34,39 @@ export function isValidBirthday(birthday: string): boolean {
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
}
-export interface IUser {
+export type IUser = {
+ _id: mongo.ObjectID;
+ created_at: Date;
+ email: string;
+ followers_count: number;
+ following_count: number;
+ links: string[];
name: string;
-}
+ password: string;
+ posts_count: number;
+ drive_capacity: number;
+ username: string;
+ username_lower: string;
+ token: string;
+ avatar_id: mongo.ObjectID;
+ banner_id: mongo.ObjectID;
+ data: any;
+ twitter: {
+ access_token: string;
+ access_token_secret: string;
+ user_id: string;
+ screen_name: string;
+ };
+ description: string;
+ profile: {
+ location: string;
+ birthday: string; // 'YYYY-MM-DD'
+ tags: string[];
+ };
+ last_used_at: Date;
+ latest_post: IPost;
+ pinned_post_id: mongo.ObjectID;
+ is_pro: boolean;
+ is_suspended: boolean;
+ keywords: string[];
+};
diff --git a/src/utils/type.ts b/src/utils/type.ts
new file mode 100644
index 0000000000..ba6ea0be77
--- /dev/null
+++ b/src/utils/type.ts
@@ -0,0 +1,3 @@
+// https://github.com/Microsoft/TypeScript/issues/12215
+export type Diff<T extends string, U extends string> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
+export type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]: T[P] };