hono-utils 0.2.0 → 0.3.1

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Hono Utils - Utilities for Hono framework
4
4
 
5
- A collection of helpers to simplify building Hono-based applications, including crypto helpers, middleware utilities, and queue integration.
5
+ A collection of helpers to simplify building Hono-based applications, including crypto helpers, middleware utilities, queue integration and RPC client.
6
6
 
7
7
  ## Installation
8
8
 
@@ -37,6 +37,12 @@ import { QueueHandler } from 'hono-utils';
37
37
  import { pbkdf2, sha } from 'hono-utils';
38
38
  ```
39
39
 
40
+ ### Client
41
+
42
+ ```ts
43
+ import { createTypedClient } from 'hono-utils';
44
+ ```
45
+
40
46
  ### Router
41
47
 
42
48
  ```ts
@@ -45,6 +51,9 @@ import { onError, onNotFound } from 'hono-utils';
45
51
 
46
52
  ## Detailed Documentation
47
53
 
54
+ - Client
55
+ - [createTypedClient](docs/client/createTypedClient.md)
56
+
48
57
  - Crypto
49
58
  - [pbkdf2](docs/crypto/pbkdf2.md)
50
59
  - [sha](docs/crypto/sha.md)
package/dist/index.cjs CHANGED
@@ -13957,6 +13957,10 @@ var response = _factory.createMiddleware.call(void 0, async (c, next) => {
13957
13957
  await next();
13958
13958
  });
13959
13959
 
13960
+ // src/middleware/clientInfo.ts
13961
+
13962
+ var _uaparserjs = require('ua-parser-js');
13963
+
13960
13964
  // src/crypto/pbkdf2.ts
13961
13965
  var pbkdf2_exports = {};
13962
13966
  __export(pbkdf2_exports, {
@@ -14038,6 +14042,91 @@ async function hash3({
14038
14042
  return hashedString;
14039
14043
  }
14040
14044
 
14045
+ // src/middleware/clientInfo.ts
14046
+ var clientInfo = (config2) => _factory.createMiddleware.call(void 0, async ({ env, req, set: set2 }, next) => {
14047
+ if (!req.raw.cf) {
14048
+ throw new Error("Cloudflare data is not available");
14049
+ }
14050
+ const hashSecret = env[_nullishCoalesce(_optionalChain([config2, 'optionalAccess', _189 => _189.hashSecretBinding]), () => ( "HASH_SECRET"))];
14051
+ if (!hashSecret) {
14052
+ throw new Error("Hash secret is not available");
14053
+ }
14054
+ const {
14055
+ latitude,
14056
+ longitude,
14057
+ city,
14058
+ country,
14059
+ continent,
14060
+ colo,
14061
+ asn,
14062
+ isEUCountry,
14063
+ region,
14064
+ regionCode,
14065
+ postalCode,
14066
+ timezone
14067
+ } = req.raw.cf;
14068
+ const cf = {
14069
+ latitude,
14070
+ longitude,
14071
+ city,
14072
+ country,
14073
+ continent,
14074
+ colo,
14075
+ asn,
14076
+ isEUCountry: Boolean(isEUCountry),
14077
+ region,
14078
+ regionCode,
14079
+ postalCode,
14080
+ timezone
14081
+ };
14082
+ const userAgent = _nullishCoalesce(req.raw.headers.get("user-agent"), () => ( "Unknown"));
14083
+ const parser = new (0, _uaparserjs.UAParser)(userAgent);
14084
+ const userAgentParsed = parser.getResult();
14085
+ const { browser, device, engine, os, cpu } = userAgentParsed;
14086
+ const ua = {
14087
+ browser: {
14088
+ name: _nullishCoalesce(browser.name, () => ( "Unknown")),
14089
+ version: _nullishCoalesce(browser.version, () => ( "Unknown"))
14090
+ },
14091
+ device: {
14092
+ model: _nullishCoalesce(device.model, () => ( "Unknown")),
14093
+ vendor: _nullishCoalesce(device.vendor, () => ( "Unknown")),
14094
+ type: _nullishCoalesce(device.type, () => ( "desktop"))
14095
+ },
14096
+ engine: {
14097
+ name: _nullishCoalesce(engine.name, () => ( "Unknown")),
14098
+ version: _nullishCoalesce(engine.version, () => ( "Unknown"))
14099
+ },
14100
+ os: {
14101
+ name: _nullishCoalesce(os.name, () => ( "Unknown")),
14102
+ version: _nullishCoalesce(os.version, () => ( "Unknown"))
14103
+ },
14104
+ cpu: _nullishCoalesce(cpu.architecture, () => ( "Unknown"))
14105
+ };
14106
+ const ip = _nullishCoalesce(req.raw.headers.get("CF-Connecting-IP"), () => ( "127.0.0.1"));
14107
+ const clientContent = { ...cf, ...ua, ip };
14108
+ set2("client", {
14109
+ ...clientContent,
14110
+ userAgent,
14111
+ securityHash: await sha_exports.hash({
14112
+ input: _nullishCoalesce(_optionalChain([config2, 'optionalAccess', _190 => _190.securityHashString, 'optionalCall', _191 => _191(clientContent)]), () => ( `${ip}${city}${country}${userAgent}`)),
14113
+ algorithm: "SHA-512",
14114
+ pepper: hashSecret
14115
+ })
14116
+ });
14117
+ next();
14118
+ });
14119
+
14120
+ // src/middleware/hydrateVariable.ts
14121
+
14122
+ var hydrateVariable = ({
14123
+ variableName,
14124
+ hydrate
14125
+ }) => _factory.createMiddleware.call(void 0, async ({ set: set2, env, get }, next) => {
14126
+ set2(variableName, hydrate({ ...env, get }));
14127
+ await next();
14128
+ });
14129
+
14041
14130
  // src/queue/QueueHandler.ts
14042
14131
  var _cuid22 = require('@paralleldrive/cuid2');
14043
14132
  var QueueHandler = (_class = class {
@@ -14120,14 +14209,18 @@ var defaultMessageMap = {
14120
14209
  };
14121
14210
 
14122
14211
  // src/router/onError.ts
14123
- var onError = (err, { json: json2, var: { logger: logger2 } }) => {
14212
+ var onError = (parseError) => async (err, { json: json2, env, get }) => {
14124
14213
  try {
14125
- const { error: error48 } = logger2.getArea(`error`);
14126
- error48(err.message, err);
14127
14214
  const status = "status" in err ? err.status : 500;
14128
- return json2({ message: err.message }, status);
14129
- } catch (err2) {
14130
- console.error(err2);
14215
+ return json2(
14216
+ {
14217
+ message: !parseError ? err.message : await parseError(err, env, get)
14218
+ },
14219
+ status
14220
+ );
14221
+ } catch (error48) {
14222
+ console.error("Failed on error handler:", err);
14223
+ console.error("Failed to parse error:", error48);
14131
14224
  return json2({ message: defaultMessageMap.internalError }, 500);
14132
14225
  }
14133
14226
  };
@@ -14142,6 +14235,50 @@ var onNotFound = async (c) => {
14142
14235
  );
14143
14236
  };
14144
14237
 
14238
+ // src/client/createTypedClient.ts
14239
+ var _client = require('hono/client');
14240
+
14241
+
14242
+ var createTypedClient = (options) => {
14243
+ const client = _client.hc.call(void 0, options.url, {
14244
+ headers: options.headers,
14245
+ fetch: options.fetch
14246
+ });
14247
+ const rpcClient = async (fn) => {
14248
+ _optionalChain([options, 'access', _192 => _192.callbacks, 'optionalAccess', _193 => _193.onStart, 'optionalCall', _194 => _194()]);
14249
+ let responseHeaders = new Headers();
14250
+ try {
14251
+ const response2 = await fn(client);
14252
+ responseHeaders = response2.headers;
14253
+ const data = await _client.parseResponse.call(void 0, response2);
14254
+ _optionalChain([options, 'access', _195 => _195.callbacks, 'optionalAccess', _196 => _196.onSuccess, 'optionalCall', _197 => _197(data, responseHeaders)]);
14255
+ return data;
14256
+ } catch (err) {
14257
+ const errorBody = { message: err.message };
14258
+ let status = 500;
14259
+ if (err instanceof _client.DetailedError) {
14260
+ const { detail, statusCode } = err;
14261
+ status = _nullishCoalesce(statusCode, () => ( 500));
14262
+ if (!detail) {
14263
+ _optionalChain([options, 'access', _198 => _198.callbacks, 'optionalAccess', _199 => _199.errorHandler, 'optionalCall', _200 => _200(500, {
14264
+ message: "Fetch malformed"
14265
+ })]);
14266
+ throw new (0, _httpexception.HTTPException)(500, { message: "Fetch malformed" });
14267
+ }
14268
+ }
14269
+ _optionalChain([options, 'access', _201 => _201.callbacks, 'optionalAccess', _202 => _202.onError, 'optionalCall', _203 => _203(errorBody, responseHeaders)]);
14270
+ _optionalChain([options, 'access', _204 => _204.callbacks, 'optionalAccess', _205 => _205.errorHandler, 'optionalCall', _206 => _206(status, errorBody)]);
14271
+ throw new (0, _httpexception.HTTPException)(status, errorBody);
14272
+ } finally {
14273
+ _optionalChain([options, 'access', _207 => _207.callbacks, 'optionalAccess', _208 => _208.onEnd, 'optionalCall', _209 => _209()]);
14274
+ }
14275
+ };
14276
+ return rpcClient;
14277
+ };
14278
+
14279
+
14280
+
14281
+
14145
14282
 
14146
14283
 
14147
14284
 
@@ -14154,5 +14291,5 @@ var onNotFound = async (c) => {
14154
14291
 
14155
14292
 
14156
14293
 
14157
- exports.PBKDF2 = pbkdf2_exports; exports.QueueHandler = QueueHandler; exports.SHA = sha_exports; exports.i18n = i18n; exports.isBot = isBot; exports.jsonValidator = jsonValidator; exports.logger = logger; exports.onError = onError; exports.onNotFound = onNotFound; exports.queue = queue; exports.response = response; exports.withLogger = withLogger;
14294
+ exports.PBKDF2 = pbkdf2_exports; exports.QueueHandler = QueueHandler; exports.SHA = sha_exports; exports.clientInfo = clientInfo; exports.createTypedClient = createTypedClient; exports.hydrateVariable = hydrateVariable; exports.i18n = i18n; exports.isBot = isBot; exports.jsonValidator = jsonValidator; exports.logger = logger; exports.onError = onError; exports.onNotFound = onNotFound; exports.queue = queue; exports.response = response; exports.withLogger = withLogger;
14158
14295
  //# sourceMappingURL=index.cjs.map