@vitrical/utils
Advanced tools
Comparing version 1.1.5 to 1.1.6
28
api.d.ts
import superagent from 'superagent'; | ||
export declare class ApiError extends Error { | ||
url: string; | ||
status: superagent.Response['status']; | ||
body: { | ||
[key: string]: any; | ||
} | string; | ||
headers: superagent.Response['headers']; | ||
} | ||
export declare const handleSuperagentError: (url: string, newStack: string, err: unknown, res?: superagent.Response) => ApiError | Error | unknown; | ||
export type CustomRequestResponse = { | ||
status: superagent.Response['status']; | ||
body: superagent.Response['body']; | ||
headers: superagent.Response['headers']; | ||
} | ||
}; | ||
export type CustomRequestOptions = { | ||
method: 'get' | 'post' | 'put' | 'delete'; | ||
url: string; | ||
body?: { | ||
[key: string]: any; | ||
}; | ||
} | string; | ||
query?: string | { | ||
@@ -18,13 +29,4 @@ [key: string]: any; | ||
}; | ||
export type CustomRequestResponse = { | ||
status: superagent.Response['status']; | ||
body: superagent.Response['body']; | ||
headers: superagent.Response['headers']; | ||
}; | ||
export type CustomRequestError = CustomRequestResponse & { | ||
stack: string; | ||
message: string; | ||
}; | ||
export declare const requestImplementation: (agent: (m: 'get' | 'post' | 'put' | 'delete', url: string) => any) => (baseURL: string, key: string, method: 'get' | 'post' | 'put' | 'delete', url: string, options?: CustomRequestOptions) => Promise<CustomRequestResponse>; | ||
export declare const internalRequest: (agent: superagent.SuperAgentStatic) => ({ method, url, body, query, headers }: CustomRequestOptions) => Promise<CustomRequestResponse>; | ||
export declare const request: (baseURL: string, key: string, method: 'get' | 'post' | 'put' | 'delete', url: string, options?: CustomRequestOptions) => Promise<CustomRequestResponse>; | ||
export declare const isApiError: (err: unknown) => err is CustomRequestError; | ||
export declare const apiRequest: (options: CustomRequestOptions) => Promise<CustomRequestResponse>; |
141
api.js
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isApiError = exports.request = exports.requestImplementation = exports.ApiError = void 0; | ||
exports.apiRequest = exports.request = exports.internalRequest = exports.handleSuperagentError = exports.ApiError = void 0; | ||
const superagent_1 = __importDefault(require("superagent")); | ||
@@ -12,67 +12,96 @@ class ApiError extends Error { | ||
exports.ApiError = ApiError; | ||
const requestImplementation = (agent) => async (baseURL, key, method, url, options) => { | ||
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'); | ||
const handleSuperagentError = (url, newStack, err, res) => { | ||
if (!res) { | ||
if (typeof err === 'string') { | ||
const newError = new Error(err); | ||
newError.stack = newStack; | ||
newError.message = err; | ||
return newError; | ||
} | ||
if (options?.headers) { | ||
req.set(options.headers); | ||
else { | ||
if (typeof err === 'object' && !!err && 'message' in err && typeof err.message === 'string') { | ||
const newError = new Error(err.message); | ||
newError.stack = newStack; | ||
newError.message = err.message; | ||
return newError; | ||
} | ||
} | ||
if (options?.query) { | ||
req.query(options.query); | ||
return err; | ||
} | ||
if (res.status) { | ||
if (typeof err === 'object' && | ||
!!err && | ||
'request' in err && | ||
!!err.request && | ||
typeof err.request === 'object' && | ||
'url' in err.request && | ||
typeof err.request.url === 'string') { | ||
url = err.request.url; | ||
} | ||
if (options?.body) { | ||
req.send(options.body); | ||
const message = res.body?.msg | ||
? `Request failed with status code ${res.status} ( ${res.body.msg} )` | ||
: `Request failed with status code ${res.status}`; | ||
const apiError = new ApiError(message); | ||
apiError.body = res.body; | ||
apiError.headers = res.headers; | ||
apiError.status = res.status; | ||
apiError.stack = newStack; | ||
apiError.url = url; | ||
return apiError; | ||
} | ||
else { | ||
if (typeof err === 'object' && !!err && 'message' in err && typeof err.message === 'string') { | ||
const newError = new Error(err.message); | ||
newError.stack = newStack; | ||
return newError; | ||
} | ||
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 err; | ||
} | ||
}; | ||
exports.handleSuperagentError = handleSuperagentError; | ||
const internalRequest = (agent) => ({ method, url, body, query, headers }) => new Promise((resolve, reject) => { | ||
const newStack = new Error().stack; | ||
const req = agent(method, url); | ||
if (body) { | ||
if (typeof body === 'string') { | ||
req.send(body); | ||
} | ||
return returnData; | ||
else { | ||
req.set('Content-Type', 'application/json'); | ||
req.send(body); | ||
} | ||
} | ||
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; | ||
if (query) { | ||
req.query(query); | ||
} | ||
if (headers) { | ||
req.set(headers); | ||
} | ||
req.end((err, res) => { | ||
if (err) { | ||
reject((0, exports.handleSuperagentError)(url, newStack || '', err, res)); | ||
} | ||
else { | ||
throw err; | ||
resolve({ | ||
status: res.status, | ||
body: res.body, | ||
headers: res.headers, | ||
}); | ||
} | ||
} | ||
}; | ||
exports.requestImplementation = requestImplementation; | ||
const request = async (baseURL, key, method, url, options) => (0, exports.requestImplementation)(superagent_1.default)(baseURL, key, method, url, options); | ||
}); | ||
}); | ||
exports.internalRequest = internalRequest; | ||
const request = async (baseURL, key, method, url, options) => (0, exports.internalRequest)(superagent_1.default)({ | ||
method, | ||
url: `${baseURL}${url}`, | ||
body: options?.body, | ||
query: options?.query, | ||
headers: { | ||
...options?.headers, | ||
key, | ||
}, | ||
}); | ||
exports.request = request; | ||
const isApiError = (err) => { | ||
return err instanceof ApiError; | ||
}; | ||
exports.isApiError = isApiError; | ||
const apiRequest = async (options) => (0, exports.internalRequest)(superagent_1.default)(options); | ||
exports.apiRequest = apiRequest; | ||
//# sourceMappingURL=api.js.map |
@@ -13,3 +13,3 @@ "use strict"; | ||
res.status(err.status).send({ | ||
msg: err.message, | ||
msg: err.message + `\nat ${err.url}\n`, | ||
body: err.body, | ||
@@ -16,0 +16,0 @@ }); |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
44752
748