Skip to content
Snippets Groups Projects
swagger.js 5.6 KiB
Newer Older
'use strict'

Tosuke's avatar
Tosuke committed
const swaggerJSDoc = require('swagger-jsdoc');
const fs = require('fs');
const yaml = require('js-yaml');
Tosuke's avatar
Tosuke committed

const apiRoot = './src/api/endpoints';
const files = [
Tosuke's avatar
Tosuke committed
  'meta.js',
Tosuke's avatar
Tosuke committed
  //app
  'app/show.js',
Tosuke's avatar
Tosuke committed
  'app/create.js',
  'app/name_id/available.js',
Tosuke's avatar
Tosuke committed
  //auth
  'auth/accept.js',
  //auth/session
  'auth/session/generate.js',
Tosuke's avatar
Tosuke committed
  'auth/session/show.js',
  'auth/session/userkey.js',
Tosuke's avatar
Tosuke committed
const defaultSwagger = {
  "swagger": "2.0",
  "info": {
    "title": "Misskey API",
syuilo's avatar
syuilo committed
    "version": "nighthike"
Tosuke's avatar
Tosuke committed
  },
Tosuke's avatar
Tosuke committed
  "host": "api.misskey.xyz",
Tosuke's avatar
Tosuke committed
  "schemes": [
Tosuke's avatar
Tosuke committed
    "https"
Tosuke's avatar
Tosuke committed
  ],
  "consumes": [
    "application/x-www-form-urlencoded"
  ],
  "produces": [
    "application/json"
  ],
Tosuke's avatar
Tosuke committed
  "parameters": {
Tosuke's avatar
Tosuke committed
    "AccessToken": {
      "name": "i",
Tosuke's avatar
Tosuke committed
      "description": "Access Token",
      "in": "formData",
      "required": true,
      "type": "string"
    },
Tosuke's avatar
Tosuke committed

    "NativeToken": {
      "name": "i",
Tosuke's avatar
Tosuke committed
      "description": "Native Access Token",
      "in": "formData",
Tosuke's avatar
Tosuke committed
      "required": true,
      "type": "string",
      "pattern": "^\!.+"
Tosuke's avatar
Tosuke committed
  "definitions": {
    "Error": {
      "type": "object",
      "properties": {
        "error": {
          "type": "string",
          "description": "Error message"
        }
      }
    },
    "User": {
      "type": "object",
      "required": [
        "created_at",
        "followers_count",
        "following_count",
        "id",
        "liked_count",
        "likes_count",
        "name",
        "posts_count",
        "username"
      ],
      "properties": {
        "avatar_id": {
          "type": "string",
          "description": "アバターに設定しているドライブのファイルのID"
        },
syuilo's avatar
syuilo committed
        "avatarUrl": {
Tosuke's avatar
Tosuke committed
          "type": "string",
          "description": "アバターURL"
        },
        "banner_id": {
          "type": "string",
          "description": "バナーに設定しているドライブのファイルのID"
        },
syuilo's avatar
syuilo committed
        "bannerUrl": {
Tosuke's avatar
Tosuke committed
          "type": "string",
          "description": "バナーURL"
        },
        "bio": {
          "type": "string",
          "description": "プロフィール"
        },
        "birthday": {
          "type": "string",
Tosuke's avatar
Tosuke committed
          "format": "date",
Tosuke's avatar
Tosuke committed
          "description": "誕生日"
        },
        "created_at": {
          "type": "string",
Tosuke's avatar
Tosuke committed
          "format": "date-time",
Tosuke's avatar
Tosuke committed
          "description": "アカウント作成日時"
        },
        "drive_capacity": {
          "type": "integer",
          "description": "ドライブの最大容量"
        },
        "followers_count": {
          "type": "integer",
          "description": "フォロワー数"
        },
        "following_count": {
          "type": "integer",
          "description": "フォロー数"
        },
        "id": {
          "type": "string",
          "description": "ユーザーID"
        },
        "is_followed": {
          "type": "boolean",
          "description": "フォローされているか"
        },
        "is_following": {
          "type": "boolean",
          "description": "フォローしているか"
        },
        "liked_count": {
          "type": "integer",
          "description": "投稿にいいねされた数"
        },
        "likes_count": {
          "type": "integer",
          "description": "投稿にいいねした数"
        },
        "location": {
          "type": "string",
          "description": "場所"
        },
        "name": {
          "type": "string",
          "description": "ニックネーム"
        },
        "posts_count": {
          "type": "integer",
          "description": "投稿数"
        },
        "username": {
          "type": "string",
          "description": "ユーザー名"
        }
      }
    },
    "Application": {
      "type": "object",
      "properties": {
        "created_at": {
          "type": "string",
Tosuke's avatar
Tosuke committed
          "format": "date-time",
Tosuke's avatar
Tosuke committed
          "description": "アプリケーションの作成日時"
        },
        "user_id": {
          "type": "string",
          "description": "アプリケーションを作成したユーザーのID"
        },
        "name": {
          "type": "string",
          "description": "アプリケーションの名前"
        },
Tosuke's avatar
Tosuke committed
        "name_id": {
          "type": "string",
          "description": "アプリケーションのユニークな名前"
        },
Tosuke's avatar
Tosuke committed
        "description": {
          "type": "string",
          "description": "アプリケーションの説明"
        },
        "permission": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "アプリケーションの持つ権限一覧"
        },
        "callback_url": {
          "type": "string",
          "description": "コールバックURL"
        },
        "id": {
          "type": "string",
          "description": "アプリケーションID"
        },
        "icon_url": {
          "type": "string",
          "description": "アプリケーションのアイコンのURL"
        }
      }
Tosuke's avatar
Tosuke committed
    }
Tosuke's avatar
Tosuke committed
  },
  "securityDefinitions": {},
  "tags": []
};
Tosuke's avatar
Tosuke committed

var options = {
Tosuke's avatar
Tosuke committed
  swaggerDefinition: defaultSwagger,
Tosuke's avatar
Tosuke committed
  apis: []
};
options.apis = files.map(c => {return `${apiRoot}/${c}`;});

if(fs.existsSync('.config/config.yml')){
  var config = yaml.safeLoad(fs.readFileSync('./.config/config.yml', 'utf8'));
Tosuke's avatar
Tosuke committed
  options.swaggerDefinition.host = `api.${config.url.match(/\:\/\/(.+)$/)[1]}`;
syuilo's avatar
syuilo committed
  options.swaggerDefinition.schemes = config.https.enable ?
                                      ['https'] :
Tosuke's avatar
Tosuke committed
                                      ['http'];
Tosuke's avatar
Tosuke committed
var swaggerSpec = swaggerJSDoc(options);

fs.writeFileSync('api-docs.json', JSON.stringify(swaggerSpec));