server.ts 9.21 KB
import { createServer, Model, Response } from "miragejs";
import { ModelDefinition } from "miragejs/-types";

interface Item {
  id: string;
  is_folder: boolean;
  name: string;
  mime_type: string | null;
  path: string | null;
  parent: string | null;
  user_id: string;
  size: number;
  is_deleted: boolean;
  created_timestamp: string;
  updated_timestamp: string;
  status: boolean;
}

interface SharedItem {
  id: string;
  file_id: string;
  until: string;
  password: string;
}

interface User {
  id: string;
  user_id: string;
  name: string;
  password: string;
  total_size: number;
  current_size: number;
}

const ItemModel: ModelDefinition<Item> = Model;
const SharedItemModel: ModelDefinition<SharedItem> = Model;
const UserModel: ModelDefinition<User> = Model;

createServer({
  models: {
    item: ItemModel,
    shared_item: SharedItemModel,
    user: UserModel,
  },

  factories: {},

  routes() {
    this.get("/items/:item_id/children", (schema, request) => {
      const directory = schema.find("item", request.params.item_id);
      if (!directory || !directory.is_folder) {
        return new Response(
          404,
          {
            "Content-Type": "application/json",
          },
          JSON.stringify({
            status: 404,
            message: "Not Found",
          })
        );
      }

      const list = schema.where("item", { parent: directory.id }).models;
      return {
        status: 200,
        data: {
          ...directory.attrs,
          count: list.length,
          list,
        },
      };
    });

    this.post("/items/:item_id/children", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "2341",
          is_folder: true,
          name: "cloudComputing",
          created_timestamp: "2020-05-14T06:20:44Z",
          updated_timestamp: "2020-05-14T06:20:44Z",
          size: 0,
        },
      };
    });

    this.post("/items/:item_id/upload", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "2342",
          name: "image.jpg",
          is_folder: false,
          size: 92929,
          path:
            "https://presignedurldemo.s3.eu-west-2.amazonaws.com/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJJWZ7B6WCRGMKFGQ%2F20180210%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20180210T171315Z&X-Amz-Expires=1800&X-Amz-Signature=12b74b0788aa036bc7c3d03b3f20c61f1f91cc9ad8873e3314255dc479a25351&X-Amz-SignedHeaders=host",
        },
      };
    });

    this.post("/items/:item_id/status", (schema, request) => {
      return {
        status: 200,
        message: "ok",
      };
    });

    this.post("/items/:item_id/move", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "2342",
          name: "image.jpg",
          is_folder: false,
          parentinfo: {
            id: "1234",
            name: "folder",
            is_folder: true,
            updated_timestamp: "2020-05-14T06:20:44Z",
          },
        },
      };
    });

    this.post("/items/:item_id/copy", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "2342",
          name: "image.jpg",
          is_folder: false,
          parentinfo: {
            id: "1234",
            name: "folder",
            is_folder: true,
            updated_timestamp: "2020-05-14T06:20:44Z",
          },
        },
      };
    });

    this.get("/items/:item_id", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "2342",
          name: "image.jpg",
          is_folder: false,
          size: 92929,
          path:
            "https://presignedurldemo.s3.eu-west-2.amazonaws.com/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJJWZ7B6WCRGMKFGQ%2F20180210%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20180210T171315Z&X-Amz-Expires=1800&X-Amz-Signature=12b74b0788aa036bc7c3d03b3f20c61f1f91cc9ad8873e3314255dc479a25351&X-Amz-SignedHeaders=host",
          created_timestamp: "2020-05-14T06:20:44Z",
          updated_timestamp: "2020-05-14T06:20:44Z",
        },
      };
    });

    this.delete("/items/:item_id", (schema, request) => {
      return {
        status: 200,
        message: "ok",
      };
    });

    this.post("/items/:item_id/share", (schema, request) => {
      return {
        status: 200,
        user_id: 1,
        shared: "2020-05-14T06:20:44Z",
        data: {
          id: "2342",
          name: "image.jpg",
          is_folder: false,
          size: 92929,
          path:
            "https://presignedurldemo.s3.eu-west-2.amazonaws.com/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJJWZ7B6WCRGMKFGQ%2F20180210%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20180210T171315Z&X-Amz-Expires=1800&X-Amz-Signature=12b74b0788aa036bc7c3d03b3f20c61f1f91cc9ad8873e3314255dc479a25351&X-Amz-SignedHeaders=host",
          created_timestamp: "2020-05-14T06:20:44Z",
          updated_timestamp: "2020-05-14T06:20:44Z",
        },
      };
    });

    this.post("/shared/:shared_item_id", (schema, request) => {
      return {
        status: 200,
        data: {
          name: "image.jpg",
          size: 92929,
          path:
            "https://presignedurldemo.s3.eu-west-2.amazonaws.com/image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJJWZ7B6WCRGMKFGQ%2F20180210%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20180210T171315Z&X-Amz-Expires=1800&X-Amz-Signature=12b74b0788aa036bc7c3d03b3f20c61f1f91cc9ad8873e3314255dc479a25351&X-Amz-SignedHeaders=host",
        },
      };
    });

    this.post("/items/search", (schema, request) => {
      return {
        status: 200,
        data: {
          list: [
            {
              id: "2341",
              is_folder: true,
              name: "cloudComputing",
              created_timestamp: "2020-05-14T06:20:44Z",
              updated_timestamp: "2020-05-14T06:20:44Z",
              size: 0,
            },
            {
              id: "2341",
              is_folder: true,
              name: "cloudComputing",
              created_timestamp: "2020-05-14T06:20:44Z",
              updated_timestamp: "2020-05-14T06:20:44Z",
              size: 0,
            },
          ],
        },
      };
    });

    this.post("/users/login", (schema, request) => {
      const body = JSON.parse(request.requestBody);
      const admin = schema.findBy("user", {
        user_id: body.user_id,
        password: body.password,
      });
      if (!admin) {
        return new Response(
          401,
          {
            "Content-Type": "application/json",
          },
          JSON.stringify({
            status: 401,
            message: "unauthorized",
          })
        );
      }
      return {
        status: 200,
        data: {
          access_token:
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
          refresh_token:
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
          expiration: "2020-05-14T07:23:24Z",
        },
      };
    });

    this.post("/users/renew", (schema, request) => {
      return {
        status: 200,
        data: {
          access_token:
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
          refresh_token:
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
          expiration: "2020-05-14T07:23:24Z",
        },
      };
    });

    this.post("/users/signup", (schema, request) => {
      return {
        status: 200,
        message: "completed",
        data: {
          user_id: "test",
          name: "test",
          total_size: 1099511627776,
          current_size: 0,
        },
      };
    });

    this.post("/users/:user_id", (schema, request) => {
      return {
        status: 200,
        data: {
          id: "123",
          user_id: "test",
          name: "test",
          total_size: 1099511627776,
          current_size: 0,
        },
      };
    });
  },

  seeds(server) {
    const user = server.create("user", {
      id: "1",
      user_id: "admin",
      name: "테스트",
      password: "password",
      total_size: 1024 * 1024 * 1024 * 5, // 5GB
      current_size: 1024 * 1024 * 1024 * 1, // 1GB
    });

    const rootDir = server.create("item", {
      id: "1",
      is_folder: true,
      name: "root",
      mime_type: null,
      path: null,
      parent: null,
      user_id: user.id,
      size: 0,
      is_deleted: false,
      created_timestamp: "2020-05-14T06:20:44Z",
      updated_timestamp: "2020-05-14T06:20:44Z",
      status: true,
    });

    server.create("item", {
      id: "2",
      is_folder: false,
      name: "image.jpg",
      mime_type: "image/jpeg",
      path: "",
      parent: rootDir.id,
      user_id: user.id,
      size: 1024 * 1024 * 5,
      is_deleted: false,
      created_timestamp: "2020-05-14T06:20:44Z",
      updated_timestamp: "2020-05-14T06:20:44Z",
      status: true,
    });
  },
});