@develit-io/backend-sdk 5.15.2 → 5.16.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 +241 -127
- package/dist/index.d.cts +331 -18
- package/dist/index.d.mts +331 -18
- package/dist/index.d.ts +331 -18
- package/dist/index.mjs +240 -124
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { sql } from 'drizzle-orm';
|
|
2
|
-
import { timestamp, uuid } from 'drizzle-orm/pg-core';
|
|
3
|
-
import { integer, text } from 'drizzle-orm/sqlite-core';
|
|
4
|
-
import { parse } from 'comment-json';
|
|
5
|
-
import { readFileSync } from 'node:fs';
|
|
6
|
-
import { join } from '@std/path';
|
|
2
|
+
import { timestamp, uuid, pgTable, text as text$1 } from 'drizzle-orm/pg-core';
|
|
3
|
+
import { integer, text, sqliteTable } from 'drizzle-orm/sqlite-core';
|
|
7
4
|
import { toSnakeCase } from '@std/text';
|
|
8
5
|
import 'http-status-codes';
|
|
9
6
|
import * as z from 'zod/v4/core';
|
|
10
7
|
import { createError } from 'h3';
|
|
11
|
-
import { consola } from 'consola';
|
|
12
8
|
import fs from 'fs';
|
|
13
9
|
import crypto$1 from 'node:crypto';
|
|
14
10
|
import path from 'path';
|
|
11
|
+
import { parse } from 'comment-json';
|
|
15
12
|
import superjson from 'superjson';
|
|
16
13
|
|
|
17
14
|
const base = {
|
|
@@ -32,85 +29,24 @@ const basePostgres = {
|
|
|
32
29
|
}).default(sql`null`)
|
|
33
30
|
};
|
|
34
31
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
namespace: {
|
|
54
|
-
title: resourceName
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const composeQueueArguments = ({
|
|
61
|
-
resourceName,
|
|
62
|
-
deliveryDelay = 5,
|
|
63
|
-
messageRetentionPeriod = 259200
|
|
64
|
-
}) => {
|
|
65
|
-
return {
|
|
66
|
-
transform: {
|
|
67
|
-
queue: {
|
|
68
|
-
queueName: resourceName,
|
|
69
|
-
settings: {
|
|
70
|
-
deliveryDelay,
|
|
71
|
-
messageRetentionPeriod
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const composeR2Arguments = ({
|
|
79
|
-
resourceName,
|
|
80
|
-
storageClass = "Standard"
|
|
81
|
-
}) => {
|
|
82
|
-
return {
|
|
83
|
-
transform: {
|
|
84
|
-
bucket: {
|
|
85
|
-
name: resourceName,
|
|
86
|
-
jurisdiction: "eu",
|
|
87
|
-
location: "weur",
|
|
88
|
-
storageClass
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
async function loadWorkerConfig({ path }) {
|
|
95
|
-
const workerConfigFile = readFileSync(join(path, "./wrangler.jsonc"), "utf-8");
|
|
96
|
-
return parse(workerConfigFile);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const composeBindingName = ({
|
|
100
|
-
resource,
|
|
101
|
-
resourceName,
|
|
102
|
-
bindingName
|
|
103
|
-
}) => {
|
|
104
|
-
const convertedBindingName = bindingName ? toSnakeCase(bindingName) : `${toSnakeCase(resourceName)}_${resource}`;
|
|
105
|
-
return convertedBindingName.toUpperCase();
|
|
106
|
-
};
|
|
107
|
-
const composeResourceName = ({
|
|
108
|
-
project,
|
|
109
|
-
environment,
|
|
110
|
-
resourceName
|
|
111
|
-
}) => {
|
|
112
|
-
return `${project}-${resourceName}-${environment}`;
|
|
113
|
-
};
|
|
32
|
+
const bankAccount = sqliteTable("bank_account", {
|
|
33
|
+
holderName: text("holder_name").notNull(),
|
|
34
|
+
number: text("number").notNull(),
|
|
35
|
+
bankCode: text("bank_code").$type().notNull(),
|
|
36
|
+
iban: text("iban").notNull(),
|
|
37
|
+
bic: text("bic").notNull(),
|
|
38
|
+
currency: text("currency").$type(),
|
|
39
|
+
countryCode: text("country_code").notNull()
|
|
40
|
+
});
|
|
41
|
+
const bankAccountPostgres = pgTable("bank_account", {
|
|
42
|
+
holderName: text$1("holder_name").notNull(),
|
|
43
|
+
number: text$1("number").notNull(),
|
|
44
|
+
bankCode: text$1("bank_code").$type().notNull(),
|
|
45
|
+
iban: text$1("iban").notNull(),
|
|
46
|
+
bic: text$1("bic").notNull(),
|
|
47
|
+
currency: text$1("currency").$type(),
|
|
48
|
+
countryCode: text$1("country_code").notNull()
|
|
49
|
+
});
|
|
114
50
|
|
|
115
51
|
class Infrastructure {
|
|
116
52
|
project;
|
|
@@ -125,19 +61,158 @@ class Infrastructure {
|
|
|
125
61
|
this.environment = environment;
|
|
126
62
|
this.sst = sst;
|
|
127
63
|
}
|
|
64
|
+
// TODO(Pookensivee): Make tests for this util
|
|
65
|
+
composeBindingName({
|
|
66
|
+
resource,
|
|
67
|
+
resourceName,
|
|
68
|
+
bindingName
|
|
69
|
+
}) {
|
|
70
|
+
const convertedBindingName = bindingName ? toSnakeCase(bindingName) : `${toSnakeCase(resourceName)}_${resource}`;
|
|
71
|
+
return convertedBindingName.toUpperCase();
|
|
72
|
+
}
|
|
73
|
+
// TODO(Pookensivee): Make tests for this util
|
|
74
|
+
composeResourceName({ resourceName }) {
|
|
75
|
+
return `${this.project}-${resourceName}-${this.environment}`;
|
|
76
|
+
}
|
|
77
|
+
// TODO(Pookensivee): Make tests for this util
|
|
78
|
+
composeKvArguments({ resourceName }) {
|
|
79
|
+
return {
|
|
80
|
+
transform: {
|
|
81
|
+
namespace: {
|
|
82
|
+
title: resourceName
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// TODO(Pookensivee): Make tests for this util
|
|
88
|
+
composeD1Arguments({ resourceName }) {
|
|
89
|
+
return {
|
|
90
|
+
transform: {
|
|
91
|
+
database: {
|
|
92
|
+
name: resourceName,
|
|
93
|
+
primaryLocationHint: "weur"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// TODO(Pookensivee): Make tests for this util
|
|
99
|
+
composeQueueArguments({
|
|
100
|
+
resourceName,
|
|
101
|
+
deliveryDelay = 5,
|
|
102
|
+
messageRetentionPeriod = 259200
|
|
103
|
+
}) {
|
|
104
|
+
return {
|
|
105
|
+
transform: {
|
|
106
|
+
queue: {
|
|
107
|
+
queueName: resourceName,
|
|
108
|
+
settings: {
|
|
109
|
+
deliveryDelay,
|
|
110
|
+
messageRetentionPeriod
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// TODO(Pookensivee): Make tests for this util
|
|
117
|
+
composeR2Arguments({
|
|
118
|
+
resourceName,
|
|
119
|
+
storageClass = "Standard"
|
|
120
|
+
}) {
|
|
121
|
+
return {
|
|
122
|
+
transform: {
|
|
123
|
+
bucket: {
|
|
124
|
+
name: resourceName,
|
|
125
|
+
jurisdiction: "eu",
|
|
126
|
+
location: "weur",
|
|
127
|
+
storageClass
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
// TODO: Solve the circular dependency on post infrastructure deploy script
|
|
133
|
+
// TODO: Cannot assign a queue as a producer, work around: https://developers.cloudflare.com/workers/wrangler/commands/#queues-consumer-add-script-name
|
|
134
|
+
// async composeWorkerArguments({
|
|
135
|
+
// resourceName,
|
|
136
|
+
// path,
|
|
137
|
+
// bindings = [],
|
|
138
|
+
// }: {
|
|
139
|
+
// resourceName: string
|
|
140
|
+
// path: string
|
|
141
|
+
// bindings?: Input<Kv | D1 | Queue | Worker | Bucket>[]
|
|
142
|
+
// }) {
|
|
143
|
+
// const workerConfig = this.loadWorkerConfig({ path })
|
|
144
|
+
// const environmentVariables = this.extractEnvironmentVariables({ workerConfigEnv:
|
|
145
|
+
// workerConfig.env as WorkerConfigEnv
|
|
146
|
+
// })
|
|
147
|
+
// // TODO: Fix this
|
|
148
|
+
// if (
|
|
149
|
+
// 'SERVICE_CONFIG' in environmentVariables &&
|
|
150
|
+
// typeof environmentVariables.SERVICE_CONFIG === 'object'
|
|
151
|
+
// ) {
|
|
152
|
+
// environmentVariables.SERVICE_CONFIG = JSON.stringify(environmentVariables.SERVICE_CONFIG)
|
|
153
|
+
// }
|
|
154
|
+
// // TODO: Fix this
|
|
155
|
+
// if (
|
|
156
|
+
// 'EMAIL_SENDER' in environmentVariables &&
|
|
157
|
+
// typeof environmentVariables.EMAIL_SENDER === 'object'
|
|
158
|
+
// ) {
|
|
159
|
+
// environmentVariables.EMAIL_SENDER = JSON.stringify(environmentVariables.EMAIL_SENDER)
|
|
160
|
+
// }
|
|
161
|
+
// return {
|
|
162
|
+
// handler: join(path, './src/index.ts'),
|
|
163
|
+
// environment: Object.entries(environmentVariables).reduce((acc, [key, value]) => ({
|
|
164
|
+
// ...acc,
|
|
165
|
+
// [key]: String(value)
|
|
166
|
+
// }), {} as Record<string, string>),
|
|
167
|
+
// link: bindings,
|
|
168
|
+
// transform: {
|
|
169
|
+
// worker: {
|
|
170
|
+
// scriptName: this.composeResourceName({ resourceName }),
|
|
171
|
+
// compatibilityDate: '2025-06-04',
|
|
172
|
+
// compatibilityFlags: ['nodejs_compat'],
|
|
173
|
+
// observability: {
|
|
174
|
+
// enabled: true,
|
|
175
|
+
// headSamplingRate: 1,
|
|
176
|
+
// logs: {
|
|
177
|
+
// // Check whether this disables logs completely or just invocation ones
|
|
178
|
+
// enabled: false,
|
|
179
|
+
// invocationLogs: false,
|
|
180
|
+
// },
|
|
181
|
+
// },
|
|
182
|
+
// },
|
|
183
|
+
// }
|
|
184
|
+
// } satisfies Partial<WorkerArgs>
|
|
185
|
+
// }
|
|
186
|
+
// loadWorkerConfig({ path }: { path: string }) {
|
|
187
|
+
// const workerConfigFile = readFileSync(
|
|
188
|
+
// join(path, './wrangler.jsonc'),
|
|
189
|
+
// 'utf-8',
|
|
190
|
+
// )
|
|
191
|
+
// TODO: Use parse from comment-json
|
|
192
|
+
// const jsonString = workerConfigFile
|
|
193
|
+
// .replace(/\/\*[\s\S]*?\*\//g, '')
|
|
194
|
+
// .replace(/\/\/.*$/gm, '')
|
|
195
|
+
// return JSON.parse(jsonString)
|
|
196
|
+
// }
|
|
197
|
+
// extractEnvironmentVariables({
|
|
198
|
+
// workerConfigEnv,
|
|
199
|
+
// }: {
|
|
200
|
+
// workerConfigEnv: WorkerConfigEnv
|
|
201
|
+
// }) {
|
|
202
|
+
// if (typeof this.environment === 'number') {
|
|
203
|
+
// return { ...workerConfigEnv.dev.vars, ENVIRONMENT: this.environment }
|
|
204
|
+
// }
|
|
205
|
+
// return { ...workerConfigEnv[this.environment].vars }
|
|
206
|
+
// }
|
|
128
207
|
/**
|
|
129
208
|
* Creates an instance of Cloudflare KV.
|
|
130
209
|
*/
|
|
131
210
|
kv(options) {
|
|
132
211
|
const { resourceName, bindingName } = options;
|
|
133
212
|
return new this.sst.cloudflare.Kv(
|
|
134
|
-
`${composeBindingName({ resource: "kv", resourceName, bindingName })}`,
|
|
135
|
-
composeKvArguments({
|
|
136
|
-
resourceName: composeResourceName({
|
|
137
|
-
project: this.project,
|
|
138
|
-
environment: this.environment,
|
|
139
|
-
resourceName
|
|
140
|
-
})
|
|
213
|
+
`${this.composeBindingName({ resource: "kv", resourceName, bindingName })}`,
|
|
214
|
+
this.composeKvArguments({
|
|
215
|
+
resourceName: this.composeResourceName({ resourceName })
|
|
141
216
|
})
|
|
142
217
|
);
|
|
143
218
|
}
|
|
@@ -147,13 +222,9 @@ class Infrastructure {
|
|
|
147
222
|
d1(options) {
|
|
148
223
|
const { resourceName, bindingName } = options;
|
|
149
224
|
return new this.sst.cloudflare.D1(
|
|
150
|
-
`${composeBindingName({ resource: "d1", resourceName, bindingName })}`,
|
|
151
|
-
composeD1Arguments({
|
|
152
|
-
resourceName: composeResourceName({
|
|
153
|
-
project: this.project,
|
|
154
|
-
environment: this.environment,
|
|
155
|
-
resourceName
|
|
156
|
-
})
|
|
225
|
+
`${this.composeBindingName({ resource: "d1", resourceName, bindingName })}`,
|
|
226
|
+
this.composeD1Arguments({
|
|
227
|
+
resourceName: this.composeResourceName({ resourceName })
|
|
157
228
|
})
|
|
158
229
|
);
|
|
159
230
|
}
|
|
@@ -163,13 +234,9 @@ class Infrastructure {
|
|
|
163
234
|
queue(options) {
|
|
164
235
|
const { resourceName, bindingName, deliveryDelay, messageRetentionPeriod } = options;
|
|
165
236
|
return new this.sst.cloudflare.Queue(
|
|
166
|
-
`${composeBindingName({ resource: "queue", resourceName, bindingName })}`,
|
|
167
|
-
composeQueueArguments({
|
|
168
|
-
resourceName: composeResourceName({
|
|
169
|
-
project: this.project,
|
|
170
|
-
environment: this.environment,
|
|
171
|
-
resourceName
|
|
172
|
-
}),
|
|
237
|
+
`${this.composeBindingName({ resource: "queue", resourceName, bindingName })}`,
|
|
238
|
+
this.composeQueueArguments({
|
|
239
|
+
resourceName: this.composeResourceName({ resourceName }),
|
|
173
240
|
deliveryDelay,
|
|
174
241
|
messageRetentionPeriod
|
|
175
242
|
})
|
|
@@ -181,28 +248,77 @@ class Infrastructure {
|
|
|
181
248
|
r2(options) {
|
|
182
249
|
const { resourceName, bindingName, storageClass } = options;
|
|
183
250
|
return new this.sst.cloudflare.Bucket(
|
|
184
|
-
`${composeBindingName({ resource: "r2", resourceName, bindingName })}`,
|
|
185
|
-
composeR2Arguments({
|
|
186
|
-
resourceName: composeResourceName({
|
|
187
|
-
project: this.project,
|
|
188
|
-
environment: this.environment,
|
|
189
|
-
resourceName
|
|
190
|
-
}),
|
|
251
|
+
`${this.composeBindingName({ resource: "r2", resourceName, bindingName })}`,
|
|
252
|
+
this.composeR2Arguments({
|
|
253
|
+
resourceName: this.composeResourceName({ resourceName }),
|
|
191
254
|
storageClass
|
|
192
255
|
})
|
|
193
256
|
);
|
|
194
257
|
}
|
|
258
|
+
// TODO: Solve the circular dependency on post infrastructure deploy script
|
|
259
|
+
// async worker({
|
|
260
|
+
// resourceName,
|
|
261
|
+
// bindingName,
|
|
262
|
+
// path,
|
|
263
|
+
// bindings = [],
|
|
264
|
+
// }: {
|
|
265
|
+
// resourceName: string
|
|
266
|
+
// bindingName: string
|
|
267
|
+
// path: string
|
|
268
|
+
// bindings?: Input<Kv | D1 | Queue | Worker | Bucket>[]
|
|
269
|
+
// }) {
|
|
270
|
+
// return new sst.cloudflare.Worker(
|
|
271
|
+
// this.composeBindingName({
|
|
272
|
+
// resource: 'worker',
|
|
273
|
+
// bindingName,
|
|
274
|
+
// resourceName,
|
|
275
|
+
// }),
|
|
276
|
+
// await this.composeWorkerArguments({
|
|
277
|
+
// resourceName: this.composeResourceName({ resourceName }),
|
|
278
|
+
// path,
|
|
279
|
+
// bindings,
|
|
280
|
+
// }),
|
|
281
|
+
// )
|
|
282
|
+
// }
|
|
283
|
+
// async service({
|
|
284
|
+
// resourceName,
|
|
285
|
+
// bindingName,
|
|
286
|
+
// path,
|
|
287
|
+
// bindings = [],
|
|
288
|
+
// }: {
|
|
289
|
+
// resourceName: string
|
|
290
|
+
// bindingName?: string
|
|
291
|
+
// path?: string
|
|
292
|
+
// bindings?: Input<Kv | D1 | Queue | Worker | Bucket>[]
|
|
293
|
+
// }) {
|
|
294
|
+
// return this.worker({
|
|
295
|
+
// resourceName: `${this.project}-${resourceName}-service-${this.environment}`,
|
|
296
|
+
// bindingName: this.composeBindingName({
|
|
297
|
+
// resource: 'service',
|
|
298
|
+
// bindingName,
|
|
299
|
+
// resourceName,
|
|
300
|
+
// }),
|
|
301
|
+
// path: `${path ?? `./services/${resourceName}`}`,
|
|
302
|
+
// bindings,
|
|
303
|
+
// })
|
|
304
|
+
// }
|
|
305
|
+
// // TODO: Add name
|
|
306
|
+
// async orchestrator({
|
|
307
|
+
// path,
|
|
308
|
+
// bindings = [],
|
|
309
|
+
// }: {
|
|
310
|
+
// path?: string
|
|
311
|
+
// bindings?: Input<Kv | D1 | Queue | Worker | Bucket>[]
|
|
312
|
+
// }) {
|
|
313
|
+
// return this.worker({
|
|
314
|
+
// resourceName: `${this.project}-gateway-${this.environment}`,
|
|
315
|
+
// bindingName: 'GATEWAY',
|
|
316
|
+
// path: `${path ?? `./apps/gateway`}`,
|
|
317
|
+
// bindings,
|
|
318
|
+
// })
|
|
319
|
+
// }
|
|
195
320
|
}
|
|
196
321
|
|
|
197
|
-
const ENVIRONMENT = ["dev", "test", "staging", "production"];
|
|
198
|
-
|
|
199
|
-
const validateEnvironment = (environment) => {
|
|
200
|
-
if (ENVIRONMENT.includes(environment)) {
|
|
201
|
-
return environment;
|
|
202
|
-
}
|
|
203
|
-
return Number(environment);
|
|
204
|
-
};
|
|
205
|
-
|
|
206
322
|
const ibanZodSchema = new z.$ZodString({
|
|
207
323
|
type: "string",
|
|
208
324
|
checks: [
|
|
@@ -376,7 +492,7 @@ const RPCResponse = {
|
|
|
376
492
|
* @returns An `IRPCResponse<T>` with `null` data and the provided error.
|
|
377
493
|
*/
|
|
378
494
|
serviceError(error) {
|
|
379
|
-
|
|
495
|
+
console.error(error.message);
|
|
380
496
|
return {
|
|
381
497
|
status: error.status,
|
|
382
498
|
message: error.message,
|
|
@@ -830,4 +946,4 @@ function develitWorker(Worker) {
|
|
|
830
946
|
return DevelitWorker;
|
|
831
947
|
}
|
|
832
948
|
|
|
833
|
-
export { DatabaseTransaction,
|
|
949
|
+
export { DatabaseTransaction, Infrastructure, RPCResponse, action, bankAccount, bankAccountPostgres, base, basePostgres, calculateExponentialBackoff, cloudflareQueue, 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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@develit-io/backend-sdk",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.16.0",
|
|
4
4
|
"description": "Develit Backend SDK",
|
|
5
5
|
"author": "Develit.io",
|
|
6
6
|
"license": "ISC",
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
"@std/path": "npm:@jsr/std__path",
|
|
36
36
|
"@std/text": "npm:@jsr/std__text",
|
|
37
37
|
"comment-json": "^4.2.5",
|
|
38
|
-
"consola": "^3.4.2",
|
|
39
38
|
"drizzle-kit": "^0.31.4",
|
|
40
39
|
"drizzle-orm": "^0.44.5",
|
|
41
40
|
"h3": "^1.15.4",
|