@vitrical/utils
Advanced tools
Comparing version 1.1.4 to 1.1.5
91
api.js
@@ -8,3 +8,2 @@ "use strict"; | ||
const superagent_1 = __importDefault(require("superagent")); | ||
const validation_1 = require("./validation"); | ||
class ApiError extends Error { | ||
@@ -14,30 +13,58 @@ } | ||
const requestImplementation = (agent) => async (baseURL, key, method, url, options) => { | ||
const req = agent(method, baseURL + url); | ||
req.set('Key', key); | ||
if (options?.body) { | ||
req.set('Content-Type', 'application/json'); | ||
const newStack = new Error().stack; | ||
try { | ||
const req = agent(method, baseURL + url); | ||
req.set('Key', key); | ||
if (options?.body) { | ||
req.set('Content-Type', 'application/json'); | ||
} | ||
if (options?.headers) { | ||
req.set(options.headers); | ||
} | ||
if (options?.query) { | ||
req.query(options.query); | ||
} | ||
if (options?.body) { | ||
req.send(options.body); | ||
} | ||
const res = await req; | ||
const returnData = { | ||
status: res.status, | ||
body: res.body, | ||
headers: res.headers, | ||
}; | ||
if (res.status !== 200) { | ||
const error = new ApiError(res.body?.msg || `Request failed with status code ${res.status}`); | ||
error.status = returnData.status; | ||
error.body = returnData.body; | ||
error.headers = returnData.headers; | ||
throw error; | ||
} | ||
return returnData; | ||
} | ||
if (options?.headers) { | ||
req.set(options.headers); | ||
catch (err) { | ||
if (typeof err === 'object' && | ||
err && | ||
'status' in err && | ||
typeof err.status === 'number' && | ||
'body' in err && | ||
typeof err.body === 'object' && | ||
'message' in err && | ||
typeof err.message === 'string' && | ||
'headers' in err && | ||
typeof err.headers === 'object') { | ||
const message = !!err.body && 'msg' in err.body && typeof err.body.msg === 'string' | ||
? err.body?.msg | ||
: err.message || `Request failed with status code ${err.status}`; | ||
const error = new ApiError(message); | ||
error.status = err.status; | ||
error.body = err.body; | ||
error.headers = err.headers; | ||
error.stack = newStack; | ||
throw error; | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
if (options?.query) { | ||
req.query(options.query); | ||
} | ||
if (options?.body) { | ||
req.send(options.body); | ||
} | ||
const res = await req; | ||
const returnData = { | ||
status: res.status, | ||
body: res.body, | ||
headers: res.headers, | ||
}; | ||
if (res.status !== 200) { | ||
const error = new ApiError(res.body?.msg || `Request failed with status code ${res.status}`); | ||
error.status = returnData.status; | ||
error.body = returnData.body; | ||
error.headers = returnData.headers; | ||
throw error; | ||
} | ||
return returnData; | ||
}; | ||
@@ -48,13 +75,5 @@ exports.requestImplementation = requestImplementation; | ||
const isApiError = (err) => { | ||
if (typeof err !== 'object' || !err) | ||
return false; | ||
if (!(0, validation_1.objectHasProperty)(err, 'message') || typeof err.message !== 'string') | ||
return false; | ||
if (!(0, validation_1.objectHasProperty)(err, 'body') || typeof err.body !== 'object') | ||
return false; | ||
if (!(0, validation_1.objectHasProperty)(err, 'status') || typeof err.status !== 'number') | ||
return false; | ||
return true; | ||
return err instanceof ApiError; | ||
}; | ||
exports.isApiError = isApiError; | ||
//# sourceMappingURL=api.js.map |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Collection of useful functions and typings", | ||
@@ -8,3 +8,5 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json" | ||
"build": "tsc --project tsconfig.json", | ||
"test": "jest", | ||
"test-init": "ts-jest config:init" | ||
}, | ||
@@ -26,7 +28,11 @@ "repository": { | ||
"jsonwebtoken": "^9.0.0", | ||
"superagent": "^8.0.9" | ||
"superagent": "^8.0.9", | ||
"typescript": "^4.9.4" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.5.0", | ||
"jest": "^29.5.0", | ||
"ts-jest": "^29.1.0", | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
@@ -21,3 +21,2 @@ { | ||
"esModuleInterop": true, | ||
"types": ["node"], | ||
"forceConsistentCasingInFileNames": true, | ||
@@ -24,0 +23,0 @@ "noImplicitOverride": true, |
@@ -18,3 +18,3 @@ export declare const objectHasProperty: (obj: unknown, property: string | number) => obj is { | ||
type MinLength = number; | ||
type SchemaInput = [ValidationLength, MinLength, MaxLength] | ValidationRegular | ValidationLength | ValidationArray | [ValidationArray, MinLength, MaxLength]; | ||
type SchemaInput = [ValidationLength, MinLength, MaxLength] | ValidationRegular | ValidationLength | ValidationArray | [ValidationArray, MinLength, MaxLength] | 'any'; | ||
export type ValidateObjectOptions = { | ||
@@ -21,0 +21,0 @@ [key: string]: SchemaInput | ValidateObjectOptions; |
@@ -143,2 +143,5 @@ "use strict"; | ||
const validationType = typeof validationSchema === 'string' ? validationSchema : validationSchema[0]; | ||
if (validationType === 'any') { | ||
return null; | ||
} | ||
if (['number?', 'string?', 'array?', 'bigint?', 'boolean?', 'object?'].includes(validationType) && | ||
@@ -145,0 +148,0 @@ value === undefined) { |
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
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
43045
25
717
6
4
+ Addedtypescript@^4.9.4
+ Addedtypescript@4.9.5(transitive)