This commit is contained in:
syuilo 2017-03-05 12:00:39 +09:00
parent f210260826
commit 17a23c3eb5
67 changed files with 202 additions and 801 deletions

View file

@ -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",

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from '../../it';
import it from 'cafy';
import App from '../../models/app';
import serialize from '../../serializers/app';

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) {

View file

@ -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

View file

@ -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';

View file

@ -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';

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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 = {

View file

@ -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<any>}
*/
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

View file

@ -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

View file

@ -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

View file

@ -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) => ({

View file

@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from '../../../it';
import it from 'cafy';
import Favorite from '../../../models/favorite';
import Post from '../../../models/post';

View file

@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from '../../../it';
import it from 'cafy';
import Favorite from '../../../models/favorite';
import Post from '../../../models/post';

View file

@ -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

View file

@ -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';

View file

@ -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';

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from '../../it';
import it from 'cafy';
import Post from '../../models/post';
import serialize from '../../serializers/post';

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from '../../it';
import it from 'cafy';
import User from '../../models/user';
import serialize from '../../serializers/user';

View file

@ -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<T> = (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<any>) {
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<boolean>) {
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<number>) {
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<string>) {
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<any[]>) {
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<mongo.ObjectID>) {
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<any>) {
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<mongo.ObjectID> | Validator<mongo.ObjectID>[]): [mongo.ObjectID, Error];
function x(value: any, type: 'string', isRequired?: boolean, validator?: Validator<string> | Validator<string>[]): [string, Error];
function x(value: any, type: 'number', isRequired?: boolean, validator?: Validator<number> | Validator<number>[]): [number, Error];
function x(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error];
function x(value: any, type: 'array', isRequired?: boolean, validator?: Validator<any[]> | Validator<any[]>[]): [any[], Error];
function x(value: any, type: 'set', isRequired?: boolean, validator?: Validator<any[]> | Validator<any[]>[]): [any[], Error];
function x(value: any, type: 'object', isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): [any, Error];
function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): 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;

View file

@ -1 +0,0 @@
export default (array: any[]) => (new Set(array)).size !== array.length;

View file

@ -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
});
});
*/