@pallasoft/ps-utilities 1.2.2 → 1.3.0

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.
@@ -5,16 +5,16 @@ export interface CreateApiClientOptions {
5
5
  }
6
6
  export interface PsHttpRequestConfig {
7
7
  headers?: Record<string, string>;
8
+ responseType?: 'arraybuffer' | 'json' | 'text';
9
+ }
10
+ export interface PsHttpResponse<T> {
11
+ data: T;
12
+ headers: Headers;
13
+ status: number;
8
14
  }
9
15
  export declare class HttpService {
10
16
  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
- }>;
17
+ get: <T>(url: string, config?: PsHttpRequestConfig) => Promise<PsHttpResponse<T>>;
18
+ post: <T>(url: string, data?: unknown, config?: PsHttpRequestConfig) => Promise<PsHttpResponse<T>>;
19
19
  };
20
20
  }
@@ -37,6 +37,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.HttpService = void 0;
40
+ var isBinaryBody = function (data) {
41
+ return data instanceof FormData ||
42
+ data instanceof Blob ||
43
+ data instanceof ArrayBuffer ||
44
+ data instanceof URLSearchParams;
45
+ };
40
46
  var HttpService = /** @class */ (function () {
41
47
  function HttpService() {
42
48
  }
@@ -45,42 +51,41 @@ var HttpService = /** @class */ (function () {
45
51
  if (options === void 0) { options = {}; }
46
52
  var apiKey = options.apiKey, _a = options.apiKeyHeader, apiKeyHeader = _a === void 0 ? 'X-API-Key' : _a, baseURL = options.baseURL;
47
53
  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) {
54
+ var fullUrl, sendsBinaryBody, headers, body, response, responseData, error;
55
+ var _a;
56
+ return __generator(this, function (_b) {
57
+ switch (_b.label) {
52
58
  case 0:
53
59
  fullUrl = baseURL ? "".concat(baseURL).concat(url) : url;
54
- headers = { 'Content-Type': 'application/json' };
60
+ sendsBinaryBody = isBinaryBody(data);
61
+ headers = sendsBinaryBody
62
+ ? {}
63
+ : { 'Content-Type': 'application/json' };
55
64
  if (apiKey)
56
65
  headers[apiKeyHeader] = apiKey;
57
66
  if (config === null || config === void 0 ? void 0 : config.headers)
58
67
  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
- })];
68
+ body = data === undefined ? undefined : sendsBinaryBody ? data : JSON.stringify(data);
69
+ return [4 /*yield*/, fetch(fullUrl, { method: method, headers: headers, body: body })];
64
70
  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()];
71
+ response = _b.sent();
72
+ return [4 /*yield*/, ((config === null || config === void 0 ? void 0 : config.responseType) === 'arraybuffer'
73
+ ? response.arrayBuffer()
74
+ : (config === null || config === void 0 ? void 0 : config.responseType) === 'text'
75
+ ? response.text()
76
+ : (config === null || config === void 0 ? void 0 : config.responseType) === 'json'
77
+ ? response.json()
78
+ : ((_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : '').includes('application/json')
79
+ ? response.json()
80
+ : response.text())];
69
81
  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;
82
+ responseData = _b.sent();
78
83
  if (!response.ok) {
79
84
  error = new Error("Request failed with status ".concat(response.status));
80
85
  error.response = { data: responseData, status: response.status };
81
86
  throw error;
82
87
  }
83
- return [2 /*return*/, { data: responseData, status: response.status }];
88
+ return [2 /*return*/, { data: responseData, headers: response.headers, status: response.status }];
84
89
  }
85
90
  });
86
91
  }); };
@@ -0,0 +1,11 @@
1
+ import { Readable } from 'stream';
2
+ import { Context } from 'hono';
3
+ interface UploadedFile {
4
+ filename: string;
5
+ type: string;
6
+ stream: Readable;
7
+ }
8
+ export declare class FileService {
9
+ static getUploadedFile(c: Context, fieldName: string): Promise<UploadedFile | null>;
10
+ }
11
+ export {};
@@ -0,0 +1,109 @@
1
+ "use strict";
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
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.FileService = void 0;
40
+ var stream_1 = require("stream");
41
+ var FileService = /** @class */ (function () {
42
+ function FileService() {
43
+ }
44
+ FileService.getUploadedFile = function (c, fieldName) {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var formData, file, webStream, nodeStream;
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0: return [4 /*yield*/, c.req.formData()];
50
+ case 1:
51
+ formData = _a.sent();
52
+ file = formData.get(fieldName);
53
+ if (!(file instanceof File))
54
+ return [2 /*return*/, null];
55
+ webStream = file.stream();
56
+ nodeStream = new stream_1.Readable({
57
+ read: function () {
58
+ return __awaiter(this, void 0, void 0, function () {
59
+ var reader, _a, done, value, error_1;
60
+ return __generator(this, function (_b) {
61
+ switch (_b.label) {
62
+ case 0:
63
+ reader = webStream.getReader();
64
+ _b.label = 1;
65
+ case 1:
66
+ _b.trys.push([1, 5, 6, 7]);
67
+ _b.label = 2;
68
+ case 2:
69
+ if (!true) return [3 /*break*/, 4];
70
+ return [4 /*yield*/, reader.read()];
71
+ case 3:
72
+ _a = _b.sent(), done = _a.done, value = _a.value;
73
+ if (done) {
74
+ this.push(null);
75
+ return [3 /*break*/, 4];
76
+ }
77
+ this.push(value);
78
+ return [3 /*break*/, 2];
79
+ case 4: return [3 /*break*/, 7];
80
+ case 5:
81
+ error_1 = _b.sent();
82
+ this.destroy(error_1);
83
+ return [3 /*break*/, 7];
84
+ case 6:
85
+ reader.releaseLock();
86
+ return [7 /*endfinally*/];
87
+ case 7: return [2 /*return*/];
88
+ }
89
+ });
90
+ });
91
+ },
92
+ });
93
+ Object.defineProperty(nodeStream, 'contentLength', {
94
+ value: file.size,
95
+ writable: false,
96
+ enumerable: true,
97
+ });
98
+ return [2 /*return*/, {
99
+ filename: file.name,
100
+ type: file.type,
101
+ stream: nodeStream,
102
+ }];
103
+ }
104
+ });
105
+ });
106
+ };
107
+ return FileService;
108
+ }());
109
+ exports.FileService = FileService;
@@ -0,0 +1 @@
1
+ export * from './file-service';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./file-service"), exports);
package/package.json CHANGED
@@ -1,10 +1,21 @@
1
1
  {
2
2
  "name": "@pallasoft/ps-utilities",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
- "export": "/dist/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "require": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./server": {
14
+ "types": "./dist/server/index.d.ts",
15
+ "require": "./dist/server/index.js",
16
+ "default": "./dist/server/index.js"
17
+ }
18
+ },
8
19
  "repository": {
9
20
  "type": "git",
10
21
  "url": "git+https://github.com/pallasoft/ps-utilities.git"
@@ -40,6 +51,7 @@
40
51
  "husky": "^9.1.7",
41
52
  "lint-staged": "^16.2.7",
42
53
  "prettier": "^3.6.2",
54
+ "hono": "^4.12.18",
43
55
  "ts-node": "^10.9.2",
44
56
  "typescript": "^5.9.3",
45
57
  "@semantic-release/changelog": "^6.0.3",
@@ -51,6 +63,9 @@
51
63
  "conventional-changelog-conventionalcommits": "^7.0.2",
52
64
  "semantic-release": "^24.2.0"
53
65
  },
66
+ "peerDependencies": {
67
+ "hono": "^4.0.0"
68
+ },
54
69
  "dependencies": {
55
70
  "@esbuild-plugins/tsconfig-paths": "^0.1.2",
56
71
  "dayjs": "^1.11.19",