@vonage/vetch
Advanced tools
Comparing version 1.0.10 to 1.0.11
@@ -1,6 +0,10 @@ | ||
import { VetchOptions } from './types'; | ||
import { Vetch } from './vetch'; | ||
export { VetchError, VetchPromise, VetchResponse, Headers, ResponseTypes, } from './types'; | ||
import { VetchOptions } from './interfaces/vetchOptions'; | ||
export { Vetch, VetchOptions }; | ||
export declare const instance: Vetch; | ||
export declare function request<T>(opts: VetchOptions): Promise<import("./types").VetchResponse<T>>; | ||
export declare function request<T>(opts: VetchOptions): Promise<import("./interfaces/vetchResponse").VetchResponse<T>>; | ||
export { VetchError } from './types/vetchError'; | ||
export { Headers } from './interfaces/headers'; | ||
export { VetchPromise } from './types/vetchPromise'; | ||
export { VetchResponse } from './interfaces/vetchResponse'; | ||
export { ResponseTypes } from './enums/responseTypes'; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.request = exports.instance = exports.Vetch = exports.ResponseTypes = exports.VetchPromise = exports.VetchError = void 0; | ||
exports.ResponseTypes = exports.VetchError = exports.request = exports.instance = exports.Vetch = void 0; | ||
const vetch_1 = require("./vetch"); | ||
Object.defineProperty(exports, "Vetch", { enumerable: true, get: function () { return vetch_1.Vetch; } }); | ||
var types_1 = require("./types"); | ||
Object.defineProperty(exports, "VetchError", { enumerable: true, get: function () { return types_1.VetchError; } }); | ||
Object.defineProperty(exports, "VetchPromise", { enumerable: true, get: function () { return types_1.VetchPromise; } }); | ||
Object.defineProperty(exports, "ResponseTypes", { enumerable: true, get: function () { return types_1.ResponseTypes; } }); | ||
exports.instance = new vetch_1.Vetch(); | ||
function request(opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return exports.instance.request(opts); | ||
}); | ||
async function request(opts) { | ||
return exports.instance.request(opts); | ||
} | ||
exports.request = request; | ||
var vetchError_1 = require("./types/vetchError"); | ||
Object.defineProperty(exports, "VetchError", { enumerable: true, get: function () { return vetchError_1.VetchError; } }); | ||
var responseTypes_1 = require("./enums/responseTypes"); | ||
Object.defineProperty(exports, "ResponseTypes", { enumerable: true, get: function () { return responseTypes_1.ResponseTypes; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,3 @@ | ||
import { VetchOptions, VetchResponse } from './types'; | ||
import { VetchResponse } from './interfaces/vetchResponse'; | ||
import { VetchOptions } from './interfaces/vetchOptions'; | ||
export declare class Vetch { | ||
@@ -3,0 +4,0 @@ defaults: VetchOptions; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -21,52 +12,46 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const https_1 = __importDefault(require("https")); | ||
const types_1 = require("./types"); | ||
const vetchError_1 = require("./types/vetchError"); | ||
const responseTypes_1 = require("./enums/responseTypes"); | ||
const debug_1 = __importDefault(require("debug")); | ||
const log = (0, debug_1.default)('vonage:vetch'); | ||
class Vetch { | ||
defaults; | ||
constructor(defaults) { | ||
this.defaults = defaults || { responseType: types_1.ResponseTypes.json }; | ||
this.defaults = defaults || { responseType: responseTypes_1.ResponseTypes.json }; | ||
if (!this.defaults.responseType) { | ||
this.defaults.responseType = types_1.ResponseTypes.json; | ||
this.defaults.responseType = responseTypes_1.ResponseTypes.json; | ||
} | ||
} | ||
_defaultAdapter(opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield (0, node_fetch_1.default)(opts.url, opts); | ||
const data = yield this.getResponseData(opts, res); | ||
return this.createResponse(opts, res, data); | ||
}); | ||
async _defaultAdapter(opts) { | ||
const res = await (0, node_fetch_1.default)(opts.url, opts); | ||
const data = await this.getResponseData(opts, res); | ||
return this.createResponse(opts, res, data); | ||
} | ||
request(opts = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
opts = this.validateOpts(opts); | ||
try { | ||
let formattedResponse; | ||
formattedResponse = yield this._defaultAdapter(opts); | ||
if (!opts.checkStatus(formattedResponse.status)) { | ||
const err = new types_1.VetchError(`Request failed with status code ${formattedResponse.status}`, opts); | ||
err.code = String(formattedResponse.status); | ||
err.response = formattedResponse; | ||
throw err; | ||
} | ||
return formattedResponse; | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
}); | ||
async request(opts = {}) { | ||
opts = this.validateOpts(opts); | ||
log('api request', opts); | ||
const formattedResponse = await this._defaultAdapter(opts); | ||
log('api response', formattedResponse); | ||
if (!opts.checkStatus(formattedResponse.status)) { | ||
const err = new vetchError_1.VetchError(`Request failed with status code ${formattedResponse.status}`, opts); | ||
err.code = String(formattedResponse.status); | ||
err.response = formattedResponse; | ||
throw err; | ||
} | ||
return formattedResponse; | ||
} | ||
getResponseData(opts, res) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
switch (opts.responseType) { | ||
case 'json': { | ||
let data = yield res.text(); | ||
try { | ||
data = JSON.parse(data); | ||
} | ||
catch (_a) { | ||
} | ||
return data; | ||
async getResponseData(opts, res) { | ||
switch (opts.responseType) { | ||
case 'json': { | ||
let data = await res.text(); | ||
try { | ||
data = JSON.parse(data); | ||
} | ||
default: | ||
return res.text(); | ||
catch { | ||
} | ||
return data; | ||
} | ||
}); | ||
default: | ||
return res.text(); | ||
} | ||
} | ||
@@ -77,3 +62,2 @@ validateOpts(options) { | ||
opts.checkStatus = this.checkStatus; | ||
opts.responseType = opts.responseType; | ||
if (!opts.url) { | ||
@@ -80,0 +64,0 @@ throw new Error('URL is required.'); |
{ | ||
"name": "@vonage/vetch", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "Vonage package for server side fetch.", | ||
@@ -26,2 +26,3 @@ "homepage": "https://github.com/vonage/vonage-node-sdk/tree/master/packages/vetch#readme", | ||
"dependencies": { | ||
"debug": "^4.3.4", | ||
"lodash.merge": "^4.6.2", | ||
@@ -28,0 +29,0 @@ "node-fetch": "^2.6" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19027
32
273
3
+ Addeddebug@^4.3.4
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)