ztechno_core 0.0.105 → 0.0.108

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 (63) hide show
  1. package/lib/core/crypto_service.d.ts +23 -0
  2. package/lib/core/crypto_service.js +100 -0
  3. package/lib/core/engine_base.d.ts +11 -0
  4. package/lib/core/engine_base.js +9 -0
  5. package/lib/core/index.d.ts +14 -0
  6. package/lib/core/index.js +101 -0
  7. package/lib/core/mail_service.d.ts +85 -0
  8. package/lib/core/mail_service.js +156 -0
  9. package/lib/core/orm/mail_blacklist_orm.d.ts +21 -0
  10. package/lib/core/orm/mail_blacklist_orm.js +79 -0
  11. package/lib/core/orm/orm.d.ts +27 -0
  12. package/lib/core/orm/orm.js +67 -0
  13. package/lib/core/sql_service.d.ts +195 -0
  14. package/lib/core/sql_service.js +333 -0
  15. package/lib/core/translate_service.d.ts +48 -0
  16. package/lib/core/translate_service.js +913 -0
  17. package/lib/core/types/crypto_types.d.ts +4 -0
  18. package/lib/core/types/crypto_types.js +2 -0
  19. package/lib/core/types/mail_types.d.ts +95 -0
  20. package/lib/core/types/mail_types.js +2 -0
  21. package/lib/core/types/site_config.d.ts +46 -0
  22. package/lib/core/types/site_config.js +2 -0
  23. package/lib/core/types/translate_types.d.ts +69 -0
  24. package/lib/core/types/translate_types.js +46 -0
  25. package/lib/core/types/user_types.d.ts +41 -0
  26. package/lib/core/types/user_types.js +2 -0
  27. package/lib/core/user_service.d.ts +87 -0
  28. package/lib/core/user_service.js +216 -0
  29. package/lib/express/index.d.ts +1 -0
  30. package/lib/express/index.js +18 -0
  31. package/lib/index.d.ts +17 -8
  32. package/lib/index.js +60 -16
  33. package/lib/mollie/index.d.ts +5 -0
  34. package/lib/mollie/index.js +62 -0
  35. package/lib/mollie/orm/customers_orm.d.ts +16 -0
  36. package/lib/mollie/orm/customers_orm.js +115 -0
  37. package/lib/mollie/orm/invoice_items_orm.d.ts +9 -0
  38. package/lib/mollie/orm/invoice_items_orm.js +71 -0
  39. package/lib/mollie/orm/invoice_payments_orm.d.ts +10 -0
  40. package/lib/mollie/orm/invoice_payments_orm.js +70 -0
  41. package/lib/mollie/orm/invoices_orm.d.ts +40 -0
  42. package/lib/mollie/orm/invoices_orm.js +172 -0
  43. package/lib/mollie/orm/subscription_items_orm.d.ts +9 -0
  44. package/lib/mollie/orm/subscription_items_orm.js +45 -0
  45. package/lib/mollie/orm/subscriptions_orm.d.ts +17 -0
  46. package/lib/mollie/orm/subscriptions_orm.js +122 -0
  47. package/lib/mollie/services/customer_service.d.ts +14 -0
  48. package/lib/mollie/services/customer_service.js +53 -0
  49. package/lib/mollie/services/invoice_service.d.ts +102 -0
  50. package/lib/mollie/services/invoice_service.js +866 -0
  51. package/lib/mollie/services/mollie_service.d.ts +42 -0
  52. package/lib/mollie/services/mollie_service.js +370 -0
  53. package/lib/mollie/services/subscription_service.d.ts +32 -0
  54. package/lib/mollie/services/subscription_service.js +134 -0
  55. package/lib/mollie/types/internal_types.d.ts +19 -0
  56. package/lib/mollie/types/internal_types.js +2 -0
  57. package/lib/mollie/types/mollie_types.d.ts +187 -0
  58. package/lib/mollie/types/mollie_types.js +3 -0
  59. package/lib/mollie/util/subscription_utils.d.ts +8 -0
  60. package/lib/mollie/util/subscription_utils.js +37 -0
  61. package/lib/schema/MySQLSchemaExtractor.d.ts +1 -1
  62. package/lib/schema/MySQLSchemaImporter.d.ts +1 -1
  63. package/package.json +3 -1
@@ -0,0 +1,4 @@
1
+ export type HashStruct = {
2
+ iv: string;
3
+ encryptedData: string;
4
+ };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,95 @@
1
+ import SMTPTransport from 'nodemailer/lib/smtp-transport';
2
+ import { Attachment } from 'nodemailer/lib/mailer';
3
+ import { ZSQLService } from '../sql_service';
4
+ export type MailAttachment = Attachment;
5
+ interface OptionalOptions {
6
+ cacheDir?:
7
+ | string
8
+ | false
9
+ | undefined /** optional location for cached messages. If not set then caching is not used. */;
10
+ cacheTreshold?:
11
+ | number
12
+ | undefined /** optional size in bytes, if message is larger than this treshold it gets cached to disk (assuming cacheDir is set and writable). Defaults to 131072 (128 kB). */;
13
+ hashAlgo?: string | undefined /** optional algorithm for the body hash, defaults to ‘sha256’ */;
14
+ headerFieldNames?:
15
+ | string
16
+ | undefined /** an optional colon separated list of header keys to sign (eg. message-id:date:from:to...') */;
17
+ skipFields?:
18
+ | string
19
+ | undefined /** optional colon separated list of header keys not to sign. This is useful if you want to sign all the relevant keys but your provider changes some values, ie Message-ID and Date. In this case you should use 'message-id:date' to prevent signing these values. */;
20
+ }
21
+ interface SingleDKIMKeyOptions extends OptionalOptions {
22
+ domainName: string /** is the domain name to use in the signature */;
23
+ keySelector: string /** is the DKIM key selector */;
24
+ privateKey:
25
+ | string
26
+ | {
27
+ key: string;
28
+ passphrase: string;
29
+ } /** is the private key for the selector in PEM format */;
30
+ }
31
+ export type MailResponse = SMTPTransport.SentMessageInfo;
32
+ export type MailServiceOptions = {
33
+ auth: {
34
+ user: string;
35
+ pass: string;
36
+ };
37
+ mailSender: string;
38
+ dkim?: SingleDKIMKeyOptions;
39
+ sqlService: ZSQLService;
40
+ hashSalt?: string;
41
+ dirTemplate?: string;
42
+ baseInject?: {
43
+ logoSrc?: string;
44
+ baseUrl?: string;
45
+ title: string;
46
+ content: string;
47
+ };
48
+ };
49
+ export type MailOptionsBase = {
50
+ recipient: string;
51
+ subject: string;
52
+ from?: string;
53
+ priority?: 'high' | 'normal' | 'low';
54
+ dkim?: SingleDKIMKeyOptions;
55
+ attachments?: MailAttachment[];
56
+ };
57
+ export type MailOptionsText = MailOptionsBase & {
58
+ body: string;
59
+ };
60
+ export type MailOptionsHtml = MailOptionsBase & {
61
+ html: string;
62
+ };
63
+ export type MailOptions = MailOptionsBase & {
64
+ body?: string;
65
+ html?: string;
66
+ };
67
+ export type ZMailSendOptTemplate = MailOptionsBase & {
68
+ template: 'C:/example/template.html' | string;
69
+ inject: {
70
+ title: string;
71
+ content: string;
72
+ } & {
73
+ [key: string]: string | number;
74
+ };
75
+ };
76
+ export type ZMailSendOptAll = MailOptionsBase & {
77
+ body: string;
78
+ html: string;
79
+ template: 'C:/example/template.html' | string;
80
+ inject: {
81
+ title: string;
82
+ content: string;
83
+ } & {
84
+ [key: string]: string | number;
85
+ };
86
+ };
87
+ export type ZMailBlacklist = {
88
+ email: string;
89
+ hash?: string;
90
+ is_blacklisted: 0 | 1 | boolean;
91
+ updated_at: string;
92
+ created_at: string;
93
+ };
94
+ export type ZMailBlacklistSearch = Pick<ZMailBlacklist, 'email' | 'is_blacklisted'>;
95
+ export {};
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,46 @@
1
+ export type Coordinates = [number, number];
2
+ export type CompanyInfo = {
3
+ baseUrl: string;
4
+ website: string;
5
+ company: string;
6
+ copyright: string;
7
+ kvk: string;
8
+ kvkUrl: string;
9
+ btwNr: string;
10
+ iban: string;
11
+ bankName: string;
12
+ };
13
+ export type Address = {
14
+ street: string;
15
+ city: string;
16
+ zipcode: string;
17
+ addressRegion: string;
18
+ country: string;
19
+ latLng: Coordinates;
20
+ };
21
+ export type ContactInfo = {
22
+ phone: string;
23
+ contact: string;
24
+ contactQuote: string;
25
+ mapsUrl: string;
26
+ whatsappUrl: string;
27
+ };
28
+ export type SeoDefaults = {
29
+ keywords: string;
30
+ description: string;
31
+ ogLocale: string;
32
+ };
33
+ export type RenderContext = {
34
+ startTimeStamp: number;
35
+ path: string;
36
+ ejsPath: string;
37
+ isProd: boolean;
38
+ projectsHtml: string;
39
+ };
40
+ export type RenderData = {
41
+ company: CompanyInfo;
42
+ address: Address;
43
+ contact: ContactInfo;
44
+ seo: SeoDefaults;
45
+ context: RenderContext;
46
+ };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,69 @@
1
+ import { ZSQLService } from '../sql_service';
2
+ export type Dom = {} & Node;
3
+ export type Node = {
4
+ getAttribute: (attr: string) => string;
5
+ getElementsByTagName: (tag: string) => Node[];
6
+ getElementsByClassName: (cls: string) => Node[];
7
+ getElementById: (id: string) => Node;
8
+ getElementsByName: (name: string) => Node[];
9
+ nodeType: string;
10
+ nodeName: string;
11
+ childNodes: Node[];
12
+ firstChild: Node;
13
+ lastChild: Node;
14
+ parentNode: Node;
15
+ attributes: any[];
16
+ innerHTML: string;
17
+ outerHTML: string;
18
+ textContent: string;
19
+ text: string;
20
+ };
21
+ export type ZNodeText = {
22
+ text: string;
23
+ } & Node;
24
+ export type TranslateData = {
25
+ value: string;
26
+ meta?: {
27
+ prefix: string;
28
+ suffix: string;
29
+ };
30
+ };
31
+ export type dbTranslationRow = {
32
+ lang: string;
33
+ key: string;
34
+ value: string;
35
+ };
36
+ export type ATranslateLang = {
37
+ lang: string;
38
+ text: string;
39
+ };
40
+ export type TranslateServiceOptions = {
41
+ sqlService: ZSQLService;
42
+ googleApiKey: string;
43
+ languages?: ATranslateLang[];
44
+ defaultLang?: string;
45
+ sourceLang?: string;
46
+ surpressErrors?: boolean;
47
+ log?: (data: any, context: any) => any;
48
+ verbose?: (data: any, context: any) => any;
49
+ maxRetries?: number;
50
+ retryDelay?: number;
51
+ fallbackText?: string;
52
+ };
53
+ export declare class TranslateError extends Error {
54
+ code: string;
55
+ context?: any;
56
+ constructor(message: string, code: string, context?: any);
57
+ }
58
+ export declare class HtmlEntityError extends TranslateError {
59
+ constructor(code: string, text: string);
60
+ }
61
+ export declare class ApiTranslationError extends TranslateError {
62
+ constructor(originalError: any, text: string, lang: string);
63
+ }
64
+ export declare class DatabaseError extends TranslateError {
65
+ constructor(originalError: any, operation: string);
66
+ }
67
+ export declare class ValidationError extends TranslateError {
68
+ constructor(field: string, value: any);
69
+ }
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.ValidationError =
4
+ exports.DatabaseError =
5
+ exports.ApiTranslationError =
6
+ exports.HtmlEntityError =
7
+ exports.TranslateError =
8
+ void 0;
9
+ // Custom error types for better error handling
10
+ class TranslateError extends Error {
11
+ constructor(message, code, context) {
12
+ super(message);
13
+ this.code = code;
14
+ this.context = context;
15
+ this.name = 'TranslateError';
16
+ }
17
+ }
18
+ exports.TranslateError = TranslateError;
19
+ class HtmlEntityError extends TranslateError {
20
+ constructor(code, text) {
21
+ super(`Cannot recognize character code="${code}" for text="${text}"`, 'HTML_ENTITY_ERROR', { code, text });
22
+ }
23
+ }
24
+ exports.HtmlEntityError = HtmlEntityError;
25
+ class ApiTranslationError extends TranslateError {
26
+ constructor(originalError, text, lang) {
27
+ super(`Translation API failed for text="${text}" to language="${lang}"`, 'API_TRANSLATION_ERROR', {
28
+ originalError,
29
+ text,
30
+ lang,
31
+ });
32
+ }
33
+ }
34
+ exports.ApiTranslationError = ApiTranslationError;
35
+ class DatabaseError extends TranslateError {
36
+ constructor(originalError, operation) {
37
+ super(`Database operation failed: ${operation}`, 'DATABASE_ERROR', { originalError, operation });
38
+ }
39
+ }
40
+ exports.DatabaseError = DatabaseError;
41
+ class ValidationError extends TranslateError {
42
+ constructor(field, value) {
43
+ super(`Invalid ${field}: ${value}`, 'VALIDATION_ERROR', { field, value });
44
+ }
45
+ }
46
+ exports.ValidationError = ValidationError;
@@ -0,0 +1,41 @@
1
+ export type ZUserCore = {
2
+ user_id: number;
3
+ email: string;
4
+ session: string;
5
+ role: string | null;
6
+ admin: 0 | 1;
7
+ updated_at: any;
8
+ created_at: any;
9
+ };
10
+ export type ZUser = ZUserCore;
11
+ export interface ZUserExtended extends ZUserCore {
12
+ username?: string;
13
+ first_name?: string;
14
+ last_name?: string;
15
+ avatar_url?: string;
16
+ bio?: string;
17
+ is_active?: 0 | 1;
18
+ email_verified?: 0 | 1;
19
+ last_login?: any;
20
+ }
21
+ export type ZRequiredUserColumns = {
22
+ email: string;
23
+ role: string | null;
24
+ pass: string;
25
+ admin: 0 | 1;
26
+ };
27
+ export type ZRequiredUserColumnsExtended<T = {}> = ZRequiredUserColumns & Partial<T>;
28
+ export type ZUserCredentials = {
29
+ email: string;
30
+ pass: string;
31
+ };
32
+ export type ZUserSession = {
33
+ session: string;
34
+ };
35
+ export type ZUserTableConfig = {
36
+ tableName?: string;
37
+ customColumns?: {
38
+ [columnName: string]: string;
39
+ };
40
+ customIndexes?: string[];
41
+ };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,87 @@
1
+ import { ZSQLService } from './sql_service';
2
+ import {
3
+ ZRequiredUserColumns,
4
+ ZUser,
5
+ ZUserCore,
6
+ ZUserSession,
7
+ ZUserCredentials,
8
+ ZUserTableConfig,
9
+ } from './types/user_types';
10
+ /**
11
+ * Generic User Service that can be extended with custom user fields
12
+ * @template TUser - The user type (defaults to ZUser for backward compatibility)
13
+ * @template TUserCreate - The user creation type (defaults to ZRequiredUserColumns)
14
+ */
15
+ export declare class ZUserService<
16
+ TUser extends ZUserCore = ZUser,
17
+ TUserCreate extends ZRequiredUserColumns = ZRequiredUserColumns,
18
+ > {
19
+ protected tableName: string;
20
+ protected sqlService: ZSQLService;
21
+ protected tableConfig: ZUserTableConfig;
22
+ private salt;
23
+ /**
24
+ * Creates a new ZUserService instance
25
+ * @param options - Configuration options including SQL service and optional table configuration
26
+ */
27
+ constructor({ sqlService, tableConfig }: { sqlService: ZSQLService; tableConfig?: ZUserTableConfig });
28
+ /**
29
+ * Gets the base table columns definition
30
+ * @returns SQL column definitions for core user fields
31
+ * @protected
32
+ */
33
+ protected getBaseTableColumns(): string;
34
+ /**
35
+ * Gets custom table columns if defined
36
+ * @returns SQL column definitions for custom fields
37
+ * @protected
38
+ */
39
+ protected getCustomTableColumns(): string;
40
+ /**
41
+ * Gets base table indexes
42
+ * @returns SQL index definitions for core fields
43
+ * @protected
44
+ */
45
+ protected getBaseTableIndexes(): string;
46
+ /**
47
+ * Gets custom table indexes if defined
48
+ * @returns SQL index definitions for custom fields
49
+ * @protected
50
+ */
51
+ protected getCustomTableIndexes(): string;
52
+ protected checkTableExists(): Promise<boolean>;
53
+ checkTableHasAdmin(): Promise<boolean>;
54
+ protected createTable(): Promise<void>;
55
+ ensureTableExists(): Promise<void>;
56
+ /**
57
+ * Registers a new user with extensible data
58
+ * @param userData - User data including core fields and any custom fields
59
+ * @returns Promise resolving to session information
60
+ */
61
+ register(userData: TUserCreate): Promise<{
62
+ session: string;
63
+ }>;
64
+ /**
65
+ * Gets all available columns for SELECT queries
66
+ * @returns Comma-separated list of column names
67
+ * @protected
68
+ */
69
+ protected getSelectColumns(): string;
70
+ find(
71
+ opt:
72
+ | {
73
+ email: string;
74
+ }
75
+ | {
76
+ user_id: number;
77
+ },
78
+ ): Promise<TUser | undefined>;
79
+ auth(opt: ZUserSession | ZUserCredentials): Promise<{
80
+ user?: TUser;
81
+ session?: string;
82
+ authenticated: boolean;
83
+ }>;
84
+ fetch(opt?: { limit?: number }): Promise<TUser[]>;
85
+ private genSession;
86
+ private hashPass;
87
+ }
@@ -0,0 +1,216 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.ZUserService = void 0;
4
+ const crypto_service_1 = require('./crypto_service');
5
+ /**
6
+ * Generic User Service that can be extended with custom user fields
7
+ * @template TUser - The user type (defaults to ZUser for backward compatibility)
8
+ * @template TUserCreate - The user creation type (defaults to ZRequiredUserColumns)
9
+ */
10
+ class ZUserService {
11
+ /**
12
+ * Creates a new ZUserService instance
13
+ * @param options - Configuration options including SQL service and optional table configuration
14
+ */
15
+ constructor({ sqlService, tableConfig }) {
16
+ this.sqlService = sqlService;
17
+ this.tableConfig = tableConfig || {};
18
+ this.tableName = this.tableConfig.tableName || 'users';
19
+ this.salt = sqlService.database;
20
+ }
21
+ /**
22
+ * Gets the base table columns definition
23
+ * @returns SQL column definitions for core user fields
24
+ * @protected
25
+ */
26
+ getBaseTableColumns() {
27
+ return `
28
+ \`user_id\` int(10) unsigned NOT NULL AUTO_INCREMENT,
29
+ \`email\` varchar(255) NOT NULL,
30
+ \`role\` varchar(64) DEFAULT NULL,
31
+ \`pass\` varchar(512) NOT NULL,
32
+ \`session\` varchar(512) NOT NULL,
33
+ \`admin\` tinyint(1) NOT NULL DEFAULT 0,
34
+ \`updated_at\` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
35
+ \`created_at\` datetime NOT NULL DEFAULT current_timestamp()
36
+ `;
37
+ }
38
+ /**
39
+ * Gets custom table columns if defined
40
+ * @returns SQL column definitions for custom fields
41
+ * @protected
42
+ */
43
+ getCustomTableColumns() {
44
+ if (!this.tableConfig.customColumns) return '';
45
+ return Object.entries(this.tableConfig.customColumns)
46
+ .map(([columnName, definition]) => `\`${columnName}\` ${definition}`)
47
+ .join(',\n ');
48
+ }
49
+ /**
50
+ * Gets base table indexes
51
+ * @returns SQL index definitions for core fields
52
+ * @protected
53
+ */
54
+ getBaseTableIndexes() {
55
+ return `
56
+ PRIMARY KEY (\`user_id\`),
57
+ UNIQUE KEY \`email_UNIQUE\` (\`email\`),
58
+ KEY \`email\` (\`email\`),
59
+ KEY \`role\` (\`role\`),
60
+ KEY \`admin\` (\`admin\`),
61
+ KEY \`created_at\` (\`created_at\`),
62
+ KEY \`updated_at\` (\`updated_at\`),
63
+ KEY \`session\` (\`session\`)
64
+ `;
65
+ }
66
+ /**
67
+ * Gets custom table indexes if defined
68
+ * @returns SQL index definitions for custom fields
69
+ * @protected
70
+ */
71
+ getCustomTableIndexes() {
72
+ if (!this.tableConfig.customIndexes) return '';
73
+ return this.tableConfig.customIndexes
74
+ .map((index) => index.trim())
75
+ .filter((index) => index.length > 0)
76
+ .join(',\n ');
77
+ }
78
+ async checkTableExists() {
79
+ const res = await this.sqlService.query(`
80
+ SELECT ENGINE, VERSION, CREATE_TIME FROM information_schema.tables
81
+ WHERE table_schema = '${this.sqlService.database}' AND table_name = '${this.tableName}'
82
+ LIMIT 1
83
+ `);
84
+ return res.length > 0;
85
+ }
86
+ async checkTableHasAdmin() {
87
+ const res = await this.sqlService.query(`
88
+ SELECT user_id FROM \`${this.tableName}\` WHERE admin=1
89
+ `);
90
+ return res.length > 0;
91
+ }
92
+ async createTable() {
93
+ const baseColumns = this.getBaseTableColumns();
94
+ const customColumns = this.getCustomTableColumns();
95
+ const baseIndexes = this.getBaseTableIndexes();
96
+ const customIndexes = this.getCustomTableIndexes();
97
+ const allColumns = customColumns ? `${baseColumns},\n ${customColumns}` : baseColumns;
98
+ const allIndexes = customIndexes ? `${baseIndexes},\n ${customIndexes}` : baseIndexes;
99
+ await this.sqlService.query(`
100
+ CREATE TABLE \`${this.tableName}\` (
101
+ ${allColumns},
102
+ ${allIndexes}
103
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
104
+ `);
105
+ }
106
+ async ensureTableExists() {
107
+ const exists = await this.checkTableExists();
108
+ if (!exists) {
109
+ await this.createTable();
110
+ }
111
+ }
112
+ /**
113
+ * Registers a new user with extensible data
114
+ * @param userData - User data including core fields and any custom fields
115
+ * @returns Promise resolving to session information
116
+ */
117
+ async register(userData) {
118
+ const session = this.genSession({ email: userData.email, pass: userData.pass });
119
+ // Build dynamic SQL for insertion
120
+ const coreFields = ['email', 'pass', 'session', 'role', 'admin'];
121
+ const customFields = Object.keys(userData).filter((key) => !coreFields.includes(key) && key !== 'pass');
122
+ const allFields = [...coreFields, ...customFields];
123
+ const placeholders = allFields.map(() => '?').join(', ');
124
+ const fieldNames = allFields.map((field) => `\`${field}\``).join(', ');
125
+ const values = allFields.map((field) => {
126
+ if (field === 'pass') return this.hashPass({ email: userData.email, pass: userData.pass });
127
+ if (field === 'session') return session;
128
+ return userData[field];
129
+ });
130
+ await this.sqlService.query(
131
+ `
132
+ INSERT INTO \`${this.tableName}\` (${fieldNames})
133
+ VALUES (${placeholders})
134
+ `,
135
+ values,
136
+ );
137
+ return { session };
138
+ }
139
+ /**
140
+ * Gets all available columns for SELECT queries
141
+ * @returns Comma-separated list of column names
142
+ * @protected
143
+ */
144
+ getSelectColumns() {
145
+ const baseColumns = ['user_id', 'email', 'session', 'role', 'admin', 'updated_at', 'created_at'];
146
+ const customColumns = this.tableConfig.customColumns ? Object.keys(this.tableConfig.customColumns) : [];
147
+ return [...baseColumns, ...customColumns].map((col) => `\`${col}\``).join(', ');
148
+ }
149
+ async find(opt) {
150
+ const selectColumns = this.getSelectColumns();
151
+ if (opt.email !== undefined) {
152
+ const rows = await this.sqlService.fetch({
153
+ Query: /*SQL*/ `
154
+ SELECT ${selectColumns} FROM \`${this.tableName}\`
155
+ WHERE email=:email
156
+ `,
157
+ Params: { email: opt.email },
158
+ });
159
+ return rows[0];
160
+ } else if (opt.user_id !== undefined) {
161
+ const rows = await this.sqlService.fetch({
162
+ Query: /*SQL*/ `
163
+ SELECT ${selectColumns} FROM \`${this.tableName}\`
164
+ WHERE user_id=:user_id
165
+ `,
166
+ Params: { user_id: opt.user_id },
167
+ });
168
+ return rows[0];
169
+ } else {
170
+ throw new Error(`Unexpected Input for ZUserService.find(${JSON.stringify(opt)})`);
171
+ }
172
+ }
173
+ async auth(opt) {
174
+ if (!opt.session && !opt.email && !opt.pass) {
175
+ return { authenticated: false };
176
+ }
177
+ const selectColumns = this.getSelectColumns();
178
+ const res = await (opt.session
179
+ ? this.sqlService.fetch({
180
+ Query: /*SQL*/ `
181
+ SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE session=?
182
+ `,
183
+ Params: [opt.session],
184
+ })
185
+ : this.sqlService.fetch({
186
+ Query: /*SQL*/ `
187
+ SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE email=? AND pass=?
188
+ `,
189
+ Params: [opt.email, this.hashPass(opt)],
190
+ }));
191
+ return res.length === 0 ? { authenticated: false } : { user: res[0], session: res[0].session, authenticated: true };
192
+ }
193
+ async fetch(opt) {
194
+ const selectColumns = this.getSelectColumns();
195
+ const limit = opt?.limit || 100;
196
+ const rows = await this.sqlService.fetch(
197
+ `
198
+ SELECT ${selectColumns} FROM \`${this.tableName}\`
199
+ ORDER BY created_at DESC
200
+ LIMIT ?
201
+ `,
202
+ [limit],
203
+ );
204
+ return rows;
205
+ }
206
+ genSession({ email }) {
207
+ const salt = this.salt;
208
+ const data = email + Date.now() * Math.random();
209
+ return crypto_service_1.ZCryptoService.hash('sha256', data, { saltMode: 'simple', salt });
210
+ }
211
+ hashPass({ email, pass }) {
212
+ const salt = email + this.salt;
213
+ return crypto_service_1.ZCryptoService.hash('sha256', pass, { saltMode: 'simple', salt });
214
+ }
215
+ }
216
+ exports.ZUserService = ZUserService;
@@ -0,0 +1 @@
1
+ export declare function middleware(): (req: Express.Request, res: Express.Response, next: any) => Promise<any>;
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.middleware = void 0;
4
+ function middleware() {
5
+ return async (req, res, next) => {
6
+ if (req.cookies === undefined) {
7
+ throw new Error(`Module 'cookie-parser' isn't initialized. Please use app.use(cookieParser())`);
8
+ }
9
+ if (req.cookies.session === undefined) {
10
+ return next();
11
+ }
12
+ const auth = await userService.auth({ session: req.cookies?.session });
13
+ req.user = auth.user;
14
+ // TODO: Implement assertAdmin or something like that
15
+ next();
16
+ };
17
+ }
18
+ exports.middleware = middleware;
package/lib/index.d.ts CHANGED
@@ -1,10 +1,19 @@
1
- import { ZEngineBase } from './engine_base';
2
- import { ZCryptoService } from './crypto_service';
3
- import { ZMailService } from './mail_service';
4
- import { ZSQLService } from './sql_service';
5
- import { ZTranslateService } from './translate_service';
6
- import { ZUserService } from './user_service';
1
+ export {
2
+ ZEngineBase,
3
+ ZCryptoService,
4
+ ZMailService,
5
+ ZSQLService,
6
+ ZTranslateService,
7
+ ZUserService,
8
+ ZOrm,
9
+ ZMailBlacklistOrm,
10
+ } from './core';
11
+ export * from './core/types/crypto_types';
12
+ export * from './core/types/mail_types';
13
+ export * from './core/types/translate_types';
14
+ export * from './core/types/user_types';
15
+ export * from './core/types/site_config';
16
+ export { InvoiceService, CustomerService, MollieService, SubscriptionService } from './mollie';
17
+ export * from './mollie/types/mollie_types';
7
18
  export { middleware } from './express';
8
- export * from './typings';
9
19
  export * from './schema';
10
- export { ZEngineBase, ZCryptoService, ZMailService, ZSQLService, ZTranslateService, ZUserService };