@develit-io/backend-sdk 5.38.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.
package/dist/index.d.mts CHANGED
@@ -36,6 +36,9 @@ declare const bankAccount: {
36
36
  countryCode: drizzle_orm.NotNull<drizzle_orm_sqlite_core.SQLiteTextBuilderInitial<"country_code", ["AF" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BO" | "BQ" | "BA" | "BW" | "BR" | "IO" | "VG" | "BG" | "BF" | "BI" | "KH" | "CM" | "CA" | "CV" | "CF" | "TD" | "CL" | "CN" | "CO" | "KM" | "CK" | "CR" | "HR" | "CW" | "CY" | "CZ" | "CD" | "DK" | "DJ" | "DM" | "DO" | "TL" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "ET" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GR" | "GD" | "GP" | "GU" | "GT" | "GN" | "GW" | "GY" | "HT" | "HN" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "CI" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "XK" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LI" | "LT" | "LU" | "MK" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MQ" | "MR" | "MU" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "NA" | "NP" | "NL" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "KP" | "NO" | "OM" | "PK" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PL" | "PT" | "PR" | "QA" | "CG" | "RE" | "RO" | "RU" | "RW" | "BL" | "KN" | "LC" | "MF" | "VC" | "WS" | "SM" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SX" | "SK" | "SI" | "SO" | "ZA" | "KR" | "SS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SZ" | "SE" | "CH" | "TW" | "TJ" | "TZ" | "TH" | "TG" | "TO" | "TT" | "TN" | "TR" | "TM" | "UM" | "VI" | "UG" | "UA" | "AE" | "GB" | "UZ" | "VU" | "VE" | "VN" | "WF" | "EH" | "YE" | "ZM" | "ZW", ...("AF" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BO" | "BQ" | "BA" | "BW" | "BR" | "IO" | "VG" | "BG" | "BF" | "BI" | "KH" | "CM" | "CA" | "CV" | "CF" | "TD" | "CL" | "CN" | "CO" | "KM" | "CK" | "CR" | "HR" | "CW" | "CY" | "CZ" | "CD" | "DK" | "DJ" | "DM" | "DO" | "TL" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "ET" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GR" | "GD" | "GP" | "GU" | "GT" | "GN" | "GW" | "GY" | "HT" | "HN" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "CI" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "XK" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LI" | "LT" | "LU" | "MK" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MQ" | "MR" | "MU" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "NA" | "NP" | "NL" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "KP" | "NO" | "OM" | "PK" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PL" | "PT" | "PR" | "QA" | "CG" | "RE" | "RO" | "RU" | "RW" | "BL" | "KN" | "LC" | "MF" | "VC" | "WS" | "SM" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SX" | "SK" | "SI" | "SO" | "ZA" | "KR" | "SS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SZ" | "SE" | "CH" | "TW" | "TJ" | "TZ" | "TH" | "TG" | "TO" | "TT" | "TN" | "TR" | "TM" | "UM" | "VI" | "UG" | "UA" | "AE" | "GB" | "UZ" | "VU" | "VE" | "VN" | "WF" | "EH" | "YE" | "ZM" | "ZW")[]], number | undefined>>;
37
37
  };
38
38
 
39
+ declare const USER_ROLES: readonly ["OWNER", "ADMIN", "MEMBER"];
40
+ type UserRole = (typeof USER_ROLES)[number];
41
+
39
42
  declare const composeWranglerBase: ({ project, name, }: {
40
43
  project: string;
41
44
  name: string;
@@ -61,6 +64,8 @@ declare const idempotency: <T extends {
61
64
  IDEMPOTENCY_KV: KVNamespace;
62
65
  }>() => MiddlewareHandler;
63
66
 
67
+ declare const jwt: <TAuthService, TJwtPayload>() => MiddlewareHandler;
68
+
64
69
  declare const logger: () => MiddlewareHandler;
65
70
 
66
71
  declare const ENVIRONMENT: string[];
@@ -73,6 +78,13 @@ interface AuditLogPayload<T> {
73
78
  entityId?: string;
74
79
  }
75
80
 
81
+ interface AuthUser {
82
+ id: string;
83
+ organizationId: string;
84
+ email: string;
85
+ role: UserRole;
86
+ }
87
+
76
88
  declare const ibanSchema: z$1.ZodString;
77
89
  declare const bicSchema: z$1.ZodString;
78
90
  declare const bankAccountMetadataSchema: z$1.ZodObject<{
@@ -173,6 +185,10 @@ interface IdempotencyVariables {
173
185
  key: string;
174
186
  };
175
187
  }
188
+ interface UserVariables<T> {
189
+ user: AuthUser;
190
+ jwt: T;
191
+ }
176
192
 
177
193
  declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
178
194
  [k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
@@ -507,5 +523,5 @@ interface WithRetryCounterOptions {
507
523
  type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
508
524
  declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
509
525
 
510
- export { DatabaseTransaction, ENVIRONMENT, RPCResponse, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
511
- export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IdempotencyVariables, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, ValidatedInput };
526
+ export { DatabaseTransaction, ENVIRONMENT, RPCResponse, USER_ROLES, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, jwt, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
527
+ export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, AuthUser, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IdempotencyVariables, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, UserRole, UserVariables, ValidatedInput };
package/dist/index.d.ts CHANGED
@@ -36,6 +36,9 @@ declare const bankAccount: {
36
36
  countryCode: drizzle_orm.NotNull<drizzle_orm_sqlite_core.SQLiteTextBuilderInitial<"country_code", ["AF" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BO" | "BQ" | "BA" | "BW" | "BR" | "IO" | "VG" | "BG" | "BF" | "BI" | "KH" | "CM" | "CA" | "CV" | "CF" | "TD" | "CL" | "CN" | "CO" | "KM" | "CK" | "CR" | "HR" | "CW" | "CY" | "CZ" | "CD" | "DK" | "DJ" | "DM" | "DO" | "TL" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "ET" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GR" | "GD" | "GP" | "GU" | "GT" | "GN" | "GW" | "GY" | "HT" | "HN" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "CI" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "XK" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LI" | "LT" | "LU" | "MK" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MQ" | "MR" | "MU" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "NA" | "NP" | "NL" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "KP" | "NO" | "OM" | "PK" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PL" | "PT" | "PR" | "QA" | "CG" | "RE" | "RO" | "RU" | "RW" | "BL" | "KN" | "LC" | "MF" | "VC" | "WS" | "SM" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SX" | "SK" | "SI" | "SO" | "ZA" | "KR" | "SS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SZ" | "SE" | "CH" | "TW" | "TJ" | "TZ" | "TH" | "TG" | "TO" | "TT" | "TN" | "TR" | "TM" | "UM" | "VI" | "UG" | "UA" | "AE" | "GB" | "UZ" | "VU" | "VE" | "VN" | "WF" | "EH" | "YE" | "ZM" | "ZW", ...("AF" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BO" | "BQ" | "BA" | "BW" | "BR" | "IO" | "VG" | "BG" | "BF" | "BI" | "KH" | "CM" | "CA" | "CV" | "CF" | "TD" | "CL" | "CN" | "CO" | "KM" | "CK" | "CR" | "HR" | "CW" | "CY" | "CZ" | "CD" | "DK" | "DJ" | "DM" | "DO" | "TL" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "ET" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GR" | "GD" | "GP" | "GU" | "GT" | "GN" | "GW" | "GY" | "HT" | "HN" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "CI" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "XK" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LI" | "LT" | "LU" | "MK" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MQ" | "MR" | "MU" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "NA" | "NP" | "NL" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "KP" | "NO" | "OM" | "PK" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PL" | "PT" | "PR" | "QA" | "CG" | "RE" | "RO" | "RU" | "RW" | "BL" | "KN" | "LC" | "MF" | "VC" | "WS" | "SM" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SX" | "SK" | "SI" | "SO" | "ZA" | "KR" | "SS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SZ" | "SE" | "CH" | "TW" | "TJ" | "TZ" | "TH" | "TG" | "TO" | "TT" | "TN" | "TR" | "TM" | "UM" | "VI" | "UG" | "UA" | "AE" | "GB" | "UZ" | "VU" | "VE" | "VN" | "WF" | "EH" | "YE" | "ZM" | "ZW")[]], number | undefined>>;
37
37
  };
38
38
 
39
+ declare const USER_ROLES: readonly ["OWNER", "ADMIN", "MEMBER"];
40
+ type UserRole = (typeof USER_ROLES)[number];
41
+
39
42
  declare const composeWranglerBase: ({ project, name, }: {
40
43
  project: string;
41
44
  name: string;
@@ -61,6 +64,8 @@ declare const idempotency: <T extends {
61
64
  IDEMPOTENCY_KV: KVNamespace;
62
65
  }>() => MiddlewareHandler;
63
66
 
67
+ declare const jwt: <TAuthService, TJwtPayload>() => MiddlewareHandler;
68
+
64
69
  declare const logger: () => MiddlewareHandler;
65
70
 
66
71
  declare const ENVIRONMENT: string[];
@@ -73,6 +78,13 @@ interface AuditLogPayload<T> {
73
78
  entityId?: string;
74
79
  }
75
80
 
81
+ interface AuthUser {
82
+ id: string;
83
+ organizationId: string;
84
+ email: string;
85
+ role: UserRole;
86
+ }
87
+
76
88
  declare const ibanSchema: z$1.ZodString;
77
89
  declare const bicSchema: z$1.ZodString;
78
90
  declare const bankAccountMetadataSchema: z$1.ZodObject<{
@@ -173,6 +185,10 @@ interface IdempotencyVariables {
173
185
  key: string;
174
186
  };
175
187
  }
188
+ interface UserVariables<T> {
189
+ user: AuthUser;
190
+ jwt: T;
191
+ }
176
192
 
177
193
  declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
178
194
  [k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
@@ -507,5 +523,5 @@ interface WithRetryCounterOptions {
507
523
  type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
508
524
  declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
509
525
 
510
- export { DatabaseTransaction, ENVIRONMENT, RPCResponse, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
511
- export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IdempotencyVariables, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, ValidatedInput };
526
+ export { DatabaseTransaction, ENVIRONMENT, RPCResponse, USER_ROLES, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, jwt, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
527
+ export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, AuthUser, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IdempotencyVariables, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, UserRole, UserVariables, ValidatedInput };
package/dist/index.mjs CHANGED
@@ -2,13 +2,14 @@ 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';
7
+ import { z as z$1 } from 'zod';
6
8
  import 'cloudflare';
7
9
  import fs from 'node:fs';
8
10
  import crypto$1 from 'node:crypto';
9
11
  import path from 'node:path';
10
12
  import { parse } from 'comment-json';
11
- import { z as z$1 } from 'zod';
12
13
  import * as z from 'zod/v4/core';
13
14
  import 'http-status-codes';
14
15
  import superjson from 'superjson';
@@ -36,6 +37,8 @@ const bankAccount = {
36
37
  countryCode: text("country_code", { enum: COUNTRY_CODES_2 }).notNull()
37
38
  };
38
39
 
40
+ const USER_ROLES = ["OWNER", "ADMIN", "MEMBER"];
41
+
39
42
  const ENVIRONMENT = ["dev", "test", "staging", "production"];
40
43
 
41
44
  const COMPATIBILITY_DATE = "2025-06-04";
@@ -68,21 +71,16 @@ const idempotency = () => {
68
71
  return createMiddleware(async (context, next) => {
69
72
  const idempotencyKeyHeader = context.req.header("X-Idempotency-Key");
70
73
  if (!idempotencyKeyHeader) {
71
- return context.json(
72
- {
73
- message: `The 'X-Idempotency-Key' header must exist and must have a value.`
74
- },
75
- 401
76
- );
74
+ throw new HTTPException(401, {
75
+ message: `The 'X-Idempotency-Key' header must exist and must have a value.`
76
+ });
77
77
  }
78
78
  const existingIdempotencyRecord = await context.env.IDEMPOTENCY_KV.get(idempotencyKeyHeader);
79
- if (existingIdempotencyRecord)
80
- return context.json(
81
- {
82
- message: "The identical request has already been processed. The idempotency key is not unique."
83
- },
84
- 409
85
- );
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
+ }
86
84
  await context.env.IDEMPOTENCY_KV.put(
87
85
  idempotencyKeyHeader,
88
86
  idempotencyKeyHeader,
@@ -98,26 +96,6 @@ const idempotency = () => {
98
96
  });
99
97
  };
100
98
 
101
- const composeRequestLog = async (request) => {
102
- return {
103
- method: request.method,
104
- path: request.path,
105
- query: request.query(),
106
- headers: request.header(),
107
- body: await request.json().catch(() => null)
108
- };
109
- };
110
- const composeResponseLog = async (response, method, path) => {
111
- return {
112
- method,
113
- path,
114
- status: response.status,
115
- statusText: response.statusText,
116
- headers: Object.fromEntries(response.headers.entries()),
117
- body: await response.json().catch(() => null)
118
- };
119
- };
120
-
121
99
  const handleActionResponse = ({
122
100
  error,
123
101
  status,
@@ -270,6 +248,16 @@ function createAuditLogWriter(table) {
270
248
  };
271
249
  }
272
250
 
251
+ const validateBearerScheme = (header) => {
252
+ return header.startsWith("Bearer ") && header.length > 7 && !header.slice(7).includes(" ");
253
+ };
254
+ const extractBearerToken = (header) => {
255
+ return header.slice(7).trim();
256
+ };
257
+ const validateBearerToken = (bearerToken) => {
258
+ return z$1.jwt().safeParse(bearerToken).success;
259
+ };
260
+
273
261
  function first(rows) {
274
262
  return rows.length > 0 ? rows[0] : void 0;
275
263
  }
@@ -501,6 +489,72 @@ const getSecret = async (secretName, env) => {
501
489
  return await secret.get();
502
490
  };
503
491
 
492
+ const jwt = () => {
493
+ return createMiddleware(async (context, next) => {
494
+ const authorizationHeader = context.req.header("Authorization");
495
+ if (!authorizationHeader) {
496
+ throw new HTTPException(401, {
497
+ message: `The 'Authorization' header must exist and must have a value.`
498
+ });
499
+ }
500
+ if (!validateBearerScheme(authorizationHeader)) {
501
+ throw new HTTPException(401, {
502
+ message: `The 'Authorization' header value must use the Bearer scheme.`
503
+ });
504
+ }
505
+ const bearerToken = extractBearerToken(authorizationHeader);
506
+ if (!validateBearerToken(bearerToken)) {
507
+ throw new HTTPException(401, {
508
+ message: `The Bearer token in the 'Authorization' header value must be a JWT.`
509
+ });
510
+ }
511
+ const authService = context.env.AUTH_SERVICE;
512
+ const { data, error } = await authService.verifyAccessToken({
513
+ accessToken: bearerToken
514
+ });
515
+ if (!data || error) {
516
+ throw new HTTPException(401, {
517
+ message: "The JWT must contain valid user information."
518
+ });
519
+ }
520
+ const rawUserMetaDataString = data.payload.user.rawUserMetaData;
521
+ const rawUserMetaData = rawUserMetaDataString ? JSON.parse(rawUserMetaDataString) : null;
522
+ const organizationId = rawUserMetaData?.organizationId ?? null;
523
+ if (!organizationId) {
524
+ throw new HTTPException(422, {
525
+ message: "User data integrity check failed."
526
+ });
527
+ }
528
+ context.set("user", {
529
+ email: data.payload.user.email,
530
+ role: data.payload.user.role,
531
+ organizationId
532
+ });
533
+ context.set("jwt", data.payload);
534
+ await next();
535
+ });
536
+ };
537
+
538
+ const composeRequestLog = async (request) => {
539
+ return {
540
+ method: request.method,
541
+ path: request.path,
542
+ query: request.query(),
543
+ headers: request.header(),
544
+ body: await request.json().catch(() => null)
545
+ };
546
+ };
547
+ const composeResponseLog = async (response, method, path) => {
548
+ return {
549
+ method,
550
+ path,
551
+ status: response.status,
552
+ statusText: response.statusText,
553
+ headers: Object.fromEntries(response.headers.entries()),
554
+ body: await response.json().catch(() => null)
555
+ };
556
+ };
557
+
504
558
  const logger = () => {
505
559
  return createMiddleware(async (context, next) => {
506
560
  const requestLog = await composeRequestLog(context.req);
@@ -758,4 +812,4 @@ function develitWorker(Worker) {
758
812
  return DevelitWorker;
759
813
  }
760
814
 
761
- export { DatabaseTransaction, ENVIRONMENT, RPCResponse, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
815
+ export { DatabaseTransaction, ENVIRONMENT, RPCResponse, USER_ROLES, action, bankAccount, bankAccountMetadataSchema, base, bicSchema, calculateExponentialBackoff, cloudflareQueue, composeWranglerBase, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getSecret, handleAction, handleActionResponse, ibanSchema, idempotency, isInternalError, jwt, logger, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-io/backend-sdk",
3
- "version": "5.38.0",
3
+ "version": "5.39.1",
4
4
  "description": "Develit Backend SDK",
5
5
  "author": "Develit.io",
6
6
  "license": "ISC",