From 17a23c3eb508bd15bc68999f448224eb7cc64354 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 5 Mar 2017 12:00:39 +0900 Subject: [PATCH] Use cafy --- package.json | 1 + src/api/endpoints/aggregation/posts/like.ts | 4 +- src/api/endpoints/aggregation/posts/likes.ts | 4 +- src/api/endpoints/aggregation/posts/reply.ts | 4 +- src/api/endpoints/aggregation/posts/repost.ts | 4 +- .../endpoints/aggregation/users/followers.ts | 4 +- .../endpoints/aggregation/users/following.ts | 4 +- src/api/endpoints/aggregation/users/like.ts | 4 +- src/api/endpoints/aggregation/users/post.ts | 4 +- src/api/endpoints/app/create.ts | 12 +- src/api/endpoints/app/name_id/available.ts | 4 +- src/api/endpoints/app/show.ts | 2 +- src/api/endpoints/auth/accept.ts | 4 +- src/api/endpoints/auth/session/generate.ts | 4 +- src/api/endpoints/auth/session/show.ts | 4 +- src/api/endpoints/auth/session/userkey.ts | 6 +- src/api/endpoints/drive/files.ts | 10 +- src/api/endpoints/drive/files/create.ts | 4 +- src/api/endpoints/drive/files/find.ts | 6 +- src/api/endpoints/drive/files/show.ts | 4 +- src/api/endpoints/drive/files/update.ts | 8 +- .../endpoints/drive/files/upload_from_url.ts | 6 +- src/api/endpoints/drive/folders.ts | 10 +- src/api/endpoints/drive/folders/create.ts | 6 +- src/api/endpoints/drive/folders/find.ts | 6 +- src/api/endpoints/drive/folders/show.ts | 4 +- src/api/endpoints/drive/folders/update.ts | 8 +- src/api/endpoints/drive/stream.ts | 10 +- src/api/endpoints/following/create.ts | 2 +- src/api/endpoints/following/delete.ts | 2 +- src/api/endpoints/i/authorized_apps.ts | 8 +- src/api/endpoints/i/favorites.ts | 8 +- src/api/endpoints/i/notifications.ts | 16 +- src/api/endpoints/i/signin_history.ts | 8 +- src/api/endpoints/i/update.ts | 14 +- src/api/endpoints/messaging/history.ts | 4 +- src/api/endpoints/messaging/messages.ts | 12 +- .../endpoints/messaging/messages/create.ts | 8 +- src/api/endpoints/my/apps.ts | 6 +- .../endpoints/notifications/mark_as_read.ts | 4 +- src/api/endpoints/posts.ts | 12 +- src/api/endpoints/posts/context.ts | 6 +- src/api/endpoints/posts/create.ts | 12 +- src/api/endpoints/posts/favorites/create.ts | 2 +- src/api/endpoints/posts/favorites/delete.ts | 2 +- src/api/endpoints/posts/likes.ts | 8 +- src/api/endpoints/posts/likes/create.ts | 2 +- src/api/endpoints/posts/likes/delete.ts | 2 +- src/api/endpoints/posts/mentions.ts | 12 +- src/api/endpoints/posts/polls/vote.ts | 4 +- src/api/endpoints/posts/replies.ts | 8 +- src/api/endpoints/posts/reposts.ts | 8 +- src/api/endpoints/posts/search.ts | 8 +- src/api/endpoints/posts/show.ts | 2 +- src/api/endpoints/posts/timeline.ts | 8 +- src/api/endpoints/username/available.ts | 4 +- src/api/endpoints/users.ts | 8 +- src/api/endpoints/users/followers.ts | 8 +- src/api/endpoints/users/following.ts | 8 +- src/api/endpoints/users/posts.ts | 12 +- src/api/endpoints/users/recommendation.ts | 6 +- src/api/endpoints/users/search.ts | 8 +- src/api/endpoints/users/search_by_username.ts | 8 +- src/api/endpoints/users/show.ts | 2 +- src/api/it.ts | 584 ------------------ src/common/has-duplicates.ts | 1 - test/it.js | 15 - 67 files changed, 202 insertions(+), 801 deletions(-) delete mode 100644 src/api/it.ts delete mode 100644 src/common/has-duplicates.ts delete mode 100644 test/it.js diff --git a/package.json b/package.json index 1c4d4add37..a6cbe504ca 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "autwh": "0.0.1", "bcryptjs": "2.4.3", "body-parser": "1.17.0", + "cafy": "1.0.0", "chai": "3.5.0", "chai-http": "3.0.0", "chalk": "1.1.3", diff --git a/src/api/endpoints/aggregation/posts/like.ts b/src/api/endpoints/aggregation/posts/like.ts index 7eaf0335c7..38f79a1202 100644 --- a/src/api/endpoints/aggregation/posts/like.ts +++ b/src/api/endpoints/aggregation/posts/like.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Post from '../../../models/post'; import Like from '../../../models/like'; @@ -13,7 +13,7 @@ import Like from '../../../models/like'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + const [postId, postIdErr] = it(params.post_id).expect.id().required().get(); if (postIdErr) return rej('invalid post_id param'); // Lookup post diff --git a/src/api/endpoints/aggregation/posts/likes.ts b/src/api/endpoints/aggregation/posts/likes.ts index c39de7d0cb..773f78b6f3 100644 --- a/src/api/endpoints/aggregation/posts/likes.ts +++ b/src/api/endpoints/aggregation/posts/likes.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Post from '../../../models/post'; import Like from '../../../models/like'; @@ -13,7 +13,7 @@ import Like from '../../../models/like'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + const [postId, postIdErr] = it(params.post_id).expect.id().required().get(); if (postIdErr) return rej('invalid post_id param'); // Lookup post diff --git a/src/api/endpoints/aggregation/posts/reply.ts b/src/api/endpoints/aggregation/posts/reply.ts index 541bc594d4..06f94f668e 100644 --- a/src/api/endpoints/aggregation/posts/reply.ts +++ b/src/api/endpoints/aggregation/posts/reply.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Post from '../../../models/post'; /** @@ -12,7 +12,7 @@ import Post from '../../../models/post'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + const [postId, postIdErr] = it(params.post_id).expect.id().required().get(); if (postIdErr) return rej('invalid post_id param'); // Lookup post diff --git a/src/api/endpoints/aggregation/posts/repost.ts b/src/api/endpoints/aggregation/posts/repost.ts index 823a6ed9e3..939f027d56 100644 --- a/src/api/endpoints/aggregation/posts/repost.ts +++ b/src/api/endpoints/aggregation/posts/repost.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Post from '../../../models/post'; /** @@ -12,7 +12,7 @@ import Post from '../../../models/post'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + const [postId, postIdErr] = it(params.post_id).expect.id().required().get(); if (postIdErr) return rej('invalid post_id param'); // Lookup post diff --git a/src/api/endpoints/aggregation/users/followers.ts b/src/api/endpoints/aggregation/users/followers.ts index cc217643cf..a5438502aa 100644 --- a/src/api/endpoints/aggregation/users/followers.ts +++ b/src/api/endpoints/aggregation/users/followers.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import User from '../../../models/user'; import Following from '../../../models/following'; @@ -13,7 +13,7 @@ import Following from '../../../models/following'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + const [userId, userIdErr] = it(params.user_id).expect.id().required().get(); if (userIdErr) return rej('invalid user_id param'); // Lookup user diff --git a/src/api/endpoints/aggregation/users/following.ts b/src/api/endpoints/aggregation/users/following.ts index 75e0c74694..1b88cd2349 100644 --- a/src/api/endpoints/aggregation/users/following.ts +++ b/src/api/endpoints/aggregation/users/following.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import User from '../../../models/user'; import Following from '../../../models/following'; @@ -13,7 +13,7 @@ import Following from '../../../models/following'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + const [userId, userIdErr] = it(params.user_id).expect.id().required().get(); if (userIdErr) return rej('invalid user_id param'); // Lookup user diff --git a/src/api/endpoints/aggregation/users/like.ts b/src/api/endpoints/aggregation/users/like.ts index a1a3b5cd9d..0fce7466f8 100644 --- a/src/api/endpoints/aggregation/users/like.ts +++ b/src/api/endpoints/aggregation/users/like.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import User from '../../../models/user'; import Like from '../../../models/like'; @@ -13,7 +13,7 @@ import Like from '../../../models/like'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + const [userId, userIdErr] = it(params.user_id).expect.id().required().get(); if (userIdErr) return rej('invalid user_id param'); // Lookup user diff --git a/src/api/endpoints/aggregation/users/post.ts b/src/api/endpoints/aggregation/users/post.ts index a4d9e2ddfb..85a702045a 100644 --- a/src/api/endpoints/aggregation/users/post.ts +++ b/src/api/endpoints/aggregation/users/post.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import User from '../../../models/user'; import Post from '../../../models/post'; @@ -13,7 +13,7 @@ import Post from '../../../models/post'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + const [userId, userIdErr] = it(params.user_id).expect.id().required().get(); if (userIdErr) return rej('invalid user_id param'); // Lookup user diff --git a/src/api/endpoints/app/create.ts b/src/api/endpoints/app/create.ts index a357280aab..43f533d216 100644 --- a/src/api/endpoints/app/create.ts +++ b/src/api/endpoints/app/create.ts @@ -2,7 +2,7 @@ * Module dependencies */ import rndstr from 'rndstr'; -import it from '../../it'; +import it from 'cafy'; import App from '../../models/app'; import { isValidNameId } from '../../models/app'; import serialize from '../../serializers/app'; @@ -69,24 +69,24 @@ import serialize from '../../serializers/app'; */ module.exports = async (params, user) => new Promise(async (res, rej) => { // Get 'name_id' parameter - const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed(); + const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).get(); if (nameIdErr) return rej('invalid name_id param'); // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().required().qed(); + const [name, nameErr] = it(params.name).expect.string().required().get(); if (nameErr) return rej('invalid name param'); // Get 'description' parameter - const [description, descriptionErr] = it(params.description).expect.string().required().qed(); + const [description, descriptionErr] = it(params.description).expect.string().required().get(); if (descriptionErr) return rej('invalid description param'); // Get 'permission' parameter - const [permission, permissionErr] = it(params.permission).expect.array().unique().allString().required().qed(); + const [permission, permissionErr] = it(params.permission).expect.array().unique().allString().required().get(); if (permissionErr) return rej('invalid permission param'); // Get 'callback_url' parameter // TODO: Check it is valid url - const [callbackUrl, callbackUrlErr] = it(params.callback_url).expect.nullable.string().default(null).qed(); + const [callbackUrl = null, callbackUrlErr] = it(params.callback_url).expect.nullable.string().get(); if (callbackUrlErr) return rej('invalid callback_url param'); // Generate secret diff --git a/src/api/endpoints/app/name_id/available.ts b/src/api/endpoints/app/name_id/available.ts index 64171fc73f..ac38c89847 100644 --- a/src/api/endpoints/app/name_id/available.ts +++ b/src/api/endpoints/app/name_id/available.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import App from '../../../models/app'; import { isValidNameId } from '../../../models/app'; @@ -42,7 +42,7 @@ import { isValidNameId } from '../../../models/app'; */ module.exports = async (params) => new Promise(async (res, rej) => { // Get 'name_id' parameter - const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed(); + const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).get(); if (nameIdErr) return rej('invalid name_id param'); // Get exist diff --git a/src/api/endpoints/app/show.ts b/src/api/endpoints/app/show.ts index e0ff02cf4b..e4aa3dc0fd 100644 --- a/src/api/endpoints/app/show.ts +++ b/src/api/endpoints/app/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import App from '../../models/app'; import serialize from '../../serializers/app'; diff --git a/src/api/endpoints/auth/accept.ts b/src/api/endpoints/auth/accept.ts index 7fc4a8a878..fc74979bb3 100644 --- a/src/api/endpoints/auth/accept.ts +++ b/src/api/endpoints/auth/accept.ts @@ -3,7 +3,7 @@ */ import rndstr from 'rndstr'; const crypto = require('crypto'); -import it from '../../it'; +import it from 'cafy'; import App from '../../models/app'; import AuthSess from '../../models/auth-session'; import AccessToken from '../../models/access-token'; @@ -40,7 +40,7 @@ import AccessToken from '../../models/access-token'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'token' parameter - const [token, tokenErr] = it(params.token).expect.string().required().qed(); + const [token, tokenErr] = it(params.token).expect.string().required().get(); if (tokenErr) return rej('invalid token param'); // Fetch token diff --git a/src/api/endpoints/auth/session/generate.ts b/src/api/endpoints/auth/session/generate.ts index ceb3079ae2..2378d123f3 100644 --- a/src/api/endpoints/auth/session/generate.ts +++ b/src/api/endpoints/auth/session/generate.ts @@ -2,7 +2,7 @@ * Module dependencies */ import * as uuid from 'uuid'; -import it from '../../../it'; +import it from 'cafy'; import App from '../../../models/app'; import AuthSess from '../../../models/auth-session'; import config from '../../../../conf'; @@ -46,7 +46,7 @@ import config from '../../../../conf'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'app_secret' parameter - const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().qed(); + const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().get(); if (appSecretErr) return rej('invalid app_secret param'); // Lookup app diff --git a/src/api/endpoints/auth/session/show.ts b/src/api/endpoints/auth/session/show.ts index 77f8c5dc9e..e16943683f 100644 --- a/src/api/endpoints/auth/session/show.ts +++ b/src/api/endpoints/auth/session/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import AuthSess from '../../../models/auth-session'; import serialize from '../../../serializers/auth-session'; @@ -54,7 +54,7 @@ import serialize from '../../../serializers/auth-session'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'token' parameter - const [token, tokenErr] = it(params.token).expect.string().required().qed(); + const [token, tokenErr] = it(params.token).expect.string().required().get(); if (tokenErr) return rej('invalid token param'); // Lookup session diff --git a/src/api/endpoints/auth/session/userkey.ts b/src/api/endpoints/auth/session/userkey.ts index 121c9ce269..9816ea2051 100644 --- a/src/api/endpoints/auth/session/userkey.ts +++ b/src/api/endpoints/auth/session/userkey.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import App from '../../../models/app'; import AuthSess from '../../../models/auth-session'; import AccessToken from '../../../models/access-token'; @@ -51,7 +51,7 @@ import serialize from '../../../serializers/user'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'app_secret' parameter - const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().qed(); + const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().get(); if (appSecretErr) return rej('invalid app_secret param'); // Lookup app @@ -64,7 +64,7 @@ module.exports = (params) => new Promise(async (res, rej) => { } // Get 'token' parameter - const [token, tokenErr] = it(params.token).expect.string().required().qed(); + const [token, tokenErr] = it(params.token).expect.string().required().get(); if (tokenErr) return rej('invalid token param'); // Fetch token diff --git a/src/api/endpoints/drive/files.ts b/src/api/endpoints/drive/files.ts index 16606d54b2..f9c5f49d2a 100644 --- a/src/api/endpoints/drive/files.ts +++ b/src/api/endpoints/drive/files.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import DriveFile from '../../models/drive-file'; import serialize from '../../serializers/drive-file'; @@ -15,15 +15,15 @@ import serialize from '../../serializers/drive-file'; */ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified @@ -32,7 +32,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { } // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + const [folderId = null, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); // Construct query diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts index c2dc18f33d..e75db60010 100644 --- a/src/api/endpoints/drive/files/create.ts +++ b/src/api/endpoints/drive/files/create.ts @@ -2,7 +2,7 @@ * Module dependencies */ import * as fs from 'fs'; -import it from '../../../it'; +import it from 'cafy'; import { validateFileName } from '../../../models/drive-file'; import serialize from '../../../serializers/drive-file'; import create from '../../../common/add-file-to-drive'; @@ -39,7 +39,7 @@ module.exports = (file, params, user) => new Promise(async (res, rej) => { } // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + const [folderId = null, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); // Create file diff --git a/src/api/endpoints/drive/files/find.ts b/src/api/endpoints/drive/files/find.ts index f2819692f1..66a4f70e3f 100644 --- a/src/api/endpoints/drive/files/find.ts +++ b/src/api/endpoints/drive/files/find.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFile from '../../../models/drive-file'; import serialize from '../../../serializers/drive-file'; @@ -14,11 +14,11 @@ import serialize from '../../../serializers/drive-file'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().required().qed(); + const [name, nameErr] = it(params.name).expect.string().required().get(); if (nameErr) return rej('invalid name param'); // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + const [folderId = null, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); // Issue query diff --git a/src/api/endpoints/drive/files/show.ts b/src/api/endpoints/drive/files/show.ts index d439ec49f3..0f22d54a4d 100644 --- a/src/api/endpoints/drive/files/show.ts +++ b/src/api/endpoints/drive/files/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFile from '../../../models/drive-file'; import serialize from '../../../serializers/drive-file'; @@ -14,7 +14,7 @@ import serialize from '../../../serializers/drive-file'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'file_id' parameter - const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed(); + const [fileId, fileIdErr] = it(params.file_id).expect.id().required().get(); if (fileIdErr) return rej('invalid file_id param'); // Fetch file diff --git a/src/api/endpoints/drive/files/update.ts b/src/api/endpoints/drive/files/update.ts index 1a75a15daf..b5309506f2 100644 --- a/src/api/endpoints/drive/files/update.ts +++ b/src/api/endpoints/drive/files/update.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFolder from '../../../models/drive-folder'; import DriveFile from '../../../models/drive-file'; import { validateFileName } from '../../../models/drive-file'; @@ -17,7 +17,7 @@ import event from '../../../event'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'file_id' parameter - const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed(); + const [fileId, fileIdErr] = it(params.file_id).expect.id().required().get(); if (fileIdErr) return rej('invalid file_id param'); // Fetch file @@ -36,12 +36,12 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(validateFileName).qed(); + const [name, nameErr] = it(params.name).expect.string().validate(validateFileName).get(); if (nameErr) return rej('invalid name param'); if (name) file.name = name; // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().qed(); + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); if (folderId !== undefined) { diff --git a/src/api/endpoints/drive/files/upload_from_url.ts b/src/api/endpoints/drive/files/upload_from_url.ts index 7042b1e020..627e81c44e 100644 --- a/src/api/endpoints/drive/files/upload_from_url.ts +++ b/src/api/endpoints/drive/files/upload_from_url.ts @@ -3,7 +3,7 @@ */ import * as URL from 'url'; const download = require('download'); -import it from '../../../it'; +import it from 'cafy'; import { validateFileName } from '../../../models/drive-file'; import serialize from '../../../serializers/drive-file'; import create from '../../../common/add-file-to-drive'; @@ -18,7 +18,7 @@ import create from '../../../common/add-file-to-drive'; module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'url' parameter // TODO: Validate this url - const [url, urlErr] = it(params.url).expect.string().required().qed(); + const [url, urlErr] = it(params.url).expect.string().required().get(); if (urlErr) return rej('invalid url param'); let name = URL.parse(url).pathname.split('/').pop(); @@ -27,7 +27,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + const [folderId = null, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); // Download file diff --git a/src/api/endpoints/drive/folders.ts b/src/api/endpoints/drive/folders.ts index 8e04aa711e..2c5a9ea44a 100644 --- a/src/api/endpoints/drive/folders.ts +++ b/src/api/endpoints/drive/folders.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import DriveFolder from '../../models/drive-folder'; import serialize from '../../serializers/drive-folder'; @@ -15,15 +15,15 @@ import serialize from '../../serializers/drive-folder'; */ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified @@ -32,7 +32,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { } // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + const [folderId = null, folderIdErr] = it(params.folder_id).expect.nullable.id().get(); if (folderIdErr) return rej('invalid folder_id param'); // Construct query diff --git a/src/api/endpoints/drive/folders/create.ts b/src/api/endpoints/drive/folders/create.ts index f2f788fe41..9d4b530924 100644 --- a/src/api/endpoints/drive/folders/create.ts +++ b/src/api/endpoints/drive/folders/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFolder from '../../../models/drive-folder'; import { isValidFolderName } from '../../../models/drive-folder'; import serialize from '../../../serializers/drive-folder'; @@ -16,11 +16,11 @@ import event from '../../../event'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).default('無題のフォルダー').qed(); + const [name = '無題のフォルダー', nameErr] = it(params.name).expect.string().validate(isValidFolderName).get(); if (nameErr) return rej('invalid name param'); // Get 'parent_id' parameter - const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().default(null).qed(); + const [parentId = null, parentIdErr] = it(params.parent_id).expect.nullable.id().get(); if (parentIdErr) return rej('invalid parent_id param'); // If the parent folder is specified diff --git a/src/api/endpoints/drive/folders/find.ts b/src/api/endpoints/drive/folders/find.ts index 27f667b21c..0877f46bc9 100644 --- a/src/api/endpoints/drive/folders/find.ts +++ b/src/api/endpoints/drive/folders/find.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFolder from '../../../models/drive-folder'; import serialize from '../../../serializers/drive-folder'; @@ -14,11 +14,11 @@ import serialize from '../../../serializers/drive-folder'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().required().qed(); + const [name, nameErr] = it(params.name).expect.string().required().get(); if (nameErr) return rej('invalid name param'); // Get 'parent_id' parameter - const [parentId, parentIdErr] = it(params.parent_id).expect.id().qed(); + const [parentId, parentIdErr] = it(params.parent_id).expect.id().get(); if (parentIdErr) return rej('invalid parent_id param'); // Issue query diff --git a/src/api/endpoints/drive/folders/show.ts b/src/api/endpoints/drive/folders/show.ts index ea9a56d7d5..bd5ef731db 100644 --- a/src/api/endpoints/drive/folders/show.ts +++ b/src/api/endpoints/drive/folders/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFolder from '../../../models/drive-folder'; import serialize from '../../../serializers/drive-folder'; @@ -14,7 +14,7 @@ import serialize from '../../../serializers/drive-folder'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed(); + const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().get(); if (folderIdErr) return rej('invalid folder_id param'); // Get folder diff --git a/src/api/endpoints/drive/folders/update.ts b/src/api/endpoints/drive/folders/update.ts index 982b44a5f9..815f458756 100644 --- a/src/api/endpoints/drive/folders/update.ts +++ b/src/api/endpoints/drive/folders/update.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import DriveFolder from '../../../models/drive-folder'; import { isValidFolderName } from '../../../models/drive-folder'; import serialize from '../../../serializers/drive-file'; @@ -16,7 +16,7 @@ import event from '../../../event'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'folder_id' parameter - const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed(); + const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().get(); if (folderIdErr) return rej('invalid folder_id param'); // Fetch folder @@ -31,12 +31,12 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).qed(); + const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).get(); if (nameErr) return rej('invalid name param'); if (name) folder.name = name; // Get 'parent_id' parameter - const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().qed(); + const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().get(); if (parentIdErr) return rej('invalid parent_id param'); if (parentId !== undefined) { if (parentId === null) { diff --git a/src/api/endpoints/drive/stream.ts b/src/api/endpoints/drive/stream.ts index 69848825a5..f5b53680db 100644 --- a/src/api/endpoints/drive/stream.ts +++ b/src/api/endpoints/drive/stream.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import DriveFile from '../../models/drive-file'; import serialize from '../../serializers/drive-file'; @@ -14,15 +14,15 @@ import serialize from '../../serializers/drive-file'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified @@ -31,7 +31,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'type' parameter - const [type, typeErr] = it(params.type).expect.string().match(/^[a-zA-Z\/\-\*]+$/).qed(); + const [type, typeErr] = it(params.type).expect.string().match(/^[a-zA-Z\/\-\*]+$/).get(); if (typeErr) return rej('invalid type param'); // Construct query diff --git a/src/api/endpoints/following/create.ts b/src/api/endpoints/following/create.ts index 7991f5fd97..45c93ef056 100644 --- a/src/api/endpoints/following/create.ts +++ b/src/api/endpoints/following/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import Following from '../../models/following'; import notify from '../../common/notify'; diff --git a/src/api/endpoints/following/delete.ts b/src/api/endpoints/following/delete.ts index 84b1efd0fc..b75fa770e0 100644 --- a/src/api/endpoints/following/delete.ts +++ b/src/api/endpoints/following/delete.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import Following from '../../models/following'; import event from '../../event'; diff --git a/src/api/endpoints/i/authorized_apps.ts b/src/api/endpoints/i/authorized_apps.ts index ed3670c7da..16de70ba31 100644 --- a/src/api/endpoints/i/authorized_apps.ts +++ b/src/api/endpoints/i/authorized_apps.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import AccessToken from '../../models/access-token'; import serialize from '../../serializers/app'; @@ -14,15 +14,15 @@ import serialize from '../../serializers/app'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'sort' parameter - const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get(); if (sortError) return rej('invalid sort param'); // Get tokens diff --git a/src/api/endpoints/i/favorites.ts b/src/api/endpoints/i/favorites.ts index 11de625be5..208509ec37 100644 --- a/src/api/endpoints/i/favorites.ts +++ b/src/api/endpoints/i/favorites.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Favorite from '../../models/favorite'; import serialize from '../../serializers/post'; @@ -14,15 +14,15 @@ import serialize from '../../serializers/post'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'sort' parameter - const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get(); if (sortError) return rej('invalid sort param'); // Get favorites diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts index e6c6558fb1..c2b36686a1 100644 --- a/src/api/endpoints/i/notifications.ts +++ b/src/api/endpoints/i/notifications.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Notification from '../../models/notification'; import serialize from '../../serializers/notification'; import getFriends from '../../common/get-friends'; @@ -15,28 +15,28 @@ import getFriends from '../../common/get-friends'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'following' parameter - const [following, followingError] = - it(params.following).expect.boolean().default(false).qed(); + const [following = false, followingError] = + it(params.following).expect.boolean().get(); if (followingError) return rej('invalid following param'); // Get 'mark_as_read' parameter - const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed(); + const [markAsRead = true, markAsReadErr] = it(params.mark_as_read).expect.boolean().get(); if (markAsReadErr) return rej('invalid mark_as_read param'); // Get 'type' parameter - const [type, typeErr] = it(params.type).expect.array().unique().allString().qed(); + const [type, typeErr] = it(params.type).expect.array().unique().allString().get(); if (typeErr) return rej('invalid type param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/i/signin_history.ts b/src/api/endpoints/i/signin_history.ts index d549f682cb..5b5f654eea 100644 --- a/src/api/endpoints/i/signin_history.ts +++ b/src/api/endpoints/i/signin_history.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Signin from '../../models/signin'; import serialize from '../../serializers/signin'; @@ -14,15 +14,15 @@ import serialize from '../../serializers/signin'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index 1895d28375..fddee6a1b1 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import { isValidName, isValidDescription, isValidLocation, isValidBirthday } from '../../models/user'; import serialize from '../../serializers/user'; @@ -19,32 +19,32 @@ import config from '../../../conf'; */ module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed(); + const [name, nameErr] = it(params.name).expect.string().validate(isValidName).get(); if (nameErr) return rej('invalid name param'); if (name) user.name = name; // Get 'description' parameter - const [description, descriptionErr] = it(params.description).expect.nullable.string().validate(isValidDescription).qed(); + const [description, descriptionErr] = it(params.description).expect.nullable.string().validate(isValidDescription).get(); if (descriptionErr) return rej('invalid description param'); if (description !== undefined) user.description = description; // Get 'location' parameter - const [location, locationErr] = it(params.location).expect.nullable.string().validate(isValidLocation).qed(); + const [location, locationErr] = it(params.location).expect.nullable.string().validate(isValidLocation).get(); if (locationErr) return rej('invalid location param'); if (location !== undefined) user.location = location; // Get 'birthday' parameter - const [birthday, birthdayErr] = it(params.birthday).expect.nullable.string().validate(isValidBirthday).qed(); + const [birthday, birthdayErr] = it(params.birthday).expect.nullable.string().validate(isValidBirthday).get(); if (birthdayErr) return rej('invalid birthday param'); if (birthday !== undefined) user.birthday = birthday; // Get 'avatar_id' parameter - const [avatarId, avatarIdErr] = it(params.avatar_id).expect.id().qed(); + const [avatarId, avatarIdErr] = it(params.avatar_id).expect.id().get(); if (avatarIdErr) return rej('invalid avatar_id param'); if (avatarId) user.avatar_id = avatarId; // Get 'banner_id' parameter - const [bannerId, bannerIdErr] = it(params.banner_id).expect.id().qed(); + const [bannerId, bannerIdErr] = it(params.banner_id).expect.id().get(); if (bannerIdErr) return rej('invalid banner_id param'); if (bannerId) user.banner_id = bannerId; diff --git a/src/api/endpoints/messaging/history.ts b/src/api/endpoints/messaging/history.ts index 73d7af77a5..68f46ca3dc 100644 --- a/src/api/endpoints/messaging/history.ts +++ b/src/api/endpoints/messaging/history.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import History from '../../models/messaging-history'; import serialize from '../../serializers/messaging-message'; @@ -14,7 +14,7 @@ import serialize from '../../serializers/messaging-message'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get history diff --git a/src/api/endpoints/messaging/messages.ts b/src/api/endpoints/messaging/messages.ts index 40fd0cf047..7adfa63611 100644 --- a/src/api/endpoints/messaging/messages.ts +++ b/src/api/endpoints/messaging/messages.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Message from '../../models/messaging-message'; import User from '../../models/user'; import serialize from '../../serializers/messaging-message'; @@ -17,7 +17,7 @@ import { publishMessagingStream } from '../../event'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed(); + const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().get(); if (recipientIdErr) return rej('invalid user_id param'); // Fetch recipient @@ -34,19 +34,19 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'mark_as_read' parameter - const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed(); + const [markAsRead = true, markAsReadErr] = it(params.mark_as_read).expect.boolean().get(); if (markAsReadErr) return rej('invalid mark_as_read param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/messaging/messages/create.ts b/src/api/endpoints/messaging/messages/create.ts index 81968c8979..a63e8b1959 100644 --- a/src/api/endpoints/messaging/messages/create.ts +++ b/src/api/endpoints/messaging/messages/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Message from '../../../models/messaging-message'; import { isValidText } from '../../../models/messaging-message'; import History from '../../../models/messaging-history'; @@ -21,7 +21,7 @@ import config from '../../../../conf'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'user_id' parameter - const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed(); + const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().get(); if (recipientIdErr) return rej('invalid user_id param'); // Myself @@ -43,11 +43,11 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Get 'text' parameter - const [text, textErr] = it(params.text).expect.string().validate(isValidText).qed(); + const [text, textErr] = it(params.text).expect.string().validate(isValidText).get(); if (textErr) return rej('invalid text'); // Get 'file_id' parameter - const [fileId, fileIdErr] = it(params.file_id).expect.id().qed(); + const [fileId, fileIdErr] = it(params.file_id).expect.id().get(); if (fileIdErr) return rej('invalid file_id param'); let file = null; diff --git a/src/api/endpoints/my/apps.ts b/src/api/endpoints/my/apps.ts index f16be81d0c..0aee1c031a 100644 --- a/src/api/endpoints/my/apps.ts +++ b/src/api/endpoints/my/apps.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import App from '../../models/app'; import serialize from '../../serializers/app'; @@ -14,11 +14,11 @@ import serialize from '../../serializers/app'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); const query = { diff --git a/src/api/endpoints/notifications/mark_as_read.ts b/src/api/endpoints/notifications/mark_as_read.ts index 703562fecd..9dc7c20646 100644 --- a/src/api/endpoints/notifications/mark_as_read.ts +++ b/src/api/endpoints/notifications/mark_as_read.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Notification from '../../models/notification'; import serialize from '../../serializers/notification'; import event from '../../event'; @@ -14,7 +14,7 @@ import event from '../../event'; * @return {Promise} */ module.exports = (params, user) => new Promise(async (res, rej) => { - const [notificationId, notificationIdErr] = it(params.notification_id).expect.id().required().qed(); + const [notificationId, notificationIdErr] = it(params.notification_id).expect.id().required().get(); if (notificationIdErr) return rej('invalid notification_id param'); // Get notification diff --git a/src/api/endpoints/posts.ts b/src/api/endpoints/posts.ts index cd2da99c25..2e8ef6c6da 100644 --- a/src/api/endpoints/posts.ts +++ b/src/api/endpoints/posts.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../it'; +import it from 'cafy'; import Post from '../models/post'; import serialize from '../serializers/post'; @@ -13,23 +13,23 @@ import serialize from '../serializers/post'; */ module.exports = (params) => new Promise(async (res, rej) => { // Get 'include_replies' parameter - const [includeReplies, includeRepliesErr] = it(params.include_replies).expect.boolean().default(true).qed(); + const [includeReplies = true, includeRepliesErr] = it(params.include_replies).expect.boolean().get(); if (includeRepliesErr) return rej('invalid include_replies param'); // Get 'include_reposts' parameter - const [includeReposts, includeRepostsErr] = it(params.include_reposts).expect.boolean().default(true).qed(); + const [includeReposts = true, includeRepostsErr] = it(params.include_reposts).expect.boolean().get(); if (includeRepostsErr) return rej('invalid include_reposts param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts index 7bccecfb98..11dd43fe97 100644 --- a/src/api/endpoints/posts/context.ts +++ b/src/api/endpoints/posts/context.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import serialize from '../../serializers/post'; @@ -18,11 +18,11 @@ module.exports = (params, user) => new Promise(async (res, rej) => { if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Lookup post diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index dfc9866515..3737012a30 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import parse from '../../../common/text'; import Post from '../../models/post'; import { isValidText } from '../../models/post'; @@ -23,11 +23,11 @@ import config from '../../../conf'; */ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'text' parameter - const [text, textErr] = it(params.text).must.be.a.string().validate(isValidText).qed(); + const [text, textErr] = it(params.text).must.be.a.string().validate(isValidText).get(); if (textErr) return rej('invalid text'); // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = it(params.media_ids).must.be.an.array().unique().range(1, 4).qed(); + const [mediaIds, mediaIdsErr] = it(params.media_ids).must.be.an.array().unique().range(1, 4).get(); if (mediaIdsErr) return rej('invalid media_ids'); let files = []; @@ -36,7 +36,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { // forEach だと途中でエラーなどがあっても return できないので // 敢えて for を使っています。 for (let i = 0; i < mediaIds.length; i++) { - const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().qed(); + const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().get(); if (mediaIdErr) return rej('invalid media id'); // Fetch file @@ -59,7 +59,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { } // Get 'repost_id' parameter - const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().qed(); + const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().get(); if (repostIdErr) return rej('invalid repost_id'); let repost = null; @@ -138,7 +138,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { if (choice.trim().length > 50) return true; return false; })) - .qed(); + .get(); if (pollChoicesErr) return rej('invalid poll choices'); _poll.choices = pollChoices.map((choice, i) => ({ diff --git a/src/api/endpoints/posts/favorites/create.ts b/src/api/endpoints/posts/favorites/create.ts index 7ae77778e3..bf316b3573 100644 --- a/src/api/endpoints/posts/favorites/create.ts +++ b/src/api/endpoints/posts/favorites/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Favorite from '../../../models/favorite'; import Post from '../../../models/post'; diff --git a/src/api/endpoints/posts/favorites/delete.ts b/src/api/endpoints/posts/favorites/delete.ts index 437863d06c..5d4ad0a79d 100644 --- a/src/api/endpoints/posts/favorites/delete.ts +++ b/src/api/endpoints/posts/favorites/delete.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Favorite from '../../../models/favorite'; import Post from '../../../models/post'; diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/likes.ts index 88d3a6e1de..8aca5f9904 100644 --- a/src/api/endpoints/posts/likes.ts +++ b/src/api/endpoints/posts/likes.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import Like from '../../models/like'; import serialize from '../../serializers/user'; @@ -19,15 +19,15 @@ module.exports = (params, user) => new Promise(async (res, rej) => { if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'sort' parameter - const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get(); if (sortError) return rej('invalid sort param'); // Lookup post diff --git a/src/api/endpoints/posts/likes/create.ts b/src/api/endpoints/posts/likes/create.ts index 43e76d1a58..9447cba247 100644 --- a/src/api/endpoints/posts/likes/create.ts +++ b/src/api/endpoints/posts/likes/create.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Like from '../../../models/like'; import Post from '../../../models/post'; import User from '../../../models/user'; diff --git a/src/api/endpoints/posts/likes/delete.ts b/src/api/endpoints/posts/likes/delete.ts index bd2f83e21d..411a1d53ca 100644 --- a/src/api/endpoints/posts/likes/delete.ts +++ b/src/api/endpoints/posts/likes/delete.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Like from '../../../models/like'; import Post from '../../../models/post'; import User from '../../../models/user'; diff --git a/src/api/endpoints/posts/mentions.ts b/src/api/endpoints/posts/mentions.ts index 3f54674955..677df52b28 100644 --- a/src/api/endpoints/posts/mentions.ts +++ b/src/api/endpoints/posts/mentions.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import getFriends from '../../common/get-friends'; import serialize from '../../serializers/post'; @@ -15,20 +15,20 @@ import serialize from '../../serializers/post'; */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'following' parameter - const [following, followingError] = - it(params.following).expect.boolean().default(false).qed(); + const [following = false, followingError] = + it(params.following).expect.boolean().get(); if (followingError) return rej('invalid following param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts index 36d0de975b..d049041b2f 100644 --- a/src/api/endpoints/posts/polls/vote.ts +++ b/src/api/endpoints/posts/polls/vote.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../../it'; +import it from 'cafy'; import Vote from '../../../models/poll-vote'; import Post from '../../../models/post'; import notify from '../../../common/notify'; @@ -36,7 +36,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { it(params.choice).expect.string() .required() .validate(c => post.poll.choices.some(x => x.id == c)) - .qed(); + .get(); if (choiceError) return rej('invalid choice param'); // if already voted diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts index 378ba7ec2b..9279cd66f6 100644 --- a/src/api/endpoints/posts/replies.ts +++ b/src/api/endpoints/posts/replies.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import serialize from '../../serializers/post'; @@ -18,15 +18,15 @@ module.exports = (params, user) => new Promise(async (res, rej) => { if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100)).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'sort' parameter - const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get(); if (sortError) return rej('invalid sort param'); // Lookup post diff --git a/src/api/endpoints/posts/reposts.ts b/src/api/endpoints/posts/reposts.ts index fc9b56cfc4..14de17992d 100644 --- a/src/api/endpoints/posts/reposts.ts +++ b/src/api/endpoints/posts/reposts.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import serialize from '../../serializers/post'; @@ -18,15 +18,15 @@ module.exports = (params, user) => new Promise(async (res, rej) => { if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/posts/search.ts b/src/api/endpoints/posts/search.ts index e869701a30..04b845bc3a 100644 --- a/src/api/endpoints/posts/search.ts +++ b/src/api/endpoints/posts/search.ts @@ -2,7 +2,7 @@ * Module dependencies */ import * as mongo from 'mongodb'; -import it from '../../it'; +import it from 'cafy'; const escapeRegexp = require('escape-regexp'); import Post from '../../models/post'; import serialize from '../../serializers/post'; @@ -17,15 +17,15 @@ import config from '../../../conf'; */ module.exports = (params, me) => new Promise(async (res, rej) => { // Get 'query' parameter - const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').qed(); + const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').get(); if (queryError) return rej('invalid query param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'max' parameter - const [max, maxErr] = it(params.max).expect.number().range(1, 30).default(10).qed(); + const [max = 10, maxErr] = it(params.max).expect.number().range(1, 30).get(); if (maxErr) return rej('invalid max param'); // If Elasticsearch is available, search by it diff --git a/src/api/endpoints/posts/show.ts b/src/api/endpoints/posts/show.ts index 0ff1947e98..e19a3dae78 100644 --- a/src/api/endpoints/posts/show.ts +++ b/src/api/endpoints/posts/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import serialize from '../../serializers/post'; diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts index 83a96e6c65..02fdf4a17c 100644 --- a/src/api/endpoints/posts/timeline.ts +++ b/src/api/endpoints/posts/timeline.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import getFriends from '../../common/get-friends'; import serialize from '../../serializers/post'; @@ -16,15 +16,15 @@ import serialize from '../../serializers/post'; */ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/username/available.ts b/src/api/endpoints/username/available.ts index 049121d2d2..309e289d06 100644 --- a/src/api/endpoints/username/available.ts +++ b/src/api/endpoints/username/available.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import { validateUsername } from '../../models/user'; @@ -13,7 +13,7 @@ import { validateUsername } from '../../models/user'; */ module.exports = async (params) => new Promise(async (res, rej) => { // Get 'username' parameter - const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).qed(); + const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).get(); if (usernameError) return rej('invalid username param'); // Get exist diff --git a/src/api/endpoints/users.ts b/src/api/endpoints/users.ts index cc4e5865a6..6b12c3072e 100644 --- a/src/api/endpoints/users.ts +++ b/src/api/endpoints/users.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../it'; +import it from 'cafy'; import User from '../models/user'; import serialize from '../serializers/user'; @@ -14,15 +14,15 @@ import serialize from '../serializers/user'; */ module.exports = (params, me) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/users/followers.ts b/src/api/endpoints/users/followers.ts index e593854e81..09bc5d83ef 100644 --- a/src/api/endpoints/users/followers.ts +++ b/src/api/endpoints/users/followers.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import Following from '../../models/following'; import serialize from '../../serializers/user'; @@ -20,15 +20,15 @@ module.exports = (params, me) => new Promise(async (res, rej) => { if (userIdErr) return rej('invalid user_id param'); // Get 'iknow' parameter - const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed(); + const [iknow = false, iknowErr] = it(params.iknow).expect.boolean().get(); if (iknowErr) return rej('invalid iknow param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'cursor' parameter - const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed(); + const [cursor = null, cursorErr] = it(params.cursor).expect.id().get(); if (cursorErr) return rej('invalid cursor param'); // Lookup user diff --git a/src/api/endpoints/users/following.ts b/src/api/endpoints/users/following.ts index 00baedbf87..a6f51410d7 100644 --- a/src/api/endpoints/users/following.ts +++ b/src/api/endpoints/users/following.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import Following from '../../models/following'; import serialize from '../../serializers/user'; @@ -20,15 +20,15 @@ module.exports = (params, me) => new Promise(async (res, rej) => { if (userIdErr) return rej('invalid user_id param'); // Get 'iknow' parameter - const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed(); + const [iknow = false, iknowErr] = it(params.iknow).expect.boolean(.get(); if (iknowErr) return rej('invalid iknow param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'cursor' parameter - const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed(); + const [cursor = null, cursorErr] = it(params.cursor).expect.id().get(); if (cursorErr) return rej('invalid cursor param'); // Lookup user diff --git a/src/api/endpoints/users/posts.ts b/src/api/endpoints/users/posts.ts index 2832c5c6c3..af7082457a 100644 --- a/src/api/endpoints/users/posts.ts +++ b/src/api/endpoints/users/posts.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import Post from '../../models/post'; import User from '../../models/user'; import serialize from '../../serializers/post'; @@ -27,23 +27,23 @@ module.exports = (params, me) => new Promise(async (res, rej) => { } // Get 'include_replies' parameter - const [includeReplies, includeRepliesErr] = it(params.include_replies).expect.boolean().default(true).qed(); + const [includeReplies = true, includeRepliesErr] = it(params.include_replies).expect.boolean().get(); if (includeRepliesErr) return rej('invalid include_replies param'); // Get 'with_media' parameter - const [withMedia, withMediaErr] = it(params.with_media).expect.boolean().default(false).qed(); + const [withMedia = false, withMediaErr] = it(params.with_media).expect.boolean().get(); if (withMediaErr) return rej('invalid with_media param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'since_id' parameter - const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get(); if (sinceIdErr) return rej('invalid since_id param'); // Get 'max_id' parameter - const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + const [maxId, maxIdErr] = it(params.max_id).expect.id().get(); if (maxIdErr) return rej('invalid max_id param'); // Check if both of since_id and max_id is specified diff --git a/src/api/endpoints/users/recommendation.ts b/src/api/endpoints/users/recommendation.ts index f502f41269..a0fb6613c6 100644 --- a/src/api/endpoints/users/recommendation.ts +++ b/src/api/endpoints/users/recommendation.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import serialize from '../../serializers/user'; import getFriends from '../../common/get-friends'; @@ -15,11 +15,11 @@ import getFriends from '../../common/get-friends'; */ module.exports = (params, me) => new Promise(async (res, rej) => { // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // ID list of the user itself and other users who the user follows diff --git a/src/api/endpoints/users/search.ts b/src/api/endpoints/users/search.ts index b21d8a2399..8be6a4dc8c 100644 --- a/src/api/endpoints/users/search.ts +++ b/src/api/endpoints/users/search.ts @@ -2,7 +2,7 @@ * Module dependencies */ import * as mongo from 'mongodb'; -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import serialize from '../../serializers/user'; import config from '../../../conf'; @@ -17,15 +17,15 @@ const escapeRegexp = require('escape-regexp'); */ module.exports = (params, me) => new Promise(async (res, rej) => { // Get 'query' parameter - const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').qed(); + const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').get(); if (queryError) return rej('invalid query param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'max' parameter - const [max, maxErr] = it(params.max).expect.number().range(1, 30).default(10).qed(); + const [max = 10, maxErr] = it(params.max).expect.number().range(1, 30).get(); if (maxErr) return rej('invalid max param'); // If Elasticsearch is available, search by it diff --git a/src/api/endpoints/users/search_by_username.ts b/src/api/endpoints/users/search_by_username.ts index 4071f341f2..b3193a4956 100644 --- a/src/api/endpoints/users/search_by_username.ts +++ b/src/api/endpoints/users/search_by_username.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import { validateUsername } from '../../models/user'; import serialize from '../../serializers/user'; @@ -15,15 +15,15 @@ import serialize from '../../serializers/user'; */ module.exports = (params, me) => new Promise(async (res, rej) => { // Get 'query' parameter - const [query, queryError] = it(params.query).expect.string().required().trim().validate(validateUsername).qed(); + const [query, queryError] = it(params.query).expect.string().required().trim().validate(validateUsername).get(); if (queryError) return rej('invalid query param'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get(); if (offsetErr) return rej('invalid offset param'); // Get 'limit' parameter - const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get(); if (limitErr) return rej('invalid limit param'); const users = await User diff --git a/src/api/endpoints/users/show.ts b/src/api/endpoints/users/show.ts index d35bfbae20..2a8d9c7724 100644 --- a/src/api/endpoints/users/show.ts +++ b/src/api/endpoints/users/show.ts @@ -1,7 +1,7 @@ /** * Module dependencies */ -import it from '../../it'; +import it from 'cafy'; import User from '../../models/user'; import serialize from '../../serializers/user'; diff --git a/src/api/it.ts b/src/api/it.ts deleted file mode 100644 index 57094ac091..0000000000 --- a/src/api/it.ts +++ /dev/null @@ -1,584 +0,0 @@ -/** - * it - * 楽しいバリデーション - */ - -/** - * Usage Examples - * - * const [val, err] = it(x).must.be.a.string().or('asc desc').default('desc').qed(); - * → xは文字列でなければならず、'asc'または'desc'でなければならない。省略された場合のデフォルトは'desc'とする。 - * - * const [val, err] = it(x).must.be.a.number().required().range(0, 100).qed(); - * → xは数値でなければならず、かつ0~100の範囲内でなければならない。この値は省略することはできない。 - * - * const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed(); - * → xは配列でなければならず、かつ中身が重複していてはならない。この値を省略することはできない。そして配列の最初の要素が'strawberry pasta'という文字列であってはならない。 - * - * ・意味的に矛盾するので、required と default は併用できません。 - * - * ~糖衣構文~ - * const [val, err] = it(x).must.be.a.string().required().qed(); - * は - * const [val, err] = it(x, 'string', true); - * と書けます - * - * ~BDD風記法~ - * must.be.a(n) の代わりに expect とも書けます: - * const [val, err] = it(x).expect.string().required().qed(); - */ - -/** - * null と undefined の扱い - * - * 「値が null または undefined」な状態を「値が空である」と表現しています。 - * 値が空である場合、バリデータやその他の処理メソッドは呼ばれません。 - * - * 内部的には null と undefined を次のように区別しています: - * null ... 値が「無い」と明示されている - * undefined ... 値を指定していない - * - * 例えばアカウントのプロフィールを更新するAPIに次のデータを含むリクエストが来たとします: - * { name: 'Alice' } - * アカウントには本来、他にも birthday といったフィールドがありますが、 - * このリクエストではそれに触れず、ただ単に name フィールドを更新することを要求しています。 - * ここで、このリクエストにおける birthday フィールドは undefined なわけですが、 - * それはnull(=birthdayを未設定にしたい)とは違うものです。 - * undefined も null も区別しないとしたら、触れていないフィールドまでリセットされることになってしまいます。 - * ですので、undefined と null は区別しています。 - * - * 明示的に null を許可しない限り、null はエラーになります。 - * null を許可する場合は nullable をプリフィックスします: - * const [val, err] = it(x).must.be.a.nullable.string().required().qed(); - */ - -import * as mongo from 'mongodb'; -import hasDuplicates from '../common/has-duplicates'; - -type Validator = (value: T) => boolean | Error; - -/** - * クエリベース - */ -abstract class Query { - protected value: any; - protected error: Error; - - constructor(value: any, nullable: boolean = false) { - if (value === null && !nullable) { - this.value = undefined; - this.error = new Error('must-be-not-a-null'); - } else { - this.value = value; - this.error = null; - } - } - - protected get isUndefined() { - return this.value === undefined; - } - - protected get isNull() { - return this.value === null; - } - - protected get isEmpty() { - return this.isUndefined || this.isNull; - } - - /** - * このインスタンスの値が空、またはエラーが存在しているなどして、処理をスキップするべきか否か - */ - protected get shouldSkip() { - return this.error !== null || this.isEmpty; - } - - /** - * このインスタンスの値が指定されていない(=undefined)ときにエラーにします - */ - required() { - if (this.error === null && this.isUndefined) { - this.error = new Error('required'); - } - return this; - } - - /** - * このインスタンスの値が設定されていない(=undefined)ときにデフォルトで設定する値を設定します - */ - default(value: any) { - if (this.isUndefined) { - this.value = value; - } - return this; - } - - /** - * このインスタンスの値およびエラーを取得します - * *qedはQ.E.D.でもあり'QueryEnD'の略でもある - */ - qed(): [any, Error] { - return [this.value, this.error]; - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - if (this.shouldSkip) return this; - const result = validator(this.value); - if (result === false) { - this.error = new Error('invalid-format'); - } else if (result instanceof Error) { - this.error = result; - } - return this; - } -} - -class BooleanQuery extends Query { - value: boolean; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && typeof value != 'boolean') { - this.error = new Error('must-be-a-boolean'); - } - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: boolean) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [boolean, Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } -} - -class NumberQuery extends Query { - value: number; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && !Number.isFinite(value)) { - this.error = new Error('must-be-a-number'); - } - } - - /** - * 値が指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.shouldSkip) return this; - if (this.value < min || this.value > max) { - this.error = new Error('invalid-range'); - } - return this; - } - - /** - * このインスタンスの値が指定された下限より下回っている場合エラーにします - * @param value 下限 - */ - min(value: number) { - if (this.shouldSkip) return this; - if (this.value < value) { - this.error = new Error('invalid-range'); - } - return this; - } - - /** - * このインスタンスの値が指定された上限より上回っている場合エラーにします - * @param value 上限 - */ - max(value: number) { - if (this.shouldSkip) return this; - if (this.value > value) { - this.error = new Error('invalid-range'); - } - return this; - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: number) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [number, Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } -} - -class StringQuery extends Query { - value: string; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && typeof value != 'string') { - this.error = new Error('must-be-a-string'); - } - } - - /** - * 文字数が指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.shouldSkip) return this; - if (this.value.length < min || this.value.length > max) { - this.error = new Error('invalid-range'); - } - return this; - } - - trim() { - if (this.shouldSkip) return this; - this.value = this.value.trim(); - return this; - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: string) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [string, Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - /** - * このインスタンスの文字列が、与えられたパターン内の文字列のどれかと一致するか検証します - * どれとも一致しない場合エラーにします - * @param pattern 文字列の配列またはスペースで区切られた文字列 - */ - or(pattern: string | string[]) { - if (this.shouldSkip) return this; - if (typeof pattern == 'string') pattern = pattern.split(' '); - const match = pattern.some(x => x === this.value); - if (!match) this.error = new Error('not-match-pattern'); - return this; - } - - /** - * このインスタンスの文字列が、与えられた正規表現と一致するか検証します - * 一致しない場合エラーにします - * @param pattern 正規表現 - */ - match(pattern: RegExp) { - if (this.shouldSkip) return this; - if (!pattern.test(this.value)) this.error = new Error('not-match-pattern'); - return this; - } -} - -class ArrayQuery extends Query { - value: any[]; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && !Array.isArray(value)) { - this.error = new Error('must-be-an-array'); - } - } - - /** - * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします - */ - unique() { - if (this.shouldSkip) return this; - if (hasDuplicates(this.value)) { - this.error = new Error('must-be-unique'); - } - return this; - } - - /** - * 配列の長さが指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.shouldSkip) return this; - if (this.value.length < min || this.value.length > max) { - this.error = new Error('invalid-range'); - } - return this; - } - - /** - * このインスタンスの配列内の要素すべてが文字列であるか検証します - * ひとつでも文字列以外の要素が存在する場合エラーにします - */ - allString() { - if (this.shouldSkip) return this; - if (this.value.some(x => typeof x != 'string')) { - this.error = new Error('dirty-array'); - } - return this; - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: any[]) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [any[], Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } -} - -class IdQuery extends Query { - value: mongo.ObjectID; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) { - this.error = new Error('must-be-an-id'); - } - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: mongo.ObjectID) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [mongo.ObjectID, Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } -} - -class ObjectQuery extends Query { - value: any; - error: Error; - - constructor(value: any, nullable: boolean = false) { - super(value, nullable); - if (!this.isEmpty && typeof value != 'object') { - this.error = new Error('must-be-an-object'); - } - } - - /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します - */ - default(value: any) { - return super.default(value); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - qed(): [any, Error] { - return super.qed(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false またはエラーを返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } -} - -type It = { - must: { - be: { - a: { - string: () => StringQuery; - number: () => NumberQuery; - boolean: () => BooleanQuery; - nullable: { - string: () => StringQuery; - number: () => NumberQuery; - boolean: () => BooleanQuery; - id: () => IdQuery; - array: () => ArrayQuery; - object: () => ObjectQuery; - }; - }; - an: { - id: () => IdQuery; - array: () => ArrayQuery; - object: () => ObjectQuery; - }; - }; - }; - expect: { - string: () => StringQuery; - number: () => NumberQuery; - boolean: () => BooleanQuery; - id: () => IdQuery; - array: () => ArrayQuery; - object: () => ObjectQuery; - nullable: { - string: () => StringQuery; - number: () => NumberQuery; - boolean: () => BooleanQuery; - id: () => IdQuery; - array: () => ArrayQuery; - object: () => ObjectQuery; - }; - }; -}; - -const it = (value: any) => ({ - must: { - be: { - a: { - string: () => new StringQuery(value), - number: () => new NumberQuery(value), - boolean: () => new BooleanQuery(value), - nullable: { - string: () => new StringQuery(value, true), - number: () => new NumberQuery(value, true), - boolean: () => new BooleanQuery(value, true), - id: () => new IdQuery(value, true), - array: () => new ArrayQuery(value, true), - object: () => new ObjectQuery(value, true) - } - }, - an: { - id: () => new IdQuery(value), - array: () => new ArrayQuery(value), - object: () => new ObjectQuery(value) - } - } - }, - expect: { - string: () => new StringQuery(value), - number: () => new NumberQuery(value), - boolean: () => new BooleanQuery(value), - id: () => new IdQuery(value), - array: () => new ArrayQuery(value), - object: () => new ObjectQuery(value), - nullable: { - string: () => new StringQuery(value, true), - number: () => new NumberQuery(value, true), - boolean: () => new BooleanQuery(value, true), - id: () => new IdQuery(value, true), - array: () => new ArrayQuery(value, true), - object: () => new ObjectQuery(value, true) - } - } -}); - -type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object'; - -function x(value: any): It; -function x(value: any, type: 'id', isRequired?: boolean, validator?: Validator | Validator[]): [mongo.ObjectID, Error]; -function x(value: any, type: 'string', isRequired?: boolean, validator?: Validator | Validator[]): [string, Error]; -function x(value: any, type: 'number', isRequired?: boolean, validator?: Validator | Validator[]): [number, Error]; -function x(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; -function x(value: any, type: 'array', isRequired?: boolean, validator?: Validator | Validator[]): [any[], Error]; -function x(value: any, type: 'set', isRequired?: boolean, validator?: Validator | Validator[]): [any[], Error]; -function x(value: any, type: 'object', isRequired?: boolean, validator?: Validator | Validator[]): [any, Error]; -function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator | Validator[]): any { - if (typeof type === 'undefined') return it(value); - - let q: Query = null; - - switch (type) { - case 'id': q = it(value).expect.id(); break; - case 'string': q = it(value).expect.string(); break; - case 'number': q = it(value).expect.number(); break; - case 'boolean': q = it(value).expect.boolean(); break; - case 'array': q = it(value).expect.array(); break; - case 'set': q = it(value).expect.array().unique(); break; - case 'object': q = it(value).expect.object(); break; - } - - if (isRequired) q = q.required(); - - if (validator) { - (Array.isArray(validator) ? validator : [validator]) - .forEach(v => q = q.validate(v)); - } - - return q; -} - -export default x; diff --git a/src/common/has-duplicates.ts b/src/common/has-duplicates.ts deleted file mode 100644 index dd5e6759f1..0000000000 --- a/src/common/has-duplicates.ts +++ /dev/null @@ -1 +0,0 @@ -export default (array: any[]) => (new Set(array)).size !== array.length; diff --git a/test/it.js b/test/it.js deleted file mode 100644 index ba3d30bde8..0000000000 --- a/test/it.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * it Tests! - */ - -const assert = require('assert'); - -const _it = require('../built/api/it').default; -/* -describe('it', () => { - it('it must be a string', () => { - const [val, err] = _it(42).must.be.a.string().qed(); - assert.eql - }); -}); -*/