• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paypay / paypayopa-sdk-node / 3889352965

pending completion
3889352965

push

github

GitHub
Merge pull request #654 from paypay/dependabot/npm_and_yarn/json5-2.2.3

41 of 45 branches covered (91.11%)

Branch coverage included in aggregate %.

177 of 188 relevant lines covered (94.15%)

20.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.78
/src/lib/httpsClient.ts
1
import * as https from "https";
26✔
2

3
export interface HttpsClientSuccess {
4
  STATUS: number;
5
  BODY: object | null;
6
}
7

8
export interface HttpsClientError {
9
  STATUS: number;
10
  ERROR: string;
11
}
12

13
export interface HttpsClientMessage {
14
  (message: HttpsClientSuccess | HttpsClientError): void;
15
}
16

17
export class HttpsClient {
26✔
18
  constructor() { }
19

20
  printDebugMessage(status: number, body: string, apiName: string | undefined) {
21
    try {
1✔
22
      const parseBody = JSON.parse(body);
1✔
23
      const code = parseBody.resultInfo.code;
1✔
24
      const codeId = parseBody.resultInfo.codeId;
1✔
25
      const RESOLVE_URL = `https://developer.paypay.ne.jp/develop/resolve?api_name=${apiName}&code=${code}&code_id=${codeId}`;
1✔
26
      console.log(`This link should help you to troubleshoot the error: ${RESOLVE_URL}`);
1✔
27
    } catch (e) {
28
      console.log(`The response to ${apiName} with status ${status} had an unexpected form`);
×
29
    }
30
  }
31

32
  httpsCall(
33
    options: https.RequestOptions & { apiKey?: string },
34
    payload: any,
35
    callback: HttpsClientMessage,
36
  ) {
37
    if (payload === undefined) {
3!
38
      payload = "";
×
39
    }
40

41
    let body = "";
3✔
42
    let status: number;
43
    const apiName = options.apiKey;
3✔
44
    delete options.apiKey; // Delete key to avoid any potential errors
3✔
45
    const req = https.request(options, (res) => {
3✔
46
      status = res?.statusCode!;
3!
47
      res.setEncoding("utf8");
3✔
48
      res.on("data", (chunk) => {
3✔
49
        body += Buffer.from(chunk);
6✔
50
      });
51
      res.on("end", () => {
3✔
52
        if (status < 200 || status > 299) {
3✔
53
          this.printDebugMessage(status, body, apiName);
1✔
54
        }
55
        let parsed;
56
        try {
3✔
57
          parsed = body.match(/\S/) ? JSON.parse(body) : null;
3✔
58
        } catch (e: any) {
59
          callback({ STATUS: 500, ERROR: e.message });
×
60
          return;
×
61
        }
62

63
        // Make the `BODY.toString()` return the raw JSON.
64
        // This makes the library compatible with calls like `JSON.parse(response.BODY)`,
65
        // which were required prior to version 2.
66
        const responseObject = parsed && Object.assign(Object.create({ toString() { return body; } }), parsed);
3✔
67

68
        callback({ STATUS: status, BODY: responseObject });
3✔
69
      });
70
    });
71

72
    req.on("error", (e) => {
3✔
73
      callback({ STATUS: status, ERROR: e.message });
×
74
    });
75

76
    if (options.method === "POST") {
3✔
77
      req.write(JSON.stringify(payload));
3✔
78
    }
79
    req.end();
3✔
80
  }
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc