Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#226、あとTypeScriptにした #232

Merged
merged 43 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import Post from '../../../models/post';
import Like from '../../../models/like';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const postId = params.post_id;
if (postId === undefined || postId === null) {
return rej('post_id is required');
}
const [postId, postIdErr] = it(params.post_id).expect.id().required().qed();
if (postIdErr) return rej('invalid post_id param');

// Lookup post
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
_id: postId
});

if (post === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import Post from '../../../models/post';
import Like from '../../../models/like';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const postId = params.post_id;
if (postId === undefined || postId === null) {
return rej('post_id is required');
}
const [postId, postIdErr] = it(params.post_id).expect.id().required().qed();
if (postIdErr) return rej('invalid post_id param');

// Lookup post
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
_id: postId
});

if (post === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import Post from '../../../models/post';

/**
Expand All @@ -16,14 +16,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const postId = params.post_id;
if (postId === undefined || postId === null) {
return rej('post_id is required');
}
const [postId, postIdErr] = it(params.post_id).expect.id().required().qed();
if (postIdErr) return rej('invalid post_id param');

// Lookup post
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
_id: postId
});

if (post === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import Post from '../../../models/post';

/**
Expand All @@ -16,14 +16,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const postId = params.post_id;
if (postId === undefined || postId === null) {
return rej('post_id is required');
}
const [postId, postIdErr] = it(params.post_id).expect.id().required().qed();
if (postIdErr) return rej('invalid post_id param');

// Lookup post
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
_id: postId
});

if (post === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import User from '../../../models/user';
import Following from '../../../models/following';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'user_id' parameter
const userId = params.user_id;
if (userId === undefined || userId === null) {
return rej('user_id is required');
}
const [userId, userIdErr] = it(params.user_id).expect.id().required().qed();
if (userIdErr) return rej('invalid user_id param');

// Lookup user
const user = await User.findOne({
_id: new mongo.ObjectID(userId)
_id: userId
}, {
fields: {
_id: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import User from '../../../models/user';
import Following from '../../../models/following';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'user_id' parameter
const userId = params.user_id;
if (userId === undefined || userId === null) {
return rej('user_id is required');
}
const [userId, userIdErr] = it(params.user_id).expect.id().required().qed();
if (userIdErr) return rej('invalid user_id param');

// Lookup user
const user = await User.findOne({
_id: new mongo.ObjectID(userId)
_id: userId
}, {
fields: {
_id: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import User from '../../../models/user';
import Like from '../../../models/like';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'user_id' parameter
const userId = params.user_id;
if (userId === undefined || userId === null) {
return rej('user_id is required');
}
const [userId, userIdErr] = it(params.user_id).expect.id().required().qed();
if (userIdErr) return rej('invalid user_id param');

// Lookup user
const user = await User.findOne({
_id: new mongo.ObjectID(userId)
_id: userId
}, {
fields: {
_id: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../../it';
import User from '../../../models/user';
import Post from '../../../models/post';

Expand All @@ -17,14 +17,12 @@ module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'user_id' parameter
const userId = params.user_id;
if (userId === undefined || userId === null) {
return rej('user_id is required');
}
const [userId, userIdErr] = it(params.user_id).expect.id().required().qed();
if (userIdErr) return rej('invalid user_id param');

// Lookup user
const user = await User.findOne({
_id: new mongo.ObjectID(userId)
_id: userId
}, {
fields: {
_id: true
Expand Down
44 changes: 15 additions & 29 deletions src/api/endpoints/app/create.js → src/api/endpoints/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Module dependencies
*/
import rndstr from 'rndstr';
import it from '../../it';
import App from '../../models/app';
import { isValidNameId } from '../../models/app';
import serialize from '../../serializers/app';

/**
Expand Down Expand Up @@ -71,41 +73,25 @@ module.exports = async (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'name_id' parameter
const nameId = params.name_id;
if (nameId == null) {
return rej('name_id is required');
} else if (typeof nameId != 'string') {
return rej('name_id must be a string');
}

// Validate name_id
if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) {
return rej('invalid name_id');
}
const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed();
if (nameIdErr) return rej('invalid name_id param');

// Get 'name' parameter
const name = params.name;
if (name == null || name == '') {
return rej('name is required');
}
const [name, nameErr] = it(params.name).expect.string().required().qed();
if (nameErr) return rej('invalid name param');

// Get 'description' parameter
const description = params.description;
if (description == null || description == '') {
return rej('description is required');
}
const [description, descriptionErr] = it(params.description).expect.string().required().qed();
if (descriptionErr) return rej('invalid description param');

// Get 'permission' parameter
const permission = params.permission;
if (permission == null || permission == '') {
return rej('permission is required');
}
const [permission, permissionErr] = it(params.permission).expect.array().unique().allString().required().qed();
if (permissionErr) return rej('invalid permission param');

// Get 'callback_url' parameter
let callback = params.callback_url;
if (callback === '') {
callback = null;
}
// TODO: Check it is valid url
const [callbackUrl, callbackUrlErr] = it(params.callback_url).expect.nullable.string().default(null).qed();
if (callbackUrlErr) return rej('invalid callback_url param');

// Generate secret
const secret = rndstr('a-zA-Z0-9', 32);
Expand All @@ -118,8 +104,8 @@ module.exports = async (params, user) =>
name_id: nameId,
name_id_lower: nameId.toLowerCase(),
description: description,
permission: permission.split(','),
callback_url: callback,
permission: permission,
callback_url: callbackUrl,
secret: secret
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/**
* Module dependencies
*/
import it from '../../../it';
import App from '../../../models/app';
import { isValidNameId } from '../../../models/app';

/**
* @swagger
Expand Down Expand Up @@ -44,15 +46,8 @@ module.exports = async (params) =>
new Promise(async (res, rej) =>
{
// Get 'name_id' parameter
const nameId = params.name_id;
if (nameId == null || nameId == '') {
return rej('name_id is required');
}

// Validate name_id
if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) {
return rej('invalid name_id');
}
const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed();
if (nameIdErr) return rej('invalid name_id param');

// Get exist
const exist = await App
Expand Down
16 changes: 6 additions & 10 deletions src/api/endpoints/app/show.js → src/api/endpoints/app/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../it';
import App from '../../models/app';
import serialize from '../../serializers/app';

Expand Down Expand Up @@ -50,24 +50,20 @@ module.exports = (params, user, _, isSecure) =>
new Promise(async (res, rej) =>
{
// Get 'app_id' parameter
let appId = params.app_id;
if (appId == null || appId == '') {
appId = null;
}
const [appId, appIdErr] = it(params.app_id, 'id');
if (appIdErr) return rej('invalid app_id param');

// Get 'name_id' parameter
let nameId = params.name_id;
if (nameId == null || nameId == '') {
nameId = null;
}
const [nameId, nameIdErr] = it(params.name_id, 'string');
if (nameIdErr) return rej('invalid name_id param');

if (appId === null && nameId === null) {
return rej('app_id or name_id is required');
}

// Lookup app
const app = appId !== null
? await App.findOne({ _id: new mongo.ObjectID(appId) })
? await App.findOne({ _id: appId })
: await App.findOne({ name_id_lower: nameId.toLowerCase() });

if (app === null) {
Expand Down
Loading