@develit-io/backend-sdk 5.19.0 → 5.21.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.cjs +35 -1
- package/dist/index.d.cts +60 -54
- package/dist/index.d.mts +60 -54
- package/dist/index.d.ts +60 -54
- package/dist/index.mjs +35 -2
- package/package.json +5 -7
package/dist/index.cjs
CHANGED
|
@@ -5,8 +5,8 @@ const pgCore = require('drizzle-orm/pg-core');
|
|
|
5
5
|
const sqliteCore = require('drizzle-orm/sqlite-core');
|
|
6
6
|
const generalCodes = require('@develit-io/general-codes');
|
|
7
7
|
const text = require('@std/text');
|
|
8
|
-
require('http-status-codes');
|
|
9
8
|
const z = require('zod/v4/core');
|
|
9
|
+
require('http-status-codes');
|
|
10
10
|
const h3 = require('h3');
|
|
11
11
|
const fs = require('node:fs');
|
|
12
12
|
const crypto$1 = require('node:crypto');
|
|
@@ -685,6 +685,39 @@ const defineCommand = (handler) => {
|
|
|
685
685
|
}));
|
|
686
686
|
};
|
|
687
687
|
|
|
688
|
+
async function useFetch(url, { parseAs = "json", ...options } = {}) {
|
|
689
|
+
const [response, fetchError] = await useResult(fetch(url, options));
|
|
690
|
+
if (fetchError || !response) {
|
|
691
|
+
return [null, createInternalError(fetchError, { code: "NETWORK_ERROR" })];
|
|
692
|
+
}
|
|
693
|
+
const parsers = {
|
|
694
|
+
json: () => response.json(),
|
|
695
|
+
text: () => response.text(),
|
|
696
|
+
blob: () => response.blob()
|
|
697
|
+
};
|
|
698
|
+
const [body, parseError] = await useResult(parsers[parseAs]());
|
|
699
|
+
if (!response.ok) {
|
|
700
|
+
return [
|
|
701
|
+
null,
|
|
702
|
+
createInternalError(parseError, {
|
|
703
|
+
message: body?.message ?? response.statusText,
|
|
704
|
+
code: "HTTP_ERROR",
|
|
705
|
+
status: response.status
|
|
706
|
+
})
|
|
707
|
+
];
|
|
708
|
+
}
|
|
709
|
+
if (parseError) {
|
|
710
|
+
return [
|
|
711
|
+
null,
|
|
712
|
+
createInternalError(parseError, {
|
|
713
|
+
code: "PARSE_ERROR",
|
|
714
|
+
status: response.status
|
|
715
|
+
})
|
|
716
|
+
];
|
|
717
|
+
}
|
|
718
|
+
return [body, null];
|
|
719
|
+
}
|
|
720
|
+
|
|
688
721
|
const getPgLocalConnectionString = (id) => `postgres://db_user:db_password@127.0.0.1:${derivePortFromId(id)}/db`;
|
|
689
722
|
const getPgDatabaseIdFromWrangler = () => {
|
|
690
723
|
try {
|
|
@@ -912,6 +945,7 @@ exports.paginationQuerySchema = paginationQuerySchema;
|
|
|
912
945
|
exports.paginationSchema = paginationSchema;
|
|
913
946
|
exports.service = service;
|
|
914
947
|
exports.swiftZodSchema = swiftZodSchema;
|
|
948
|
+
exports.useFetch = useFetch;
|
|
915
949
|
exports.useResult = useResult;
|
|
916
950
|
exports.useResultSync = useResultSync;
|
|
917
951
|
exports.uuidv4 = uuidv4;
|
package/dist/index.d.cts
CHANGED
|
@@ -4,9 +4,9 @@ import * as drizzle_orm from 'drizzle-orm';
|
|
|
4
4
|
import { ExtractTablesWithRelations, DBQueryConfig, BuildQueryResult } from 'drizzle-orm';
|
|
5
5
|
import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
|
|
6
6
|
import { AnySQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
7
|
+
import * as z from 'zod/v4/core';
|
|
7
8
|
import { StatusCodes, ReasonPhrases } from 'http-status-codes';
|
|
8
9
|
export { ReasonPhrases as InternalResponsePhrase, StatusCodes as InternalResponseStatus } from 'http-status-codes';
|
|
9
|
-
import * as z from 'zod/v4/core';
|
|
10
10
|
import { Queue } from '@cloudflare/workers-types';
|
|
11
11
|
import { BatchItem } from 'drizzle-orm/batch';
|
|
12
12
|
import { DrizzleD1Database } from 'drizzle-orm/d1';
|
|
@@ -130,25 +130,28 @@ declare class Infrastructure {
|
|
|
130
130
|
}): any;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
message: string;
|
|
133
|
+
interface AuditLogPayload<T> {
|
|
134
|
+
action: T;
|
|
135
|
+
actorId: string;
|
|
136
|
+
actorUsername?: string;
|
|
137
|
+
service: string;
|
|
138
|
+
entityId?: string;
|
|
140
139
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
|
|
141
|
+
declare const ibanZodSchema: z.$ZodString<unknown>;
|
|
142
|
+
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
143
|
+
|
|
144
|
+
interface CommandLogPayload<T = string> {
|
|
145
|
+
action: T;
|
|
146
|
+
actorId: string;
|
|
147
|
+
actorUsername?: string;
|
|
148
|
+
entityId?: string;
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
|
|
151
|
+
interface CommandItem<TAuditAction = string> {
|
|
152
|
+
command: BatchItem<'sqlite'>;
|
|
153
|
+
logPayload: CommandLogPayload<TAuditAction>;
|
|
154
|
+
id: string;
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
/**
|
|
@@ -171,21 +174,7 @@ type InferResultType<Tables extends Record<string, unknown>, TableName extends k
|
|
|
171
174
|
with: With;
|
|
172
175
|
}>;
|
|
173
176
|
|
|
174
|
-
|
|
175
|
-
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
176
|
-
|
|
177
|
-
interface CommandLogPayload<T = string> {
|
|
178
|
-
action: T;
|
|
179
|
-
actorId: string;
|
|
180
|
-
actorUsername?: string;
|
|
181
|
-
entityId?: string;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
interface CommandItem<TAuditAction = string> {
|
|
185
|
-
command: BatchItem<'sqlite'>;
|
|
186
|
-
logPayload: CommandLogPayload<TAuditAction>;
|
|
187
|
-
id: string;
|
|
188
|
-
}
|
|
177
|
+
type Resource = 'kv' | 'd1' | 'queue' | 'r2' | 'worker' | 'service' | 'orchestrator';
|
|
189
178
|
|
|
190
179
|
declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
|
|
191
180
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
@@ -194,6 +183,38 @@ declare const paginationSchema: z.$ZodObject<Readonly<Readonly<{
|
|
|
194
183
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
195
184
|
}>>, z.$ZodObjectConfig>;
|
|
196
185
|
|
|
186
|
+
type InternalErrorResponseStatus = Exclude<StatusCodes, 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207>;
|
|
187
|
+
interface InternalError {
|
|
188
|
+
status: InternalErrorResponseStatus;
|
|
189
|
+
code: string;
|
|
190
|
+
message: string;
|
|
191
|
+
}
|
|
192
|
+
interface IRPCResponse<T> {
|
|
193
|
+
status: StatusCodes;
|
|
194
|
+
message: string;
|
|
195
|
+
data: T | null | undefined;
|
|
196
|
+
error: boolean;
|
|
197
|
+
phrase?: ReasonPhrases;
|
|
198
|
+
}
|
|
199
|
+
interface GatewayResponse<T> {
|
|
200
|
+
status?: number;
|
|
201
|
+
message: string;
|
|
202
|
+
data?: T;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* A utility function to handle operations and return a standardized result.
|
|
207
|
+
*
|
|
208
|
+
* This function wraps the call and ensures that both the resolved value
|
|
209
|
+
* and any potential errors are captured in a structured tuple format.
|
|
210
|
+
*
|
|
211
|
+
* @template T - The type of the expected result.
|
|
212
|
+
* @returns A call that resolves to a tuple containing:
|
|
213
|
+
* - The resolved data (`T | null`) if successful.
|
|
214
|
+
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
215
|
+
*/
|
|
216
|
+
type Result<T> = [data: T | null, error: InternalError | null];
|
|
217
|
+
|
|
197
218
|
declare const composeBindingName: ({ resource, resourceName, bindingName, }: {
|
|
198
219
|
resource: Resource;
|
|
199
220
|
resourceName: string;
|
|
@@ -287,14 +308,6 @@ type ValidatedInput<T extends z.$ZodType> = z.infer<T>;
|
|
|
287
308
|
*/
|
|
288
309
|
type ActionExecution<TInput, TOutput> = TInput extends null ? () => Promise<TOutput> : (input: TInput) => Promise<TOutput>;
|
|
289
310
|
|
|
290
|
-
interface AuditLogPayload<T> {
|
|
291
|
-
action: T;
|
|
292
|
-
actorId: string;
|
|
293
|
-
actorUsername?: string;
|
|
294
|
-
service: string;
|
|
295
|
-
entityId?: string;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
311
|
type AuditLogTable = AnySQLiteTable | AnyPgTable;
|
|
299
312
|
type AuditLogWriter<TAuditAction = string> = (logs: AuditLogPayload<TAuditAction>[], db: DrizzleD1Database<Record<string, unknown>>) => BatchItem<'sqlite'>[];
|
|
300
313
|
/**
|
|
@@ -406,6 +419,11 @@ declare const createInternalError: (error: unknown, details?: {
|
|
|
406
419
|
}) => InternalError;
|
|
407
420
|
declare const isInternalError: (error: unknown) => error is InternalError;
|
|
408
421
|
|
|
422
|
+
interface UseFetchOptions extends RequestInit {
|
|
423
|
+
parseAs?: 'json' | 'text' | 'blob';
|
|
424
|
+
}
|
|
425
|
+
declare function useFetch<T = unknown>(url: string, { parseAs, ...options }?: UseFetchOptions): Promise<Result<T>>;
|
|
426
|
+
|
|
409
427
|
declare const getPgLocalConnectionString: (id: string) => string;
|
|
410
428
|
declare const getPgDatabaseIdFromWrangler: () => string | undefined;
|
|
411
429
|
declare const getPgCredentials: (serviceName?: string) => {
|
|
@@ -467,18 +485,6 @@ declare const RPCResponse: {
|
|
|
467
485
|
validationError<T>(error: InternalError): IRPCResponse<T>;
|
|
468
486
|
};
|
|
469
487
|
|
|
470
|
-
/**
|
|
471
|
-
* A utility function to handle operations and return a standardized result.
|
|
472
|
-
*
|
|
473
|
-
* This function wraps the call and ensures that both the resolved value
|
|
474
|
-
* and any potential errors are captured in a structured tuple format.
|
|
475
|
-
*
|
|
476
|
-
* @template T - The type of the expected result.
|
|
477
|
-
* @returns A call that resolves to a tuple containing:
|
|
478
|
-
* - The resolved data (`T | null`) if successful.
|
|
479
|
-
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
480
|
-
*/
|
|
481
|
-
type Result<T> = [data: T | null, error: InternalError | null];
|
|
482
488
|
/**
|
|
483
489
|
* Executes a given promise and returns the result in a structured format.
|
|
484
490
|
*
|
|
@@ -525,5 +531,5 @@ interface WithRetryCounterOptions {
|
|
|
525
531
|
type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
|
|
526
532
|
declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
|
|
527
533
|
|
|
528
|
-
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
534
|
+
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useFetch, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
529
535
|
export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, ValidatedInput };
|
package/dist/index.d.mts
CHANGED
|
@@ -4,9 +4,9 @@ import * as drizzle_orm from 'drizzle-orm';
|
|
|
4
4
|
import { ExtractTablesWithRelations, DBQueryConfig, BuildQueryResult } from 'drizzle-orm';
|
|
5
5
|
import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
|
|
6
6
|
import { AnySQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
7
|
+
import * as z from 'zod/v4/core';
|
|
7
8
|
import { StatusCodes, ReasonPhrases } from 'http-status-codes';
|
|
8
9
|
export { ReasonPhrases as InternalResponsePhrase, StatusCodes as InternalResponseStatus } from 'http-status-codes';
|
|
9
|
-
import * as z from 'zod/v4/core';
|
|
10
10
|
import { Queue } from '@cloudflare/workers-types';
|
|
11
11
|
import { BatchItem } from 'drizzle-orm/batch';
|
|
12
12
|
import { DrizzleD1Database } from 'drizzle-orm/d1';
|
|
@@ -130,25 +130,28 @@ declare class Infrastructure {
|
|
|
130
130
|
}): any;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
message: string;
|
|
133
|
+
interface AuditLogPayload<T> {
|
|
134
|
+
action: T;
|
|
135
|
+
actorId: string;
|
|
136
|
+
actorUsername?: string;
|
|
137
|
+
service: string;
|
|
138
|
+
entityId?: string;
|
|
140
139
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
|
|
141
|
+
declare const ibanZodSchema: z.$ZodString<unknown>;
|
|
142
|
+
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
143
|
+
|
|
144
|
+
interface CommandLogPayload<T = string> {
|
|
145
|
+
action: T;
|
|
146
|
+
actorId: string;
|
|
147
|
+
actorUsername?: string;
|
|
148
|
+
entityId?: string;
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
|
|
151
|
+
interface CommandItem<TAuditAction = string> {
|
|
152
|
+
command: BatchItem<'sqlite'>;
|
|
153
|
+
logPayload: CommandLogPayload<TAuditAction>;
|
|
154
|
+
id: string;
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
/**
|
|
@@ -171,21 +174,7 @@ type InferResultType<Tables extends Record<string, unknown>, TableName extends k
|
|
|
171
174
|
with: With;
|
|
172
175
|
}>;
|
|
173
176
|
|
|
174
|
-
|
|
175
|
-
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
176
|
-
|
|
177
|
-
interface CommandLogPayload<T = string> {
|
|
178
|
-
action: T;
|
|
179
|
-
actorId: string;
|
|
180
|
-
actorUsername?: string;
|
|
181
|
-
entityId?: string;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
interface CommandItem<TAuditAction = string> {
|
|
185
|
-
command: BatchItem<'sqlite'>;
|
|
186
|
-
logPayload: CommandLogPayload<TAuditAction>;
|
|
187
|
-
id: string;
|
|
188
|
-
}
|
|
177
|
+
type Resource = 'kv' | 'd1' | 'queue' | 'r2' | 'worker' | 'service' | 'orchestrator';
|
|
189
178
|
|
|
190
179
|
declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
|
|
191
180
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
@@ -194,6 +183,38 @@ declare const paginationSchema: z.$ZodObject<Readonly<Readonly<{
|
|
|
194
183
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
195
184
|
}>>, z.$ZodObjectConfig>;
|
|
196
185
|
|
|
186
|
+
type InternalErrorResponseStatus = Exclude<StatusCodes, 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207>;
|
|
187
|
+
interface InternalError {
|
|
188
|
+
status: InternalErrorResponseStatus;
|
|
189
|
+
code: string;
|
|
190
|
+
message: string;
|
|
191
|
+
}
|
|
192
|
+
interface IRPCResponse<T> {
|
|
193
|
+
status: StatusCodes;
|
|
194
|
+
message: string;
|
|
195
|
+
data: T | null | undefined;
|
|
196
|
+
error: boolean;
|
|
197
|
+
phrase?: ReasonPhrases;
|
|
198
|
+
}
|
|
199
|
+
interface GatewayResponse<T> {
|
|
200
|
+
status?: number;
|
|
201
|
+
message: string;
|
|
202
|
+
data?: T;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* A utility function to handle operations and return a standardized result.
|
|
207
|
+
*
|
|
208
|
+
* This function wraps the call and ensures that both the resolved value
|
|
209
|
+
* and any potential errors are captured in a structured tuple format.
|
|
210
|
+
*
|
|
211
|
+
* @template T - The type of the expected result.
|
|
212
|
+
* @returns A call that resolves to a tuple containing:
|
|
213
|
+
* - The resolved data (`T | null`) if successful.
|
|
214
|
+
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
215
|
+
*/
|
|
216
|
+
type Result<T> = [data: T | null, error: InternalError | null];
|
|
217
|
+
|
|
197
218
|
declare const composeBindingName: ({ resource, resourceName, bindingName, }: {
|
|
198
219
|
resource: Resource;
|
|
199
220
|
resourceName: string;
|
|
@@ -287,14 +308,6 @@ type ValidatedInput<T extends z.$ZodType> = z.infer<T>;
|
|
|
287
308
|
*/
|
|
288
309
|
type ActionExecution<TInput, TOutput> = TInput extends null ? () => Promise<TOutput> : (input: TInput) => Promise<TOutput>;
|
|
289
310
|
|
|
290
|
-
interface AuditLogPayload<T> {
|
|
291
|
-
action: T;
|
|
292
|
-
actorId: string;
|
|
293
|
-
actorUsername?: string;
|
|
294
|
-
service: string;
|
|
295
|
-
entityId?: string;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
311
|
type AuditLogTable = AnySQLiteTable | AnyPgTable;
|
|
299
312
|
type AuditLogWriter<TAuditAction = string> = (logs: AuditLogPayload<TAuditAction>[], db: DrizzleD1Database<Record<string, unknown>>) => BatchItem<'sqlite'>[];
|
|
300
313
|
/**
|
|
@@ -406,6 +419,11 @@ declare const createInternalError: (error: unknown, details?: {
|
|
|
406
419
|
}) => InternalError;
|
|
407
420
|
declare const isInternalError: (error: unknown) => error is InternalError;
|
|
408
421
|
|
|
422
|
+
interface UseFetchOptions extends RequestInit {
|
|
423
|
+
parseAs?: 'json' | 'text' | 'blob';
|
|
424
|
+
}
|
|
425
|
+
declare function useFetch<T = unknown>(url: string, { parseAs, ...options }?: UseFetchOptions): Promise<Result<T>>;
|
|
426
|
+
|
|
409
427
|
declare const getPgLocalConnectionString: (id: string) => string;
|
|
410
428
|
declare const getPgDatabaseIdFromWrangler: () => string | undefined;
|
|
411
429
|
declare const getPgCredentials: (serviceName?: string) => {
|
|
@@ -467,18 +485,6 @@ declare const RPCResponse: {
|
|
|
467
485
|
validationError<T>(error: InternalError): IRPCResponse<T>;
|
|
468
486
|
};
|
|
469
487
|
|
|
470
|
-
/**
|
|
471
|
-
* A utility function to handle operations and return a standardized result.
|
|
472
|
-
*
|
|
473
|
-
* This function wraps the call and ensures that both the resolved value
|
|
474
|
-
* and any potential errors are captured in a structured tuple format.
|
|
475
|
-
*
|
|
476
|
-
* @template T - The type of the expected result.
|
|
477
|
-
* @returns A call that resolves to a tuple containing:
|
|
478
|
-
* - The resolved data (`T | null`) if successful.
|
|
479
|
-
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
480
|
-
*/
|
|
481
|
-
type Result<T> = [data: T | null, error: InternalError | null];
|
|
482
488
|
/**
|
|
483
489
|
* Executes a given promise and returns the result in a structured format.
|
|
484
490
|
*
|
|
@@ -525,5 +531,5 @@ interface WithRetryCounterOptions {
|
|
|
525
531
|
type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
|
|
526
532
|
declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
|
|
527
533
|
|
|
528
|
-
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
534
|
+
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useFetch, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
529
535
|
export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, ValidatedInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ import * as drizzle_orm from 'drizzle-orm';
|
|
|
4
4
|
import { ExtractTablesWithRelations, DBQueryConfig, BuildQueryResult } from 'drizzle-orm';
|
|
5
5
|
import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
|
|
6
6
|
import { AnySQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
7
|
+
import * as z from 'zod/v4/core';
|
|
7
8
|
import { StatusCodes, ReasonPhrases } from 'http-status-codes';
|
|
8
9
|
export { ReasonPhrases as InternalResponsePhrase, StatusCodes as InternalResponseStatus } from 'http-status-codes';
|
|
9
|
-
import * as z from 'zod/v4/core';
|
|
10
10
|
import { Queue } from '@cloudflare/workers-types';
|
|
11
11
|
import { BatchItem } from 'drizzle-orm/batch';
|
|
12
12
|
import { DrizzleD1Database } from 'drizzle-orm/d1';
|
|
@@ -130,25 +130,28 @@ declare class Infrastructure {
|
|
|
130
130
|
}): any;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
message: string;
|
|
133
|
+
interface AuditLogPayload<T> {
|
|
134
|
+
action: T;
|
|
135
|
+
actorId: string;
|
|
136
|
+
actorUsername?: string;
|
|
137
|
+
service: string;
|
|
138
|
+
entityId?: string;
|
|
140
139
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
|
|
141
|
+
declare const ibanZodSchema: z.$ZodString<unknown>;
|
|
142
|
+
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
143
|
+
|
|
144
|
+
interface CommandLogPayload<T = string> {
|
|
145
|
+
action: T;
|
|
146
|
+
actorId: string;
|
|
147
|
+
actorUsername?: string;
|
|
148
|
+
entityId?: string;
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
|
|
151
|
+
interface CommandItem<TAuditAction = string> {
|
|
152
|
+
command: BatchItem<'sqlite'>;
|
|
153
|
+
logPayload: CommandLogPayload<TAuditAction>;
|
|
154
|
+
id: string;
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
/**
|
|
@@ -171,21 +174,7 @@ type InferResultType<Tables extends Record<string, unknown>, TableName extends k
|
|
|
171
174
|
with: With;
|
|
172
175
|
}>;
|
|
173
176
|
|
|
174
|
-
|
|
175
|
-
declare const swiftZodSchema: z.$ZodString<unknown>;
|
|
176
|
-
|
|
177
|
-
interface CommandLogPayload<T = string> {
|
|
178
|
-
action: T;
|
|
179
|
-
actorId: string;
|
|
180
|
-
actorUsername?: string;
|
|
181
|
-
entityId?: string;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
interface CommandItem<TAuditAction = string> {
|
|
185
|
-
command: BatchItem<'sqlite'>;
|
|
186
|
-
logPayload: CommandLogPayload<TAuditAction>;
|
|
187
|
-
id: string;
|
|
188
|
-
}
|
|
177
|
+
type Resource = 'kv' | 'd1' | 'queue' | 'r2' | 'worker' | 'service' | 'orchestrator';
|
|
189
178
|
|
|
190
179
|
declare const paginationQuerySchema: z.$ZodObject<Readonly<Readonly<{
|
|
191
180
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
@@ -194,6 +183,38 @@ declare const paginationSchema: z.$ZodObject<Readonly<Readonly<{
|
|
|
194
183
|
[k: string]: z.$ZodType<unknown, unknown, z.$ZodTypeInternals<unknown, unknown>>;
|
|
195
184
|
}>>, z.$ZodObjectConfig>;
|
|
196
185
|
|
|
186
|
+
type InternalErrorResponseStatus = Exclude<StatusCodes, 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207>;
|
|
187
|
+
interface InternalError {
|
|
188
|
+
status: InternalErrorResponseStatus;
|
|
189
|
+
code: string;
|
|
190
|
+
message: string;
|
|
191
|
+
}
|
|
192
|
+
interface IRPCResponse<T> {
|
|
193
|
+
status: StatusCodes;
|
|
194
|
+
message: string;
|
|
195
|
+
data: T | null | undefined;
|
|
196
|
+
error: boolean;
|
|
197
|
+
phrase?: ReasonPhrases;
|
|
198
|
+
}
|
|
199
|
+
interface GatewayResponse<T> {
|
|
200
|
+
status?: number;
|
|
201
|
+
message: string;
|
|
202
|
+
data?: T;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* A utility function to handle operations and return a standardized result.
|
|
207
|
+
*
|
|
208
|
+
* This function wraps the call and ensures that both the resolved value
|
|
209
|
+
* and any potential errors are captured in a structured tuple format.
|
|
210
|
+
*
|
|
211
|
+
* @template T - The type of the expected result.
|
|
212
|
+
* @returns A call that resolves to a tuple containing:
|
|
213
|
+
* - The resolved data (`T | null`) if successful.
|
|
214
|
+
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
215
|
+
*/
|
|
216
|
+
type Result<T> = [data: T | null, error: InternalError | null];
|
|
217
|
+
|
|
197
218
|
declare const composeBindingName: ({ resource, resourceName, bindingName, }: {
|
|
198
219
|
resource: Resource;
|
|
199
220
|
resourceName: string;
|
|
@@ -287,14 +308,6 @@ type ValidatedInput<T extends z.$ZodType> = z.infer<T>;
|
|
|
287
308
|
*/
|
|
288
309
|
type ActionExecution<TInput, TOutput> = TInput extends null ? () => Promise<TOutput> : (input: TInput) => Promise<TOutput>;
|
|
289
310
|
|
|
290
|
-
interface AuditLogPayload<T> {
|
|
291
|
-
action: T;
|
|
292
|
-
actorId: string;
|
|
293
|
-
actorUsername?: string;
|
|
294
|
-
service: string;
|
|
295
|
-
entityId?: string;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
311
|
type AuditLogTable = AnySQLiteTable | AnyPgTable;
|
|
299
312
|
type AuditLogWriter<TAuditAction = string> = (logs: AuditLogPayload<TAuditAction>[], db: DrizzleD1Database<Record<string, unknown>>) => BatchItem<'sqlite'>[];
|
|
300
313
|
/**
|
|
@@ -406,6 +419,11 @@ declare const createInternalError: (error: unknown, details?: {
|
|
|
406
419
|
}) => InternalError;
|
|
407
420
|
declare const isInternalError: (error: unknown) => error is InternalError;
|
|
408
421
|
|
|
422
|
+
interface UseFetchOptions extends RequestInit {
|
|
423
|
+
parseAs?: 'json' | 'text' | 'blob';
|
|
424
|
+
}
|
|
425
|
+
declare function useFetch<T = unknown>(url: string, { parseAs, ...options }?: UseFetchOptions): Promise<Result<T>>;
|
|
426
|
+
|
|
409
427
|
declare const getPgLocalConnectionString: (id: string) => string;
|
|
410
428
|
declare const getPgDatabaseIdFromWrangler: () => string | undefined;
|
|
411
429
|
declare const getPgCredentials: (serviceName?: string) => {
|
|
@@ -467,18 +485,6 @@ declare const RPCResponse: {
|
|
|
467
485
|
validationError<T>(error: InternalError): IRPCResponse<T>;
|
|
468
486
|
};
|
|
469
487
|
|
|
470
|
-
/**
|
|
471
|
-
* A utility function to handle operations and return a standardized result.
|
|
472
|
-
*
|
|
473
|
-
* This function wraps the call and ensures that both the resolved value
|
|
474
|
-
* and any potential errors are captured in a structured tuple format.
|
|
475
|
-
*
|
|
476
|
-
* @template T - The type of the expected result.
|
|
477
|
-
* @returns A call that resolves to a tuple containing:
|
|
478
|
-
* - The resolved data (`T | null`) if successful.
|
|
479
|
-
* - An `RPCError` object (`RPCError | null`) if an error occurs.
|
|
480
|
-
*/
|
|
481
|
-
type Result<T> = [data: T | null, error: InternalError | null];
|
|
482
488
|
/**
|
|
483
489
|
* Executes a given promise and returns the result in a structured format.
|
|
484
490
|
*
|
|
@@ -525,5 +531,5 @@ interface WithRetryCounterOptions {
|
|
|
525
531
|
type AsyncMethod<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => Promise<TResult>;
|
|
526
532
|
declare function cloudflareQueue<TArgs extends unknown[] = unknown[], TResult = unknown>(options: WithRetryCounterOptions): (target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<AsyncMethod<TArgs, TResult>>) => void;
|
|
527
533
|
|
|
528
|
-
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
534
|
+
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useFetch, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
529
535
|
export type { ActionExecution, ActionHandlerOptions, AuditLogWriter, Command, CommandLogPayload, DevelitWorkerMethods, Environment, GatewayResponse, IRPCResponse, IncludeRelation, InferResultType, InternalError, InternalErrorResponseStatus, Project, ValidatedInput };
|
package/dist/index.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { timestamp, uuid, pgEnum, text as text$1 } from 'drizzle-orm/pg-core';
|
|
|
3
3
|
import { integer, text } from 'drizzle-orm/sqlite-core';
|
|
4
4
|
import { COUNTRY_CODES_2, CURRENCY_CODES, BANK_CODES } from '@develit-io/general-codes';
|
|
5
5
|
import { toSnakeCase } from '@std/text';
|
|
6
|
-
import 'http-status-codes';
|
|
7
6
|
import * as z from 'zod/v4/core';
|
|
7
|
+
import 'http-status-codes';
|
|
8
8
|
import { createError } from 'h3';
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
import crypto$1 from 'node:crypto';
|
|
@@ -663,6 +663,39 @@ const defineCommand = (handler) => {
|
|
|
663
663
|
}));
|
|
664
664
|
};
|
|
665
665
|
|
|
666
|
+
async function useFetch(url, { parseAs = "json", ...options } = {}) {
|
|
667
|
+
const [response, fetchError] = await useResult(fetch(url, options));
|
|
668
|
+
if (fetchError || !response) {
|
|
669
|
+
return [null, createInternalError(fetchError, { code: "NETWORK_ERROR" })];
|
|
670
|
+
}
|
|
671
|
+
const parsers = {
|
|
672
|
+
json: () => response.json(),
|
|
673
|
+
text: () => response.text(),
|
|
674
|
+
blob: () => response.blob()
|
|
675
|
+
};
|
|
676
|
+
const [body, parseError] = await useResult(parsers[parseAs]());
|
|
677
|
+
if (!response.ok) {
|
|
678
|
+
return [
|
|
679
|
+
null,
|
|
680
|
+
createInternalError(parseError, {
|
|
681
|
+
message: body?.message ?? response.statusText,
|
|
682
|
+
code: "HTTP_ERROR",
|
|
683
|
+
status: response.status
|
|
684
|
+
})
|
|
685
|
+
];
|
|
686
|
+
}
|
|
687
|
+
if (parseError) {
|
|
688
|
+
return [
|
|
689
|
+
null,
|
|
690
|
+
createInternalError(parseError, {
|
|
691
|
+
code: "PARSE_ERROR",
|
|
692
|
+
status: response.status
|
|
693
|
+
})
|
|
694
|
+
];
|
|
695
|
+
}
|
|
696
|
+
return [body, null];
|
|
697
|
+
}
|
|
698
|
+
|
|
666
699
|
const getPgLocalConnectionString = (id) => `postgres://db_user:db_password@127.0.0.1:${derivePortFromId(id)}/db`;
|
|
667
700
|
const getPgDatabaseIdFromWrangler = () => {
|
|
668
701
|
try {
|
|
@@ -852,4 +885,4 @@ function develitWorker(Worker) {
|
|
|
852
885
|
return DevelitWorker;
|
|
853
886
|
}
|
|
854
887
|
|
|
855
|
-
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useResult, useResultSync, uuidv4, validateEnvironment };
|
|
888
|
+
export { DatabaseTransaction, ENVIRONMENT, Infrastructure, RPCResponse, action, bankAccount, bankAccountBankCodeEnum, bankAccountCountryCodeEnum, bankAccountCurrencyEnum, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, composeBindingName, createAuditLogWriter, createInternalError, defineCommand, derivePortFromId, develitWorker, durableObjectNamespaceIdFromName, first, firstOrError, getD1Credentials, getD1DatabaseIdFromWrangler, getDrizzleD1Config, getDrizzlePgConfig, getPgCredentials, getPgDatabaseIdFromWrangler, getPgLocalConnectionString, handleAction, handleActionResponse, ibanZodSchema, isInternalError, paginationQuerySchema, paginationSchema, service, swiftZodSchema, useFetch, useResult, useResultSync, uuidv4, validateEnvironment };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@develit-io/backend-sdk",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.21.0",
|
|
4
4
|
"description": "Develit Backend SDK",
|
|
5
5
|
"author": "Develit.io",
|
|
6
6
|
"license": "ISC",
|
|
@@ -18,19 +18,17 @@
|
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
21
22
|
"import": "./dist/index.mjs",
|
|
22
|
-
"require": "./dist/index.cjs"
|
|
23
|
-
"types": "./dist/index.d.ts"
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
24
|
},
|
|
25
25
|
"./node": {
|
|
26
|
+
"types": "./dist/node/index.d.ts",
|
|
26
27
|
"import": "./dist/node/index.mjs",
|
|
27
|
-
"require": "./dist/node/index.cjs"
|
|
28
|
-
"types": "./dist/node/index.d.ts"
|
|
28
|
+
"require": "./dist/node/index.cjs"
|
|
29
29
|
},
|
|
30
30
|
"./package.json": "./package.json"
|
|
31
31
|
},
|
|
32
|
-
"main": "./dist/index.cjs",
|
|
33
|
-
"types": "./dist/index.d.ts",
|
|
34
32
|
"files": [
|
|
35
33
|
"dist"
|
|
36
34
|
],
|