@develit-io/backend-sdk 5.39.0 → 5.39.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.
Files changed (2) hide show
  1. package/dist/index.mjs +24 -43
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@ import { sql } from 'drizzle-orm';
2
2
  import { integer, text } from 'drizzle-orm/sqlite-core';
3
3
  import { COUNTRY_CODES_2, CURRENCY_CODES, BANK_CODES } from '@develit-io/general-codes';
4
4
  import { createMiddleware } from 'hono/factory';
5
+ import { HTTPException } from 'hono/http-exception';
5
6
  import { createError } from 'h3';
6
7
  import { z as z$1 } from 'zod';
7
8
  import 'cloudflare';
@@ -70,21 +71,16 @@ const idempotency = () => {
70
71
  return createMiddleware(async (context, next) => {
71
72
  const idempotencyKeyHeader = context.req.header("X-Idempotency-Key");
72
73
  if (!idempotencyKeyHeader) {
73
- return context.json(
74
- {
75
- message: `The 'X-Idempotency-Key' header must exist and must have a value.`
76
- },
77
- 401
78
- );
74
+ throw new HTTPException(401, {
75
+ message: `The 'X-Idempotency-Key' header must exist and must have a value.`
76
+ });
79
77
  }
80
78
  const existingIdempotencyRecord = await context.env.IDEMPOTENCY_KV.get(idempotencyKeyHeader);
81
- if (existingIdempotencyRecord)
82
- return context.json(
83
- {
84
- message: "The identical request has already been processed. The idempotency key is not unique."
85
- },
86
- 409
87
- );
79
+ if (existingIdempotencyRecord) {
80
+ throw new HTTPException(409, {
81
+ message: "The identical request has already been processed. The idempotency key is not unique."
82
+ });
83
+ }
88
84
  await context.env.IDEMPOTENCY_KV.put(
89
85
  idempotencyKeyHeader,
90
86
  idempotencyKeyHeader,
@@ -497,52 +493,37 @@ const jwt = () => {
497
493
  return createMiddleware(async (context, next) => {
498
494
  const authorizationHeader = context.req.header("Authorization");
499
495
  if (!authorizationHeader) {
500
- return context.json(
501
- {
502
- message: `The 'Authorization' header must exist and must have a value.`
503
- },
504
- 401
505
- );
496
+ throw new HTTPException(401, {
497
+ message: `The 'Authorization' header must exist and must have a value.`
498
+ });
506
499
  }
507
500
  if (!validateBearerScheme(authorizationHeader)) {
508
- return context.json(
509
- {
510
- message: `The 'Authorization' header value must use the Bearer scheme.`
511
- },
512
- 401
513
- );
501
+ throw new HTTPException(401, {
502
+ message: `The 'Authorization' header value must use the Bearer scheme.`
503
+ });
514
504
  }
515
505
  const bearerToken = extractBearerToken(authorizationHeader);
516
506
  if (!validateBearerToken(bearerToken)) {
517
- return context.json(
518
- {
519
- message: `The Bearer token in the 'Authorization' header value must be a JWT.`
520
- },
521
- 401
522
- );
507
+ throw new HTTPException(401, {
508
+ message: `The Bearer token in the 'Authorization' header value must be a JWT.`
509
+ });
523
510
  }
524
511
  const authService = context.env.AUTH_SERVICE;
525
512
  const { data, error } = await authService.verifyAccessToken({
526
513
  accessToken: bearerToken
527
514
  });
528
515
  if (!data || error) {
529
- return context.json(
530
- {
531
- message: "The JWT must contain valid user information."
532
- },
533
- 401
534
- );
516
+ throw new HTTPException(401, {
517
+ message: "The JWT must contain valid user information."
518
+ });
535
519
  }
536
520
  const rawUserMetaDataString = data.payload.user.rawUserMetaData;
537
521
  const rawUserMetaData = rawUserMetaDataString ? JSON.parse(rawUserMetaDataString) : null;
538
522
  const organizationId = rawUserMetaData?.organizationId ?? null;
539
523
  if (!organizationId) {
540
- return context.json(
541
- {
542
- message: "User data integrity check failed."
543
- },
544
- 422
545
- );
524
+ throw new HTTPException(422, {
525
+ message: "User data integrity check failed."
526
+ });
546
527
  }
547
528
  context.set("user", {
548
529
  email: data.payload.user.email,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-io/backend-sdk",
3
- "version": "5.39.0",
3
+ "version": "5.39.1",
4
4
  "description": "Develit Backend SDK",
5
5
  "author": "Develit.io",
6
6
  "license": "ISC",