@plunk/node
Advanced tools
Comparing version 1.1.1 to 1.1.3
@@ -5,3 +5,3 @@ import { PublishParams } from "../types/events"; | ||
private readonly key; | ||
private readonly client; | ||
private fetch; | ||
constructor(key: string); | ||
@@ -8,0 +8,0 @@ /** |
@@ -5,4 +5,5 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const axios_1 = tslib_1.__importDefault(require("axios")); | ||
const request_1 = require("../utils/request"); | ||
const native_fetch_1 = require("native-fetch"); | ||
const NotFound_1 = require("../errors/NotFound"); | ||
const TokenError_1 = require("../errors/TokenError"); | ||
class Plunk { | ||
@@ -21,6 +22,6 @@ constructor(key) { | ||
track: (event) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return yield (0, request_1.request)(this.client, { | ||
return yield this.fetch({ | ||
method: "POST", | ||
url: "/track", | ||
body: Object.assign({}, event), | ||
url: "track", | ||
json: Object.assign({}, event), | ||
}); | ||
@@ -41,6 +42,6 @@ }), | ||
send: (body) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return yield (0, request_1.request)(this.client, { | ||
return yield this.fetch({ | ||
method: "POST", | ||
url: "/send", | ||
body: Object.assign({}, body), | ||
url: "send", | ||
json: Object.assign({}, body), | ||
}); | ||
@@ -50,7 +51,20 @@ }), | ||
this.key = key; | ||
this.client = axios_1.default.create({ | ||
baseURL: "https://api.useplunk.com/v1", | ||
headers: { | ||
Authorization: `Bearer ${this.key}`, | ||
}, | ||
} | ||
fetch(_a) { | ||
var _b; | ||
var { json, url } = _a, options = tslib_1.__rest(_a, ["json", "url"]); | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const res = yield (0, native_fetch_1.fetch)(new URL(url, "https://api.useplunk.com/v1/").toString(), Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({ Authorization: `Bearer ${this.key}` }, (json && { "Content-Type": "application/json" })), options.headers), body: json ? JSON.stringify(json) : undefined })); | ||
const text = yield res.text(); | ||
const data = safeJsonParse(text); | ||
if ((res === null || res === void 0 ? void 0 : res.status) === 401) { | ||
throw new TokenError_1.TokenError(data === null || data === void 0 ? void 0 : data.message); | ||
} | ||
if ((res === null || res === void 0 ? void 0 : res.status) === 404) { | ||
throw new NotFound_1.NotFoundError(data === null || data === void 0 ? void 0 : data.message); | ||
} | ||
if (!res.ok) { | ||
throw new Error((_b = data === null || data === void 0 ? void 0 : data.message) !== null && _b !== void 0 ? _b : "Unknown API Error"); | ||
} | ||
return data; | ||
}); | ||
@@ -60,1 +74,9 @@ } | ||
exports.Plunk = Plunk; | ||
function safeJsonParse(text) { | ||
try { | ||
return JSON.parse(text); | ||
} | ||
catch (e) { | ||
return text; | ||
} | ||
} |
export interface SendParams { | ||
to: string; | ||
to: string | string[]; | ||
subject: string; | ||
@@ -4,0 +4,0 @@ body: string; |
{ | ||
"name": "@plunk/node", | ||
"version": "1.1.1", | ||
"version": "1.1.3", | ||
"description": "Official Node.js library for useplunk.com", | ||
@@ -22,4 +22,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"axios": "^0.27.2", | ||
"tslib": "^2.4.0" | ||
"native-fetch": "^4.0.2", | ||
"tslib": "^2.4.0", | ||
"undici": "^5.22.1" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
9352
3
16
182
+ Addednative-fetch@^4.0.2
+ Addedundici@^5.22.1
+ Added@fastify/busboy@2.1.1(transitive)
+ Addednative-fetch@4.0.2(transitive)
+ Addedundici@5.28.5(transitive)
- Removedaxios@^0.27.2
- Removedasynckit@0.4.0(transitive)
- Removedaxios@0.27.2(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedform-data@4.0.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)