@qrvey/fetch
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -44,22 +44,41 @@ 'use strict'; | ||
// src/helpers/errors.ts | ||
var RestBadHttpActionParams = class extends Error { | ||
constructor(url, message) { | ||
super(`Bad RestHttpAction params at ${url}: ${message}`); | ||
this.name = "RestBadHttpActionParams"; | ||
// src/helpers/errors/customError.ts | ||
var CustomError = class extends Error { | ||
constructor(message, errorDetails, stack, context = process.env.SERVICE_NAME || "QrveyCustomError") { | ||
super(message); | ||
this.errorDetails = errorDetails; | ||
this.context = context; | ||
this.name = "ERROR in " + this.context; | ||
this.stack = stack; | ||
} | ||
toJSON() { | ||
return { | ||
name: this.name, | ||
message: this.message, | ||
stack: this.stack, | ||
details: this.errorDetails | ||
}; | ||
} | ||
}; | ||
var RestClientError = class extends Error { | ||
// src/helpers/errors/restClientError.ts | ||
var RestClientError = class extends CustomError { | ||
constructor(url, restError) { | ||
super(`REST Client Error at ${url}`); | ||
this.name = "RestClientError"; | ||
this.details = restError; | ||
const message = `REST Client Error at ${url}`; | ||
super(message, restError); | ||
} | ||
}; | ||
// src/helpers/errors/restBadHttpActionParams.ts | ||
var RestBadHttpActionParams = class extends CustomError { | ||
constructor(url, message) { | ||
const errorMessage = `Bad RestHttpAction params at ${url}: ${message}`; | ||
super(errorMessage, message); | ||
} | ||
}; | ||
// src/services/fetchClient.service.ts | ||
var FetchClientService = class { | ||
static validateEndpoint(endpoint) { | ||
if (endpoint.startsWith("/")) | ||
return; | ||
if (endpoint.startsWith("/")) return; | ||
const errorMessage = `Invalid endpoint "${endpoint}". Please replace with "/${endpoint}"`; | ||
@@ -69,5 +88,7 @@ throw new RestBadHttpActionParams(endpoint, errorMessage); | ||
static buildUrl(endpoint, options) { | ||
var _a; | ||
var _a, _b; | ||
const baseDomain = (_a = process.env.DOMAIN) != null ? _a : ""; | ||
return `${options.baseDomain || baseDomain}${endpoint}`; | ||
const privateDomain = (_b = process.env.PRIVATE_DOMAIN) != null ? _b : ""; | ||
const defaultDomain = options.privateDomain ? privateDomain : baseDomain; | ||
return `${options.baseDomain || defaultDomain}${endpoint}`; | ||
} | ||
@@ -105,4 +126,3 @@ static baseHeaders(options) { | ||
}; | ||
if (body) | ||
requestOptions["body"] = JSON.stringify(body); | ||
if (body) requestOptions["body"] = JSON.stringify(body); | ||
return requestOptions; | ||
@@ -147,4 +167,3 @@ } | ||
const responseData = await (isJsonResponse ? response.json() : response.text()); | ||
if (response.ok) | ||
return responseData; | ||
if (response.ok) return responseData; | ||
throw new RestClientError(response.url, responseData); | ||
@@ -154,4 +173,3 @@ } | ||
return fetch(url, requestOptions).then(this.handleResponse).catch((error) => { | ||
if (error instanceof RestClientError) | ||
throw error; | ||
if (error instanceof RestClientError) throw error; | ||
throw new RestClientError(url, error); | ||
@@ -166,4 +184,3 @@ }); | ||
return queryValue.map((val) => `${queryName}=${val}`).join("&"); | ||
else | ||
return `${queryName}=${queryValue}`; | ||
else return `${queryName}=${queryValue}`; | ||
} | ||
@@ -223,3 +240,3 @@ ); | ||
exports.FetchService = FetchService; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
@@ -10,2 +10,3 @@ interface IRequestHeaders { | ||
useApiKey?: boolean; | ||
privateDomain?: boolean; | ||
} | ||
@@ -18,2 +19,3 @@ | ||
baseDomain?: string; | ||
privateDomain?: boolean; | ||
queryParameters?: Record<string, string | string[]>; | ||
@@ -20,0 +22,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"main": "dist/cjs/index.js", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "publishConfig": { |
@@ -26,67 +26,34 @@ # @qrvey/fetch | ||
## Example Usage | ||
### CommonJS | ||
### IHttpActionOptions | ||
- `headers` (object): An optional object containing custom headers to include in the request. It allows you to set specific HTTP headers like Authorization, Content-Type, etc. | ||
- `useApiKey` (boolean): If set to true, the request will include an API key in the headers. The API key is retrieved from the **process.env.API_KEY** environment variable. | ||
```js | ||
const { FetchService } = require('@qrvey/fetch'); | ||
- `baseDomain` (string): An optional base domain to override the default domain used in the request URL. If not provided, the value from the **process.env.DOMAIN** environment variable will be used as base domain. | ||
class MyFetchClient extends FetchService { | ||
/** | ||
* Performs an HTTP GET request. | ||
* | ||
* @param {string} endpoint - The endpoint of the request. | ||
* @param {object} options - Options for the request. | ||
* @returns {Promise<any>} The response of the request. | ||
*/ | ||
static findData(endpoint, options) { | ||
return this.get(endpoint, options); | ||
} | ||
- `privateDomain` (boolean): If set to true, the request will use the private domain specified in the **process.env.PRIVATE_DOMAIN** environment variable instead of the base domain. | ||
/** | ||
* Performs an HTTP POST request. | ||
* | ||
* @param {string} endpoint - The endpoint of the request. | ||
* @param {object} data - The data to send in the request. | ||
* @param {object} options - Options for the request. | ||
* @returns {Promise<any>} The response of the request. | ||
*/ | ||
static sendData(endpoint, data, options) { | ||
return this.post(endpoint, data, options); | ||
} | ||
} | ||
module.exports = MyFetchClient; | ||
``` | ||
### ECMAScript | ||
```ts | ||
import { FetchService } from '@qrvey/fetch'; | ||
- `queryParameters` (Record<string, string | string[]>): An optional object representing query parameters to include in the request URL. It allows you to pass multiple query parameters as key-value pairs, with support for arrays to represent multiple values for a single key. | ||
class MyFetchClient extends FetchService { | ||
/** | ||
* Performs an HTTP GET request. | ||
* | ||
* @param {string} endpoint - The endpoint of the request. | ||
* @param {object} options - Options for the request. | ||
* @returns {Promise<any>} The response of the request. | ||
*/ | ||
static findData(endpoint, options) { | ||
return this.get(endpoint, options); | ||
} | ||
## Example Usage | ||
/** | ||
* Performs an HTTP POST request. | ||
* | ||
* @param {string} endpoint - The endpoint of the request. | ||
* @param {object} data - The data to send in the request. | ||
* @param {object} options - Options for the request. | ||
* @returns {Promise<any>} The response of the request. | ||
*/ | ||
static sendData(endpoint, data, options) { | ||
return this.post(endpoint, data, options); | ||
```js | ||
const options: IHttpActionOptions = { | ||
headers: { | ||
'Authorization': 'Bearer BEARER_TOKEN_VALUE', | ||
}, | ||
useApiKey: true, | ||
baseDomain: 'https://api.example.com', | ||
privateDomain: false, | ||
queryParameters: { | ||
filter: 'active', | ||
page: '1' | ||
} | ||
} | ||
}; | ||
export default MyFetchClient; | ||
FetchService.get('/endpoint', options) | ||
.then(response => console.log(response)) | ||
.catch(error => console.error('Error:', error)); | ||
``` |
Sorry, the diff of this file is not supported yet
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
54575
478
59