From 772063aade67b43545098751b8dc0dbfb7af4ba0 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Mon, 15 Apr 2019 12:20:48 +0900
Subject: [PATCH] Refactor

---
 src/client/app/dev/views/new-app.vue    |  8 ++----
 src/server/api/endpoints/permissions.ts | 29 -------------------
 src/server/api/kinds.ts                 | 38 +------------------------
 src/server/api/openapi/description.ts   | 37 +++++++++++++++++++++++-
 test/api.ts                             | 10 -------
 5 files changed, 39 insertions(+), 83 deletions(-)
 delete mode 100644 src/server/api/endpoints/permissions.ts

diff --git a/src/client/app/dev/views/new-app.vue b/src/client/app/dev/views/new-app.vue
index 6b67d220a7..dbb41211cc 100644
--- a/src/client/app/dev/views/new-app.vue
+++ b/src/client/app/dev/views/new-app.vue
@@ -30,6 +30,7 @@
 <script lang="ts">
 import Vue from 'vue';
 import i18n from '../../i18n';
+import { kinds } from '../../../../server/api/kinds';
 
 export default Vue.extend({
 	i18n: i18n('dev/views/new-app.vue'),
@@ -40,14 +41,9 @@ export default Vue.extend({
 			cb: '',
 			nidState: null,
 			permission: [],
-			permissionsList: []
+			permissionsList: kinds
 		};
 	},
-	created() {
-		this.$root.api('permissions').then(permissions => {
-			this.permissionsList = permissions
-		});
-	},
 	methods: {
 		onSubmit() {
 			this.$root.api('app/create', {
diff --git a/src/server/api/endpoints/permissions.ts b/src/server/api/endpoints/permissions.ts
deleted file mode 100644
index 347e1e3f2e..0000000000
--- a/src/server/api/endpoints/permissions.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import define from '../define';
-import { kindsList } from '../kinds';
-
-export const meta = {
-	stability: 'stable',
-
-	desc: {
-		'ja-JP': 'パーミッションの一覧を返します。',
-		'en-US': 'Get the list of permissons.'
-	},
-
-	tags: ['meta'],
-
-	requireCredential: false,
-
-	params: {
-	},
-
-	res: {
-		type: 'array',
-		items: {
-			type: 'string',
-		}
-	},
-};
-
-export default define(meta, async () => {
-	return kindsList;
-});
diff --git a/src/server/api/kinds.ts b/src/server/api/kinds.ts
index d496fa6919..03ed3e7cd8 100644
--- a/src/server/api/kinds.ts
+++ b/src/server/api/kinds.ts
@@ -1,8 +1,4 @@
-import endpoints from './endpoints';
-import * as locale from '../../../locales/';
-import { fromEntries } from '../../prelude/array';
-
-export const kindsList = [
+export const kinds = [
 	'read:account',
 	'write:account',
 	'read:blocks',
@@ -24,35 +20,3 @@ export const kindsList = [
 	'write:reactions',
 	'write:votes'
 ];
-
-export interface IKindInfo {
-	endpoints: string[];
-	descs: { [x: string]: string; };
-}
-
-export function kinds() {
-	const kinds = fromEntries(
-		kindsList
-			.map(k => [k, {
-					endpoints: [],
-					descs: fromEntries(
-						Object.keys(locale)
-							.map(l => [l, locale[l].common.permissions[k] as string] as [string, string])
-						) as { [x: string]: string; }
-				}] as [ string, IKindInfo ])
-			) as { [x: string]: IKindInfo; };
-
-	const errors = [] as string[][];
-
-	for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) {
-		if (endpoint.meta.kind) {
-			const kind = endpoint.meta.kind;
-			if (kind in kinds) kinds[kind].endpoints.push(endpoint.name);
-			else errors.push([kind, endpoint.name]);
-		}
-	}
-
-	if (errors.length > 0) throw Error('\n  ' + errors.map((e) => `Unknown kind (permission) "${e[0]}" found at ${e[1]}.`).join('\n  '));
-
-	return kinds;
-}
diff --git a/src/server/api/openapi/description.ts b/src/server/api/openapi/description.ts
index b801c86387..0a4c8699d1 100644
--- a/src/server/api/openapi/description.ts
+++ b/src/server/api/openapi/description.ts
@@ -1,5 +1,40 @@
 import config from '../../../config';
-import { IKindInfo, kinds } from '../kinds';
+import endpoints from '../endpoints';
+import * as locale from '../../../../locales/';
+import { fromEntries } from '../../../prelude/array';
+import { kinds as kindsList } from '../kinds';
+
+export interface IKindInfo {
+	endpoints: string[];
+	descs: { [x: string]: string; };
+}
+
+export function kinds() {
+	const kinds = fromEntries(
+		kindsList
+			.map(k => [k, {
+					endpoints: [],
+					descs: fromEntries(
+						Object.keys(locale)
+							.map(l => [l, locale[l].common.permissions[k] as string] as [string, string])
+						) as { [x: string]: string; }
+				}] as [ string, IKindInfo ])
+			) as { [x: string]: IKindInfo; };
+
+	const errors = [] as string[][];
+
+	for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) {
+		if (endpoint.meta.kind) {
+			const kind = endpoint.meta.kind;
+			if (kind in kinds) kinds[kind].endpoints.push(endpoint.name);
+			else errors.push([kind, endpoint.name]);
+		}
+	}
+
+	if (errors.length > 0) throw Error('\n  ' + errors.map((e) => `Unknown kind (permission) "${e[0]}" found at ${e[1]}.`).join('\n  '));
+
+	return kinds;
+}
 
 export function getDescription(lang = 'ja-JP'): string {
 	const permissionTable = (Object.entries(kinds()) as [string, IKindInfo][])
diff --git a/test/api.ts b/test/api.ts
index 318aa84248..570ab6833d 100644
--- a/test/api.ts
+++ b/test/api.ts
@@ -18,8 +18,6 @@ import * as assert from 'assert';
 import * as childProcess from 'child_process';
 import { async, signup, request, post, react, uploadFile } from './utils';
 
-import { kinds } from '../src/server/api/kinds';
-
 describe('API', () => {
 	let p: childProcess.ChildProcess;
 
@@ -967,13 +965,5 @@ describe('API', () => {
 			assert.strictEqual(res.body[0].id, alicePost.id);
 		}));
 	});
-
-	describe('kinds', () => {
-		it('登録されていないパーミッションを利用しているAPIがない', () => {
-			const res = kinds();
-
-			assert.strictEqual(typeof res === 'object', true);
-		});
-	});
 });
 */