@pallasoft/ps-utilities 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,20 @@
1
- import { AxiosInstance } from 'axios';
2
1
  export interface CreateApiClientOptions {
3
2
  apiKey?: string;
4
3
  apiKeyHeader?: string;
5
4
  baseURL?: string;
6
5
  }
6
+ export interface PsHttpRequestConfig {
7
+ headers?: Record<string, string>;
8
+ }
7
9
  export declare class HttpService {
8
- static createApiClient(options?: CreateApiClientOptions): AxiosInstance;
10
+ static createApiClient(options?: CreateApiClientOptions): {
11
+ get: <T>(url: string, config?: PsHttpRequestConfig) => Promise<{
12
+ data: T;
13
+ status: number;
14
+ }>;
15
+ post: <T>(url: string, data?: unknown, config?: PsHttpRequestConfig) => Promise<{
16
+ data: T;
17
+ status: number;
18
+ }>;
19
+ };
9
20
  }
@@ -1,26 +1,97 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
4
37
  };
5
38
  Object.defineProperty(exports, "__esModule", { value: true });
6
39
  exports.HttpService = void 0;
7
- var axios_1 = __importDefault(require("axios"));
8
40
  var HttpService = /** @class */ (function () {
9
41
  function HttpService() {
10
42
  }
11
43
  HttpService.createApiClient = function (options) {
44
+ var _this = this;
12
45
  if (options === void 0) { options = {}; }
13
46
  var apiKey = options.apiKey, _a = options.apiKeyHeader, apiKeyHeader = _a === void 0 ? 'X-API-Key' : _a, baseURL = options.baseURL;
14
- var client = axios_1.default.create(baseURL ? { baseURL: baseURL } : undefined);
15
- client.interceptors.request.use(function (config) {
16
- if (apiKey)
17
- config.headers[apiKeyHeader] = apiKey;
18
- config.headers['Content-Type'] = 'application/json';
19
- return config;
20
- }, function (error) {
21
- return Promise.reject(error);
22
- });
23
- return client;
47
+ var request = function (method, url, data, config) { return __awaiter(_this, void 0, void 0, function () {
48
+ var fullUrl, headers, response, contentType, responseData, _a, error;
49
+ var _b;
50
+ return __generator(this, function (_c) {
51
+ switch (_c.label) {
52
+ case 0:
53
+ fullUrl = baseURL ? "".concat(baseURL).concat(url) : url;
54
+ headers = { 'Content-Type': 'application/json' };
55
+ if (apiKey)
56
+ headers[apiKeyHeader] = apiKey;
57
+ if (config === null || config === void 0 ? void 0 : config.headers)
58
+ Object.assign(headers, config.headers);
59
+ return [4 /*yield*/, fetch(fullUrl, {
60
+ method: method,
61
+ headers: headers,
62
+ body: data !== undefined ? JSON.stringify(data) : undefined,
63
+ })];
64
+ case 1:
65
+ response = _c.sent();
66
+ contentType = (_b = response.headers.get('content-type')) !== null && _b !== void 0 ? _b : '';
67
+ if (!contentType.includes('application/json')) return [3 /*break*/, 3];
68
+ return [4 /*yield*/, response.json()];
69
+ case 2:
70
+ _a = _c.sent();
71
+ return [3 /*break*/, 5];
72
+ case 3: return [4 /*yield*/, response.text()];
73
+ case 4:
74
+ _a = _c.sent();
75
+ _c.label = 5;
76
+ case 5:
77
+ responseData = _a;
78
+ if (!response.ok) {
79
+ error = new Error("Request failed with status ".concat(response.status));
80
+ error.response = { data: responseData, status: response.status };
81
+ throw error;
82
+ }
83
+ return [2 /*return*/, { data: responseData, status: response.status }];
84
+ }
85
+ });
86
+ }); };
87
+ return {
88
+ get: function (url, config) {
89
+ return request('GET', url, undefined, config);
90
+ },
91
+ post: function (url, data, config) {
92
+ return request('POST', url, data, config);
93
+ },
94
+ };
24
95
  };
25
96
  return HttpService;
26
97
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pallasoft/ps-utilities",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -53,7 +53,6 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "@esbuild-plugins/tsconfig-paths": "^0.1.2",
56
- "axios": "^1.16.0",
57
56
  "dayjs": "^1.11.19",
58
57
  "esbuild": "^0.27.0"
59
58
  },