@devmoods/express-extras 0.63.1 → 0.65.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.
Files changed (75) hide show
  1. package/README.md +0 -111
  2. package/dist/auth/emailAndPassword.js +2 -1
  3. package/dist/auth/google.d.ts +4 -4
  4. package/dist/auth/google.js +14 -11
  5. package/dist/auth/passwordless.js +2 -1
  6. package/dist/auth/token.js +6 -6
  7. package/dist/auth/types.d.ts +1 -1
  8. package/dist/auth.d.ts +1 -1
  9. package/dist/auth.js +17 -10
  10. package/dist/cache.d.ts +3 -0
  11. package/dist/cache.js +3 -2
  12. package/dist/channels.d.ts +9 -5
  13. package/dist/channels.js +115 -23
  14. package/dist/cli.js +1 -1
  15. package/dist/config.d.ts +66 -66
  16. package/dist/config.js +35 -36
  17. package/dist/context.d.ts +5 -2
  18. package/dist/context.js +10 -9
  19. package/dist/csrf.js +6 -6
  20. package/dist/email.d.ts +1 -1
  21. package/dist/email.js +3 -3
  22. package/dist/errors.js +1 -1
  23. package/dist/faktory.d.ts +1 -1
  24. package/dist/faktory.js +5 -15
  25. package/dist/index.d.ts +2 -2
  26. package/dist/index.js +4 -3
  27. package/dist/listen.d.ts +5 -4
  28. package/dist/listen.js +47 -45
  29. package/dist/logger.d.ts +1 -0
  30. package/dist/logger.js +11 -5
  31. package/dist/manage.d.ts +4 -6
  32. package/dist/manage.js +41 -37
  33. package/dist/middleware.d.ts +3 -0
  34. package/dist/middleware.js +24 -7
  35. package/dist/rate-limit.d.ts +1 -0
  36. package/dist/rate-limit.js +25 -3
  37. package/dist/redis.d.ts +3 -0
  38. package/dist/redis.js +25 -2
  39. package/dist/sentry.js +7 -3
  40. package/dist/setupTests.global.d.ts +2 -0
  41. package/dist/setupTests.global.js +4 -0
  42. package/dist/setupTests.js +5 -1
  43. package/dist/slack.js +2 -2
  44. package/dist/utils.d.ts +2 -14
  45. package/dist/utils.js +2 -21
  46. package/dist/validate.js +7 -4
  47. package/dist/vite/__tests__/fixtures/src/entry-server.d.ts +5 -0
  48. package/dist/vite/__tests__/fixtures/src/entry-server.js +15 -0
  49. package/dist/vite/defineViteConfig.js +11 -1
  50. package/dist/vite/index.d.ts +1 -0
  51. package/dist/vite/index.js +3 -0
  52. package/dist/vite/ssr.d.ts +1 -0
  53. package/dist/vite/ssr.js +9 -4
  54. package/dist/vitest/index.d.ts +6 -0
  55. package/dist/vitest/index.js +25 -0
  56. package/dist/vitest/matchers.d.ts +11 -0
  57. package/dist/vitest/matchers.js +65 -0
  58. package/dist/vitest/testClient.d.ts +30 -0
  59. package/dist/vitest/testClient.js +98 -0
  60. package/dist/webapi.js +4 -0
  61. package/package.json +38 -43
  62. package/dist/__tests__/__migrations__/1_a.d.ts +0 -3
  63. package/dist/__tests__/__migrations__/1_a.js +0 -9
  64. package/dist/migrate.d.ts +0 -47
  65. package/dist/migrate.js +0 -255
  66. package/dist/postgres-ts-generator.d.ts +0 -11
  67. package/dist/postgres-ts-generator.js +0 -114
  68. package/dist/postgres.d.ts +0 -71
  69. package/dist/postgres.js +0 -261
  70. package/dist/sql.d.ts +0 -33
  71. package/dist/sql.js +0 -135
  72. package/dist/vite/vitest.d.ts +0 -1
  73. package/dist/vite/vitest.js +0 -1
  74. package/dist/vite.d.ts +0 -1
  75. package/dist/vite.js +0 -1
package/dist/postgres.js DELETED
@@ -1,261 +0,0 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
- import {} from 'express';
3
- import pg, {} from 'pg';
4
- import { config } from './config.js';
5
- import { formatSql } from './sql.js';
6
- import { startTimer } from './utils.js';
7
- export const escapePostgresIdentifier = pg.escapeIdentifier;
8
- export const escapePostgresLiteral = pg.escapeLiteral;
9
- export class DoesNotExistError extends Error {
10
- constructor(message) {
11
- super(message);
12
- this.name = 'DoesNotExistError';
13
- }
14
- }
15
- export class MultipleRowsReturnedError extends Error {
16
- constructor(message) {
17
- super(message);
18
- this.name = 'MultipleRowsReturnedError';
19
- }
20
- }
21
- export class IntegrityError extends Error {
22
- constructor(message) {
23
- super(message);
24
- this.name = 'IntegrityError';
25
- }
26
- }
27
- export const transactionStorage = new AsyncLocalStorage();
28
- export class Connection {
29
- client;
30
- options;
31
- constructor(client, options) {
32
- this.client = client;
33
- this.options = options;
34
- }
35
- logStatement(text, values, duration) {
36
- if (this.options.onLogStatement) {
37
- return this.options.onLogStatement(text, values, duration);
38
- }
39
- if (config.value.LOG_SQL) {
40
- console.log(`\n${formatSql(text)}\n\nValues: ${JSON.stringify(values)}\nDuration: ${duration}ms\n`);
41
- }
42
- }
43
- async query(text, values) {
44
- const timer = startTimer();
45
- try {
46
- return await this.client.query(text, values);
47
- }
48
- catch (error) {
49
- if (error.code && error.code === '23505') {
50
- throw new IntegrityError(`${error.message}: ${error.detail}`);
51
- }
52
- throw error;
53
- }
54
- finally {
55
- this.logStatement?.(text, values, timer.getDuration());
56
- }
57
- }
58
- async all(text, values) {
59
- const result = await this.query(text, values);
60
- if (result.rows) {
61
- return result.rows;
62
- /* c8 ignore next 2*/
63
- }
64
- return [];
65
- }
66
- async insert(text, values) {
67
- return await this.query(text, values);
68
- }
69
- async first(text, values) {
70
- const result = await this.query(text, values);
71
- return result.rows && result.rows.length ? result.rows[0] : null;
72
- }
73
- async get(text, values) {
74
- const result = await this.query(text, values);
75
- if (result.rowCount === 0) {
76
- throw new DoesNotExistError('Row matching query not found');
77
- }
78
- if (result.rowCount > 1) {
79
- throw new MultipleRowsReturnedError(`Call to postgres.get() returned ${result.rowCount} rows. Expected 1.`);
80
- }
81
- return result.rows[0];
82
- }
83
- }
84
- export class Transaction extends Connection {
85
- savepoints = [];
86
- isCommitted = false;
87
- isRolledBack = false;
88
- async createSavepoint() {
89
- const savepointName = `sp_${this.savepoints.length + 1}`;
90
- await this.query(`SAVEPOINT ${savepointName}`);
91
- this.savepoints.push(savepointName);
92
- return savepointName;
93
- }
94
- async rollbackToSavepoint(savepointName) {
95
- await this.query(`ROLLBACK TO SAVEPOINT ${savepointName}`);
96
- }
97
- async releaseSavepoint(savepointName) {
98
- await this.query(`RELEASE SAVEPOINT ${savepointName}`);
99
- const index = this.savepoints.indexOf(savepointName);
100
- if (index !== -1) {
101
- this.savepoints.splice(index, 1);
102
- }
103
- }
104
- }
105
- /**
106
- * Create a new Postgres connection pool with
107
- * conveience methods for executing queries using
108
- * one of the available connections.
109
- *
110
- * @param config
111
- */
112
- export function createPostgres({ onError, url = config.value.POSTGRES_URL, ...postgresOptions } = {}) {
113
- const pool = new pg.Pool({ connectionString: url, ...postgresOptions });
114
- pool.on('error', (e, client) => {
115
- onError?.(e, client);
116
- });
117
- async function withConnection(callback) {
118
- // Check if we're already in a transaction context
119
- const txContext = transactionStorage.getStore();
120
- if (txContext) {
121
- return callback(txContext.connection);
122
- }
123
- const client = await pool.connect();
124
- try {
125
- const result = await callback(new Connection(client, {
126
- onLogStatement: postgresOptions.onLogStatement,
127
- }));
128
- return result;
129
- }
130
- finally {
131
- client.release();
132
- }
133
- }
134
- /**
135
- * Wipe the database schema
136
- */
137
- async function clear(force = false) {
138
- if (!force) {
139
- return;
140
- }
141
- return withConnection(async (connection) => {
142
- await connection.query('DROP SCHEMA public CASCADE');
143
- await connection.query(`CREATE SCHEMA public`);
144
- });
145
- }
146
- /**
147
- * Run queries inside a BEGIN, COMMIT / ROLLBACK transaction.
148
- * Supports nested transactions using SAVEPOINT.
149
- */
150
- async function transaction(callback) {
151
- // Check if we're already in a transaction context
152
- const existingTxContext = transactionStorage.getStore();
153
- if (existingTxContext) {
154
- // We're already in a transaction, create a savepoint for nested transaction
155
- const txConnection = existingTxContext.connection;
156
- const savepointName = await txConnection.createSavepoint();
157
- try {
158
- const result = await callback();
159
- await txConnection.releaseSavepoint(savepointName);
160
- return result;
161
- }
162
- catch (error) {
163
- await txConnection.rollbackToSavepoint(savepointName);
164
- throw error;
165
- }
166
- }
167
- // Start a new top-level transaction
168
- const client = await pool.connect();
169
- const txConnection = new Transaction(client, {
170
- onLogStatement: postgresOptions.onLogStatement,
171
- });
172
- try {
173
- await txConnection.query('BEGIN');
174
- // Create transaction context
175
- const txContext = {
176
- connection: txConnection,
177
- savepoints: [],
178
- isCommitted: false,
179
- isRolledBack: false,
180
- };
181
- // Run the callback within the transaction context
182
- const result = await transactionStorage.run(txContext, async () => {
183
- return await callback();
184
- });
185
- await txConnection.query('COMMIT');
186
- txContext.isCommitted = true;
187
- return result;
188
- }
189
- catch (error) {
190
- await txConnection.query('ROLLBACK');
191
- throw error;
192
- }
193
- finally {
194
- client.release();
195
- }
196
- }
197
- const query = async (...args) => {
198
- return await withConnection(async (connection) => {
199
- return connection.query(...args);
200
- });
201
- };
202
- const insert = async (...args) => {
203
- return await withConnection(async (connection) => {
204
- return connection.insert(...args);
205
- });
206
- };
207
- const first = async (...args) => {
208
- return await withConnection(async (connection) => {
209
- return connection.first(...args);
210
- });
211
- };
212
- const all = async (...args) => {
213
- return await withConnection(async (connection) => {
214
- return connection.all(...args);
215
- });
216
- };
217
- const get = async (...args) => {
218
- return await withConnection(async (connection) => {
219
- return connection.get(...args);
220
- });
221
- };
222
- /**
223
- * Get the current transaction from the AsyncLocalStorage context.
224
- * Returns null if not in a transaction.
225
- */
226
- function getCurrentTransaction() {
227
- const txContext = transactionStorage.getStore();
228
- return txContext ? txContext.connection : null;
229
- }
230
- return {
231
- insert,
232
- query,
233
- first,
234
- all,
235
- get,
236
- withConnection,
237
- transaction,
238
- getCurrentTransaction,
239
- instance: pool,
240
- clear,
241
- end() {
242
- return pool.end();
243
- },
244
- };
245
- }
246
- export function withTransaction(postgres) {
247
- return async function withTransactionMiddleware(req, res, next) {
248
- try {
249
- await postgres.transaction(async () => {
250
- await new Promise((resolve, reject) => {
251
- res.on('finish', resolve);
252
- res.on('error', reject);
253
- next();
254
- });
255
- });
256
- }
257
- catch (error) {
258
- next(error);
259
- }
260
- };
261
- }
package/dist/sql.d.ts DELETED
@@ -1,33 +0,0 @@
1
- export declare class SqlStatement {
2
- stringsAndValues: [string, unknown | undefined][];
3
- constructor(strings: string[], values: unknown[]);
4
- _extract(strings: string[], values: unknown[]): void;
5
- append(statement: SqlStatement | string): this;
6
- get values(): ({} | null)[];
7
- get text(): string;
8
- }
9
- export declare const sql: {
10
- (strings: TemplateStringsArray, ...values: unknown[]): SqlStatement;
11
- identifier(name: string): {
12
- name: string;
13
- raw: boolean;
14
- };
15
- raw(name: string): {
16
- name: string;
17
- raw: boolean;
18
- };
19
- literal(name: string | null): {
20
- name: string;
21
- raw: boolean;
22
- };
23
- spreadUpdate: typeof spreadUpdate;
24
- spreadInsert: typeof spreadInsert;
25
- };
26
- declare function spreadUpdate(data: Record<string, any>): SqlStatement;
27
- declare function spreadInsert<TData extends Record<string, any>>(data: TData | TData[]): SqlStatement;
28
- export declare function joinStatements(statements: SqlStatement[], separator: string): SqlStatement;
29
- /**
30
- * Returns a pretty-printed SQL statement.
31
- */
32
- export declare const formatSql: (statement: SqlStatement | string) => string;
33
- export {};
package/dist/sql.js DELETED
@@ -1,135 +0,0 @@
1
- import { format } from 'sql-formatter';
2
- import { escapePostgresIdentifier, escapePostgresLiteral } from './postgres.js';
3
- const isRaw = (value) => {
4
- return (typeof value === 'object' && value !== null && 'raw' in value && !!value.raw);
5
- };
6
- export class SqlStatement {
7
- stringsAndValues;
8
- constructor(strings, values) {
9
- this.stringsAndValues = [];
10
- this._extract(strings, values);
11
- }
12
- _extract(strings, values) {
13
- for (let i = 0; i < strings.length; i++) {
14
- const string = strings[i];
15
- const value = values[i];
16
- if (isRaw(value)) {
17
- this.stringsAndValues.push([string + value.name, undefined]);
18
- }
19
- else if (value instanceof SqlStatement) {
20
- this.stringsAndValues.push([string, undefined]);
21
- this.stringsAndValues.push(...value.stringsAndValues);
22
- }
23
- else {
24
- this.stringsAndValues.push([string, value]);
25
- }
26
- }
27
- }
28
- append(statement) {
29
- if (statement instanceof SqlStatement) {
30
- this.stringsAndValues.push(...statement.stringsAndValues);
31
- }
32
- else {
33
- this.stringsAndValues.push([statement, undefined]);
34
- }
35
- return this;
36
- }
37
- get values() {
38
- return this.stringsAndValues
39
- .map(([, value]) => value)
40
- .filter((x) => x !== undefined);
41
- }
42
- get text() {
43
- let i = 1;
44
- let result = '';
45
- for (const [string, value] of this.stringsAndValues) {
46
- if (value == null) {
47
- result += string;
48
- }
49
- else {
50
- result += `${string}$${i}`;
51
- i += 1;
52
- }
53
- }
54
- return result;
55
- }
56
- }
57
- export const sql = (strings, ...values) => {
58
- return new SqlStatement(Array.from(strings), values);
59
- };
60
- function spreadUpdate(data) {
61
- const entries = Object.entries(data);
62
- if (entries.length === 0) {
63
- return sql ``;
64
- }
65
- return joinStatements(entries
66
- .filter(([, value]) => typeof value !== 'undefined')
67
- .map(([key, value]) => sql `${sql.identifier(key)} = ${value == null ? sql.raw('NULL') : value}`), ', ');
68
- }
69
- function spreadInsert(data) {
70
- const rows = Array.isArray(data) ? data : [data];
71
- if (rows.length === 0) {
72
- return sql ``;
73
- }
74
- const statement = sql `(`;
75
- statement.append(Object.keys(rows[0])
76
- .map((k) => escapePostgresIdentifier(k))
77
- .join(', '));
78
- statement.append(sql `) VALUES `);
79
- const toInsert = [];
80
- for (const row of rows) {
81
- const values = Object.values(row);
82
- if (values.length === 0) {
83
- continue;
84
- }
85
- toInsert.push(sql `(${joinStatements(values.map((value) => sql `${value ?? sql.raw('NULL')}`), ', ')})`);
86
- }
87
- statement.append(joinStatements(toInsert, ', '));
88
- return statement;
89
- }
90
- /**
91
- * Inject an identifier into the template string
92
- */
93
- sql.identifier = (name) => {
94
- return { name: escapePostgresIdentifier(name), raw: true };
95
- };
96
- /**
97
- * Inject a raw value into the template string
98
- */
99
- sql.raw = (name) => {
100
- return { name, raw: true };
101
- };
102
- /**
103
- * Inject a literal value into the template string
104
- */
105
- sql.literal = (name) => {
106
- if (name === null) {
107
- return { name: 'NULL', raw: true };
108
- }
109
- return { name: escapePostgresLiteral(name), raw: true };
110
- };
111
- sql.spreadUpdate = spreadUpdate;
112
- sql.spreadInsert = spreadInsert;
113
- export function joinStatements(statements, separator) {
114
- let i = 0;
115
- const statement = sql ``;
116
- for (const o of statements) {
117
- if (i++ !== 0) {
118
- statement.append(separator);
119
- }
120
- statement.append(o);
121
- }
122
- return statement;
123
- }
124
- /**
125
- * Returns a pretty-printed SQL statement.
126
- */
127
- export const formatSql = (statement) => {
128
- const value = statement instanceof SqlStatement ? statement.text : statement;
129
- return format(value, {
130
- language: 'postgresql',
131
- keywordCase: 'upper',
132
- tabWidth: 2,
133
- useTabs: false,
134
- });
135
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
package/dist/vite.d.ts DELETED
@@ -1 +0,0 @@
1
- export { defineViteConfig } from './vite/defineViteConfig.js';
package/dist/vite.js DELETED
@@ -1 +0,0 @@
1
- export { defineViteConfig } from './vite/defineViteConfig.js';