@develit-io/backend-sdk 5.37.1 → 5.39.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.
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,10 @@ declare const idempotency: <T extends {
61
64
  IDEMPOTENCY_KV: KVNamespace;
62
65
  }>() => MiddlewareHandler;
63
66
 
67
+ declare const jwt: <TAuthService, TJwtPayload>() => MiddlewareHandler;
68
+
69
+ declare const logger: () => MiddlewareHandler;
70
+
64
71
  declare const ENVIRONMENT: string[];
65
72
 
66
73
  interface AuditLogPayload<T> {
@@ -71,6 +78,13 @@ interface AuditLogPayload<T> {
71
78
  entityId?: string;
72
79
  }
73
80
 
81
+ interface AuthUser {
82
+ id: string;
83
+ organizationId: string;
84
+ email: string;
85
+ role: UserRole;
86
+ }
87
+
74
88
  declare const ibanSchema: z$1.ZodString;
75
89
  declare const bicSchema: z$1.ZodString;
76
90
  declare const bankAccountMetadataSchema: z$1.ZodObject<{
@@ -166,6 +180,16 @@ type InferResultType<Tables extends Record<string, unknown>, TableName extends k
166
180
  with: With;
167
181
  }>;
168
182
 
183
+ interface IdempotencyVariables {
184
+ idempotency: {
185
+ key: string;
186
+ };
187
+ }
188
+ interface UserVariables<T> {
189
+ user: AuthUser;
190
+ jwt: T;
191
+ }
192
+
169
193
  declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
170
194
  [k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
171
195
  }>>, z.$ZodObjectConfig>;
@@ -499,5 +523,5 @@ interface WithRetryCounterOptions {
499
523
  type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
500
524
  declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
501
525
 
502
- 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, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
503
- export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, 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,10 @@ declare const idempotency: <T extends {
61
64
  IDEMPOTENCY_KV: KVNamespace;
62
65
  }>() => MiddlewareHandler;
63
66
 
67
+ declare const jwt: <TAuthService, TJwtPayload>() => MiddlewareHandler;
68
+
69
+ declare const logger: () => MiddlewareHandler;
70
+
64
71
  declare const ENVIRONMENT: string[];
65
72
 
66
73
  interface AuditLogPayload<T> {
@@ -71,6 +78,13 @@ interface AuditLogPayload<T> {
71
78
  entityId?: string;
72
79
  }
73
80
 
81
+ interface AuthUser {
82
+ id: string;
83
+ organizationId: string;
84
+ email: string;
85
+ role: UserRole;
86
+ }
87
+
74
88
  declare const ibanSchema: z$1.ZodString;
75
89
  declare const bicSchema: z$1.ZodString;
76
90
  declare const bankAccountMetadataSchema: z$1.ZodObject<{
@@ -166,6 +180,16 @@ type InferResultType<Tables extends Record<string, unknown>, TableName extends k
166
180
  with: With;
167
181
  }>;
168
182
 
183
+ interface IdempotencyVariables {
184
+ idempotency: {
185
+ key: string;
186
+ };
187
+ }
188
+ interface UserVariables<T> {
189
+ user: AuthUser;
190
+ jwt: T;
191
+ }
192
+
169
193
  declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
170
194
  [k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
171
195
  }>>, z.$ZodObjectConfig>;
@@ -499,5 +523,5 @@ interface WithRetryCounterOptions {
499
523
  type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
500
524
  declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
501
525
 
502
- 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, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
503
- export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, BankAccountMetadata, BaseEvent, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, 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,15 +2,15 @@ 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 { z as z$1 } from 'zod';
6
- import * as z from 'zod/v4/core';
7
- import 'http-status-codes';
8
5
  import { createError } from 'h3';
6
+ import { z as z$1 } from 'zod';
9
7
  import 'cloudflare';
10
8
  import fs from 'node:fs';
11
9
  import crypto$1 from 'node:crypto';
12
10
  import path from 'node:path';
13
11
  import { parse } from 'comment-json';
12
+ import * as z from 'zod/v4/core';
13
+ import 'http-status-codes';
14
14
  import superjson from 'superjson';
15
15
 
16
16
  const uuidv4 = () => crypto.randomUUID();
@@ -36,6 +36,8 @@ const bankAccount = {
36
36
  countryCode: text("country_code", { enum: COUNTRY_CODES_2 }).notNull()
37
37
  };
38
38
 
39
+ const USER_ROLES = ["OWNER", "ADMIN", "MEMBER"];
40
+
39
41
  const ENVIRONMENT = ["dev", "test", "staging", "production"];
40
42
 
41
43
  const COMPATIBILITY_DATE = "2025-06-04";
@@ -98,108 +100,6 @@ const idempotency = () => {
98
100
  });
99
101
  };
100
102
 
101
- const ibanSchema = z$1.string().min(1).regex(/^[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}$/, {
102
- message: "Invalid IBAN format"
103
- });
104
- const bicSchema = z$1.string().min(1).regex(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/, {
105
- message: "Invalid SWIFT/BIC format"
106
- });
107
- const bankAccountMetadataSchema = z$1.object({
108
- id: z$1.string().optional(),
109
- number: z$1.string().optional(),
110
- bankCode: z$1.enum(BANK_CODES).optional(),
111
- holderName: z$1.string().optional(),
112
- iban: ibanSchema.optional(),
113
- address: z$1.string().optional(),
114
- swiftBic: bicSchema.optional(),
115
- bicCor: bicSchema.optional()
116
- });
117
-
118
- const paginationQuerySchema = new z.$ZodObject({
119
- type: "object",
120
- shape: {
121
- page: new z.$ZodDefault({
122
- type: "default",
123
- innerType: new z.$ZodNumber({
124
- type: "number",
125
- coerce: true
126
- }),
127
- defaultValue: () => 1
128
- }),
129
- limit: new z.$ZodDefault({
130
- type: "default",
131
- innerType: new z.$ZodNumber({
132
- type: "number",
133
- coerce: true
134
- }),
135
- defaultValue: () => 20
136
- }),
137
- sort: new z.$ZodDefault({
138
- type: "default",
139
- innerType: new z.$ZodObject({
140
- type: "object",
141
- shape: {
142
- column: new z.$ZodDefault({
143
- type: "default",
144
- innerType: new z.$ZodEnum({
145
- type: "enum",
146
- entries: {
147
- createdAt: "createdAt",
148
- updatedAt: "updatedAt",
149
- deletedAt: "deletedAt"
150
- }
151
- }),
152
- defaultValue: () => "updatedAt"
153
- }),
154
- direction: new z.$ZodDefault({
155
- type: "default",
156
- innerType: new z.$ZodEnum({
157
- type: "enum",
158
- entries: {
159
- asc: "asc",
160
- desc: "desc"
161
- }
162
- }),
163
- defaultValue: () => "desc"
164
- })
165
- }
166
- }),
167
- defaultValue: () => ({ column: "updatedAt", direction: "desc" })
168
- })
169
- }
170
- });
171
- const paginationSchema = new z.$ZodObject({
172
- type: "object",
173
- shape: {
174
- page: new z.$ZodNumber({
175
- type: "number"
176
- }),
177
- limit: new z.$ZodNumber({
178
- type: "number"
179
- }),
180
- sort: new z.$ZodObject({
181
- type: "object",
182
- shape: {
183
- column: new z.$ZodEnum({
184
- type: "enum",
185
- entries: {
186
- createdAt: "createdAt",
187
- updatedAt: "updatedAt",
188
- deletedAt: "deletedAt"
189
- }
190
- }),
191
- direction: new z.$ZodEnum({
192
- type: "enum",
193
- entries: {
194
- asc: "asc",
195
- desc: "desc"
196
- }
197
- })
198
- }
199
- })
200
- }
201
- });
202
-
203
103
  const handleActionResponse = ({
204
104
  error,
205
105
  status,
@@ -352,6 +252,16 @@ function createAuditLogWriter(table) {
352
252
  };
353
253
  }
354
254
 
255
+ const validateBearerScheme = (header) => {
256
+ return header.startsWith("Bearer ") && header.length > 7 && !header.slice(7).includes(" ");
257
+ };
258
+ const extractBearerToken = (header) => {
259
+ return header.slice(7).trim();
260
+ };
261
+ const validateBearerToken = (bearerToken) => {
262
+ return z$1.jwt().safeParse(bearerToken).success;
263
+ };
264
+
355
265
  function first(rows) {
356
266
  return rows.length > 0 ? rows[0] : void 0;
357
267
  }
@@ -560,6 +470,13 @@ async function useFetch(url, { parseAs = "json", ...options } = {}) {
560
470
  return [body, null];
561
471
  }
562
472
 
473
+ const logRequest = (log) => {
474
+ console.log("REQUEST | An incoming request has been recorded.", log);
475
+ };
476
+ const logResponse = (log) => {
477
+ console.log(`RESPONSE | An outgoing response has been recorded.`, log);
478
+ };
479
+
563
480
  const calculateExponentialBackoff = (attempts, baseDelaySeconds) => {
564
481
  return baseDelaySeconds ** attempts;
565
482
  };
@@ -576,6 +493,204 @@ const getSecret = async (secretName, env) => {
576
493
  return await secret.get();
577
494
  };
578
495
 
496
+ const jwt = () => {
497
+ return createMiddleware(async (context, next) => {
498
+ const authorizationHeader = context.req.header("Authorization");
499
+ if (!authorizationHeader) {
500
+ return context.json(
501
+ {
502
+ message: `The 'Authorization' header must exist and must have a value.`
503
+ },
504
+ 401
505
+ );
506
+ }
507
+ if (!validateBearerScheme(authorizationHeader)) {
508
+ return context.json(
509
+ {
510
+ message: `The 'Authorization' header value must use the Bearer scheme.`
511
+ },
512
+ 401
513
+ );
514
+ }
515
+ const bearerToken = extractBearerToken(authorizationHeader);
516
+ 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
+ );
523
+ }
524
+ const authService = context.env.AUTH_SERVICE;
525
+ const { data, error } = await authService.verifyAccessToken({
526
+ accessToken: bearerToken
527
+ });
528
+ if (!data || error) {
529
+ return context.json(
530
+ {
531
+ message: "The JWT must contain valid user information."
532
+ },
533
+ 401
534
+ );
535
+ }
536
+ const rawUserMetaDataString = data.payload.user.rawUserMetaData;
537
+ const rawUserMetaData = rawUserMetaDataString ? JSON.parse(rawUserMetaDataString) : null;
538
+ const organizationId = rawUserMetaData?.organizationId ?? null;
539
+ if (!organizationId) {
540
+ return context.json(
541
+ {
542
+ message: "User data integrity check failed."
543
+ },
544
+ 422
545
+ );
546
+ }
547
+ context.set("user", {
548
+ email: data.payload.user.email,
549
+ role: data.payload.user.role,
550
+ organizationId
551
+ });
552
+ context.set("jwt", data.payload);
553
+ await next();
554
+ });
555
+ };
556
+
557
+ const composeRequestLog = async (request) => {
558
+ return {
559
+ method: request.method,
560
+ path: request.path,
561
+ query: request.query(),
562
+ headers: request.header(),
563
+ body: await request.json().catch(() => null)
564
+ };
565
+ };
566
+ const composeResponseLog = async (response, method, path) => {
567
+ return {
568
+ method,
569
+ path,
570
+ status: response.status,
571
+ statusText: response.statusText,
572
+ headers: Object.fromEntries(response.headers.entries()),
573
+ body: await response.json().catch(() => null)
574
+ };
575
+ };
576
+
577
+ const logger = () => {
578
+ return createMiddleware(async (context, next) => {
579
+ const requestLog = await composeRequestLog(context.req);
580
+ logRequest(requestLog);
581
+ await next();
582
+ const response = context.res.clone();
583
+ const responseLog = await composeResponseLog(
584
+ response,
585
+ context.req.method,
586
+ context.req.url
587
+ );
588
+ logResponse(responseLog);
589
+ });
590
+ };
591
+
592
+ const ibanSchema = z$1.string().min(1).regex(/^[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}$/, {
593
+ message: "Invalid IBAN format"
594
+ });
595
+ const bicSchema = z$1.string().min(1).regex(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/, {
596
+ message: "Invalid SWIFT/BIC format"
597
+ });
598
+ const bankAccountMetadataSchema = z$1.object({
599
+ id: z$1.string().optional(),
600
+ number: z$1.string().optional(),
601
+ bankCode: z$1.enum(BANK_CODES).optional(),
602
+ holderName: z$1.string().optional(),
603
+ iban: ibanSchema.optional(),
604
+ address: z$1.string().optional(),
605
+ swiftBic: bicSchema.optional(),
606
+ bicCor: bicSchema.optional()
607
+ });
608
+
609
+ const paginationQuerySchema = new z.$ZodObject({
610
+ type: "object",
611
+ shape: {
612
+ page: new z.$ZodDefault({
613
+ type: "default",
614
+ innerType: new z.$ZodNumber({
615
+ type: "number",
616
+ coerce: true
617
+ }),
618
+ defaultValue: () => 1
619
+ }),
620
+ limit: new z.$ZodDefault({
621
+ type: "default",
622
+ innerType: new z.$ZodNumber({
623
+ type: "number",
624
+ coerce: true
625
+ }),
626
+ defaultValue: () => 20
627
+ }),
628
+ sort: new z.$ZodDefault({
629
+ type: "default",
630
+ innerType: new z.$ZodObject({
631
+ type: "object",
632
+ shape: {
633
+ column: new z.$ZodDefault({
634
+ type: "default",
635
+ innerType: new z.$ZodEnum({
636
+ type: "enum",
637
+ entries: {
638
+ createdAt: "createdAt",
639
+ updatedAt: "updatedAt",
640
+ deletedAt: "deletedAt"
641
+ }
642
+ }),
643
+ defaultValue: () => "updatedAt"
644
+ }),
645
+ direction: new z.$ZodDefault({
646
+ type: "default",
647
+ innerType: new z.$ZodEnum({
648
+ type: "enum",
649
+ entries: {
650
+ asc: "asc",
651
+ desc: "desc"
652
+ }
653
+ }),
654
+ defaultValue: () => "desc"
655
+ })
656
+ }
657
+ }),
658
+ defaultValue: () => ({ column: "updatedAt", direction: "desc" })
659
+ })
660
+ }
661
+ });
662
+ const paginationSchema = new z.$ZodObject({
663
+ type: "object",
664
+ shape: {
665
+ page: new z.$ZodNumber({
666
+ type: "number"
667
+ }),
668
+ limit: new z.$ZodNumber({
669
+ type: "number"
670
+ }),
671
+ sort: new z.$ZodObject({
672
+ type: "object",
673
+ shape: {
674
+ column: new z.$ZodEnum({
675
+ type: "enum",
676
+ entries: {
677
+ createdAt: "createdAt",
678
+ updatedAt: "updatedAt",
679
+ deletedAt: "deletedAt"
680
+ }
681
+ }),
682
+ direction: new z.$ZodEnum({
683
+ type: "enum",
684
+ entries: {
685
+ asc: "asc",
686
+ desc: "desc"
687
+ }
688
+ })
689
+ }
690
+ })
691
+ }
692
+ });
693
+
579
694
  const service = (serviceName) => {
580
695
  return function(constructor) {
581
696
  return class extends constructor {
@@ -716,4 +831,4 @@ function develitWorker(Worker) {
716
831
  return DevelitWorker;
717
832
  }
718
833
 
719
- 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, paginationQuerySchema, paginationSchema, service, useFetch, useResult, useResultSync, uuidv4 };
834
+ 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.37.1",
3
+ "version": "5.39.0",
4
4
  "description": "Develit Backend SDK",
5
5
  "author": "Develit.io",
6
6
  "license": "ISC",