@youri-kane/heroku-client
Advanced tools
Comparing version 0.1.8 to 0.1.9
import { HerokuClient, HerokuClientParams } from './types'; | ||
export declare const createClient: ({ baseURL, metricsURL, token, cache, }: HerokuClientParams) => HerokuClient; | ||
export declare const createClient: ({ baseURL, metricsURL, token, cache, debug, }: HerokuClientParams) => HerokuClient; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,3 +31,3 @@ exports.createClient = void 0; | ||
const getRateLimit_1 = require("./requests/miscellaneous/getRateLimit"); | ||
const createClient = ({ baseURL = constants_1.defaultBaseURL, metricsURL = constants_1.defaultMetricsURL, token, cache, }) => { | ||
const createClient = ({ baseURL = constants_1.defaultBaseURL, metricsURL = constants_1.defaultMetricsURL, token, cache, debug, }) => { | ||
if (!baseURL) { | ||
@@ -19,2 +42,8 @@ throw new errors_1.HerokuClientError('Missing Heroku baseURL'); | ||
} | ||
let logger; | ||
if (debug) { | ||
Promise.resolve().then(() => __importStar(require('./logger'))).then(({ logger: definedLogger }) => { | ||
logger = definedLogger; | ||
}); | ||
} | ||
const state = { | ||
@@ -24,4 +53,7 @@ rateLimit: 0, | ||
const handleResponse = (request, response) => { | ||
const remaining = Number(response.headers['RateLimit-Remaining'] || 0); | ||
const remaining = Number(response.headers['ratelimit-remaining'] || 0); | ||
if (!Number.isNaN(remaining)) { | ||
if (logger) { | ||
logger.debug(`Rate Limit Remaining: ${remaining}`); | ||
} | ||
state.rateLimit = remaining; | ||
@@ -48,2 +80,3 @@ } | ||
metricsURL, | ||
getLogger: () => logger, | ||
})({}); | ||
@@ -62,2 +95,3 @@ if (result.hasFailed) { | ||
requests: (0, requests_1.buildRequests)({ | ||
getLogger: () => logger, | ||
baseURL, | ||
@@ -64,0 +98,0 @@ token, |
@@ -28,3 +28,3 @@ "use strict"; | ||
function createRequest(config) { | ||
return async ({ useCache = true, timeout, ...req }) => { | ||
return async ({ useCache = false, timeout, ...req }) => { | ||
const method = config?.method ?? 'GET'; | ||
@@ -40,5 +40,5 @@ const query = req?.query || config.defaultQuery | ||
}; | ||
if (useCache && config.onRequest) { | ||
if (config.onRequest) { | ||
const res = config.onRequest(computedParams); | ||
if (res) { | ||
if (res && useCache) { | ||
return handleResponse({ ...req, config }, res); | ||
@@ -61,3 +61,3 @@ } | ||
}); | ||
if (useCache && config.onResponse) { | ||
if (config.onResponse) { | ||
config.onResponse(computedParams, response); | ||
@@ -64,0 +64,0 @@ } |
@@ -6,3 +6,3 @@ import * as apps from './apps'; | ||
import { RequestConfig } from './types'; | ||
export declare const buildRequests: ({ baseURL, metricsURL, token, onResponse, onRequest, }: RequestConfig) => { | ||
export declare const buildRequests: ({ baseURL, metricsURL, token, getLogger, onResponse, onRequest, }: RequestConfig) => { | ||
getDynoLoad: (params: import("./types").RequestParams & { | ||
@@ -9,0 +9,0 @@ params: { |
@@ -41,15 +41,28 @@ "use strict"; | ||
}; | ||
const buildRequests = ({ baseURL, metricsURL, token, onResponse, onRequest, }) => { | ||
const buildRequests = ({ baseURL, metricsURL, token, getLogger, onResponse, onRequest, }) => { | ||
return Object.keys(requests).reduce((builtRequests, requestId) => { | ||
const reqId = requestId; | ||
const request = requests[reqId]; | ||
const fn = request({ | ||
getLogger, | ||
baseURL, | ||
metricsURL, | ||
token, | ||
onResponse, | ||
onRequest, | ||
}); | ||
return { | ||
...builtRequests, | ||
[reqId]: request({ | ||
baseURL, | ||
metricsURL, | ||
token, | ||
onResponse, | ||
onRequest, | ||
}), | ||
[reqId]: async (...args) => { | ||
const start = Date.now(); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const res = await fn(...args); | ||
const end = Date.now(); | ||
const logger = getLogger(); | ||
if (logger) { | ||
logger.debug(`${reqId} took ${end - start}ms`); | ||
} | ||
return res; | ||
}, | ||
}; | ||
@@ -56,0 +69,0 @@ }, {}); |
import { AxiosResponse } from 'axios'; | ||
import { TypedEmitter } from 'tiny-typed-emitter'; | ||
import { Logger } from 'winston'; | ||
export declare type HTTPVerb = | ||
@@ -77,2 +78,3 @@ /** | ||
onResponse?: (request: FullRequestParams, response: AxiosResponse) => void; | ||
getLogger: () => Logger | undefined; | ||
}; | ||
@@ -79,0 +81,0 @@ export declare type PollRequest<R extends PollRequestParams = PollRequestParams, T extends PollAbleResponse = PollAbleResponse> = (config: { |
@@ -8,2 +8,3 @@ import { ResponseCacheResult } from './cache'; | ||
cache?: ResponseCacheResult; | ||
debug?: boolean; | ||
}; | ||
@@ -10,0 +11,0 @@ export declare type HerokuClient = { |
{ | ||
"name": "@youri-kane/heroku-client", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "heroku developer api client with typescript support", | ||
@@ -54,4 +54,5 @@ "main": "dist/index.js", | ||
"query-string": "^7.0.1", | ||
"tiny-typed-emitter": "^2.1.0" | ||
"tiny-typed-emitter": "^2.1.0", | ||
"winston": "^3.6.0" | ||
} | ||
} |
101577
122
1820
4
+ Addedwinston@^3.6.0
+ Added@colors/colors@1.6.0(transitive)
+ Added@dabh/diagnostics@2.0.3(transitive)
+ Added@types/triple-beam@1.3.5(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedcolor@3.2.1(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcolor-string@1.9.1(transitive)
+ Addedcolorspace@1.1.4(transitive)
+ Addedenabled@2.0.0(transitive)
+ Addedfecha@4.2.3(transitive)
+ Addedfn.name@1.1.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arrayish@0.3.2(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedkuler@2.0.0(transitive)
+ Addedlogform@2.7.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedone-time@1.0.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafe-stable-stringify@2.5.0(transitive)
+ Addedsimple-swizzle@0.2.2(transitive)
+ Addedstack-trace@0.0.10(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtext-hex@1.0.0(transitive)
+ Addedtriple-beam@1.4.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwinston@3.17.0(transitive)
+ Addedwinston-transport@4.9.0(transitive)