|
| 1 | +const got = require("got"); |
| 2 | +const { CurrencyService } = require("./currency"); |
| 3 | +const { EmailService } = require("./email"); |
| 4 | +const { DomainService } = require("./domain"); |
| 5 | +const { IPService } = require("./ip"); |
| 6 | +const { WebpageService } = require("./webpage"); |
| 7 | + |
| 8 | +class Client { |
| 9 | + constructor(key) { |
| 10 | + this.key = key; |
| 11 | + } |
| 12 | + |
| 13 | + _request(method, url, query, body, json) { |
| 14 | + console.log(method, url); |
| 15 | + return got(url, { |
| 16 | + method, |
| 17 | + headers: { |
| 18 | + Authorization: `Bearer ${this.key}` |
| 19 | + }, |
| 20 | + query, |
| 21 | + body, |
| 22 | + json |
| 23 | + }) |
| 24 | + .then(response => { |
| 25 | + if (json) { |
| 26 | + return response.body; |
| 27 | + } |
| 28 | + return JSON.parse(response.body); |
| 29 | + }) |
| 30 | + .catch(error => { |
| 31 | + if (error instanceof got.HTTPError) { |
| 32 | + const body = JSON.parse(error.response.body); |
| 33 | + throw new LabStackError(body.code, body.message); |
| 34 | + } else { |
| 35 | + throw new LabStackError(0, error); |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + currency() { |
| 41 | + return new CurrencyService(this); |
| 42 | + } |
| 43 | + |
| 44 | + domain() { |
| 45 | + return new DomainService(this); |
| 46 | + } |
| 47 | + |
| 48 | + email() { |
| 49 | + return new EmailService(this); |
| 50 | + } |
| 51 | + |
| 52 | + ip() { |
| 53 | + return new IPService(this); |
| 54 | + } |
| 55 | + |
| 56 | + webpage() { |
| 57 | + return new WebpageService(this); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class LabStackError extends Error { |
| 62 | + constructor(code, message) { |
| 63 | + super(message); |
| 64 | + this.code = code; |
| 65 | + Error.captureStackTrace(this, LabStackError); |
| 66 | + this.name = this.constructor.name; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +module.exports = { |
| 71 | + Client, |
| 72 | + LabStackError |
| 73 | +}; |
0 commit comments