File size: 2,154 Bytes
f535bab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { sendSuccess, ErrorResponses } from "../../lib/response-helper.js";
import { Komiku } from "./komiku.js"

/** @type {import("../../types/plugin.ts").ApiPluginHandler}*/
const handler = {
  name: "Komiku: Read chapter",
  method: "GET",
  version: "1.0.0",
  category: ["komiku"],
  alias: ["readChapter"],
  tags: ["comic"],
  parameters: {
    query: [
      {
        name: "slug",
        type: "string",
        required: true,
        description: "Chapter slug of comic",
        example: "otonari-no-tenshi-sama-ni-itsunomanika-dame-ningen-ni-sareteita-ken-chapter-00"
      }
    ],
    body: [],
    headers: []
  },
  responses: {
    200: {
      status: 200,
      description: "Successfully retrieved data",
      example: {
        status: 200,
        author: "Ditzzy",
        note: "Thank you for using this API!",
        results: {}
      }
    },
    400: {
      status: 400,
      description: "Invalid Slug provided",
      example: {
        status: 400,
        message: "Invalid slug - must be a valid of Comic slug"
      }
    },
    404: {
      status: 404,
      description: "Missing required parameter",
      example: {
        status: 404,
        message: "Missing required parameter: ..."
      }
    },
    500: {
      status: 500,
      description: "Server error or unavailable",
      example: {
        status: 500,
        message: "An error occurred, please try again later."
      }
    }
  },
  exec: async (req, res) => {
    const { slug } = req.query;    
    if (!slug) return ErrorResponses.missingParameter(res, "slug");
    
    const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*-chapter-\d+(?:-\d+)?$/;
    
    if (!regex.test(slug)) return ErrorResponses.invalidUrl(res, "Invalid slug chapter - must be a valid slug chapter of Comic");
    
    const komik = new Komiku();
    try {      
      const detail = await komik.readChapter(slug);
      
      if (detail.results === null) return ErrorResponses.notFound(res);
      
      sendSuccess(res, detail.results)
    } catch (e) {
      ErrorResponses.serverError(res, "An error occurred, try again later.");
    }
  }
}

export default handler;